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
|
|
|
|
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
|
|
|
|
2015-10-07 15:03:21 +03:00
|
|
|
var {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
2009-01-07 00:54:18 +03:00
|
|
|
|
2016-01-13 07:55:00 +03:00
|
|
|
Cu.import("resource://services-common/async.js");
|
2012-04-06 10:26:06 +04:00
|
|
|
Cu.import("resource://services-common/stringbundle.js");
|
2010-06-17 01:30:08 +04:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
Cu.import("resource://services-sync/engines.js");
|
2011-01-19 03:23:30 +03:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
2016-04-15 19:00:59 +03:00
|
|
|
Cu.import("resource://services-sync/resource.js");
|
2010-06-17 01:30:08 +04:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2016-01-12 04:21:27 +03:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2009-01-07 00:54:18 +03:00
|
|
|
|
2016-01-13 07:55:00 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts",
|
|
|
|
"resource://gre/modules/FxAccounts.jsm");
|
|
|
|
|
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
|
|
|
|
2014-02-11 06:36:02 +04:00
|
|
|
const SUPPORTED_PROTOCOL_VERSIONS = ["1.1", "1.5"];
|
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);
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
"formfactor", "os", "appPackage", "application", "device"]);
|
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
|
|
|
|
|
|
|
// Reset the client on every startup so that we fetch recent clients
|
|
|
|
this._resetClient();
|
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,
|
2010-03-17 02:39:08 +03:00
|
|
|
|
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() {
|
|
|
|
return Object.values(this._store._remoteClients);
|
|
|
|
},
|
|
|
|
|
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) {
|
|
|
|
let {name, type} = this._store._remoteClients[id];
|
2016-01-13 07:55:00 +03:00
|
|
|
stats.hasMobile = stats.hasMobile || type == DEVICE_TYPE_MOBILE;
|
2010-03-17 02:39:08 +03:00
|
|
|
stats.names.push(name);
|
|
|
|
stats.numClients++;
|
|
|
|
}
|
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.
|
|
|
|
*
|
|
|
|
* Returns a Map of device types to integer counts.
|
|
|
|
*/
|
|
|
|
get deviceTypes() {
|
|
|
|
let counts = new Map();
|
|
|
|
|
|
|
|
counts.set(this.localType, 1);
|
|
|
|
|
2015-10-18 21:52:58 +03:00
|
|
|
for (let id in this._store._remoteClients) {
|
|
|
|
let record = this._store._remoteClients[id];
|
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;
|
|
|
|
},
|
|
|
|
|
2010-03-17 02:39:08 +03:00
|
|
|
get localID() {
|
|
|
|
// Generate a random GUID id we don't have one
|
|
|
|
let localID = Svc.Prefs.get("client.GUID", "");
|
|
|
|
return localID == "" ? this.localID = Utils.makeGUID() : localID;
|
2009-01-24 02:09:21 +03:00
|
|
|
},
|
2015-09-23 12:40:53 +03:00
|
|
|
set localID(value) {
|
|
|
|
Svc.Prefs.set("client.GUID", value);
|
|
|
|
},
|
2009-01-24 02:09:21 +03:00
|
|
|
|
2014-11-18 06:06:00 +03:00
|
|
|
get brandName() {
|
|
|
|
let brand = new StringBundle("chrome://branding/locale/brand.properties");
|
|
|
|
return brand.get("brandShortName");
|
|
|
|
},
|
|
|
|
|
2010-03-17 02:39:08 +03:00
|
|
|
get localName() {
|
2016-04-06 03:44:39 +03:00
|
|
|
let name = Utils.getDeviceName();
|
|
|
|
// If `getDeviceName` returns the default name, set the pref. FxA registers
|
|
|
|
// the device before syncing, so we don't need to update the registration
|
|
|
|
// in this case.
|
|
|
|
Svc.Prefs.set("client.name", name);
|
|
|
|
return name;
|
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.
|
|
|
|
fxAccounts.updateDeviceRegistration().catch(error => {
|
|
|
|
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-01 00:36:40 +03:00
|
|
|
remoteClientExists(id) {
|
|
|
|
return !!this._store._remoteClients[id];
|
|
|
|
},
|
|
|
|
|
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 : "";
|
|
|
|
},
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2011-01-15 00:22:20 +03:00
|
|
|
_syncStartup: function _syncStartup() {
|
|
|
|
// Reupload new client record periodically.
|
|
|
|
if (Date.now() / 1000 - this.lastRecordUpload > CLIENTS_TTL_REFRESH) {
|
|
|
|
this._tracker.addChangedID(this.localID);
|
|
|
|
this.lastRecordUpload = Date.now() / 1000;
|
|
|
|
}
|
|
|
|
SyncEngine.prototype._syncStartup.call(this);
|
|
|
|
},
|
|
|
|
|
2016-04-01 20:55:10 +03:00
|
|
|
_processIncoming() {
|
|
|
|
// 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 {
|
|
|
|
SyncEngine.prototype._processIncoming.call(this);
|
|
|
|
// 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}`);
|
|
|
|
this._removeRemoteClient(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Bug 1264498: Mobile clients don't remove themselves from the clients
|
|
|
|
// collection when the user disconnects Sync, so we filter out clients
|
|
|
|
// with the same name that haven't synced in over a week.
|
|
|
|
delete this._incomingClients[this.localID];
|
|
|
|
let names = new Set([this.localName]);
|
|
|
|
for (let id in this._incomingClients) {
|
|
|
|
let record = this._store._remoteClients[id];
|
|
|
|
if (!names.has(record.name)) {
|
|
|
|
names.add(record.name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let remoteAge = AsyncResource.serverTime - this._incomingClients[id];
|
|
|
|
if (remoteAge > STALE_CLIENT_REMOTE_AGE) {
|
|
|
|
this._log.info(`Hiding stale client ${id} with age ${remoteAge}`);
|
|
|
|
this._removeRemoteClient(id);
|
|
|
|
}
|
2016-04-01 20:55:10 +03:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this._incomingClients = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-05 01:39:37 +03:00
|
|
|
_uploadOutgoing() {
|
|
|
|
this._clearedCommands = null;
|
|
|
|
SyncEngine.prototype._uploadOutgoing.call(this);
|
|
|
|
},
|
|
|
|
|
2016-01-12 04:21:27 +03:00
|
|
|
_syncFinish() {
|
2016-04-21 01:43:36 +03:00
|
|
|
// Record histograms for our device types, and also write them to a pref
|
|
|
|
// so non-histogram telemetry (eg, UITelemetry) has easy access to them.
|
2016-01-12 04:21:27 +03:00
|
|
|
for (let [deviceType, count] of this.deviceTypes) {
|
|
|
|
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) {
|
|
|
|
case "desktop":
|
|
|
|
hid = "WEAVE_DEVICE_COUNT_DESKTOP";
|
2016-04-21 01:43:36 +03:00
|
|
|
prefName += "desktop";
|
2016-01-12 04:21:27 +03:00
|
|
|
break;
|
|
|
|
case "mobile":
|
|
|
|
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);
|
2016-04-21 01:43:36 +03:00
|
|
|
Svc.Prefs.set(prefName, count);
|
2016-01-12 04:21:27 +03:00
|
|
|
}
|
|
|
|
SyncEngine.prototype._syncFinish.call(this);
|
|
|
|
},
|
|
|
|
|
2016-04-01 20:55:10 +03:00
|
|
|
_reconcile: function _reconcile(item) {
|
|
|
|
// 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
|
|
|
|
|
|
|
if (!this._store.itemExists(item.id)) {
|
|
|
|
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.
|
|
|
|
this._store.update(item);
|
|
|
|
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
|
2015-01-25 10:50:01 +03:00
|
|
|
_resetClient() {
|
|
|
|
this._wipeClient();
|
|
|
|
},
|
2010-02-20 00:36:42 +03:00
|
|
|
|
|
|
|
_wipeClient: function _wipeClient() {
|
2009-12-03 01:44:17 +03:00
|
|
|
SyncEngine.prototype._resetClient.call(this);
|
2016-04-05 01:39:37 +03:00
|
|
|
delete this.localCommands;
|
2009-02-27 09:36:14 +03:00
|
|
|
this._store.wipe();
|
2011-03-01 22:56:29 +03:00
|
|
|
},
|
2011-04-09 01:51:55 +04:00
|
|
|
|
|
|
|
removeClientData: function removeClientData() {
|
2012-09-15 03:02:32 +04:00
|
|
|
let res = this.service.resource(this.engineURL + "/" + this.localID);
|
2011-04-09 01:51:55 +04:00
|
|
|
res.delete();
|
|
|
|
},
|
|
|
|
|
2011-03-01 22:56:29 +03:00
|
|
|
// Override the default behavior to delete bad records from the server.
|
2011-03-21 02:10:40 +03:00
|
|
|
handleHMACMismatch: function 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
|
|
|
|
2011-03-21 02:10:40 +03:00
|
|
|
let base = SyncEngine.prototype.handleHMACMismatch.call(this, item, mayRetry);
|
|
|
|
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.");
|
|
|
|
this._deleteId(item.id);
|
|
|
|
|
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
|
|
|
|
* number of arguments and description.
|
|
|
|
*/
|
|
|
|
_commands: {
|
|
|
|
resetAll: { args: 0, desc: "Clear temporary local data for all engines" },
|
|
|
|
resetEngine: { args: 1, desc: "Clear temporary local data for engine" },
|
|
|
|
wipeAll: { args: 0, desc: "Delete all client data for all engines" },
|
|
|
|
wipeEngine: { args: 1, desc: "Delete all client data for engine" },
|
2011-08-09 20:23:55 +04:00
|
|
|
logout: { args: 0, desc: "Log out client" },
|
2012-03-27 21:13:52 +04:00
|
|
|
displayURI: { args: 3, desc: "Instruct a client to display a URI" },
|
2011-08-05 03:19:02 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove any commands for the local client and mark it for upload.
|
|
|
|
*/
|
|
|
|
clearCommands: function clearCommands() {
|
2016-04-05 01:39:37 +03:00
|
|
|
if (!this._clearedCommands) {
|
|
|
|
this._clearedCommands = [];
|
|
|
|
}
|
|
|
|
// Keep track of cleared local commands until the next sync, so that we
|
|
|
|
// don't reupload them.
|
|
|
|
this._clearedCommands = this._clearedCommands.concat(this.localCommands);
|
2011-08-05 03:19:02 +04:00
|
|
|
delete this.localCommands;
|
|
|
|
this._tracker.addChangedID(this.localID);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
_sendCommandToClient: function sendCommandToClient(command, args, clientId) {
|
|
|
|
this._log.trace("Sending " + command + " to " + clientId);
|
|
|
|
|
|
|
|
let client = this._store._remoteClients[clientId];
|
|
|
|
if (!client) {
|
|
|
|
throw new Error("Unknown remote client ID: '" + clientId + "'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
let action = {
|
|
|
|
command: command,
|
|
|
|
args: args,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!client.commands) {
|
|
|
|
client.commands = [action];
|
|
|
|
}
|
|
|
|
// Add the new action if there are no duplicates.
|
2016-04-01 20:55:10 +03:00
|
|
|
else if (!hasDupeCommand(client.commands, action)) {
|
2011-08-05 03:19:02 +04:00
|
|
|
client.commands.push(action);
|
|
|
|
}
|
|
|
|
// It must be a dupe. Skip.
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.trace("Client " + clientId + " got a new action: " + [command, args]);
|
|
|
|
this._tracker.addChangedID(clientId);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the local client has any remote commands and perform them.
|
|
|
|
*
|
|
|
|
* @return false to abort sync
|
|
|
|
*/
|
|
|
|
processIncomingCommands: function processIncomingCommands() {
|
2011-08-06 03:01:49 +04:00
|
|
|
return this._notify("clients:process-commands", "", function() {
|
2011-08-05 03:19:02 +04:00
|
|
|
let commands = this.localCommands;
|
|
|
|
|
|
|
|
// Immediately clear out the commands as we've got them locally.
|
|
|
|
this.clearCommands();
|
|
|
|
|
|
|
|
// Process each command in order.
|
2015-10-18 21:52:58 +03:00
|
|
|
if (!commands) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-07 23:33:29 +03:00
|
|
|
let URIsToDisplay = [];
|
2015-10-18 21:52:58 +03:00
|
|
|
for (let key in commands) {
|
|
|
|
let {command, args} = commands[key];
|
2011-08-05 03:19:02 +04:00
|
|
|
this._log.debug("Processing command: " + command + "(" + args + ")");
|
|
|
|
|
|
|
|
let engines = [args[0]];
|
|
|
|
switch (command) {
|
|
|
|
case "resetAll":
|
|
|
|
engines = null;
|
|
|
|
// Fallthrough
|
|
|
|
case "resetEngine":
|
2012-08-30 01:43:41 +04:00
|
|
|
this.service.resetClient(engines);
|
2011-08-05 03:19:02 +04:00
|
|
|
break;
|
|
|
|
case "wipeAll":
|
|
|
|
engines = null;
|
|
|
|
// Fallthrough
|
|
|
|
case "wipeEngine":
|
2012-08-30 01:43:41 +04:00
|
|
|
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;
|
2011-08-05 03:19:02 +04:00
|
|
|
default:
|
|
|
|
this._log.debug("Received an unknown command: " + command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
sendCommand: function sendCommand(command, args, clientId) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// Don't send a command with the wrong number of arguments.
|
|
|
|
else if (!args || args.length != commandData.args) {
|
|
|
|
this._log.error("Expected " + commandData.args + " args for '" +
|
|
|
|
command + "', but got " + args);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientId) {
|
|
|
|
this._sendCommandToClient(command, args, clientId);
|
|
|
|
} else {
|
|
|
|
for (let id in this._store._remoteClients) {
|
|
|
|
this._sendCommandToClient(command, args, id);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
2012-03-27 21:13:52 +04:00
|
|
|
sendURIToClientForDisplay: function sendURIToClientForDisplay(uri, clientId, title) {
|
|
|
|
this._log.info("Sending URI to client: " + uri + " -> " +
|
|
|
|
clientId + " (" + title + ")");
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
_removeRemoteClient(id) {
|
|
|
|
delete this._store._remoteClients[id];
|
|
|
|
this._tracker.removeChangedID(id);
|
|
|
|
},
|
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,
|
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
create(record) {
|
|
|
|
this.update(record)
|
|
|
|
},
|
2009-04-01 10:56:32 +04:00
|
|
|
|
2010-03-17 02:39:08 +03:00
|
|
|
update: function update(record) {
|
2016-04-05 01:39:37 +03:00
|
|
|
if (record.id == this.engine.localID) {
|
|
|
|
this._updateLocalRecord(record);
|
|
|
|
} else {
|
|
|
|
this._updateRemoteRecord(record);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateLocalRecord(record) {
|
|
|
|
// Local changes for our client means we're clearing commands or
|
|
|
|
// uploading a new record.
|
|
|
|
let incomingCommands = record.commands;
|
|
|
|
if (incomingCommands) {
|
|
|
|
// Filter out incoming commands that we've cleared.
|
|
|
|
incomingCommands = incomingCommands.filter(action =>
|
|
|
|
!hasDupeCommand(this.engine._clearedCommands, action));
|
|
|
|
if (!incomingCommands.length) {
|
|
|
|
// Use `undefined` instead of `null` to avoid creating a null field
|
|
|
|
// in the uploaded record.
|
|
|
|
incomingCommands = undefined;
|
2016-04-01 20:55:10 +03:00
|
|
|
}
|
2016-04-05 01:39:37 +03:00
|
|
|
}
|
|
|
|
// Only grab commands from the server; local name/type always wins
|
|
|
|
this.engine.localCommands = incomingCommands;
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateRemoteRecord(record) {
|
|
|
|
let currentRecord = this._remoteClients[record.id];
|
|
|
|
if (!currentRecord || !currentRecord.commands ||
|
|
|
|
!(record.id in this.engine._modified)) {
|
|
|
|
|
|
|
|
// If we have a new incoming record or no outgoing commands, use the
|
|
|
|
// full incoming record from the server.
|
2010-03-17 02:39:08 +03:00
|
|
|
this._remoteClients[record.id] = record.cleartext;
|
2016-04-05 01:39:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we have outgoing commands for a client, so merge them
|
|
|
|
// with the commands that we downloaded from the server.
|
|
|
|
for (let action of currentRecord.commands) {
|
|
|
|
if (hasDupeCommand(record.cleartext.commands, action)) {
|
|
|
|
// Ignore commands the server already knows about.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (record.cleartext.commands) {
|
|
|
|
record.cleartext.commands.push(action);
|
|
|
|
} else {
|
|
|
|
record.cleartext.commands = [action];
|
|
|
|
}
|
2016-04-01 20:55:10 +03:00
|
|
|
}
|
2016-04-05 01:39:37 +03:00
|
|
|
this._remoteClients[record.id] = record.cleartext;
|
2009-01-07 00:54:18 +03:00
|
|
|
},
|
|
|
|
|
2010-11-30 03:41:17 +03:00
|
|
|
createRecord: function createRecord(id, collection) {
|
|
|
|
let record = new ClientsRec(collection, id);
|
2009-02-20 12:52:07 +03:00
|
|
|
|
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
|
|
|
let cb = Async.makeSpinningCallback();
|
|
|
|
fxAccounts.getDeviceId().then(id => cb(null, id), cb);
|
|
|
|
try {
|
|
|
|
record.fxaDeviceId = cb.wait();
|
|
|
|
} catch(error) {
|
|
|
|
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;
|
|
|
|
record.commands = this.engine.localCommands;
|
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
|
|
|
|
|
|
|
// Optional fields.
|
|
|
|
record.os = Services.appinfo.OS; // "Darwin"
|
|
|
|
record.appPackage = Services.appinfo.ID;
|
|
|
|
record.application = this.engine.brandName // "Nightly"
|
|
|
|
|
|
|
|
// We can't compute these yet.
|
|
|
|
// record.device = ""; // Bug 1100723
|
|
|
|
// record.formfactor = ""; // Bug 1100722
|
|
|
|
} else {
|
2010-11-30 03:41:17 +03:00
|
|
|
record.cleartext = this._remoteClients[id];
|
2014-11-18 06:06:00 +03:00
|
|
|
}
|
2009-01-07 00:54:18 +03:00
|
|
|
|
2009-04-01 10:56:32 +04:00
|
|
|
return record;
|
2009-01-07 00:54:18 +03:00
|
|
|
},
|
|
|
|
|
2015-01-25 10:50:01 +03:00
|
|
|
itemExists(id) {
|
|
|
|
return id in this.getAllIDs();
|
|
|
|
},
|
2009-01-07 00:54:18 +03:00
|
|
|
|
2010-03-17 02:39:08 +03:00
|
|
|
getAllIDs: function getAllIDs() {
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2010-03-17 02:39:08 +03:00
|
|
|
wipe: function wipe() {
|
|
|
|
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
|
|
|
Svc.Obs.add("weave:engine:start-tracking", this);
|
|
|
|
Svc.Obs.add("weave:engine:stop-tracking", this);
|
|
|
|
}
|
|
|
|
ClientsTracker.prototype = {
|
|
|
|
__proto__: Tracker.prototype,
|
|
|
|
|
|
|
|
_enabled: false,
|
|
|
|
|
|
|
|
observe: function observe(subject, topic, data) {
|
|
|
|
switch (topic) {
|
|
|
|
case "weave:engine:start-tracking":
|
|
|
|
if (!this._enabled) {
|
|
|
|
Svc.Prefs.observe("client.name", this);
|
|
|
|
this._enabled = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "weave:engine:stop-tracking":
|
|
|
|
if (this._enabled) {
|
2016-07-04 09:26:49 +03:00
|
|
|
Svc.Prefs.ignore("client.name", this);
|
2011-07-27 08:48:50 +04:00
|
|
|
this._enabled = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "nsPref:changed":
|
|
|
|
this._log.debug("client.name preference changed");
|
|
|
|
this.addChangedID(Svc.Prefs.get("client.GUID"));
|
|
|
|
this.score += SCORE_INCREMENT_XLARGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|