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