mobile/android/components/AddonUpdateService.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const Cc = Components.classes;
     6 const Ci = Components.interfaces;
     7 const Cu = Components.utils;
     9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    10 Cu.import("resource://gre/modules/Services.jsm");
    12 XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
    13                                   "resource://gre/modules/AddonManager.jsm");
    15 XPCOMUtils.defineLazyModuleGetter(this, "AddonRepository",
    16                                   "resource://gre/modules/addons/AddonRepository.jsm");
    18 XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
    20 function getPref(func, preference, defaultValue) {
    21   try {
    22     return Services.prefs[func](preference);
    23   }
    24   catch (e) {}
    25   return defaultValue;
    26 }
    28 // -----------------------------------------------------------------------
    29 // Add-on auto-update management service
    30 // -----------------------------------------------------------------------
    32 const PREF_ADDON_UPDATE_ENABLED  = "extensions.autoupdate.enabled";
    34 var gNeedsRestart = false;
    36 function AddonUpdateService() {}
    38 AddonUpdateService.prototype = {
    39   classDescription: "Add-on auto-update management",
    40   classID: Components.ID("{93c8824c-9b87-45ae-bc90-5b82a1e4d877}"),
    42   QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback]),
    44   notify: function aus_notify(aTimer) {
    45     if (aTimer && !getPref("getBoolPref", PREF_ADDON_UPDATE_ENABLED, true))
    46       return;
    48     // If we already auto-upgraded and installed new versions, ignore this check
    49     if (gNeedsRestart)
    50       return;
    52     Services.io.offline = false;
    54     // Assume we are doing a periodic update check
    55     let reason = AddonManager.UPDATE_WHEN_PERIODIC_UPDATE;
    56     if (!aTimer)
    57       reason = AddonManager.UPDATE_WHEN_USER_REQUESTED;
    59     AddonManager.getAddonsByTypes(null, function(aAddonList) {
    60       aAddonList.forEach(function(aAddon) {
    61         if (aAddon.permissions & AddonManager.PERM_CAN_UPGRADE) {
    62           let data = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
    63           data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name });
    64           Services.obs.notifyObservers(data, "addon-update-started", null);
    66           let listener = new UpdateCheckListener();
    67           aAddon.findUpdates(listener, reason);
    68         }
    69       });
    70     });
    71   }
    72 };
    74 // -----------------------------------------------------------------------
    75 // Add-on update listener. Starts a download for any add-on with a viable
    76 // update waiting
    77 // -----------------------------------------------------------------------
    79 function UpdateCheckListener() {
    80   this._status = null;
    81   this._version = null;
    82 }
    84 UpdateCheckListener.prototype = {
    85   onCompatibilityUpdateAvailable: function(aAddon) {
    86     this._status = "compatibility";
    87   },
    89   onUpdateAvailable: function(aAddon, aInstall) {
    90     this._status = "update";
    91     this._version = aInstall.version;
    92     aInstall.install();
    93   },
    95   onNoUpdateAvailable: function(aAddon) {
    96     if (!this._status)
    97       this._status = "no-update";
    98   },
   100   onUpdateFinished: function(aAddon, aError) {
   101     let data = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
   102     if (this._version)
   103       data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name, version: this._version });
   104     else
   105       data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name });
   107     if (aError)
   108       this._status = "error";
   110     Services.obs.notifyObservers(data, "addon-update-ended", this._status);
   111   }
   112 };
   114 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AddonUpdateService]);

mercurial