Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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 | const Cc = Components.classes; |
michael@0 | 6 | const Ci = Components.interfaces; |
michael@0 | 7 | const Cu = Components.utils; |
michael@0 | 8 | |
michael@0 | 9 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | XPCOMUtils.defineLazyModuleGetter(this, "AddonManager", |
michael@0 | 13 | "resource://gre/modules/AddonManager.jsm"); |
michael@0 | 14 | |
michael@0 | 15 | XPCOMUtils.defineLazyModuleGetter(this, "AddonRepository", |
michael@0 | 16 | "resource://gre/modules/addons/AddonRepository.jsm"); |
michael@0 | 17 | |
michael@0 | 18 | XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm"); |
michael@0 | 19 | |
michael@0 | 20 | function getPref(func, preference, defaultValue) { |
michael@0 | 21 | try { |
michael@0 | 22 | return Services.prefs[func](preference); |
michael@0 | 23 | } |
michael@0 | 24 | catch (e) {} |
michael@0 | 25 | return defaultValue; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | // ----------------------------------------------------------------------- |
michael@0 | 29 | // Add-on auto-update management service |
michael@0 | 30 | // ----------------------------------------------------------------------- |
michael@0 | 31 | |
michael@0 | 32 | const PREF_ADDON_UPDATE_ENABLED = "extensions.autoupdate.enabled"; |
michael@0 | 33 | |
michael@0 | 34 | var gNeedsRestart = false; |
michael@0 | 35 | |
michael@0 | 36 | function AddonUpdateService() {} |
michael@0 | 37 | |
michael@0 | 38 | AddonUpdateService.prototype = { |
michael@0 | 39 | classDescription: "Add-on auto-update management", |
michael@0 | 40 | classID: Components.ID("{93c8824c-9b87-45ae-bc90-5b82a1e4d877}"), |
michael@0 | 41 | |
michael@0 | 42 | QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback]), |
michael@0 | 43 | |
michael@0 | 44 | notify: function aus_notify(aTimer) { |
michael@0 | 45 | if (aTimer && !getPref("getBoolPref", PREF_ADDON_UPDATE_ENABLED, true)) |
michael@0 | 46 | return; |
michael@0 | 47 | |
michael@0 | 48 | // If we already auto-upgraded and installed new versions, ignore this check |
michael@0 | 49 | if (gNeedsRestart) |
michael@0 | 50 | return; |
michael@0 | 51 | |
michael@0 | 52 | Services.io.offline = false; |
michael@0 | 53 | |
michael@0 | 54 | // Assume we are doing a periodic update check |
michael@0 | 55 | let reason = AddonManager.UPDATE_WHEN_PERIODIC_UPDATE; |
michael@0 | 56 | if (!aTimer) |
michael@0 | 57 | reason = AddonManager.UPDATE_WHEN_USER_REQUESTED; |
michael@0 | 58 | |
michael@0 | 59 | AddonManager.getAddonsByTypes(null, function(aAddonList) { |
michael@0 | 60 | aAddonList.forEach(function(aAddon) { |
michael@0 | 61 | if (aAddon.permissions & AddonManager.PERM_CAN_UPGRADE) { |
michael@0 | 62 | let data = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); |
michael@0 | 63 | data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name }); |
michael@0 | 64 | Services.obs.notifyObservers(data, "addon-update-started", null); |
michael@0 | 65 | |
michael@0 | 66 | let listener = new UpdateCheckListener(); |
michael@0 | 67 | aAddon.findUpdates(listener, reason); |
michael@0 | 68 | } |
michael@0 | 69 | }); |
michael@0 | 70 | }); |
michael@0 | 71 | } |
michael@0 | 72 | }; |
michael@0 | 73 | |
michael@0 | 74 | // ----------------------------------------------------------------------- |
michael@0 | 75 | // Add-on update listener. Starts a download for any add-on with a viable |
michael@0 | 76 | // update waiting |
michael@0 | 77 | // ----------------------------------------------------------------------- |
michael@0 | 78 | |
michael@0 | 79 | function UpdateCheckListener() { |
michael@0 | 80 | this._status = null; |
michael@0 | 81 | this._version = null; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | UpdateCheckListener.prototype = { |
michael@0 | 85 | onCompatibilityUpdateAvailable: function(aAddon) { |
michael@0 | 86 | this._status = "compatibility"; |
michael@0 | 87 | }, |
michael@0 | 88 | |
michael@0 | 89 | onUpdateAvailable: function(aAddon, aInstall) { |
michael@0 | 90 | this._status = "update"; |
michael@0 | 91 | this._version = aInstall.version; |
michael@0 | 92 | aInstall.install(); |
michael@0 | 93 | }, |
michael@0 | 94 | |
michael@0 | 95 | onNoUpdateAvailable: function(aAddon) { |
michael@0 | 96 | if (!this._status) |
michael@0 | 97 | this._status = "no-update"; |
michael@0 | 98 | }, |
michael@0 | 99 | |
michael@0 | 100 | onUpdateFinished: function(aAddon, aError) { |
michael@0 | 101 | let data = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); |
michael@0 | 102 | if (this._version) |
michael@0 | 103 | data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name, version: this._version }); |
michael@0 | 104 | else |
michael@0 | 105 | data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name }); |
michael@0 | 106 | |
michael@0 | 107 | if (aError) |
michael@0 | 108 | this._status = "error"; |
michael@0 | 109 | |
michael@0 | 110 | Services.obs.notifyObservers(data, "addon-update-ended", this._status); |
michael@0 | 111 | } |
michael@0 | 112 | }; |
michael@0 | 113 | |
michael@0 | 114 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AddonUpdateService]); |
michael@0 | 115 |