services/sync/modules/engines/addons.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 * This file defines the add-on sync functionality.
michael@0 7 *
michael@0 8 * There are currently a number of known limitations:
michael@0 9 * - We only sync XPI extensions and themes available from addons.mozilla.org.
michael@0 10 * We hope to expand support for other add-ons eventually.
michael@0 11 * - We only attempt syncing of add-ons between applications of the same type.
michael@0 12 * This means add-ons will not synchronize between Firefox desktop and
michael@0 13 * Firefox mobile, for example. This is because of significant add-on
michael@0 14 * incompatibility between application types.
michael@0 15 *
michael@0 16 * Add-on records exist for each known {add-on, app-id} pair in the Sync client
michael@0 17 * set. Each record has a randomly chosen GUID. The records then contain
michael@0 18 * basic metadata about the add-on.
michael@0 19 *
michael@0 20 * We currently synchronize:
michael@0 21 *
michael@0 22 * - Installations
michael@0 23 * - Uninstallations
michael@0 24 * - User enabling and disabling
michael@0 25 *
michael@0 26 * Synchronization is influenced by the following preferences:
michael@0 27 *
michael@0 28 * - services.sync.addons.ignoreRepositoryChecking
michael@0 29 * - services.sync.addons.ignoreUserEnabledChanges
michael@0 30 * - services.sync.addons.trustedSourceHostnames
michael@0 31 *
michael@0 32 * See the documentation in services-sync.js for the behavior of these prefs.
michael@0 33 */
michael@0 34 "use strict";
michael@0 35
michael@0 36 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
michael@0 37
michael@0 38 Cu.import("resource://services-sync/addonutils.js");
michael@0 39 Cu.import("resource://services-sync/addonsreconciler.js");
michael@0 40 Cu.import("resource://services-sync/engines.js");
michael@0 41 Cu.import("resource://services-sync/record.js");
michael@0 42 Cu.import("resource://services-sync/util.js");
michael@0 43 Cu.import("resource://services-sync/constants.js");
michael@0 44 Cu.import("resource://services-common/async.js");
michael@0 45
michael@0 46 Cu.import("resource://gre/modules/Preferences.jsm");
michael@0 47 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 48 XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
michael@0 49 "resource://gre/modules/AddonManager.jsm");
michael@0 50 XPCOMUtils.defineLazyModuleGetter(this, "AddonRepository",
michael@0 51 "resource://gre/modules/addons/AddonRepository.jsm");
michael@0 52
michael@0 53 this.EXPORTED_SYMBOLS = ["AddonsEngine"];
michael@0 54
michael@0 55 // 7 days in milliseconds.
michael@0 56 const PRUNE_ADDON_CHANGES_THRESHOLD = 60 * 60 * 24 * 7 * 1000;
michael@0 57
michael@0 58 /**
michael@0 59 * AddonRecord represents the state of an add-on in an application.
michael@0 60 *
michael@0 61 * Each add-on has its own record for each application ID it is installed
michael@0 62 * on.
michael@0 63 *
michael@0 64 * The ID of add-on records is a randomly-generated GUID. It is random instead
michael@0 65 * of deterministic so the URIs of the records cannot be guessed and so
michael@0 66 * compromised server credentials won't result in disclosure of the specific
michael@0 67 * add-ons present in a Sync account.
michael@0 68 *
michael@0 69 * The record contains the following fields:
michael@0 70 *
michael@0 71 * addonID
michael@0 72 * ID of the add-on. This correlates to the "id" property on an Addon type.
michael@0 73 *
michael@0 74 * applicationID
michael@0 75 * The application ID this record is associated with.
michael@0 76 *
michael@0 77 * enabled
michael@0 78 * Boolean stating whether add-on is enabled or disabled by the user.
michael@0 79 *
michael@0 80 * source
michael@0 81 * String indicating where an add-on is from. Currently, we only support
michael@0 82 * the value "amo" which indicates that the add-on came from the official
michael@0 83 * add-ons repository, addons.mozilla.org. In the future, we may support
michael@0 84 * installing add-ons from other sources. This provides a future-compatible
michael@0 85 * mechanism for clients to only apply records they know how to handle.
michael@0 86 */
michael@0 87 function AddonRecord(collection, id) {
michael@0 88 CryptoWrapper.call(this, collection, id);
michael@0 89 }
michael@0 90 AddonRecord.prototype = {
michael@0 91 __proto__: CryptoWrapper.prototype,
michael@0 92 _logName: "Record.Addon"
michael@0 93 };
michael@0 94
michael@0 95 Utils.deferGetSet(AddonRecord, "cleartext", ["addonID",
michael@0 96 "applicationID",
michael@0 97 "enabled",
michael@0 98 "source"]);
michael@0 99
michael@0 100 /**
michael@0 101 * The AddonsEngine handles synchronization of add-ons between clients.
michael@0 102 *
michael@0 103 * The engine maintains an instance of an AddonsReconciler, which is the entity
michael@0 104 * maintaining state for add-ons. It provides the history and tracking APIs
michael@0 105 * that AddonManager doesn't.
michael@0 106 *
michael@0 107 * The engine instance overrides a handful of functions on the base class. The
michael@0 108 * rationale for each is documented by that function.
michael@0 109 */
michael@0 110 this.AddonsEngine = function AddonsEngine(service) {
michael@0 111 SyncEngine.call(this, "Addons", service);
michael@0 112
michael@0 113 this._reconciler = new AddonsReconciler();
michael@0 114 }
michael@0 115 AddonsEngine.prototype = {
michael@0 116 __proto__: SyncEngine.prototype,
michael@0 117 _storeObj: AddonsStore,
michael@0 118 _trackerObj: AddonsTracker,
michael@0 119 _recordObj: AddonRecord,
michael@0 120 version: 1,
michael@0 121
michael@0 122 _reconciler: null,
michael@0 123
michael@0 124 /**
michael@0 125 * Override parent method to find add-ons by their public ID, not Sync GUID.
michael@0 126 */
michael@0 127 _findDupe: function _findDupe(item) {
michael@0 128 let id = item.addonID;
michael@0 129
michael@0 130 // The reconciler should have been updated at the top of the sync, so we
michael@0 131 // can assume it is up to date when this function is called.
michael@0 132 let addons = this._reconciler.addons;
michael@0 133 if (!(id in addons)) {
michael@0 134 return null;
michael@0 135 }
michael@0 136
michael@0 137 let addon = addons[id];
michael@0 138 if (addon.guid != item.id) {
michael@0 139 return addon.guid;
michael@0 140 }
michael@0 141
michael@0 142 return null;
michael@0 143 },
michael@0 144
michael@0 145 /**
michael@0 146 * Override getChangedIDs to pull in tracker changes plus changes from the
michael@0 147 * reconciler log.
michael@0 148 */
michael@0 149 getChangedIDs: function getChangedIDs() {
michael@0 150 let changes = {};
michael@0 151 for (let [id, modified] in Iterator(this._tracker.changedIDs)) {
michael@0 152 changes[id] = modified;
michael@0 153 }
michael@0 154
michael@0 155 let lastSyncDate = new Date(this.lastSync * 1000);
michael@0 156
michael@0 157 // The reconciler should have been refreshed at the beginning of a sync and
michael@0 158 // we assume this function is only called from within a sync.
michael@0 159 let reconcilerChanges = this._reconciler.getChangesSinceDate(lastSyncDate);
michael@0 160 let addons = this._reconciler.addons;
michael@0 161 for each (let change in reconcilerChanges) {
michael@0 162 let changeTime = change[0];
michael@0 163 let id = change[2];
michael@0 164
michael@0 165 if (!(id in addons)) {
michael@0 166 continue;
michael@0 167 }
michael@0 168
michael@0 169 // Keep newest modified time.
michael@0 170 if (id in changes && changeTime < changes[id]) {
michael@0 171 continue;
michael@0 172 }
michael@0 173
michael@0 174 if (!this._store.isAddonSyncable(addons[id])) {
michael@0 175 continue;
michael@0 176 }
michael@0 177
michael@0 178 this._log.debug("Adding changed add-on from changes log: " + id);
michael@0 179 let addon = addons[id];
michael@0 180 changes[addon.guid] = changeTime.getTime() / 1000;
michael@0 181 }
michael@0 182
michael@0 183 return changes;
michael@0 184 },
michael@0 185
michael@0 186 /**
michael@0 187 * Override start of sync function to refresh reconciler.
michael@0 188 *
michael@0 189 * Many functions in this class assume the reconciler is refreshed at the
michael@0 190 * top of a sync. If this ever changes, those functions should be revisited.
michael@0 191 *
michael@0 192 * Technically speaking, we don't need to refresh the reconciler on every
michael@0 193 * sync since it is installed as an AddonManager listener. However, add-ons
michael@0 194 * are complicated and we force a full refresh, just in case the listeners
michael@0 195 * missed something.
michael@0 196 */
michael@0 197 _syncStartup: function _syncStartup() {
michael@0 198 // We refresh state before calling parent because syncStartup in the parent
michael@0 199 // looks for changed IDs, which is dependent on add-on state being up to
michael@0 200 // date.
michael@0 201 this._refreshReconcilerState();
michael@0 202
michael@0 203 SyncEngine.prototype._syncStartup.call(this);
michael@0 204 },
michael@0 205
michael@0 206 /**
michael@0 207 * Override end of sync to perform a little housekeeping on the reconciler.
michael@0 208 *
michael@0 209 * We prune changes to prevent the reconciler state from growing without
michael@0 210 * bound. Even if it grows unbounded, there would have to be many add-on
michael@0 211 * changes (thousands) for it to slow things down significantly. This is
michael@0 212 * highly unlikely to occur. Still, we exercise defense just in case.
michael@0 213 */
michael@0 214 _syncCleanup: function _syncCleanup() {
michael@0 215 let ms = 1000 * this.lastSync - PRUNE_ADDON_CHANGES_THRESHOLD;
michael@0 216 this._reconciler.pruneChangesBeforeDate(new Date(ms));
michael@0 217
michael@0 218 SyncEngine.prototype._syncCleanup.call(this);
michael@0 219 },
michael@0 220
michael@0 221 /**
michael@0 222 * Helper function to ensure reconciler is up to date.
michael@0 223 *
michael@0 224 * This will synchronously load the reconciler's state from the file
michael@0 225 * system (if needed) and refresh the state of the reconciler.
michael@0 226 */
michael@0 227 _refreshReconcilerState: function _refreshReconcilerState() {
michael@0 228 this._log.debug("Refreshing reconciler state");
michael@0 229 let cb = Async.makeSpinningCallback();
michael@0 230 this._reconciler.refreshGlobalState(cb);
michael@0 231 cb.wait();
michael@0 232 }
michael@0 233 };
michael@0 234
michael@0 235 /**
michael@0 236 * This is the primary interface between Sync and the Addons Manager.
michael@0 237 *
michael@0 238 * In addition to the core store APIs, we provide convenience functions to wrap
michael@0 239 * Add-on Manager APIs with Sync-specific semantics.
michael@0 240 */
michael@0 241 function AddonsStore(name, engine) {
michael@0 242 Store.call(this, name, engine);
michael@0 243 }
michael@0 244 AddonsStore.prototype = {
michael@0 245 __proto__: Store.prototype,
michael@0 246
michael@0 247 // Define the add-on types (.type) that we support.
michael@0 248 _syncableTypes: ["extension", "theme"],
michael@0 249
michael@0 250 _extensionsPrefs: new Preferences("extensions."),
michael@0 251
michael@0 252 get reconciler() {
michael@0 253 return this.engine._reconciler;
michael@0 254 },
michael@0 255
michael@0 256 /**
michael@0 257 * Override applyIncoming to filter out records we can't handle.
michael@0 258 */
michael@0 259 applyIncoming: function applyIncoming(record) {
michael@0 260 // The fields we look at aren't present when the record is deleted.
michael@0 261 if (!record.deleted) {
michael@0 262 // Ignore records not belonging to our application ID because that is the
michael@0 263 // current policy.
michael@0 264 if (record.applicationID != Services.appinfo.ID) {
michael@0 265 this._log.info("Ignoring incoming record from other App ID: " +
michael@0 266 record.id);
michael@0 267 return;
michael@0 268 }
michael@0 269
michael@0 270 // Ignore records that aren't from the official add-on repository, as that
michael@0 271 // is our current policy.
michael@0 272 if (record.source != "amo") {
michael@0 273 this._log.info("Ignoring unknown add-on source (" + record.source + ")" +
michael@0 274 " for " + record.id);
michael@0 275 return;
michael@0 276 }
michael@0 277 }
michael@0 278
michael@0 279 Store.prototype.applyIncoming.call(this, record);
michael@0 280 },
michael@0 281
michael@0 282
michael@0 283 /**
michael@0 284 * Provides core Store API to create/install an add-on from a record.
michael@0 285 */
michael@0 286 create: function create(record) {
michael@0 287 let cb = Async.makeSpinningCallback();
michael@0 288 AddonUtils.installAddons([{
michael@0 289 id: record.addonID,
michael@0 290 syncGUID: record.id,
michael@0 291 enabled: record.enabled,
michael@0 292 requireSecureURI: !Svc.Prefs.get("addons.ignoreRepositoryChecking", false),
michael@0 293 }], cb);
michael@0 294
michael@0 295 // This will throw if there was an error. This will get caught by the sync
michael@0 296 // engine and the record will try to be applied later.
michael@0 297 let results = cb.wait();
michael@0 298
michael@0 299 let addon;
michael@0 300 for each (let a in results.addons) {
michael@0 301 if (a.id == record.addonID) {
michael@0 302 addon = a;
michael@0 303 break;
michael@0 304 }
michael@0 305 }
michael@0 306
michael@0 307 // This should never happen, but is present as a fail-safe.
michael@0 308 if (!addon) {
michael@0 309 throw new Error("Add-on not found after install: " + record.addonID);
michael@0 310 }
michael@0 311
michael@0 312 this._log.info("Add-on installed: " + record.addonID);
michael@0 313 },
michael@0 314
michael@0 315 /**
michael@0 316 * Provides core Store API to remove/uninstall an add-on from a record.
michael@0 317 */
michael@0 318 remove: function remove(record) {
michael@0 319 // If this is called, the payload is empty, so we have to find by GUID.
michael@0 320 let addon = this.getAddonByGUID(record.id);
michael@0 321 if (!addon) {
michael@0 322 // We don't throw because if the add-on could not be found then we assume
michael@0 323 // it has already been uninstalled and there is nothing for this function
michael@0 324 // to do.
michael@0 325 return;
michael@0 326 }
michael@0 327
michael@0 328 this._log.info("Uninstalling add-on: " + addon.id);
michael@0 329 let cb = Async.makeSpinningCallback();
michael@0 330 AddonUtils.uninstallAddon(addon, cb);
michael@0 331 cb.wait();
michael@0 332 },
michael@0 333
michael@0 334 /**
michael@0 335 * Provides core Store API to update an add-on from a record.
michael@0 336 */
michael@0 337 update: function update(record) {
michael@0 338 let addon = this.getAddonByID(record.addonID);
michael@0 339
michael@0 340 // update() is called if !this.itemExists. And, since itemExists consults
michael@0 341 // the reconciler only, we need to take care of some corner cases.
michael@0 342 //
michael@0 343 // First, the reconciler could know about an add-on that was uninstalled
michael@0 344 // and no longer present in the add-ons manager.
michael@0 345 if (!addon) {
michael@0 346 this.create(record);
michael@0 347 return;
michael@0 348 }
michael@0 349
michael@0 350 // It's also possible that the add-on is non-restartless and has pending
michael@0 351 // install/uninstall activity.
michael@0 352 //
michael@0 353 // We wouldn't get here if the incoming record was for a deletion. So,
michael@0 354 // check for pending uninstall and cancel if necessary.
michael@0 355 if (addon.pendingOperations & AddonManager.PENDING_UNINSTALL) {
michael@0 356 addon.cancelUninstall();
michael@0 357
michael@0 358 // We continue with processing because there could be state or ID change.
michael@0 359 }
michael@0 360
michael@0 361 let cb = Async.makeSpinningCallback();
michael@0 362 this.updateUserDisabled(addon, !record.enabled, cb);
michael@0 363 cb.wait();
michael@0 364 },
michael@0 365
michael@0 366 /**
michael@0 367 * Provide core Store API to determine if a record exists.
michael@0 368 */
michael@0 369 itemExists: function itemExists(guid) {
michael@0 370 let addon = this.reconciler.getAddonStateFromSyncGUID(guid);
michael@0 371
michael@0 372 return !!addon;
michael@0 373 },
michael@0 374
michael@0 375 /**
michael@0 376 * Create an add-on record from its GUID.
michael@0 377 *
michael@0 378 * @param guid
michael@0 379 * Add-on GUID (from extensions DB)
michael@0 380 * @param collection
michael@0 381 * Collection to add record to.
michael@0 382 *
michael@0 383 * @return AddonRecord instance
michael@0 384 */
michael@0 385 createRecord: function createRecord(guid, collection) {
michael@0 386 let record = new AddonRecord(collection, guid);
michael@0 387 record.applicationID = Services.appinfo.ID;
michael@0 388
michael@0 389 let addon = this.reconciler.getAddonStateFromSyncGUID(guid);
michael@0 390
michael@0 391 // If we don't know about this GUID or if it has been uninstalled, we mark
michael@0 392 // the record as deleted.
michael@0 393 if (!addon || !addon.installed) {
michael@0 394 record.deleted = true;
michael@0 395 return record;
michael@0 396 }
michael@0 397
michael@0 398 record.modified = addon.modified.getTime() / 1000;
michael@0 399
michael@0 400 record.addonID = addon.id;
michael@0 401 record.enabled = addon.enabled;
michael@0 402
michael@0 403 // This needs to be dynamic when add-ons don't come from AddonRepository.
michael@0 404 record.source = "amo";
michael@0 405
michael@0 406 return record;
michael@0 407 },
michael@0 408
michael@0 409 /**
michael@0 410 * Changes the id of an add-on.
michael@0 411 *
michael@0 412 * This implements a core API of the store.
michael@0 413 */
michael@0 414 changeItemID: function changeItemID(oldID, newID) {
michael@0 415 // We always update the GUID in the reconciler because it will be
michael@0 416 // referenced later in the sync process.
michael@0 417 let state = this.reconciler.getAddonStateFromSyncGUID(oldID);
michael@0 418 if (state) {
michael@0 419 state.guid = newID;
michael@0 420 let cb = Async.makeSpinningCallback();
michael@0 421 this.reconciler.saveState(null, cb);
michael@0 422 cb.wait();
michael@0 423 }
michael@0 424
michael@0 425 let addon = this.getAddonByGUID(oldID);
michael@0 426 if (!addon) {
michael@0 427 this._log.debug("Cannot change item ID (" + oldID + ") in Add-on " +
michael@0 428 "Manager because old add-on not present: " + oldID);
michael@0 429 return;
michael@0 430 }
michael@0 431
michael@0 432 addon.syncGUID = newID;
michael@0 433 },
michael@0 434
michael@0 435 /**
michael@0 436 * Obtain the set of all syncable add-on Sync GUIDs.
michael@0 437 *
michael@0 438 * This implements a core Store API.
michael@0 439 */
michael@0 440 getAllIDs: function getAllIDs() {
michael@0 441 let ids = {};
michael@0 442
michael@0 443 let addons = this.reconciler.addons;
michael@0 444 for each (let addon in addons) {
michael@0 445 if (this.isAddonSyncable(addon)) {
michael@0 446 ids[addon.guid] = true;
michael@0 447 }
michael@0 448 }
michael@0 449
michael@0 450 return ids;
michael@0 451 },
michael@0 452
michael@0 453 /**
michael@0 454 * Wipe engine data.
michael@0 455 *
michael@0 456 * This uninstalls all syncable addons from the application. In case of
michael@0 457 * error, it logs the error and keeps trying with other add-ons.
michael@0 458 */
michael@0 459 wipe: function wipe() {
michael@0 460 this._log.info("Processing wipe.");
michael@0 461
michael@0 462 this.engine._refreshReconcilerState();
michael@0 463
michael@0 464 // We only wipe syncable add-ons. Wipe is a Sync feature not a security
michael@0 465 // feature.
michael@0 466 for (let guid in this.getAllIDs()) {
michael@0 467 let addon = this.getAddonByGUID(guid);
michael@0 468 if (!addon) {
michael@0 469 this._log.debug("Ignoring add-on because it couldn't be obtained: " +
michael@0 470 guid);
michael@0 471 continue;
michael@0 472 }
michael@0 473
michael@0 474 this._log.info("Uninstalling add-on as part of wipe: " + addon.id);
michael@0 475 Utils.catch(addon.uninstall)();
michael@0 476 }
michael@0 477 },
michael@0 478
michael@0 479 /***************************************************************************
michael@0 480 * Functions below are unique to this store and not part of the Store API *
michael@0 481 ***************************************************************************/
michael@0 482
michael@0 483 /**
michael@0 484 * Synchronously obtain an add-on from its public ID.
michael@0 485 *
michael@0 486 * @param id
michael@0 487 * Add-on ID
michael@0 488 * @return Addon or undefined if not found
michael@0 489 */
michael@0 490 getAddonByID: function getAddonByID(id) {
michael@0 491 let cb = Async.makeSyncCallback();
michael@0 492 AddonManager.getAddonByID(id, cb);
michael@0 493 return Async.waitForSyncCallback(cb);
michael@0 494 },
michael@0 495
michael@0 496 /**
michael@0 497 * Synchronously obtain an add-on from its Sync GUID.
michael@0 498 *
michael@0 499 * @param guid
michael@0 500 * Add-on Sync GUID
michael@0 501 * @return DBAddonInternal or null
michael@0 502 */
michael@0 503 getAddonByGUID: function getAddonByGUID(guid) {
michael@0 504 let cb = Async.makeSyncCallback();
michael@0 505 AddonManager.getAddonBySyncGUID(guid, cb);
michael@0 506 return Async.waitForSyncCallback(cb);
michael@0 507 },
michael@0 508
michael@0 509 /**
michael@0 510 * Determines whether an add-on is suitable for Sync.
michael@0 511 *
michael@0 512 * @param addon
michael@0 513 * Addon instance
michael@0 514 * @return Boolean indicating whether it is appropriate for Sync
michael@0 515 */
michael@0 516 isAddonSyncable: function isAddonSyncable(addon) {
michael@0 517 // Currently, we limit syncable add-ons to those that are:
michael@0 518 // 1) In a well-defined set of types
michael@0 519 // 2) Installed in the current profile
michael@0 520 // 3) Not installed by a foreign entity (i.e. installed by the app)
michael@0 521 // since they act like global extensions.
michael@0 522 // 4) Is not a hotfix.
michael@0 523 // 5) Are installed from AMO
michael@0 524
michael@0 525 // We could represent the test as a complex boolean expression. We go the
michael@0 526 // verbose route so the failure reason is logged.
michael@0 527 if (!addon) {
michael@0 528 this._log.debug("Null object passed to isAddonSyncable.");
michael@0 529 return false;
michael@0 530 }
michael@0 531
michael@0 532 if (this._syncableTypes.indexOf(addon.type) == -1) {
michael@0 533 this._log.debug(addon.id + " not syncable: type not in whitelist: " +
michael@0 534 addon.type);
michael@0 535 return false;
michael@0 536 }
michael@0 537
michael@0 538 if (!(addon.scope & AddonManager.SCOPE_PROFILE)) {
michael@0 539 this._log.debug(addon.id + " not syncable: not installed in profile.");
michael@0 540 return false;
michael@0 541 }
michael@0 542
michael@0 543 // This may be too aggressive. If an add-on is downloaded from AMO and
michael@0 544 // manually placed in the profile directory, foreignInstall will be set.
michael@0 545 // Arguably, that add-on should be syncable.
michael@0 546 // TODO Address the edge case and come up with more robust heuristics.
michael@0 547 if (addon.foreignInstall) {
michael@0 548 this._log.debug(addon.id + " not syncable: is foreign install.");
michael@0 549 return false;
michael@0 550 }
michael@0 551
michael@0 552 // Ignore hotfix extensions (bug 741670). The pref may not be defined.
michael@0 553 if (this._extensionsPrefs.get("hotfix.id", null) == addon.id) {
michael@0 554 this._log.debug(addon.id + " not syncable: is a hotfix.");
michael@0 555 return false;
michael@0 556 }
michael@0 557
michael@0 558 // We provide a back door to skip the repository checking of an add-on.
michael@0 559 // This is utilized by the tests to make testing easier. Users could enable
michael@0 560 // this, but it would sacrifice security.
michael@0 561 if (Svc.Prefs.get("addons.ignoreRepositoryChecking", false)) {
michael@0 562 return true;
michael@0 563 }
michael@0 564
michael@0 565 let cb = Async.makeSyncCallback();
michael@0 566 AddonRepository.getCachedAddonByID(addon.id, cb);
michael@0 567 let result = Async.waitForSyncCallback(cb);
michael@0 568
michael@0 569 if (!result) {
michael@0 570 this._log.debug(addon.id + " not syncable: add-on not found in add-on " +
michael@0 571 "repository.");
michael@0 572 return false;
michael@0 573 }
michael@0 574
michael@0 575 return this.isSourceURITrusted(result.sourceURI);
michael@0 576 },
michael@0 577
michael@0 578 /**
michael@0 579 * Determine whether an add-on's sourceURI field is trusted and the add-on
michael@0 580 * can be installed.
michael@0 581 *
michael@0 582 * This function should only ever be called from isAddonSyncable(). It is
michael@0 583 * exposed as a separate function to make testing easier.
michael@0 584 *
michael@0 585 * @param uri
michael@0 586 * nsIURI instance to validate
michael@0 587 * @return bool
michael@0 588 */
michael@0 589 isSourceURITrusted: function isSourceURITrusted(uri) {
michael@0 590 // For security reasons, we currently limit synced add-ons to those
michael@0 591 // installed from trusted hostname(s). We additionally require TLS with
michael@0 592 // the add-ons site to help prevent forgeries.
michael@0 593 let trustedHostnames = Svc.Prefs.get("addons.trustedSourceHostnames", "")
michael@0 594 .split(",");
michael@0 595
michael@0 596 if (!uri) {
michael@0 597 this._log.debug("Undefined argument to isSourceURITrusted().");
michael@0 598 return false;
michael@0 599 }
michael@0 600
michael@0 601 // Scheme is validated before the hostname because uri.host may not be
michael@0 602 // populated for certain schemes. It appears to always be populated for
michael@0 603 // https, so we avoid the potential NS_ERROR_FAILURE on field access.
michael@0 604 if (uri.scheme != "https") {
michael@0 605 this._log.debug("Source URI not HTTPS: " + uri.spec);
michael@0 606 return false;
michael@0 607 }
michael@0 608
michael@0 609 if (trustedHostnames.indexOf(uri.host) == -1) {
michael@0 610 this._log.debug("Source hostname not trusted: " + uri.host);
michael@0 611 return false;
michael@0 612 }
michael@0 613
michael@0 614 return true;
michael@0 615 },
michael@0 616
michael@0 617 /**
michael@0 618 * Update the userDisabled flag on an add-on.
michael@0 619 *
michael@0 620 * This will enable or disable an add-on and call the supplied callback when
michael@0 621 * the action is complete. If no action is needed, the callback gets called
michael@0 622 * immediately.
michael@0 623 *
michael@0 624 * @param addon
michael@0 625 * Addon instance to manipulate.
michael@0 626 * @param value
michael@0 627 * Boolean to which to set userDisabled on the passed Addon.
michael@0 628 * @param callback
michael@0 629 * Function to be called when action is complete. Will receive 2
michael@0 630 * arguments, a truthy value that signifies error, and the Addon
michael@0 631 * instance passed to this function.
michael@0 632 */
michael@0 633 updateUserDisabled: function updateUserDisabled(addon, value, callback) {
michael@0 634 if (addon.userDisabled == value) {
michael@0 635 callback(null, addon);
michael@0 636 return;
michael@0 637 }
michael@0 638
michael@0 639 // A pref allows changes to the enabled flag to be ignored.
michael@0 640 if (Svc.Prefs.get("addons.ignoreUserEnabledChanges", false)) {
michael@0 641 this._log.info("Ignoring enabled state change due to preference: " +
michael@0 642 addon.id);
michael@0 643 callback(null, addon);
michael@0 644 return;
michael@0 645 }
michael@0 646
michael@0 647 AddonUtils.updateUserDisabled(addon, value, callback);
michael@0 648 },
michael@0 649 };
michael@0 650
michael@0 651 /**
michael@0 652 * The add-ons tracker keeps track of real-time changes to add-ons.
michael@0 653 *
michael@0 654 * It hooks up to the reconciler and receives notifications directly from it.
michael@0 655 */
michael@0 656 function AddonsTracker(name, engine) {
michael@0 657 Tracker.call(this, name, engine);
michael@0 658 }
michael@0 659 AddonsTracker.prototype = {
michael@0 660 __proto__: Tracker.prototype,
michael@0 661
michael@0 662 get reconciler() {
michael@0 663 return this.engine._reconciler;
michael@0 664 },
michael@0 665
michael@0 666 get store() {
michael@0 667 return this.engine._store;
michael@0 668 },
michael@0 669
michael@0 670 /**
michael@0 671 * This callback is executed whenever the AddonsReconciler sends out a change
michael@0 672 * notification. See AddonsReconciler.addChangeListener().
michael@0 673 */
michael@0 674 changeListener: function changeHandler(date, change, addon) {
michael@0 675 this._log.debug("changeListener invoked: " + change + " " + addon.id);
michael@0 676 // Ignore changes that occur during sync.
michael@0 677 if (this.ignoreAll) {
michael@0 678 return;
michael@0 679 }
michael@0 680
michael@0 681 if (!this.store.isAddonSyncable(addon)) {
michael@0 682 this._log.debug("Ignoring change because add-on isn't syncable: " +
michael@0 683 addon.id);
michael@0 684 return;
michael@0 685 }
michael@0 686
michael@0 687 this.addChangedID(addon.guid, date.getTime() / 1000);
michael@0 688 this.score += SCORE_INCREMENT_XLARGE;
michael@0 689 },
michael@0 690
michael@0 691 startTracking: function() {
michael@0 692 if (this.engine.enabled) {
michael@0 693 this.reconciler.startListening();
michael@0 694 }
michael@0 695
michael@0 696 this.reconciler.addChangeListener(this);
michael@0 697 },
michael@0 698
michael@0 699 stopTracking: function() {
michael@0 700 this.reconciler.removeChangeListener(this);
michael@0 701 this.reconciler.stopListening();
michael@0 702 },
michael@0 703 };

mercurial