2017-04-24 17:08:32 +02:00
|
|
|
|
function SyncService(loginService, folderService, userService, apiService, settingsService, cryptoService) {
|
2017-01-04 00:40:07 +01:00
|
|
|
|
this.loginService = loginService;
|
2016-09-07 02:41:17 +02:00
|
|
|
|
this.folderService = folderService;
|
|
|
|
|
this.userService = userService;
|
|
|
|
|
this.apiService = apiService;
|
2017-01-14 17:20:44 +01:00
|
|
|
|
this.settingsService = settingsService;
|
2017-04-24 17:08:32 +02:00
|
|
|
|
this.cryptoService = settingsService;
|
2016-09-07 02:41:17 +02:00
|
|
|
|
this.syncInProgress = false;
|
|
|
|
|
|
|
|
|
|
initSyncService();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function initSyncService() {
|
2017-01-14 18:47:11 +01:00
|
|
|
|
SyncService.prototype.fullSync = function (forceSync, callback) {
|
2016-09-07 02:41:17 +02:00
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
|
throw 'callback function required';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var self = this;
|
2016-09-27 02:29:23 +02:00
|
|
|
|
|
|
|
|
|
self.syncStarted();
|
2016-09-07 05:30:49 +02:00
|
|
|
|
self.userService.isAuthenticated(function (isAuthenticated) {
|
2016-09-07 02:41:17 +02:00
|
|
|
|
if (!isAuthenticated) {
|
2016-09-27 02:29:23 +02:00
|
|
|
|
self.syncCompleted(false);
|
2016-09-07 02:41:17 +02:00
|
|
|
|
callback(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 05:30:49 +02:00
|
|
|
|
self.userService.getUserId(function (userId) {
|
|
|
|
|
var now = new Date();
|
2017-01-14 18:47:11 +01:00
|
|
|
|
needsSyncing(self, forceSync, function (needsSync) {
|
|
|
|
|
if (!needsSync) {
|
2017-03-15 03:51:47 +01:00
|
|
|
|
self.setLastSync(now, function () {
|
|
|
|
|
self.syncCompleted(false);
|
|
|
|
|
callback(false);
|
|
|
|
|
});
|
2017-01-14 18:47:11 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-09-07 02:41:17 +02:00
|
|
|
|
|
2017-04-24 17:08:32 +02:00
|
|
|
|
syncProfile().then(function () {
|
|
|
|
|
return syncFolders(userId);
|
|
|
|
|
}).then(function () {
|
|
|
|
|
return syncCiphers(userId);
|
|
|
|
|
}).then(function () {
|
2017-01-24 06:30:45 +01:00
|
|
|
|
return syncSettings(userId);
|
|
|
|
|
}).then(function () {
|
2017-03-15 03:51:47 +01:00
|
|
|
|
self.setLastSync(now, function () {
|
2017-01-14 18:47:11 +01:00
|
|
|
|
self.syncCompleted(true);
|
|
|
|
|
callback(true);
|
|
|
|
|
});
|
|
|
|
|
}, function () {
|
|
|
|
|
self.syncCompleted(false);
|
|
|
|
|
callback(false);
|
2016-09-07 02:41:17 +02:00
|
|
|
|
});
|
2017-01-14 17:20:44 +01:00
|
|
|
|
});
|
2016-09-07 05:30:49 +02:00
|
|
|
|
});
|
2016-09-07 02:41:17 +02:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-01-14 18:47:11 +01:00
|
|
|
|
function needsSyncing(self, forceSync, callback) {
|
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
|
throw 'callback function required';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (forceSync) {
|
|
|
|
|
callback(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.getLastSync(function (lastSync) {
|
|
|
|
|
self.apiService.getAccountRevisionDate(function (response) {
|
|
|
|
|
var accountRevisionDate = new Date(response);
|
2017-02-08 03:45:29 +01:00
|
|
|
|
if (lastSync && accountRevisionDate <= lastSync) {
|
2017-01-14 18:47:11 +01:00
|
|
|
|
callback(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-24 17:08:32 +02:00
|
|
|
|
function syncProfile() {
|
|
|
|
|
var deferred = Q.defer();
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2017-04-26 17:59:31 +02:00
|
|
|
|
function setKeys(hasPrivateKey, response, d) {
|
|
|
|
|
if (response.organizations && response.organizations.length && !hasPrivateKey) {
|
|
|
|
|
self.apiService.getKeys(function (keysResponse) {
|
|
|
|
|
if (keysResponse.privateKey) {
|
|
|
|
|
self.cryptoService.setEncPrivateKey(keysResponse.privateKey).then(function () {
|
|
|
|
|
return self.cryptoService.setOrgKeys(response.organizations);
|
|
|
|
|
}, function () {
|
|
|
|
|
d.reject();
|
|
|
|
|
}).then(function () {
|
|
|
|
|
d.resolve();
|
|
|
|
|
}, function () {
|
|
|
|
|
d.reject();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
d.resolve();
|
|
|
|
|
}
|
|
|
|
|
}, function () {
|
|
|
|
|
d.reject();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
self.cryptoService.setOrgKeys(response.organizations).then(function () {
|
|
|
|
|
d.resolve();
|
|
|
|
|
}, function () {
|
|
|
|
|
d.reject();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-24 17:08:32 +02:00
|
|
|
|
self.apiService.getProfile(function (response) {
|
2017-04-25 22:23:37 +02:00
|
|
|
|
self.cryptoService.getPrivateKey().then(function (privateKey) {
|
2017-04-26 17:59:31 +02:00
|
|
|
|
setKeys(!!privateKey, response, deferred);
|
|
|
|
|
}, function () {
|
|
|
|
|
setKeys(false, response, deferred);
|
2017-04-24 17:08:32 +02:00
|
|
|
|
});
|
|
|
|
|
}, function () {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function syncFolders(userId) {
|
|
|
|
|
var deferred = Q.defer();
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
self.apiService.getFolders(function (response) {
|
|
|
|
|
var folders = {};
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < response.data.length; i++) {
|
2017-04-25 22:23:37 +02:00
|
|
|
|
folders[response.data[i].id] = new FolderData(response.data[i], userId);
|
2017-04-24 17:08:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.folderService.replace(folders, function () {
|
|
|
|
|
deferred.resolve();
|
|
|
|
|
});
|
|
|
|
|
}, function () {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function syncCiphers(userId) {
|
2017-01-14 17:20:44 +01:00
|
|
|
|
var deferred = Q.defer();
|
|
|
|
|
var self = this;
|
2016-09-07 02:41:17 +02:00
|
|
|
|
|
2017-01-14 17:20:44 +01:00
|
|
|
|
self.apiService.getCiphers(function (response) {
|
|
|
|
|
var logins = {};
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < response.data.length; i++) {
|
|
|
|
|
var data = response.data[i];
|
|
|
|
|
if (data.type === 1) {
|
|
|
|
|
logins[data.id] = new LoginData(data, userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-24 17:08:32 +02:00
|
|
|
|
self.loginService.replace(logins, function () {
|
|
|
|
|
deferred.resolve();
|
2017-01-14 17:20:44 +01:00
|
|
|
|
});
|
|
|
|
|
}, function () {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise
|
|
|
|
|
}
|
2016-09-07 02:41:17 +02:00
|
|
|
|
|
2017-01-14 17:20:44 +01:00
|
|
|
|
function syncSettings(userId) {
|
|
|
|
|
var deferred = Q.defer();
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
var ciphers = self.apiService.getIncludedDomains(function (response) {
|
|
|
|
|
var eqDomains = [];
|
|
|
|
|
if (response && response.equivalentDomains) {
|
|
|
|
|
eqDomains = eqDomains.concat(response.equivalentDomains);
|
|
|
|
|
}
|
|
|
|
|
if (response && response.globalEquivalentDomains) {
|
|
|
|
|
for (var i = 0; i < response.globalEquivalentDomains.length; i++) {
|
2017-01-14 19:43:59 +01:00
|
|
|
|
if (response.globalEquivalentDomains[i].domains.length) {
|
|
|
|
|
eqDomains.push(response.globalEquivalentDomains[i].domains);
|
|
|
|
|
}
|
2017-01-14 17:20:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.settingsService.setEquivalentDomains(eqDomains, function () {
|
|
|
|
|
deferred.resolve();
|
|
|
|
|
});
|
|
|
|
|
}, function () {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-07 02:41:17 +02:00
|
|
|
|
SyncService.prototype.getLastSync = function (callback) {
|
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
|
throw 'callback function required';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.userService.getUserId(function (userId) {
|
|
|
|
|
var lastSyncKey = 'lastSync_' + userId;
|
|
|
|
|
chrome.storage.local.get(lastSyncKey, function (obj) {
|
|
|
|
|
var lastSync = obj[lastSyncKey];
|
|
|
|
|
if (lastSync) {
|
|
|
|
|
callback(new Date(lastSync));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
callback(null);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SyncService.prototype.setLastSync = function (date, callback) {
|
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
|
throw 'callback function required';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.userService.getUserId(function (userId) {
|
|
|
|
|
var lastSyncKey = 'lastSync_' + userId;
|
|
|
|
|
|
|
|
|
|
var obj = {};
|
|
|
|
|
obj[lastSyncKey] = date.toJSON();
|
|
|
|
|
|
|
|
|
|
chrome.storage.local.set(obj, function () {
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-27 02:29:23 +02:00
|
|
|
|
SyncService.prototype.syncStarted = function () {
|
2016-09-07 02:41:17 +02:00
|
|
|
|
this.syncInProgress = true;
|
2016-09-27 02:29:23 +02:00
|
|
|
|
chrome.runtime.sendMessage({ command: 'syncStarted' });
|
|
|
|
|
};
|
2016-09-07 02:41:17 +02:00
|
|
|
|
|
2016-09-27 02:29:23 +02:00
|
|
|
|
SyncService.prototype.syncCompleted = function (successfully) {
|
2016-09-07 02:41:17 +02:00
|
|
|
|
this.syncInProgress = false;
|
2016-09-27 02:29:23 +02:00
|
|
|
|
chrome.runtime.sendMessage({ command: 'syncCompleted', successfully: successfully });
|
|
|
|
|
};
|
2016-09-07 02:41:17 +02:00
|
|
|
|
};
|