mirror of
https://github.com/bitwarden/browser.git
synced 2025-02-16 01:21:48 +01:00
log sync service
This commit is contained in:
parent
f36c046a36
commit
e9e0abd792
@ -19,24 +19,31 @@ function initSyncService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
log('fullSync');
|
||||||
|
|
||||||
self.syncStarted();
|
self.syncStarted();
|
||||||
self.userService.isAuthenticated(function (isAuthenticated) {
|
self.userService.isAuthenticated(function (isAuthenticated) {
|
||||||
if (!isAuthenticated) {
|
if (!isAuthenticated) {
|
||||||
|
log('is not authenticated');
|
||||||
self.syncCompleted(false);
|
self.syncCompleted(false);
|
||||||
callback(false);
|
callback(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log('is authenticated');
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
needsSyncing(self, forceSync, function (needsSync, skipped) {
|
needsSyncing(self, forceSync, function (needsSync, skipped) {
|
||||||
|
log('needsSyncing result: ' + needsSync + ', ' + skipped);
|
||||||
|
|
||||||
if (skipped) {
|
if (skipped) {
|
||||||
|
log('skipped');
|
||||||
self.syncCompleted(false);
|
self.syncCompleted(false);
|
||||||
callback(false);
|
callback(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!needsSync) {
|
if (!needsSync) {
|
||||||
|
log('doesn\'t need sync');
|
||||||
self.setLastSync(now, function () {
|
self.setLastSync(now, function () {
|
||||||
self.syncCompleted(false);
|
self.syncCompleted(false);
|
||||||
callback(false);
|
callback(false);
|
||||||
@ -44,19 +51,26 @@ function initSyncService() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log('starting sync');
|
||||||
self.userService.getUserId(function (userId) {
|
self.userService.getUserId(function (userId) {
|
||||||
|
log('sync profile');
|
||||||
syncProfile(self).then(function () {
|
syncProfile(self).then(function () {
|
||||||
|
log('sync folders');
|
||||||
return syncFolders(self, userId);
|
return syncFolders(self, userId);
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
|
log('sync ciphers');
|
||||||
return syncCiphers(self, userId);
|
return syncCiphers(self, userId);
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
|
log('sync settings');
|
||||||
return syncSettings(self, userId);
|
return syncSettings(self, userId);
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
|
log('all done with the syncs - ' + now);
|
||||||
self.setLastSync(now, function () {
|
self.setLastSync(now, function () {
|
||||||
self.syncCompleted(true);
|
self.syncCompleted(true);
|
||||||
callback(true);
|
callback(true);
|
||||||
});
|
});
|
||||||
}, function () {
|
}, function () {
|
||||||
|
log('and error happened during the syncs');
|
||||||
self.syncCompleted(false);
|
self.syncCompleted(false);
|
||||||
callback(false);
|
callback(false);
|
||||||
});
|
});
|
||||||
@ -71,21 +85,27 @@ function initSyncService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (forceSync) {
|
if (forceSync) {
|
||||||
|
log('needs sync since force');
|
||||||
callback(true, false);
|
callback(true, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log('getting revision date from api');
|
||||||
self.apiService.getAccountRevisionDate(function (response) {
|
self.apiService.getAccountRevisionDate(function (response) {
|
||||||
var accountRevisionDate = new Date(response);
|
var accountRevisionDate = new Date(response);
|
||||||
|
log('account last revised: ' + accountRevisionDate);
|
||||||
self.getLastSync(function (lastSync) {
|
self.getLastSync(function (lastSync) {
|
||||||
if (lastSync && accountRevisionDate <= lastSync) {
|
if (lastSync && accountRevisionDate <= lastSync) {
|
||||||
|
log('already synced since this revision date');
|
||||||
callback(false, false);
|
callback(false, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log('we haven\'t synced since this revision');
|
||||||
callback(true, false);
|
callback(true, false);
|
||||||
});
|
});
|
||||||
}, function () {
|
}, function () {
|
||||||
|
log('there was an error getting the account revision date');
|
||||||
callback(false, true);
|
callback(false, true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -198,15 +218,21 @@ function initSyncService() {
|
|||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function log(msg) {
|
||||||
|
console.log(new Date() + ' - Sync Service: ' + msg);
|
||||||
|
}
|
||||||
|
|
||||||
SyncService.prototype.getLastSync = function (callback) {
|
SyncService.prototype.getLastSync = function (callback) {
|
||||||
if (!callback || typeof callback !== 'function') {
|
if (!callback || typeof callback !== 'function') {
|
||||||
throw 'callback function required';
|
throw 'callback function required';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log('getting last sync');
|
||||||
this.userService.getUserId(function (userId) {
|
this.userService.getUserId(function (userId) {
|
||||||
var lastSyncKey = 'lastSync_' + userId;
|
var lastSyncKey = 'lastSync_' + userId;
|
||||||
chrome.storage.local.get(lastSyncKey, function (obj) {
|
chrome.storage.local.get(lastSyncKey, function (obj) {
|
||||||
var lastSync = obj[lastSyncKey];
|
var lastSync = obj[lastSyncKey];
|
||||||
|
log('done getting last sync: ' + lastSync);
|
||||||
if (lastSync) {
|
if (lastSync) {
|
||||||
callback(new Date(lastSync));
|
callback(new Date(lastSync));
|
||||||
}
|
}
|
||||||
@ -228,7 +254,9 @@ function initSyncService() {
|
|||||||
var obj = {};
|
var obj = {};
|
||||||
obj[lastSyncKey] = date.toJSON();
|
obj[lastSyncKey] = date.toJSON();
|
||||||
|
|
||||||
|
log('setting last sync');
|
||||||
chrome.storage.local.set(obj, function () {
|
chrome.storage.local.set(obj, function () {
|
||||||
|
log('done setting last sync');
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -237,10 +265,12 @@ function initSyncService() {
|
|||||||
SyncService.prototype.syncStarted = function () {
|
SyncService.prototype.syncStarted = function () {
|
||||||
this.syncInProgress = true;
|
this.syncInProgress = true;
|
||||||
chrome.runtime.sendMessage({ command: 'syncStarted' });
|
chrome.runtime.sendMessage({ command: 'syncStarted' });
|
||||||
|
log('sync started');
|
||||||
};
|
};
|
||||||
|
|
||||||
SyncService.prototype.syncCompleted = function (successfully) {
|
SyncService.prototype.syncCompleted = function (successfully) {
|
||||||
this.syncInProgress = false;
|
this.syncInProgress = false;
|
||||||
chrome.runtime.sendMessage({ command: 'syncCompleted', successfully: successfully });
|
chrome.runtime.sendMessage({ command: 'syncCompleted', successfully: successfully });
|
||||||
|
log('sync completed');
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user