mobile/android/components/AddonUpdateService.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:cce00e21affe
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/. */
4
5 const Cc = Components.classes;
6 const Ci = Components.interfaces;
7 const Cu = Components.utils;
8
9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
10 Cu.import("resource://gre/modules/Services.jsm");
11
12 XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
13 "resource://gre/modules/AddonManager.jsm");
14
15 XPCOMUtils.defineLazyModuleGetter(this, "AddonRepository",
16 "resource://gre/modules/addons/AddonRepository.jsm");
17
18 XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
19
20 function getPref(func, preference, defaultValue) {
21 try {
22 return Services.prefs[func](preference);
23 }
24 catch (e) {}
25 return defaultValue;
26 }
27
28 // -----------------------------------------------------------------------
29 // Add-on auto-update management service
30 // -----------------------------------------------------------------------
31
32 const PREF_ADDON_UPDATE_ENABLED = "extensions.autoupdate.enabled";
33
34 var gNeedsRestart = false;
35
36 function AddonUpdateService() {}
37
38 AddonUpdateService.prototype = {
39 classDescription: "Add-on auto-update management",
40 classID: Components.ID("{93c8824c-9b87-45ae-bc90-5b82a1e4d877}"),
41
42 QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback]),
43
44 notify: function aus_notify(aTimer) {
45 if (aTimer && !getPref("getBoolPref", PREF_ADDON_UPDATE_ENABLED, true))
46 return;
47
48 // If we already auto-upgraded and installed new versions, ignore this check
49 if (gNeedsRestart)
50 return;
51
52 Services.io.offline = false;
53
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;
58
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);
65
66 let listener = new UpdateCheckListener();
67 aAddon.findUpdates(listener, reason);
68 }
69 });
70 });
71 }
72 };
73
74 // -----------------------------------------------------------------------
75 // Add-on update listener. Starts a download for any add-on with a viable
76 // update waiting
77 // -----------------------------------------------------------------------
78
79 function UpdateCheckListener() {
80 this._status = null;
81 this._version = null;
82 }
83
84 UpdateCheckListener.prototype = {
85 onCompatibilityUpdateAvailable: function(aAddon) {
86 this._status = "compatibility";
87 },
88
89 onUpdateAvailable: function(aAddon, aInstall) {
90 this._status = "update";
91 this._version = aInstall.version;
92 aInstall.install();
93 },
94
95 onNoUpdateAvailable: function(aAddon) {
96 if (!this._status)
97 this._status = "no-update";
98 },
99
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 });
106
107 if (aError)
108 this._status = "error";
109
110 Services.obs.notifyObservers(data, "addon-update-ended", this._status);
111 }
112 };
113
114 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AddonUpdateService]);
115

mercurial