michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "AddonManager", michael@0: "resource://gre/modules/AddonManager.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "AddonRepository", michael@0: "resource://gre/modules/addons/AddonRepository.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm"); michael@0: michael@0: function getPref(func, preference, defaultValue) { michael@0: try { michael@0: return Services.prefs[func](preference); michael@0: } michael@0: catch (e) {} michael@0: return defaultValue; michael@0: } michael@0: michael@0: // ----------------------------------------------------------------------- michael@0: // Add-on auto-update management service michael@0: // ----------------------------------------------------------------------- michael@0: michael@0: const PREF_ADDON_UPDATE_ENABLED = "extensions.autoupdate.enabled"; michael@0: michael@0: var gNeedsRestart = false; michael@0: michael@0: function AddonUpdateService() {} michael@0: michael@0: AddonUpdateService.prototype = { michael@0: classDescription: "Add-on auto-update management", michael@0: classID: Components.ID("{93c8824c-9b87-45ae-bc90-5b82a1e4d877}"), michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback]), michael@0: michael@0: notify: function aus_notify(aTimer) { michael@0: if (aTimer && !getPref("getBoolPref", PREF_ADDON_UPDATE_ENABLED, true)) michael@0: return; michael@0: michael@0: // If we already auto-upgraded and installed new versions, ignore this check michael@0: if (gNeedsRestart) michael@0: return; michael@0: michael@0: Services.io.offline = false; michael@0: michael@0: // Assume we are doing a periodic update check michael@0: let reason = AddonManager.UPDATE_WHEN_PERIODIC_UPDATE; michael@0: if (!aTimer) michael@0: reason = AddonManager.UPDATE_WHEN_USER_REQUESTED; michael@0: michael@0: AddonManager.getAddonsByTypes(null, function(aAddonList) { michael@0: aAddonList.forEach(function(aAddon) { michael@0: if (aAddon.permissions & AddonManager.PERM_CAN_UPGRADE) { michael@0: let data = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); michael@0: data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name }); michael@0: Services.obs.notifyObservers(data, "addon-update-started", null); michael@0: michael@0: let listener = new UpdateCheckListener(); michael@0: aAddon.findUpdates(listener, reason); michael@0: } michael@0: }); michael@0: }); michael@0: } michael@0: }; michael@0: michael@0: // ----------------------------------------------------------------------- michael@0: // Add-on update listener. Starts a download for any add-on with a viable michael@0: // update waiting michael@0: // ----------------------------------------------------------------------- michael@0: michael@0: function UpdateCheckListener() { michael@0: this._status = null; michael@0: this._version = null; michael@0: } michael@0: michael@0: UpdateCheckListener.prototype = { michael@0: onCompatibilityUpdateAvailable: function(aAddon) { michael@0: this._status = "compatibility"; michael@0: }, michael@0: michael@0: onUpdateAvailable: function(aAddon, aInstall) { michael@0: this._status = "update"; michael@0: this._version = aInstall.version; michael@0: aInstall.install(); michael@0: }, michael@0: michael@0: onNoUpdateAvailable: function(aAddon) { michael@0: if (!this._status) michael@0: this._status = "no-update"; michael@0: }, michael@0: michael@0: onUpdateFinished: function(aAddon, aError) { michael@0: let data = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); michael@0: if (this._version) michael@0: data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name, version: this._version }); michael@0: else michael@0: data.data = JSON.stringify({ id: aAddon.id, name: aAddon.name }); michael@0: michael@0: if (aError) michael@0: this._status = "error"; michael@0: michael@0: Services.obs.notifyObservers(data, "addon-update-ended", this._status); michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AddonUpdateService]); michael@0: