2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-01-07 00:54:18 +03:00
|
|
|
|
2016-08-11 21:01:15 +03:00
|
|
|
/**
|
|
|
|
* How does the clients engine work?
|
|
|
|
*
|
|
|
|
* - We use 2 files - commands.json and commands-syncing.json.
|
|
|
|
*
|
|
|
|
* - At sync upload time, we attempt a rename of commands.json to
|
|
|
|
* commands-syncing.json, and ignore errors (helps for crash during sync!).
|
|
|
|
* - We load commands-syncing.json and stash the contents in
|
|
|
|
* _currentlySyncingCommands which lives for the duration of the upload process.
|
|
|
|
* - We use _currentlySyncingCommands to build the outgoing records
|
|
|
|
* - Immediately after successful upload, we delete commands-syncing.json from
|
|
|
|
* disk (and clear _currentlySyncingCommands). We reconcile our local records
|
|
|
|
* with what we just wrote in the server, and add failed IDs commands
|
|
|
|
* back in commands.json
|
|
|
|
* - Any time we need to "save" a command for future syncs, we load
|
|
|
|
* commands.json, update it, and write it back out.
|
|
|
|
*/
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
2012-08-30 01:43:41 +04:00
|
|
|
"ClientEngine",
|
|
|
|
"ClientsRec"
|
|
|
|
];
|
2009-01-07 00:54:18 +03:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
ChromeUtils.import("resource://services-common/async.js");
|
|
|
|
ChromeUtils.import("resource://services-sync/constants.js");
|
|
|
|
ChromeUtils.import("resource://services-sync/engines.js");
|
|
|
|
ChromeUtils.import("resource://services-sync/record.js");
|
|
|
|
ChromeUtils.import("resource://services-sync/resource.js");
|
|
|
|
ChromeUtils.import("resource://services-sync/util.js");
|
|
|
|
|
|
|
|
ChromeUtils.defineModuleGetter(this, "fxAccounts",
|
2016-01-13 07:55:00 +03:00
|
|
|
"resource://gre/modules/FxAccounts.jsm");
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "getRepairRequestor",
|
2017-03-02 08:14:51 +03:00
|
|
|
"resource://services-sync/collection_repair.js");
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "getRepairResponder",
|
2017-03-02 08:14:51 +03:00
|
|
|
"resource://services-sync/collection_repair.js");
|
|
|
|
|
2011-01-19 03:23:20 +03:00
|
|
|
const CLIENTS_TTL = 1814400; // 21 days
|
2011-01-15 00:22:20 +03:00
|
|
|
const CLIENTS_TTL_REFRESH = 604800; // 7 days
|
2016-04-15 19:00:59 +03:00
|
|
|
const STALE_CLIENT_REMOTE_AGE = 604800; // 7 days
|
2011-01-15 00:22:20 +03:00
|
|
|
|
2017-10-11 21:58:21 +03:00
|
|
|
// TTL of the message sent to another device when sending a tab
|
|
|
|
const NOTIFY_TAB_SENT_TTL_SECS = 1 * 3600; // 1 hour
|
|
|
|
|
|
|
|
// Reasons behind sending collection_changed push notifications.
|
|
|
|
const COLLECTION_MODIFIED_REASON_SENDTAB = "sendtab";
|
|
|
|
const COLLECTION_MODIFIED_REASON_FIRSTSYNC = "firstsync";
|
|
|
|
|
2017-03-16 05:18:43 +03:00
|
|
|
const SUPPORTED_PROTOCOL_VERSIONS = [SYNC_API_VERSION];
|
2017-10-03 21:45:11 +03:00
|
|
|
const LAST_MODIFIED_ON_PROCESS_COMMAND_PREF = "services.sync.clients.lastModifiedOnProcessCommands";
|
2014-01-07 09:45:26 +04:00
|
|
|
|
2016-04-01 20:55:10 +03:00
|
|
|
function hasDupeCommand(commands, action) {
|
2016-04-05 01:39:37 +03:00
|
|
|
if (!commands) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-01 20:55:10 +03:00
|
|
|
return commands.some(other => other.command == action.command &&
|
|
|
|
Utils.deepEquals(other.args, action.args));
|
|
|
|
}
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.ClientsRec = function ClientsRec(collection, id) {
|
2011-01-19 03:23:20 +03:00
|
|
|
CryptoWrapper.call(this, collection, id);
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2011-01-19 03:23:20 +03:00
|
|
|
ClientsRec.prototype = {
|
|
|
|
__proto__: CryptoWrapper.prototype,
|
2011-06-13 22:42:18 +04:00
|
|
|
_logName: "Sync.Record.Clients",
|
2011-01-19 03:23:20 +03:00
|
|
|
ttl: CLIENTS_TTL
|
|
|
|
};
|
|
|
|
|
2014-11-18 06:06:00 +03:00
|
|
|
Utils.deferGetSet(ClientsRec,
|
|
|
|
"cleartext",
|
|
|
|
["name", "type", "commands",
|
|
|
|
"version", "protocols",
|
2016-08-02 20:09:30 +03:00
|
|
|
"formfactor", "os", "appPackage", "application", "device",
|
|
|
|
"fxaDeviceId"]);
|
2011-01-19 03:23:20 +03:00
|
|
|
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.ClientEngine = function ClientEngine(service) {
|
2012-08-30 01:43:41 +04:00
|
|
|
SyncEngine.call(this, "Clients", service);
|
2010-02-12 02:25:31 +03:00
|
|
|
|
2016-08-11 21:01:15 +03:00
|
|
|
// Reset the last sync timestamp on every startup so that we fetch all clients
|
|
|
|
this.resetLastSync();
|
2017-03-01 00:35:01 +03:00
|
|
|
this.fxAccounts = fxAccounts;
|
2017-08-02 18:33:24 +03:00
|
|
|
this.addClientCommandQueue = Promise.resolve();
|
2017-12-20 20:36:18 +03:00
|
|
|
Utils.defineLazyIDProperty(this, "localID", "services.sync.client.GUID");
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2009-01-07 00:54:18 +03:00
|
|
|
ClientEngine.prototype = {
|
|
|
|
__proto__: SyncEngine.prototype,
|
|
|
|
_storeObj: ClientStore,
|
2010-03-17 02:39:08 +03:00
|
|
|
_recordObj: ClientsRec,
|
2011-07-27 08:48:50 +04:00
|
|
|
_trackerObj: ClientsTracker,
|
2016-12-01 01:28:52 +03:00
|
|
|
allowSkippedRecord: false,
|
2017-03-01 00:35:01 +03:00
|
|
|
_knownStaleFxADeviceIds: null,
|
2017-12-20 20:36:18 +03:00
|
|
|
_lastDeviceCounts: null,
|
2010-03-17 02:39:08 +03:00
|
|
|
|
2017-10-03 21:45:11 +03:00
|
|
|
// These two properties allow us to avoid replaying the same commands
|
|
|
|
// continuously if we cannot manage to upload our own record.
|
|
|
|
_localClientLastModified: 0,
|
|
|
|
get _lastModifiedOnProcessCommands() {
|
|
|
|
return Services.prefs.getIntPref(LAST_MODIFIED_ON_PROCESS_COMMAND_PREF, -1);
|
|
|
|
},
|
|
|
|
|
|
|
|
set _lastModifiedOnProcessCommands(value) {
|
|
|
|
Services.prefs.setIntPref(LAST_MODIFIED_ON_PROCESS_COMMAND_PREF, value);
|
|
|
|
},
|
|
|
|
|
2010-05-06 04:16:17 +04:00
|
|
|
// Always sync client data as it controls other sync behavior
|
2015-09-23 12:40:53 +03:00
|
|
|
get enabled() {
|
|
|
|
return true;
|
|
|
|
},
|
2010-05-06 04:16:17 +04:00
|
|
|
|
2011-01-15 00:22:20 +03:00
|
|
|
get lastRecordUpload() {
|
|
|
|
return Svc.Prefs.get(this.name + ".lastRecordUpload", 0);
|
|
|
|
},
|
|
|
|
set lastRecordUpload(value) {
|
|
|
|
Svc.Prefs.set(this.name + ".lastRecordUpload", Math.floor(value));
|
|
|
|
},
|
|
|
|
|
2016-06-28 02:46:43 +03:00
|
|
|
get remoteClients() {
|
2016-07-19 09:30:01 +03:00
|
|
|
// return all non-stale clients for external consumption.
|
|
|
|
return Object.values(this._store._remoteClients).filter(v => !v.stale);
|
|
|
|
},
|
|
|
|
|
2017-03-02 08:14:51 +03:00
|
|
|
remoteClient(id) {
|
2016-07-19 09:30:01 +03:00
|
|
|
let client = this._store._remoteClients[id];
|
2017-03-02 08:14:51 +03:00
|
|
|
return client && !client.stale ? client : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
remoteClientExists(id) {
|
|
|
|
return !!this.remoteClient(id);
|
2016-06-28 02:46:43 +03:00
|
|
|
},
|
|
|
|
|
2010-03-17 02:39:08 +03:00
|
|
|
// Aggregate some stats on the composition of clients on this account
|
|
|
|
get stats() {
|
|
|
|
let stats = {
|
2016-01-13 07:55:00 +03:00
|
|
|
hasMobile: this.localType == DEVICE_TYPE_MOBILE,
|
2010-03-17 02:39:08 +03:00
|
|
|
names: [this.localName],
|
|
|
|
numClients: 1,
|
|
|
|
};
|
|
|
|
|
2015-10-18 21:52:58 +03:00
|
|
|
for (let id in this._store._remoteClients) {
|
2016-07-19 09:30:01 +03:00
|
|
|
let {name, type, stale} = this._store._remoteClients[id];
|
|
|
|
if (!stale) {
|
|
|
|
stats.hasMobile = stats.hasMobile || type == DEVICE_TYPE_MOBILE;
|
|
|
|
stats.names.push(name);
|
|
|
|
stats.numClients++;
|
|
|
|
}
|
2010-03-17 02:39:08 +03:00
|
|
|
}
|
2009-01-24 02:09:21 +03:00
|
|
|
|
2010-03-17 02:39:08 +03:00
|
|
|
return stats;
|
2009-01-24 02:09:21 +03:00
|
|
|
},
|
|
|
|
|
2014-02-05 20:08:14 +04:00
|
|
|
/**
|
|
|
|
* Obtain information about device types.
|
|
|
|
*
|
2017-01-25 10:11:05 +03:00
|
|
|
* Returns a Map of device types to integer counts. Guaranteed to include
|
|
|
|
* "desktop" (which will have at least 1 - this device) and "mobile" (which
|
|
|
|
* may have zero) counts. It almost certainly will include only these 2.
|
2014-02-05 20:08:14 +04:00
|
|
|
*/
|
|
|
|
get deviceTypes() {
|
|
|
|
let counts = new Map();
|
|
|
|
|
2017-01-25 10:11:05 +03:00
|
|
|
counts.set(this.localType, 1); // currently this must be DEVICE_TYPE_DESKTOP
|
|
|
|
counts.set(DEVICE_TYPE_MOBILE, 0);
|
2014-02-05 20:08:14 +04:00
|
|
|
|
2015-10-18 21:52:58 +03:00
|
|
|
for (let id in this._store._remoteClients) {
|
|
|
|
let record = this._store._remoteClients[id];
|
2016-07-19 09:30:01 +03:00
|
|
|
if (record.stale) {
|
|
|
|
continue; // pretend "stale" records don't exist.
|
|
|
|
}
|
2014-02-05 20:08:14 +04:00
|
|
|
let type = record.type;
|
|
|
|
if (!counts.has(type)) {
|
|
|
|
counts.set(type, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
counts.set(type, counts.get(type) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return counts;
|
|
|
|
},
|
|
|
|
|
2014-11-18 06:06:00 +03:00
|
|
|
get brandName() {
|
2017-03-06 20:39:52 +03:00
|
|
|
let brand = Services.strings.createBundle(
|
|
|
|
"chrome://branding/locale/brand.properties");
|
|
|
|
return brand.GetStringFromName("brandShortName");
|
2014-11-18 06:06:00 +03:00
|
|
|
},
|
|
|
|
|
2010-03-17 02:39:08 +03:00
|
|
|
get localName() {
|
2017-12-20 20:36:18 +03:00
|
|
|
return Utils.getDeviceName();
|
2009-11-20 10:31:04 +03:00
|
|
|
},
|
2015-09-23 12:40:53 +03:00
|
|
|
set localName(value) {
|
|
|
|
Svc.Prefs.set("client.name", value);
|
2016-04-06 03:44:39 +03:00
|
|
|
// Update the registration in the background.
|
2017-03-01 00:35:01 +03:00
|
|
|
this.fxAccounts.updateDeviceRegistration().catch(error => {
|
2016-04-06 03:44:39 +03:00
|
|
|
this._log.warn("failed to update fxa device registration", error);
|
|
|
|
});
|
2015-09-23 12:40:53 +03:00
|
|
|
},
|
2010-03-17 02:39:08 +03:00
|
|
|
|
2015-09-23 12:40:53 +03:00
|
|
|
get localType() {
|
2016-01-13 07:55:00 +03:00
|
|
|
return Utils.getDeviceType();
|
2015-09-23 12:40:53 +03:00
|
|
|
},
|
|
|
|
set localType(value) {
|
|
|
|
Svc.Prefs.set("client.type", value);
|
|
|
|
},
|
2009-02-27 09:36:14 +03:00
|
|
|
|
2016-04-11 21:10:40 +03:00
|
|
|
getClientName(id) {
|
|
|
|
if (id == this.localID) {
|
|
|
|
return this.localName;
|
|
|
|
}
|
|
|
|
let client = this._store._remoteClients[id];
|
|
|
|
return client ? client.name : "";
|
|
|
|
},
|
|
|
|
|
2016-08-02 20:09:30 +03:00
|
|
|
getClientFxaDeviceId(id) {
|
|
|
|
if (this._store._remoteClients[id]) {
|
|
|
|
return this._store._remoteClients[id].fxaDeviceId;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2010-03-13 03:14:09 +03:00
|
|
|
isMobile: function isMobile(id) {
|
2010-04-29 06:20:08 +04:00
|
|
|
if (this._store._remoteClients[id])
|
2016-01-13 07:55:00 +03:00
|
|
|
return this._store._remoteClients[id].type == DEVICE_TYPE_MOBILE;
|
2010-04-29 06:20:08 +04:00
|
|
|
return false;
|
2010-03-13 03:14:09 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _readCommands() {
|
|
|
|
let commands = await Utils.jsonLoad("commands", this);
|
|
|
|
return commands || {};
|
2016-08-11 21:01:15 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Low level function, do not use directly (use _addClientCommand instead).
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async _saveCommands(commands) {
|
|
|
|
try {
|
|
|
|
await Utils.jsonSave("commands", this, commands);
|
|
|
|
} catch (error) {
|
|
|
|
this._log.error("Failed to save JSON outgoing commands", error);
|
|
|
|
}
|
2016-08-11 21:01:15 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _prepareCommandsForUpload() {
|
|
|
|
try {
|
2017-10-15 21:50:30 +03:00
|
|
|
await Utils.jsonMove("commands", "commands-syncing", this);
|
2017-06-06 01:50:07 +03:00
|
|
|
} catch (e) {
|
|
|
|
// Ignore errors
|
|
|
|
}
|
|
|
|
let commands = await Utils.jsonLoad("commands-syncing", this);
|
|
|
|
return commands || {};
|
2016-08-11 21:01:15 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _deleteUploadedCommands() {
|
2016-08-11 21:01:15 +03:00
|
|
|
delete this._currentlySyncingCommands;
|
2017-06-06 01:50:07 +03:00
|
|
|
try {
|
|
|
|
await Utils.jsonRemove("commands-syncing", this);
|
|
|
|
} catch (err) {
|
|
|
|
this._log.error("Failed to delete syncing-commands file", err);
|
|
|
|
}
|
2016-08-11 21:01:15 +03:00
|
|
|
},
|
|
|
|
|
2017-03-02 08:14:51 +03:00
|
|
|
// Gets commands for a client we are yet to write to the server. Doesn't
|
|
|
|
// include commands for that client which are already on the server.
|
|
|
|
// We should rename this!
|
2017-06-06 01:50:07 +03:00
|
|
|
async getClientCommands(clientId) {
|
|
|
|
const allCommands = await this._readCommands();
|
2017-03-02 08:14:51 +03:00
|
|
|
return allCommands[clientId] || [];
|
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async removeLocalCommand(command) {
|
2017-03-02 08:14:51 +03:00
|
|
|
// the implementation of this engine is such that adding a command to
|
|
|
|
// the local client is how commands are deleted! ¯\_(ツ)_/¯
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._addClientCommand(this.localID, command);
|
2017-03-02 08:14:51 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _addClientCommand(clientId, command) {
|
2017-08-02 18:33:24 +03:00
|
|
|
return this.addClientCommandQueue = (async () => {
|
|
|
|
await this.addClientCommandQueue;
|
|
|
|
try {
|
|
|
|
const localCommands = await this._readCommands();
|
|
|
|
const localClientCommands = localCommands[clientId] || [];
|
|
|
|
const remoteClient = this._store._remoteClients[clientId];
|
2017-10-15 21:50:30 +03:00
|
|
|
let remoteClientCommands = [];
|
2017-08-02 18:33:24 +03:00
|
|
|
if (remoteClient && remoteClient.commands) {
|
|
|
|
remoteClientCommands = remoteClient.commands;
|
|
|
|
}
|
|
|
|
const clientCommands = localClientCommands.concat(remoteClientCommands);
|
|
|
|
if (hasDupeCommand(clientCommands, command)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
localCommands[clientId] = localClientCommands.concat(command);
|
|
|
|
await this._saveCommands(localCommands);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
// Failing to save a command should not "break the queue" of pending operations.
|
|
|
|
this._log.error(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})();
|
2016-08-11 21:01:15 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _removeClientCommands(clientId) {
|
|
|
|
const allCommands = await this._readCommands();
|
2017-01-25 01:05:12 +03:00
|
|
|
delete allCommands[clientId];
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._saveCommands(allCommands);
|
2017-01-25 01:05:12 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async updateKnownStaleClients() {
|
2017-06-06 21:28:00 +03:00
|
|
|
this._log.debug("Updating the known stale clients");
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._refreshKnownStaleClients();
|
2017-10-10 20:41:32 +03:00
|
|
|
let localFxADeviceId = await fxAccounts.getDeviceId();
|
|
|
|
// Process newer records first, so that if we hit a record with a device ID
|
|
|
|
// we've seen before, we can mark it stale immediately.
|
|
|
|
let clientList = Object.values(this._store._remoteClients).sort((a, b) =>
|
|
|
|
b.serverLastModified - a.serverLastModified);
|
|
|
|
let seenDeviceIds = new Set([localFxADeviceId]);
|
|
|
|
for (let client of clientList) {
|
|
|
|
// Clients might not have an `fxaDeviceId` if they fail the FxA
|
|
|
|
// registration process.
|
|
|
|
if (!client.fxaDeviceId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (this._knownStaleFxADeviceIds.includes(client.fxaDeviceId)) {
|
2017-06-06 21:28:00 +03:00
|
|
|
this._log.info(`Hiding stale client ${client.id} - in known stale clients list`);
|
|
|
|
client.stale = true;
|
2017-10-10 20:41:32 +03:00
|
|
|
} else if (seenDeviceIds.has(client.fxaDeviceId)) {
|
|
|
|
this._log.info(`Hiding stale client ${client.id}` +
|
|
|
|
` - duplicate device id ${client.fxaDeviceId}`);
|
|
|
|
client.stale = true;
|
|
|
|
} else {
|
|
|
|
seenDeviceIds.add(client.fxaDeviceId);
|
2017-06-06 21:28:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-03-01 00:35:01 +03:00
|
|
|
// We assume that clients not present in the FxA Device Manager list have been
|
|
|
|
// disconnected and so are stale
|
2017-06-06 01:50:07 +03:00
|
|
|
async _refreshKnownStaleClients() {
|
2017-04-24 20:52:57 +03:00
|
|
|
this._log.debug("Refreshing the known stale clients list");
|
2017-03-01 00:35:01 +03:00
|
|
|
let localClients = Object.values(this._store._remoteClients)
|
|
|
|
.filter(client => client.fxaDeviceId) // iOS client records don't have fxaDeviceId
|
|
|
|
.map(client => client.fxaDeviceId);
|
|
|
|
let fxaClients;
|
|
|
|
try {
|
2017-06-06 01:50:07 +03:00
|
|
|
let deviceList = await this.fxAccounts.getDeviceList();
|
|
|
|
fxaClients = deviceList.map(device => device.id);
|
2017-03-01 00:35:01 +03:00
|
|
|
} catch (ex) {
|
2017-04-24 20:52:57 +03:00
|
|
|
this._log.error("Could not retrieve the FxA device list", ex);
|
2017-03-01 00:35:01 +03:00
|
|
|
this._knownStaleFxADeviceIds = [];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._knownStaleFxADeviceIds = Utils.arraySub(localClients, fxaClients);
|
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _syncStartup() {
|
2017-06-12 21:51:17 +03:00
|
|
|
this.isFirstSync = !this.lastRecordUpload;
|
2011-01-15 00:22:20 +03:00
|
|
|
// Reupload new client record periodically.
|
|
|
|
if (Date.now() / 1000 - this.lastRecordUpload > CLIENTS_TTL_REFRESH) {
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._tracker.addChangedID(this.localID);
|
2011-01-15 00:22:20 +03:00
|
|
|
this.lastRecordUpload = Date.now() / 1000;
|
|
|
|
}
|
2017-06-06 01:50:07 +03:00
|
|
|
return SyncEngine.prototype._syncStartup.call(this);
|
2011-01-15 00:22:20 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _processIncoming() {
|
2016-04-01 20:55:10 +03:00
|
|
|
// Fetch all records from the server.
|
|
|
|
this.lastSync = 0;
|
2016-04-15 19:00:59 +03:00
|
|
|
this._incomingClients = {};
|
2016-04-01 20:55:10 +03:00
|
|
|
try {
|
2017-06-06 01:50:07 +03:00
|
|
|
await SyncEngine.prototype._processIncoming.call(this);
|
2017-06-06 21:28:00 +03:00
|
|
|
// Refresh the known stale clients list at startup and when we receive
|
|
|
|
// "device connected/disconnected" push notifications.
|
2017-03-01 00:35:01 +03:00
|
|
|
if (!this._knownStaleFxADeviceIds) {
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._refreshKnownStaleClients();
|
2017-03-01 00:35:01 +03:00
|
|
|
}
|
2016-04-01 20:55:10 +03:00
|
|
|
// Since clients are synced unconditionally, any records in the local store
|
|
|
|
// that don't exist on the server must be for disconnected clients. Remove
|
|
|
|
// them, so that we don't upload records with commands for clients that will
|
|
|
|
// never see them. We also do this to filter out stale clients from the
|
|
|
|
// tabs collection, since showing their list of tabs is confusing.
|
2016-04-15 19:00:59 +03:00
|
|
|
for (let id in this._store._remoteClients) {
|
|
|
|
if (!this._incomingClients[id]) {
|
|
|
|
this._log.info(`Removing local state for deleted client ${id}`);
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._removeRemoteClient(id);
|
2016-04-15 19:00:59 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 20:41:32 +03:00
|
|
|
let localFxADeviceId = await fxAccounts.getDeviceId();
|
2016-04-15 19:00:59 +03:00
|
|
|
// Bug 1264498: Mobile clients don't remove themselves from the clients
|
2016-07-19 09:30:01 +03:00
|
|
|
// collection when the user disconnects Sync, so we mark as stale clients
|
2016-04-15 19:00:59 +03:00
|
|
|
// with the same name that haven't synced in over a week.
|
2016-07-19 09:30:01 +03:00
|
|
|
// (Note we can't simply delete them, or we re-apply them next sync - see
|
|
|
|
// bug 1287687)
|
2017-10-03 21:45:11 +03:00
|
|
|
this._localClientLastModified = Math.round(this._incomingClients[this.localID]);
|
2016-04-15 19:00:59 +03:00
|
|
|
delete this._incomingClients[this.localID];
|
|
|
|
let names = new Set([this.localName]);
|
2017-10-10 20:41:32 +03:00
|
|
|
let seenDeviceIds = new Set([localFxADeviceId]);
|
|
|
|
let idToLastModifiedList = Object.entries(this._incomingClients)
|
|
|
|
.sort((a, b) => b[1] - a[1]);
|
|
|
|
for (let [id, serverLastModified] of idToLastModifiedList) {
|
2016-04-15 19:00:59 +03:00
|
|
|
let record = this._store._remoteClients[id];
|
2017-02-27 04:44:12 +03:00
|
|
|
// stash the server last-modified time on the record.
|
|
|
|
record.serverLastModified = serverLastModified;
|
2017-03-01 00:35:01 +03:00
|
|
|
if (record.fxaDeviceId && this._knownStaleFxADeviceIds.includes(record.fxaDeviceId)) {
|
|
|
|
this._log.info(`Hiding stale client ${id} - in known stale clients list`);
|
|
|
|
record.stale = true;
|
|
|
|
}
|
2016-04-15 19:00:59 +03:00
|
|
|
if (!names.has(record.name)) {
|
2017-10-10 20:41:32 +03:00
|
|
|
if (record.fxaDeviceId) {
|
|
|
|
seenDeviceIds.add(record.fxaDeviceId);
|
|
|
|
}
|
2016-04-15 19:00:59 +03:00
|
|
|
names.add(record.name);
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-02 21:30:59 +03:00
|
|
|
let remoteAge = Resource.serverTime - this._incomingClients[id];
|
2016-04-15 19:00:59 +03:00
|
|
|
if (remoteAge > STALE_CLIENT_REMOTE_AGE) {
|
|
|
|
this._log.info(`Hiding stale client ${id} with age ${remoteAge}`);
|
2016-07-19 09:30:01 +03:00
|
|
|
record.stale = true;
|
2017-10-10 20:41:32 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (record.fxaDeviceId && seenDeviceIds.has(record.fxaDeviceId)) {
|
|
|
|
this._log.info(`Hiding stale client ${record.id}` +
|
|
|
|
` - duplicate device id ${record.fxaDeviceId}`);
|
|
|
|
record.stale = true;
|
|
|
|
} else if (record.fxaDeviceId) {
|
|
|
|
seenDeviceIds.add(record.fxaDeviceId);
|
2016-04-15 19:00:59 +03:00
|
|
|
}
|
2016-04-01 20:55:10 +03:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this._incomingClients = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _uploadOutgoing() {
|
|
|
|
this._currentlySyncingCommands = await this._prepareCommandsForUpload();
|
2016-08-11 21:01:15 +03:00
|
|
|
const clientWithPendingCommands = Object.keys(this._currentlySyncingCommands);
|
|
|
|
for (let clientId of clientWithPendingCommands) {
|
|
|
|
if (this._store._remoteClients[clientId] || this.localID == clientId) {
|
2016-09-06 21:39:13 +03:00
|
|
|
this._modified.set(clientId, 0);
|
2016-08-11 21:01:15 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-27 04:44:12 +03:00
|
|
|
let updatedIDs = this._modified.ids();
|
2017-06-06 01:50:07 +03:00
|
|
|
await SyncEngine.prototype._uploadOutgoing.call(this);
|
2017-02-27 04:44:12 +03:00
|
|
|
// Record the response time as the server time for each item we uploaded.
|
|
|
|
for (let id of updatedIDs) {
|
|
|
|
if (id != this.localID) {
|
|
|
|
this._store._remoteClients[id].serverLastModified = this.lastSync;
|
|
|
|
}
|
|
|
|
}
|
2016-04-05 01:39:37 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _onRecordsWritten(succeeded, failed) {
|
2016-08-11 21:01:15 +03:00
|
|
|
// Reconcile the status of the local records with what we just wrote on the
|
|
|
|
// server
|
|
|
|
for (let id of succeeded) {
|
|
|
|
const commandChanges = this._currentlySyncingCommands[id];
|
|
|
|
if (id == this.localID) {
|
2017-06-12 21:51:17 +03:00
|
|
|
if (this.isFirstSync) {
|
|
|
|
this._log.info("Uploaded our client record for the first time, notifying other clients.");
|
2017-10-11 21:58:21 +03:00
|
|
|
this._notifyClientRecordUploaded();
|
2017-06-12 21:51:17 +03:00
|
|
|
}
|
2016-08-11 21:01:15 +03:00
|
|
|
if (this.localCommands) {
|
|
|
|
this.localCommands = this.localCommands.filter(command => !hasDupeCommand(commandChanges, command));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const clientRecord = this._store._remoteClients[id];
|
|
|
|
if (!commandChanges || !clientRecord) {
|
|
|
|
// should be impossible, else we wouldn't have been writing it.
|
|
|
|
this._log.warn("No command/No record changes for a client we uploaded");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// fixup the client record, so our copy of _remoteClients matches what we uploaded.
|
2017-06-06 01:50:07 +03:00
|
|
|
this._store._remoteClients[id] = await this._store.createRecord(id);
|
2016-08-11 21:01:15 +03:00
|
|
|
// we could do better and pass the reference to the record we just uploaded,
|
|
|
|
// but this will do for now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-add failed commands
|
|
|
|
for (let id of failed) {
|
|
|
|
const commandChanges = this._currentlySyncingCommands[id];
|
|
|
|
if (!commandChanges) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._addClientCommand(id, commandChanges);
|
2016-08-11 21:01:15 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._deleteUploadedCommands();
|
2016-08-11 21:01:15 +03:00
|
|
|
|
2016-08-02 20:09:30 +03:00
|
|
|
// Notify other devices that their own client collection changed
|
|
|
|
const idsToNotify = succeeded.reduce((acc, id) => {
|
|
|
|
if (id == this.localID) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
const fxaDeviceId = this.getClientFxaDeviceId(id);
|
|
|
|
return fxaDeviceId ? acc.concat(fxaDeviceId) : acc;
|
|
|
|
}, []);
|
|
|
|
if (idsToNotify.length > 0) {
|
2017-10-11 21:58:21 +03:00
|
|
|
this._notifyOtherClientsModified(idsToNotify);
|
2016-08-02 20:09:30 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-11 21:58:21 +03:00
|
|
|
_notifyOtherClientsModified(ids) {
|
|
|
|
// We are not waiting on this promise on purpose.
|
|
|
|
this._notifyCollectionChanged(ids, NOTIFY_TAB_SENT_TTL_SECS,
|
|
|
|
COLLECTION_MODIFIED_REASON_SENDTAB);
|
|
|
|
},
|
|
|
|
|
|
|
|
_notifyClientRecordUploaded() {
|
|
|
|
// We are not waiting on this promise on purpose.
|
|
|
|
this._notifyCollectionChanged(null, 0, COLLECTION_MODIFIED_REASON_FIRSTSYNC);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {?string[]} ids FxA Client IDs to notify. null means everyone else.
|
|
|
|
* @param {number} ttl TTL of the push notification.
|
|
|
|
* @param {string} reason Reason for sending this push notification.
|
|
|
|
*/
|
|
|
|
async _notifyCollectionChanged(ids, ttl, reason) {
|
2016-08-02 20:09:30 +03:00
|
|
|
const message = {
|
|
|
|
version: 1,
|
|
|
|
command: "sync:collection_changed",
|
|
|
|
data: {
|
2017-10-11 21:58:21 +03:00
|
|
|
collections: ["clients"],
|
|
|
|
reason
|
2016-08-02 20:09:30 +03:00
|
|
|
}
|
|
|
|
};
|
2017-06-12 21:51:17 +03:00
|
|
|
let excludedIds = null;
|
|
|
|
if (!ids) {
|
|
|
|
const localFxADeviceId = await fxAccounts.getDeviceId();
|
|
|
|
excludedIds = [localFxADeviceId];
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this.fxAccounts.notifyDevices(ids, excludedIds, message, ttl);
|
|
|
|
} catch (e) {
|
|
|
|
this._log.error("Could not notify of changes in the collection", e);
|
|
|
|
}
|
2016-08-02 20:09:30 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _syncFinish() {
|
2016-04-21 01:43:36 +03:00
|
|
|
// Record histograms for our device types, and also write them to a pref
|
2017-01-25 10:11:05 +03:00
|
|
|
// so non-histogram telemetry (eg, UITelemetry) and the sync scheduler
|
|
|
|
// has easy access to them, and so they are accurate even before we've
|
|
|
|
// successfully synced the first time after startup.
|
2017-12-20 20:36:18 +03:00
|
|
|
let deviceTypeCounts = this.deviceTypes;
|
|
|
|
for (let [deviceType, count] of deviceTypeCounts) {
|
2016-01-12 04:21:27 +03:00
|
|
|
let hid;
|
2016-04-21 01:43:36 +03:00
|
|
|
let prefName = this.name + ".devices.";
|
2016-01-12 04:21:27 +03:00
|
|
|
switch (deviceType) {
|
2017-01-25 10:11:05 +03:00
|
|
|
case DEVICE_TYPE_DESKTOP:
|
2016-01-12 04:21:27 +03:00
|
|
|
hid = "WEAVE_DEVICE_COUNT_DESKTOP";
|
2016-04-21 01:43:36 +03:00
|
|
|
prefName += "desktop";
|
2016-01-12 04:21:27 +03:00
|
|
|
break;
|
2017-01-25 10:11:05 +03:00
|
|
|
case DEVICE_TYPE_MOBILE:
|
2016-01-12 04:21:27 +03:00
|
|
|
hid = "WEAVE_DEVICE_COUNT_MOBILE";
|
2016-04-21 01:43:36 +03:00
|
|
|
prefName += "mobile";
|
2016-01-12 04:21:27 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this._log.warn(`Unexpected deviceType "${deviceType}" recording device telemetry.`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Services.telemetry.getHistogramById(hid).add(count);
|
2017-12-20 20:36:18 +03:00
|
|
|
// Optimization: only write the pref if it changed since our last sync.
|
|
|
|
if (this._lastDeviceCounts == null ||
|
|
|
|
this._lastDeviceCounts.get(prefName) != count) {
|
|
|
|
Svc.Prefs.set(prefName, count);
|
|
|
|
}
|
2016-01-12 04:21:27 +03:00
|
|
|
}
|
2017-12-20 20:36:18 +03:00
|
|
|
this._lastDeviceCounts = deviceTypeCounts;
|
2017-06-06 01:50:07 +03:00
|
|
|
return SyncEngine.prototype._syncFinish.call(this);
|
2016-01-12 04:21:27 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _reconcile(item) {
|
2016-04-01 20:55:10 +03:00
|
|
|
// Every incoming record is reconciled, so we use this to track the
|
|
|
|
// contents of the collection on the server.
|
2016-04-15 19:00:59 +03:00
|
|
|
this._incomingClients[item.id] = item.modified;
|
2016-04-01 20:55:10 +03:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
if (!(await this._store.itemExists(item.id))) {
|
2016-04-01 20:55:10 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Clients are synced unconditionally, so we'll always have new records.
|
|
|
|
// Unfortunately, this will cause the scheduler to use the immediate sync
|
|
|
|
// interval for the multi-device case, instead of the active interval. We
|
|
|
|
// work around this by updating the record during reconciliation, and
|
|
|
|
// returning false to indicate that the record doesn't need to be applied
|
|
|
|
// later.
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._store.update(item);
|
2016-04-01 20:55:10 +03:00
|
|
|
return false;
|
2009-12-11 05:39:51 +03:00
|
|
|
},
|
|
|
|
|
2010-02-20 00:36:42 +03:00
|
|
|
// Treat reset the same as wiping for locally cached clients
|
2017-06-06 01:50:07 +03:00
|
|
|
async _resetClient() {
|
|
|
|
await this._wipeClient();
|
2015-01-25 10:50:01 +03:00
|
|
|
},
|
2010-02-20 00:36:42 +03:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _wipeClient() {
|
|
|
|
await SyncEngine.prototype._resetClient.call(this);
|
2017-03-01 00:35:01 +03:00
|
|
|
this._knownStaleFxADeviceIds = null;
|
2016-04-05 01:39:37 +03:00
|
|
|
delete this.localCommands;
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._store.wipe();
|
|
|
|
try {
|
|
|
|
await Utils.jsonRemove("commands", this);
|
|
|
|
} catch (err) {
|
|
|
|
this._log.warn("Could not delete commands.json", err);
|
|
|
|
}
|
|
|
|
try {
|
2017-10-15 21:50:30 +03:00
|
|
|
await Utils.jsonRemove("commands-syncing", this);
|
2017-06-06 01:50:07 +03:00
|
|
|
} catch (err) {
|
|
|
|
this._log.warn("Could not delete commands-syncing.json", err);
|
|
|
|
}
|
2011-03-01 22:56:29 +03:00
|
|
|
},
|
2011-04-09 01:51:55 +04:00
|
|
|
|
2017-04-11 16:40:53 +03:00
|
|
|
async removeClientData() {
|
2012-09-15 03:02:32 +04:00
|
|
|
let res = this.service.resource(this.engineURL + "/" + this.localID);
|
2017-04-11 16:40:53 +03:00
|
|
|
await res.delete();
|
2011-04-09 01:51:55 +04:00
|
|
|
},
|
|
|
|
|
2011-03-01 22:56:29 +03:00
|
|
|
// Override the default behavior to delete bad records from the server.
|
2017-06-06 01:50:07 +03:00
|
|
|
async handleHMACMismatch(item, mayRetry) {
|
2011-03-01 22:56:29 +03:00
|
|
|
this._log.debug("Handling HMAC mismatch for " + item.id);
|
2011-07-27 08:48:50 +04:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
let base = await SyncEngine.prototype.handleHMACMismatch.call(this, item, mayRetry);
|
2011-03-21 02:10:40 +03:00
|
|
|
if (base != SyncEngine.kRecoveryStrategy.error)
|
|
|
|
return base;
|
2011-03-01 22:56:29 +03:00
|
|
|
|
|
|
|
// It's a bad client record. Save it to be deleted at the end of the sync.
|
|
|
|
this._log.debug("Bad client record detected. Scheduling for deletion.");
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._deleteId(item.id);
|
2011-03-01 22:56:29 +03:00
|
|
|
|
2011-03-21 02:10:40 +03:00
|
|
|
// Neither try again nor error; we're going to delete it.
|
|
|
|
return SyncEngine.kRecoveryStrategy.ignore;
|
2011-08-05 03:19:02 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hash of valid commands that the client knows about. The key is a command
|
|
|
|
* and the value is a hash containing information about the command such as
|
2017-11-10 23:57:33 +03:00
|
|
|
* number of arguments, description, and importance (lower importance numbers
|
|
|
|
* indicate higher importance.
|
2011-08-05 03:19:02 +04:00
|
|
|
*/
|
|
|
|
_commands: {
|
2017-11-10 23:57:33 +03:00
|
|
|
resetAll: { args: 0, importance: 0, desc: "Clear temporary local data for all engines" },
|
|
|
|
resetEngine: { args: 1, importance: 0, desc: "Clear temporary local data for engine" },
|
|
|
|
wipeAll: { args: 0, importance: 0, desc: "Delete all client data for all engines" },
|
|
|
|
wipeEngine: { args: 1, importance: 0, desc: "Delete all client data for engine" },
|
|
|
|
logout: { args: 0, importance: 0, desc: "Log out client" },
|
|
|
|
displayURI: { args: 3, importance: 1, desc: "Instruct a client to display a URI" },
|
|
|
|
repairRequest: { args: 1, importance: 2, desc: "Instruct a client to initiate a repair" },
|
|
|
|
repairResponse: { args: 1, importance: 2, desc: "Instruct a client a repair request is complete" },
|
2011-08-05 03:19:02 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a command+args pair to a specific client.
|
|
|
|
*
|
|
|
|
* @param command Command string
|
|
|
|
* @param args Array of arguments/data for command
|
|
|
|
* @param clientId Client to send command to
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async _sendCommandToClient(command, args, clientId, telemetryExtra) {
|
2011-08-05 03:19:02 +04:00
|
|
|
this._log.trace("Sending " + command + " to " + clientId);
|
|
|
|
|
|
|
|
let client = this._store._remoteClients[clientId];
|
|
|
|
if (!client) {
|
|
|
|
throw new Error("Unknown remote client ID: '" + clientId + "'.");
|
|
|
|
}
|
2016-07-19 09:30:01 +03:00
|
|
|
if (client.stale) {
|
|
|
|
throw new Error("Stale remote client ID: '" + clientId + "'.");
|
|
|
|
}
|
2011-08-05 03:19:02 +04:00
|
|
|
|
|
|
|
let action = {
|
2017-01-10 20:09:02 +03:00
|
|
|
command,
|
|
|
|
args,
|
2017-02-10 08:49:33 +03:00
|
|
|
// We send the flowID to the other client so *it* can report it in its
|
|
|
|
// telemetry - we record it in ours below.
|
|
|
|
flowID: telemetryExtra.flowID,
|
2011-08-05 03:19:02 +04:00
|
|
|
};
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
if ((await this._addClientCommand(clientId, action))) {
|
2016-11-04 04:46:57 +03:00
|
|
|
this._log.trace(`Client ${clientId} got a new action`, [command, args]);
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._tracker.addChangedID(clientId);
|
2016-11-04 04:46:57 +03:00
|
|
|
try {
|
2017-02-10 08:49:33 +03:00
|
|
|
telemetryExtra.deviceID = this.service.identity.hashedDeviceID(clientId);
|
2016-11-04 04:46:57 +03:00
|
|
|
} catch (_) {}
|
2017-02-10 08:49:33 +03:00
|
|
|
|
|
|
|
this.service.recordTelemetryEvent("sendcommand", command, undefined, telemetryExtra);
|
2016-11-04 04:46:57 +03:00
|
|
|
} else {
|
|
|
|
this._log.trace(`Client ${clientId} got a duplicate action`, [command, args]);
|
|
|
|
}
|
2011-08-05 03:19:02 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the local client has any remote commands and perform them.
|
|
|
|
*
|
|
|
|
* @return false to abort sync
|
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async processIncomingCommands() {
|
|
|
|
return this._notify("clients:process-commands", "", async function() {
|
2017-10-03 21:45:11 +03:00
|
|
|
if (!this.localCommands ||
|
|
|
|
(this._lastModifiedOnProcessCommands == this._localClientLastModified
|
|
|
|
&& !this.ignoreLastModifiedOnProcessCommands)) {
|
2016-08-23 02:17:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-03 21:45:11 +03:00
|
|
|
this._lastModifiedOnProcessCommands = this._localClientLastModified;
|
2016-08-11 21:01:15 +03:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
const clearedCommands = await this._readCommands()[this.localID];
|
2016-08-11 21:01:15 +03:00
|
|
|
const commands = this.localCommands.filter(command => !hasDupeCommand(clearedCommands, command));
|
2017-03-02 08:14:51 +03:00
|
|
|
let didRemoveCommand = false;
|
2016-08-23 02:17:59 +03:00
|
|
|
let URIsToDisplay = [];
|
2016-08-11 21:01:15 +03:00
|
|
|
// Process each command in order.
|
|
|
|
for (let rawCommand of commands) {
|
2017-03-02 08:14:51 +03:00
|
|
|
let shouldRemoveCommand = true; // most commands are auto-removed.
|
2016-11-04 04:46:57 +03:00
|
|
|
let {command, args, flowID} = rawCommand;
|
2017-03-02 08:14:51 +03:00
|
|
|
this._log.debug("Processing command " + command, args);
|
2011-08-05 03:19:02 +04:00
|
|
|
|
2016-11-04 04:46:57 +03:00
|
|
|
this.service.recordTelemetryEvent("processcommand", command, undefined,
|
|
|
|
{ flowID });
|
|
|
|
|
2011-08-05 03:19:02 +04:00
|
|
|
let engines = [args[0]];
|
|
|
|
switch (command) {
|
|
|
|
case "resetAll":
|
|
|
|
engines = null;
|
|
|
|
// Fallthrough
|
|
|
|
case "resetEngine":
|
2017-06-06 01:50:07 +03:00
|
|
|
await this.service.resetClient(engines);
|
2011-08-05 03:19:02 +04:00
|
|
|
break;
|
|
|
|
case "wipeAll":
|
|
|
|
engines = null;
|
|
|
|
// Fallthrough
|
|
|
|
case "wipeEngine":
|
2017-06-06 01:50:07 +03:00
|
|
|
await this.service.wipeClient(engines);
|
2011-08-05 03:19:02 +04:00
|
|
|
break;
|
|
|
|
case "logout":
|
2012-08-30 01:43:41 +04:00
|
|
|
this.service.logout();
|
2011-08-05 03:19:02 +04:00
|
|
|
return false;
|
2011-08-09 20:23:55 +04:00
|
|
|
case "displayURI":
|
2016-07-07 23:33:29 +03:00
|
|
|
let [uri, clientId, title] = args;
|
|
|
|
URIsToDisplay.push({ uri, clientId, title });
|
2011-08-09 20:23:55 +04:00
|
|
|
break;
|
2017-03-02 08:14:51 +03:00
|
|
|
case "repairResponse": {
|
|
|
|
// When we send a repair request to another device that understands
|
|
|
|
// it, that device will send a response indicating what it did.
|
|
|
|
let response = args[0];
|
|
|
|
let requestor = getRepairRequestor(response.collection);
|
|
|
|
if (!requestor) {
|
|
|
|
this._log.warn("repairResponse for unknown collection", response);
|
|
|
|
break;
|
|
|
|
}
|
2017-06-06 01:50:07 +03:00
|
|
|
if (!(await requestor.continueRepairs(response))) {
|
2017-03-02 08:14:51 +03:00
|
|
|
this._log.warn("repairResponse couldn't continue the repair", response);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "repairRequest": {
|
|
|
|
// Another device has sent us a request to make some repair.
|
|
|
|
let request = args[0];
|
|
|
|
let responder = getRepairResponder(request.collection);
|
|
|
|
if (!responder) {
|
|
|
|
this._log.warn("repairRequest for unknown collection", request);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
2017-06-06 01:50:07 +03:00
|
|
|
if ((await responder.repair(request, rawCommand))) {
|
2017-03-02 08:14:51 +03:00
|
|
|
// We've started a repair - once that collection has synced it
|
|
|
|
// will write a "response" command and arrange for this repair
|
|
|
|
// request to be removed from the local command list - if we
|
|
|
|
// removed it now we might fail to write a response in cases of
|
|
|
|
// premature shutdown etc.
|
|
|
|
shouldRemoveCommand = false;
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
if (Async.isShutdownException(ex)) {
|
|
|
|
// Let's assume this error was caused by the shutdown, so let
|
|
|
|
// it try again next time.
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
// otherwise there are no second chances - the command is removed
|
|
|
|
// and will not be tried again.
|
|
|
|
// (Note that this shouldn't be hit in the normal case - it's
|
|
|
|
// expected the responder will handle all reasonable failures and
|
|
|
|
// write a response indicating that it couldn't do what was asked.)
|
|
|
|
this._log.error("Failed to handle a repair request", ex);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-08-05 03:19:02 +04:00
|
|
|
default:
|
2017-03-02 08:14:51 +03:00
|
|
|
this._log.warn("Received an unknown command: " + command);
|
2011-08-05 03:19:02 +04:00
|
|
|
break;
|
|
|
|
}
|
2016-08-11 21:01:15 +03:00
|
|
|
// Add the command to the "cleared" commands list
|
2017-03-02 08:14:51 +03:00
|
|
|
if (shouldRemoveCommand) {
|
2017-06-06 01:50:07 +03:00
|
|
|
await this.removeLocalCommand(rawCommand);
|
2017-03-02 08:14:51 +03:00
|
|
|
didRemoveCommand = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (didRemoveCommand) {
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._tracker.addChangedID(this.localID);
|
2011-08-05 03:19:02 +04:00
|
|
|
}
|
2016-08-11 21:01:15 +03:00
|
|
|
|
2016-07-07 23:33:29 +03:00
|
|
|
if (URIsToDisplay.length) {
|
|
|
|
this._handleDisplayURIs(URIsToDisplay);
|
|
|
|
}
|
2011-08-05 03:19:02 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
})();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and sends a command to a client or all clients.
|
|
|
|
*
|
|
|
|
* Calling this does not actually sync the command data to the server. If the
|
|
|
|
* client already has the command/args pair, it won't receive a duplicate
|
|
|
|
* command.
|
2017-06-06 01:50:07 +03:00
|
|
|
* This method is async since it writes the command to a file.
|
2011-08-05 03:19:02 +04:00
|
|
|
*
|
|
|
|
* @param command
|
|
|
|
* Command to invoke on remote clients
|
|
|
|
* @param args
|
|
|
|
* Array of arguments to give to the command
|
|
|
|
* @param clientId
|
|
|
|
* Client ID to send command to. If undefined, send to all remote
|
|
|
|
* clients.
|
2016-11-04 04:46:57 +03:00
|
|
|
* @param flowID
|
|
|
|
* A unique identifier used to track success for this operation across
|
|
|
|
* devices.
|
2011-08-05 03:19:02 +04:00
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async sendCommand(command, args, clientId = null, telemetryExtra = {}) {
|
2011-08-05 03:19:02 +04:00
|
|
|
let commandData = this._commands[command];
|
|
|
|
// Don't send commands that we don't know about.
|
|
|
|
if (!commandData) {
|
|
|
|
this._log.error("Unknown command to send: " + command);
|
|
|
|
return;
|
2017-01-13 02:24:52 +03:00
|
|
|
} else if (!args || args.length != commandData.args) {
|
|
|
|
// Don't send a command with the wrong number of arguments.
|
2011-08-05 03:19:02 +04:00
|
|
|
this._log.error("Expected " + commandData.args + " args for '" +
|
|
|
|
command + "', but got " + args);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-10 08:49:33 +03:00
|
|
|
// We allocate a "flowID" here, so it is used for each client.
|
|
|
|
telemetryExtra = Object.assign({}, telemetryExtra); // don't clobber the caller's object
|
|
|
|
if (!telemetryExtra.flowID) {
|
|
|
|
telemetryExtra.flowID = Utils.makeGUID();
|
|
|
|
}
|
|
|
|
|
2011-08-05 03:19:02 +04:00
|
|
|
if (clientId) {
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._sendCommandToClient(command, args, clientId, telemetryExtra);
|
2011-08-05 03:19:02 +04:00
|
|
|
} else {
|
2016-08-09 05:40:23 +03:00
|
|
|
for (let [id, record] of Object.entries(this._store._remoteClients)) {
|
2016-07-19 09:30:01 +03:00
|
|
|
if (!record.stale) {
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._sendCommandToClient(command, args, id, telemetryExtra);
|
2016-07-19 09:30:01 +03:00
|
|
|
}
|
2011-08-05 03:19:02 +04:00
|
|
|
}
|
|
|
|
}
|
2011-08-09 20:23:55 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a URI to another client for display.
|
|
|
|
*
|
|
|
|
* A side effect is the score is increased dramatically to incur an
|
|
|
|
* immediate sync.
|
|
|
|
*
|
|
|
|
* If an unknown client ID is specified, sendCommand() will throw an
|
|
|
|
* Error object.
|
|
|
|
*
|
|
|
|
* @param uri
|
|
|
|
* URI (as a string) to send and display on the remote client
|
|
|
|
* @param clientId
|
|
|
|
* ID of client to send the command to. If not defined, will be sent
|
|
|
|
* to all remote clients.
|
2012-03-27 21:13:52 +04:00
|
|
|
* @param title
|
|
|
|
* Title of the page being sent.
|
2011-08-09 20:23:55 +04:00
|
|
|
*/
|
2017-06-06 01:50:07 +03:00
|
|
|
async sendURIToClientForDisplay(uri, clientId, title) {
|
2017-11-10 23:57:33 +03:00
|
|
|
this._log.trace("Sending URI to client: " + uri + " -> " +
|
2012-03-27 21:13:52 +04:00
|
|
|
clientId + " (" + title + ")");
|
2017-06-06 01:50:07 +03:00
|
|
|
await this.sendCommand("displayURI", [uri, this.localID, title], clientId);
|
2011-08-09 20:23:55 +04:00
|
|
|
|
2012-08-30 01:43:41 +04:00
|
|
|
this._tracker.score += SCORE_INCREMENT_XLARGE;
|
2011-08-09 20:23:55 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-07-07 23:33:29 +03:00
|
|
|
* Handle a bunch of received 'displayURI' commands.
|
2011-08-09 20:23:55 +04:00
|
|
|
*
|
2016-07-07 23:33:29 +03:00
|
|
|
* Interested parties should observe the "weave:engine:clients:display-uris"
|
|
|
|
* topic. The callback will receive an array as the subject parameter
|
|
|
|
* containing objects with the following keys:
|
2011-08-09 20:23:55 +04:00
|
|
|
*
|
2012-03-28 23:53:57 +04:00
|
|
|
* uri URI (string) that is requested for display.
|
|
|
|
* clientId ID of client that sent the command.
|
|
|
|
* title Title of page that loaded URI (likely) corresponds to.
|
2011-08-09 20:23:55 +04:00
|
|
|
*
|
|
|
|
* The 'data' parameter to the callback will not be defined.
|
|
|
|
*
|
2016-07-07 23:33:29 +03:00
|
|
|
* @param uris
|
|
|
|
* An array containing URI objects to display
|
|
|
|
* @param uris[].uri
|
2011-08-09 20:23:55 +04:00
|
|
|
* String URI that was received
|
2016-07-07 23:33:29 +03:00
|
|
|
* @param uris[].clientId
|
2011-08-09 20:23:55 +04:00
|
|
|
* ID of client that sent URI
|
2016-07-07 23:33:29 +03:00
|
|
|
* @param uris[].title
|
2012-03-28 23:53:57 +04:00
|
|
|
* String title of page that URI corresponds to. Older clients may not
|
|
|
|
* send this.
|
2011-08-09 20:23:55 +04:00
|
|
|
*/
|
2016-07-07 23:33:29 +03:00
|
|
|
_handleDisplayURIs: function _handleDisplayURIs(uris) {
|
|
|
|
Svc.Obs.notify("weave:engine:clients:display-uris", uris);
|
2016-04-01 20:55:10 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async _removeRemoteClient(id) {
|
2016-04-01 20:55:10 +03:00
|
|
|
delete this._store._remoteClients[id];
|
2018-01-05 02:07:10 +03:00
|
|
|
await this._tracker.removeChangedID(id);
|
2017-06-06 01:50:07 +03:00
|
|
|
await this._removeClientCommands(id);
|
2017-01-25 01:05:12 +03:00
|
|
|
this._modified.delete(id);
|
2016-04-01 20:55:10 +03:00
|
|
|
},
|
2009-01-07 00:54:18 +03:00
|
|
|
};
|
|
|
|
|
2012-08-30 01:43:41 +04:00
|
|
|
function ClientStore(name, engine) {
|
|
|
|
Store.call(this, name, engine);
|
2009-01-07 00:54:18 +03:00
|
|
|
}
|
|
|
|
ClientStore.prototype = {
|
|
|
|
__proto__: Store.prototype,
|
|
|
|
|
2016-08-11 21:01:15 +03:00
|
|
|
_remoteClients: {},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async create(record) {
|
|
|
|
await this.update(record);
|
2015-01-25 10:50:01 +03:00
|
|
|
},
|
2009-04-01 10:56:32 +04:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async update(record) {
|
2016-04-05 01:39:37 +03:00
|
|
|
if (record.id == this.engine.localID) {
|
2016-08-11 21:01:15 +03:00
|
|
|
// Only grab commands from the server; local name/type always wins
|
|
|
|
this.engine.localCommands = record.commands;
|
2016-04-05 01:39:37 +03:00
|
|
|
} else {
|
2010-03-17 02:39:08 +03:00
|
|
|
this._remoteClients[record.id] = record.cleartext;
|
2016-04-01 20:55:10 +03:00
|
|
|
}
|
2009-01-07 00:54:18 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async createRecord(id, collection) {
|
2010-11-30 03:41:17 +03:00
|
|
|
let record = new ClientsRec(collection, id);
|
2009-02-20 12:52:07 +03:00
|
|
|
|
2016-08-11 21:01:15 +03:00
|
|
|
const commandsChanges = this.engine._currentlySyncingCommands ?
|
|
|
|
this.engine._currentlySyncingCommands[id] :
|
|
|
|
[];
|
|
|
|
|
2010-03-17 02:39:08 +03:00
|
|
|
// Package the individual components into a record for the local client
|
2012-08-30 01:43:41 +04:00
|
|
|
if (id == this.engine.localID) {
|
2016-01-13 07:55:00 +03:00
|
|
|
try {
|
2017-06-06 01:50:07 +03:00
|
|
|
record.fxaDeviceId = await this.engine.fxAccounts.getDeviceId();
|
2017-01-10 20:09:02 +03:00
|
|
|
} catch (error) {
|
2016-01-13 07:55:00 +03:00
|
|
|
this._log.warn("failed to get fxa device id", error);
|
|
|
|
}
|
2012-08-30 01:43:41 +04:00
|
|
|
record.name = this.engine.localName;
|
|
|
|
record.type = this.engine.localType;
|
2014-01-07 06:41:12 +04:00
|
|
|
record.version = Services.appinfo.version;
|
2014-01-07 09:45:26 +04:00
|
|
|
record.protocols = SUPPORTED_PROTOCOL_VERSIONS;
|
2014-11-18 06:06:00 +03:00
|
|
|
|
2016-08-11 21:01:15 +03:00
|
|
|
// Substract the commands we recorded that we've already executed
|
|
|
|
if (commandsChanges && commandsChanges.length &&
|
|
|
|
this.engine.localCommands && this.engine.localCommands.length) {
|
|
|
|
record.commands = this.engine.localCommands.filter(command => !hasDupeCommand(commandsChanges, command));
|
|
|
|
}
|
|
|
|
|
2014-11-18 06:06:00 +03:00
|
|
|
// Optional fields.
|
2017-10-26 13:47:01 +03:00
|
|
|
record.os = Services.appinfo.OS; // "Darwin"
|
2014-11-18 06:06:00 +03:00
|
|
|
record.appPackage = Services.appinfo.ID;
|
2017-10-26 13:47:01 +03:00
|
|
|
record.application = this.engine.brandName; // "Nightly"
|
2014-11-18 06:06:00 +03:00
|
|
|
|
|
|
|
// We can't compute these yet.
|
|
|
|
// record.device = ""; // Bug 1100723
|
|
|
|
// record.formfactor = ""; // Bug 1100722
|
|
|
|
} else {
|
2017-02-27 04:44:12 +03:00
|
|
|
record.cleartext = Object.assign({}, this._remoteClients[id]);
|
|
|
|
delete record.cleartext.serverLastModified; // serverLastModified is a local only attribute.
|
2016-08-11 21:01:15 +03:00
|
|
|
|
|
|
|
// Add the commands we have to send
|
|
|
|
if (commandsChanges && commandsChanges.length) {
|
|
|
|
const recordCommands = record.cleartext.commands || [];
|
|
|
|
const newCommands = commandsChanges.filter(command => !hasDupeCommand(recordCommands, command));
|
|
|
|
record.cleartext.commands = recordCommands.concat(newCommands);
|
|
|
|
}
|
|
|
|
|
2016-07-19 09:30:01 +03:00
|
|
|
if (record.cleartext.stale) {
|
|
|
|
// It's almost certainly a logic error for us to upload a record we
|
|
|
|
// consider stale, so make log noise, but still remove the flag.
|
|
|
|
this._log.error(`Preparing to upload record ${id} that we consider stale`);
|
|
|
|
delete record.cleartext.stale;
|
|
|
|
}
|
2014-11-18 06:06:00 +03:00
|
|
|
}
|
2017-11-10 23:57:33 +03:00
|
|
|
if (record.commands) {
|
|
|
|
const maxPayloadSize = this.engine.service.getMemcacheMaxRecordPayloadSize();
|
|
|
|
let origOrder = new Map(record.commands.map((c, i) => [c, i]));
|
|
|
|
// we sort first by priority, and second by age (indicated by order in the
|
|
|
|
// original list)
|
|
|
|
let commands = record.commands.slice().sort((a, b) => {
|
|
|
|
let infoA = this.engine._commands[a.command];
|
|
|
|
let infoB = this.engine._commands[b.command];
|
|
|
|
// Treat unknown command types as highest priority, to allow us to add
|
|
|
|
// high priority commands in the future without worrying about clients
|
|
|
|
// removing them on each-other unnecessarially.
|
|
|
|
let importA = infoA ? infoA.importance : 0;
|
|
|
|
let importB = infoB ? infoB.importance : 0;
|
|
|
|
// Higher importantance numbers indicate that we care less, so they
|
|
|
|
// go to the end of the list where they'll be popped off.
|
|
|
|
let importDelta = importA - importB;
|
|
|
|
if (importDelta != 0) {
|
|
|
|
return importDelta;
|
|
|
|
}
|
|
|
|
let origIdxA = origOrder.get(a);
|
|
|
|
let origIdxB = origOrder.get(b);
|
|
|
|
// Within equivalent priorities, we put older entries near the end
|
|
|
|
// of the list, so that they are removed first.
|
|
|
|
return origIdxB - origIdxA;
|
|
|
|
});
|
|
|
|
let truncatedCommands = Utils.tryFitItems(commands, maxPayloadSize);
|
|
|
|
if (truncatedCommands.length != record.commands.length) {
|
|
|
|
this._log.warn(`Removing commands from client ${id} (from ${record.commands.length} to ${truncatedCommands.length})`);
|
|
|
|
// Restore original order.
|
|
|
|
record.commands = truncatedCommands.sort((a, b) =>
|
|
|
|
origOrder.get(a) - origOrder.get(b));
|
|
|
|
}
|
|
|
|
}
|
2009-04-01 10:56:32 +04:00
|
|
|
return record;
|
2009-01-07 00:54:18 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async itemExists(id) {
|
|
|
|
return id in (await this.getAllIDs());
|
2015-01-25 10:50:01 +03:00
|
|
|
},
|
2009-01-07 00:54:18 +03:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async getAllIDs() {
|
2010-03-17 02:39:08 +03:00
|
|
|
let ids = {};
|
2012-08-30 01:43:41 +04:00
|
|
|
ids[this.engine.localID] = true;
|
2010-03-17 02:39:08 +03:00
|
|
|
for (let id in this._remoteClients)
|
|
|
|
ids[id] = true;
|
|
|
|
return ids;
|
2009-01-07 00:54:18 +03:00
|
|
|
},
|
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async wipe() {
|
2010-03-17 02:39:08 +03:00
|
|
|
this._remoteClients = {};
|
2009-04-01 10:56:32 +04:00
|
|
|
},
|
2009-01-07 00:54:18 +03:00
|
|
|
};
|
2011-07-27 08:48:50 +04:00
|
|
|
|
2012-08-30 01:43:41 +04:00
|
|
|
function ClientsTracker(name, engine) {
|
|
|
|
Tracker.call(this, name, engine);
|
2011-07-27 08:48:50 +04:00
|
|
|
}
|
|
|
|
ClientsTracker.prototype = {
|
|
|
|
__proto__: Tracker.prototype,
|
|
|
|
|
|
|
|
_enabled: false,
|
|
|
|
|
2018-01-05 02:07:10 +03:00
|
|
|
onStart() {
|
|
|
|
Svc.Prefs.observe("client.name", this.asyncObserver);
|
|
|
|
},
|
|
|
|
onStop() {
|
|
|
|
Svc.Prefs.ignore("client.name", this.asyncObserver);
|
|
|
|
},
|
|
|
|
|
|
|
|
async observe(subject, topic, data) {
|
2011-07-27 08:48:50 +04:00
|
|
|
switch (topic) {
|
|
|
|
case "nsPref:changed":
|
|
|
|
this._log.debug("client.name preference changed");
|
2018-01-05 02:07:10 +03:00
|
|
|
await this.addChangedID(this.engine.localID);
|
2011-07-27 08:48:50 +04:00
|
|
|
this.score += SCORE_INCREMENT_XLARGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|