autosaving to localStorage every 5 mins

This commit is contained in:
Mavis Ou 2013-06-03 17:04:21 -07:00
Родитель 75fdb9d0e3
Коммит 70f49a5e75
5 изменённых файлов: 46 добавлений и 75 удалений

Просмотреть файл

@ -3,16 +3,34 @@ var currentVisualization;
var allConnections = [];
// FIXME: Read this from config file
var uploadServer = 'http://collusiondb-development.herokuapp.com/shareData';
//var uploadServer = 'http://localhost:7000/shareData';
var uploadTimer;
var saveTimer;
// Constants for indexes of properties in array format
const SOURCE = 0;
const TARGET = 1;
const TIMESTAMP = 2;
const CONTENT_TYPE = 3;
const COOKIE = 4;
const SOURCE_VISITED = 5;
const SECURE = 6;
const SOURCE_PATH_DEPTH = 7;
const SOURCE_QUERY_DEPTH = 8;
const SOURCE_SUB = 9;
const TARGET_SUB = 10;
const METHOD = 11;
const STATUS = 12;
const CACHEABLE = 13;
window.addEventListener('load', function(evt){
addon.emit('uiready');
// Wire up events
document.querySelector('.btn_group.visualization').click();
document.querySelector('[data-value=' + (localStorage.visualization || 'Graph') + ']').click();
if ( localStorage.userHasOptedIntoSharing && localStorage.userHasOptedIntoSharing === 'true' ){
startUploadTimer();
}
saveTimer = setInterval(saveConnections, 5 * 60 * 1000); // save to localStorage every 5 minutes
});
@ -35,59 +53,41 @@ function switchVisualization(name){
}
localStorage.visualization = initCap(name);
currentVisualization = visualizations[name];
// currentVisualization.emit('setFilter'); // mavis: to be deleted?
// currentVisualization.emit('setFilter');
// toggle off info panel, settings page, help bubbles
document.querySelector("#content").classList.remove("showinfo");
document.querySelector(".settings-page").classList.add("hide");
clearAllBubbles();
// show vizcanvas again in case it is hidden
document.querySelector(".vizcanvas").classList.remove("hide");
addon.emit('uiready');
}
function saveConnections(connections){
function saveConnections(){
if ( localStorage.connections && localStorage.connections != "[]" ){
// TODO: currently using localStorage.totalSize to define the lastSavedIndex
// will have to use timestamp once we have enable fitlering
console.log("== existed ============");
var paresedConnections = JSON.parse(localStorage.connections);
var unsavedConnections = connections.slice(localStorage.totalSize, connections.length);
localStorage.connections = JSON.stringify( paresedConnections.concat(unsavedConnections) );
var lastSaved = localStorage.lastSaved || 0;
var unsavedConnections = allConnections.slice(localStorage.totalNumConnections, allConnections.length);
var connections = allConnections.filter(function(connection){
return ( connection[TIMESTAMP] ) > lastSaved;
});
if ( connections.length > 0 ){
localStorage.connections = localStorage.connections.slice(0,-1) + "," + JSON.stringify(connections).slice(1);
}
console.log("--- unsavedConnections.length = " + unsavedConnections.length );
localStorage.lastSaved = Date.now();
}else{
console.log("== NOT existed ============");
localStorage.connections = JSON.stringify(connections);
localStorage.connections = JSON.stringify(allConnections);
}
localStorage.totalSize = JSON.parse(localStorage.connections).length;
localStorage.totalNumConnections = JSON.parse(localStorage.connections).length;
}
// Mavis: should we have this here? or keep them in aggregate.js
// Constants for indexes of properties in array format
const SOURCE = 0;
const TARGET = 1;
const TIMESTAMP = 2;
const CONTENT_TYPE = 3;
const COOKIE = 4;
const SOURCE_VISITED = 5;
const SECURE = 6;
const SOURCE_PATH_DEPTH = 7;
const SOURCE_QUERY_DEPTH = 8;
const SOURCE_SUB = 9;
const TARGET_SUB = 10;
const METHOD = 11;
const STATUS = 12;
const CACHEABLE = 13;
function startSharing(){
if (confirm('You are about to start uploading anonymized data to the Mozilla Collusion server. ' +
'Your data will continue to be uploaded periodically until you turn off sharing. ' +
'For more information about the data we upload, how it is anonymized, and what Mozilla\'s ' +
'privacy policies are, please visit http://ItsOurData.com/privacy/.\n\nBy clicking Okay ' +
'you are agreeing to share your data under those terms.')){
// addon.emit('startUpload');
sharingData();
uploadButton.innerHTML = 'Stop Sharing';
localStorage.userHasOptedIntoSharing = true;
@ -109,7 +109,6 @@ function stopSharing(){
function sharingData(){
console.log("Beginning Upload...");
var lastUpload = localStorage.lastUpload || 0;
// mavis: use allConnections or localStorage.connections?
var connections = allConnections.filter(function(connection){
return ( connection[TIMESTAMP] ) > lastUpload;
});

Просмотреть файл

@ -21,7 +21,7 @@ self.port.on('init', function(collusionToken){
localStorage.collusionToken = collusionToken;
if (unsafeWindow && unsafeWindow.currentVisualization){
if ( localStorage.connections ){
if ( localStorage.connections && localStorage.connections != "[]" ){
unsafeWindow.allConnections = JSON.parse(localStorage.connections);
}
unsafeWindow.currentVisualization.emit('init', unsafeWindow.allConnections);
@ -33,20 +33,19 @@ self.port.on('init', function(collusionToken){
self.port.on("sendTempConnections", function(message){
// message is an array of connection [ [],[],[] ]
localStorage.tempConnections = JSON.stringify(message);
localStorage.tempSize = message.length;
self.port.emit("tempConnecitonTransferred", true);
localStorage.tempConnectionsSize = message.length;
self.port.emit("tempConnectionTransferred", true);
var parsedTempConnections = localStorage.tempConnections ? JSON.parse(localStorage.tempConnections) : [ {} ] ;
if ( localStorage.connections ){
var paresedConnections = JSON.parse(localStorage.connections);
localStorage.temp = JSON.stringify(paresedConnections);
unsafeWindow.allConnections = paresedConnections.concat(parsedTempConnections);
localStorage.connections = JSON.stringify( paresedConnections.concat(parsedTempConnections) );
if ( localStorage.connections & localStorage.connections != "[]" ){
var allConnectionsAsString = localStorage.connections.slice(0,-1) + "," + localStorage.tempConnections.slice(1);
localStorage.connections = allConnectionsAsString;
unsafeWindow.allConnections = JSON.parse(allConnectionsAsString);
}else{
var parsedTempConnections = localStorage.tempConnections ? JSON.parse(localStorage.tempConnections) : [ [] ] ;
localStorage.connections = localStorage.tempConnections;
unsafeWindow.allConnections = parsedTempConnections;
}
localStorage.totalSize = unsafeWindow.allConnections.length;
localStorage.totalNumConnections = unsafeWindow.allConnections.length;
});

Просмотреть файл

@ -80,10 +80,10 @@ document.querySelector('.reset-data').addEventListener('click', function(){
aggregate.emit('reset');
currentVisualization.emit('reset');
allConnections = [];
delete localStorage.tempSize;
delete localStorage.tempConnectionsSize;
delete localStorage.connections;
delete localStorage.temp;
delete localStorage.totalSize;
delete localStorage.totalNumConnections;
// FIXME: empty the data from current view too
});

Просмотреть файл

@ -18,7 +18,6 @@ var captureAndStoreConnections = true;
exports.Connection = Connection;
exports.addConnection = addConnection;
exports.exportFormat = exportFormat;
// FIXME: Move persistence into a component
@ -29,10 +28,7 @@ var storage = ss.storage;
function restore(){
// only called when add-on is initialized, not when ui page is refreshed
Connection.emit('restored');
console.error("%%%%%%%%% RESTORE() ");
if ( storage.tempConnections ){
console.error("i hate you =====");
// delete storage.tempConnections;
if ( storage.tempConnections.length > 0 ){
Connection.emit("tempConnections", storage.tempConnections);
}else{
Connection.emit("tempConnections", []);
@ -343,29 +339,6 @@ Connection.on("clearStoredTempConnections", function(result){
/* END FLAG EVENTS */
/* BEGIN FLAG SERIALIZATION */
function exportFormat(connections, lastSync){
if (!connections){
connections = storage.connections.map(Connection.restore);
}
if (!lastSync){
lastSync = 0;
}
return JSON.stringify({
format: 'Collusion Save File',
version: '1.1',
token: collusionToken,
connections: connections.map(function(connection){
if (connection && connection.toLog){
return connection.toLog();
}else{
log('Connection could not convert [' + connection.toLog + '] ' + JSON.stringify(connection) + ' to log format');
}
})
});
}
/* END FLAG SERIALIZATION */
/* BEGIN FLAG PERSISTENCE */
if (!restored) restore();
/* END FLAG PERSISTENCE */

Просмотреть файл

@ -2,7 +2,7 @@
const tabs = require('tabs');
const { data } = require("self");
let { Connection, saveConnections, exportFormat } = require('./connection');
let { Connection } = require('./connection');
const mainPage = data.url("index.html");
const contentScript = data.url('content-script.js');
@ -116,7 +116,7 @@ function attachToCollusionPage(worker) {
Connection.on('restored', onRestored);
});
worker.port.on("tempConnecitonTransferred", function(result){
worker.port.on("tempConnectionTransferred", function(result){
Connection.emit("clearStoredTempConnections", result);
});