mobile/android/components/AddonUpdateService.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/components/AddonUpdateService.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,115 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Cc = Components.classes;
     1.9 +const Ci = Components.interfaces;
    1.10 +const Cu = Components.utils;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +
    1.15 +XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
    1.16 +                                  "resource://gre/modules/AddonManager.jsm");
    1.17 +
    1.18 +XPCOMUtils.defineLazyModuleGetter(this, "AddonRepository",
    1.19 +                                  "resource://gre/modules/addons/AddonRepository.jsm");
    1.20 +
    1.21 +XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
    1.22 +
    1.23 +function getPref(func, preference, defaultValue) {
    1.24 +  try {
    1.25 +    return Services.prefs[func](preference);
    1.26 +  }
    1.27 +  catch (e) {}
    1.28 +  return defaultValue;
    1.29 +}
    1.30 +
    1.31 +// -----------------------------------------------------------------------
    1.32 +// Add-on auto-update management service
    1.33 +// -----------------------------------------------------------------------
    1.34 +
    1.35 +const PREF_ADDON_UPDATE_ENABLED  = "extensions.autoupdate.enabled";
    1.36 +
    1.37 +var gNeedsRestart = false;
    1.38 +
    1.39 +function AddonUpdateService() {}
    1.40 +
    1.41 +AddonUpdateService.prototype = {
    1.42 +  classDescription: "Add-on auto-update management",
    1.43 +  classID: Components.ID("{93c8824c-9b87-45ae-bc90-5b82a1e4d877}"),
    1.44 +  
    1.45 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback]),
    1.46 +
    1.47 +  notify: function aus_notify(aTimer) {
    1.48 +    if (aTimer && !getPref("getBoolPref", PREF_ADDON_UPDATE_ENABLED, true))
    1.49 +      return;
    1.50 +
    1.51 +    // If we already auto-upgraded and installed new versions, ignore this check
    1.52 +    if (gNeedsRestart)
    1.53 +      return;
    1.54 +
    1.55 +    Services.io.offline = false;
    1.56 +
    1.57 +    // Assume we are doing a periodic update check
    1.58 +    let reason = AddonManager.UPDATE_WHEN_PERIODIC_UPDATE;
    1.59 +    if (!aTimer)
    1.60 +      reason = AddonManager.UPDATE_WHEN_USER_REQUESTED;
    1.61 +
    1.62 +    AddonManager.getAddonsByTypes(null, function(aAddonList) {
    1.63 +      aAddonList.forEach(function(aAddon) {
    1.64 +        if (aAddon.permissions & AddonManager.PERM_CAN_UPGRADE) {
    1.65 +          let data = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
    1.66 +          data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name });
    1.67 +          Services.obs.notifyObservers(data, "addon-update-started", null);
    1.68 +
    1.69 +          let listener = new UpdateCheckListener();
    1.70 +          aAddon.findUpdates(listener, reason);
    1.71 +        }
    1.72 +      });
    1.73 +    });
    1.74 +  }
    1.75 +};
    1.76 +
    1.77 +// -----------------------------------------------------------------------
    1.78 +// Add-on update listener. Starts a download for any add-on with a viable
    1.79 +// update waiting
    1.80 +// -----------------------------------------------------------------------
    1.81 +
    1.82 +function UpdateCheckListener() {
    1.83 +  this._status = null;
    1.84 +  this._version = null;
    1.85 +}
    1.86 +
    1.87 +UpdateCheckListener.prototype = {
    1.88 +  onCompatibilityUpdateAvailable: function(aAddon) {
    1.89 +    this._status = "compatibility";
    1.90 +  },
    1.91 +
    1.92 +  onUpdateAvailable: function(aAddon, aInstall) {
    1.93 +    this._status = "update";
    1.94 +    this._version = aInstall.version;
    1.95 +    aInstall.install();
    1.96 +  },
    1.97 +
    1.98 +  onNoUpdateAvailable: function(aAddon) {
    1.99 +    if (!this._status)
   1.100 +      this._status = "no-update";
   1.101 +  },
   1.102 +
   1.103 +  onUpdateFinished: function(aAddon, aError) {
   1.104 +    let data = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
   1.105 +    if (this._version)
   1.106 +      data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name, version: this._version });
   1.107 +    else
   1.108 +      data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name });
   1.109 +
   1.110 +    if (aError)
   1.111 +      this._status = "error";
   1.112 +
   1.113 +    Services.obs.notifyObservers(data, "addon-update-ended", this._status);
   1.114 +  }
   1.115 +};
   1.116 +
   1.117 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AddonUpdateService]);
   1.118 +

mercurial