Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Reports Generation #74

Merged
merged 3 commits into from
Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions js/background/reports.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var db = create_luminous_db();

var update_reports_for_tab_id = function() {};

var on_db_open = function() {
var update_report = function(data) {
var update_report = function(data, callback) {
db.reports.where({id: data.id }).first(function(report) {
var put = false;

Expand All @@ -27,41 +29,46 @@ var on_db_open = function() {
});
}

var set_reports_for_tab = function(tab_ids) {
var set_reports_for_tab = function(tab_ids, callback) {
chrome.storage.sync.get('reports', function(sync_data) {
if(sync_data['reports']['collect_data']) {
chrome.storage.local.get(null, function(local_data) {
for(i in tab_ids) {
var tab_id = tab_ids[i];
chrome.tabs.get(parseInt(tab_id), function(tab) {
if(tab && local_data[tab.id]) {
var a_element = document.createElement('a');
a_element.href = tab.url;
var domain = a_element.hostname;

setTimeout(function(tab_id, callback) {
if(local_data[tab_id]) {
var domain = local_data[tab_id]['domain'];

if(/\./.test(domain)) {
for(kind in local_data[tab.id]['counters']) {
for(code in local_data[tab.id]['counters'][kind]) {
for(kind in local_data[tab_id]['counters']) {
for(code in local_data[tab_id]['counters'][kind]) {
if(validates_code(code, 'almost_all')) {
var key = domain + '^' + kind + '^' + code;

var allowed = local_data[tab.id]['counters'][kind][code]['allowed'];
var blocked = local_data[tab.id]['counters'][kind][code]['blocked'];
var allowed = local_data[tab_id]['counters'][kind][code]['allowed'];
var blocked = local_data[tab_id]['counters'][kind][code]['blocked'];
var calls = allowed + blocked;

var execution_time = local_data[tab.id]['counters'][kind][code]['execution_time'];
var execution_time = local_data[tab_id]['counters'][kind][code]['execution_time'];

update_report({
var report_params = {
id: key, domain: domain, kind: kind, code: code,
allowed: allowed, blocked: blocked, calls: calls,
execution_time: execution_time
});
}

setTimeout(function(report_params) {
update_report(report_params,);
}, 0, report_params);
}
}
}
}
}
});

if(callback) { setTimeout(function() { callback(); }, 0); }
}, 0, tab_id, callback);
}
});
}
Expand All @@ -76,7 +83,7 @@ var on_db_open = function() {

set_reports_for_tab(tab_ids);
});
}, 500);
}, 2000);

var set_tab_reports = function(activeInfo) {
chrome.tabs.get(parseInt(activeInfo.tabId), function(tab) {
Expand All @@ -87,6 +94,10 @@ var on_db_open = function() {
}

chrome.tabs.onActivated.addListener(set_tab_reports);

update_reports_for_tab_id = function(tab_id, callback) {
set_reports_for_tab([tab_id], callback);
};
}

db.open().then(function() {
Expand All @@ -109,8 +120,9 @@ db.open().then(function() {

db.open().then(function() {
on_db_open();
}).catch(function(_) {
}).catch(function(error) {
// Giving up...
console.error('Failed to open Luminous db: ' + (err.stack || err));
});
});
});
29 changes: 13 additions & 16 deletions js/background/set_current_tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,29 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
}
});

var reset_counters_for_tab = function(tab_id) {
var remove_data_for_tab = function(tab_id) {
var tab_id = tab_id.toString();

chrome.storage.local.get(tab_id, function(current_storage_data) {
var data_to_write = current_storage_data;

if(data_to_write && data_to_write[tab_id] && data_to_write[tab_id]) {
data_to_write[tab_id]['counters'] = {};
data_to_write[tab_id]['badge'] = { 'text': '', 'calls': 0 };
chrome.storage.local.set(data_to_write);
chrome.storage.local.get(tab_id, function(data) {
if(data && data[tab_id]) {
update_reports_for_tab_id(tab_id, function() {
chrome.storage.local.remove(tab_id);
});
}
});
}

var remove_data_for_tab = function(tab_id) {
chrome.storage.local.remove(tab_id.toString());
}

chrome.tabs.onCreated.addListener(function(tab) {
reset_counters_for_tab(tab.id);
remove_data_for_tab(tab.id);
});
chrome.tabs.onUpdated.addListener(function(tab_id) { reset_counters_for_tab(tab_id); });
chrome.tabs.onRemoved.addListener(function(tab_id) { remove_data_for_tab(tab_id); });

chrome.tabs.onRemoved.addListener(function(tab_id) {
remove_data_for_tab(tab_id);
});

chrome.webRequest.onBeforeRequest.addListener(
function(details) { remove_data_for_tab(details.tabId) },
function(details) {
remove_data_for_tab(details.tabId);
},
{ urls: ['<all_urls>'], types: ['main_frame', 'sub_frame'] }
);
2 changes: 1 addition & 1 deletion js/background/update_badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var load_stored_data = function(update_badge = false, update_icon = false, chang
update_badge = true;
}

if(changes && changes && changes && changes.newValue && changes.newValue['injection_disabled']) {
if(changes && changes['injection_disabled']) {
update_icon = true;
}

Expand Down
4 changes: 3 additions & 1 deletion js/content/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ original_window_setInterval(function() {

var element = document.getElementById('luminous-data');

element.innerHTML = JSON.stringify({ counters: counters });
element.innerHTML = JSON.stringify(
{ domain: document.location.host, counters: counters }
);

element.setAttribute('data-changed', 'true');
}
Expand Down
4 changes: 4 additions & 0 deletions js/content/readers/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ injections_controller(function() {
'text': short_number_for_badge(calls), 'calls': calls
};

if(document_data && document_data['domain']) {
data_to_write[tab_id]['domain'] = document_data['domain'];
}

data_to_write[tab_id]['counters'] = document_data['counters'];

chrome.storage.local.set(data_to_write);
Expand Down
1 change: 1 addition & 0 deletions js/settings/reports/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $(document).ready(function() {
setTimeout(function() {
if(complete_loads < 2) {
alert('Reports loading failed! db.isOpen(): ' + db.isOpen());
loaded();
}
}, 5000);

Expand Down