|
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 file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 this.EXPORTED_SYMBOLS = ["WebappRT"]; |
|
6 |
|
7 const Cc = Components.classes; |
|
8 const Ci = Components.interfaces; |
|
9 const Cu = Components.utils; |
|
10 |
|
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
12 Cu.import("resource://gre/modules/Services.jsm"); |
|
13 Cu.import("resource://gre/modules/AppsUtils.jsm"); |
|
14 |
|
15 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils", |
|
16 "resource://gre/modules/FileUtils.jsm"); |
|
17 |
|
18 XPCOMUtils.defineLazyModuleGetter(this, "OS", |
|
19 "resource://gre/modules/osfile.jsm"); |
|
20 |
|
21 XPCOMUtils.defineLazyModuleGetter(this, "Task", |
|
22 "resource://gre/modules/Task.jsm"); |
|
23 |
|
24 XPCOMUtils.defineLazyModuleGetter(this, 'NativeApp', |
|
25 'resource://gre/modules/NativeApp.jsm'); |
|
26 |
|
27 XPCOMUtils.defineLazyServiceGetter(this, "appsService", |
|
28 "@mozilla.org/AppsService;1", |
|
29 "nsIAppsService"); |
|
30 |
|
31 this.WebappRT = { |
|
32 get launchURI() { |
|
33 return this.localeManifest.fullLaunchPath(); |
|
34 }, |
|
35 |
|
36 get localeManifest() { |
|
37 return new ManifestHelper(this.config.app.manifest, |
|
38 this.config.app.origin); |
|
39 }, |
|
40 |
|
41 get appID() { |
|
42 let manifestURL = WebappRT.config.app.manifestURL; |
|
43 if (!manifestURL) { |
|
44 return Ci.nsIScriptSecurityManager.NO_APP_ID; |
|
45 } |
|
46 |
|
47 return appsService.getAppLocalIdByManifestURL(manifestURL); |
|
48 }, |
|
49 |
|
50 loadConfig: function() { |
|
51 if (this.config) { |
|
52 return; |
|
53 } |
|
54 |
|
55 let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path, |
|
56 "webapp.json"); |
|
57 this.config = yield AppsUtils.loadJSONAsync(webappJson); |
|
58 }, |
|
59 |
|
60 isUpdatePending: Task.async(function*() { |
|
61 let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path, |
|
62 "update", "webapp.json"); |
|
63 |
|
64 if (!(yield OS.File.exists(webappJson))) { |
|
65 return false; |
|
66 } |
|
67 |
|
68 return true; |
|
69 }), |
|
70 |
|
71 applyUpdate: Task.async(function*() { |
|
72 let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path, |
|
73 "update", "webapp.json"); |
|
74 let config = yield AppsUtils.loadJSONAsync(webappJson); |
|
75 |
|
76 let nativeApp = new NativeApp(config.app, config.app.manifest, |
|
77 config.app.categories, |
|
78 config.registryDir); |
|
79 try { |
|
80 yield nativeApp.applyUpdate(); |
|
81 } catch (ex) { |
|
82 return false; |
|
83 } |
|
84 |
|
85 // The update has been applied successfully, the new config file |
|
86 // is the config file that was in the update directory. |
|
87 this.config = config; |
|
88 |
|
89 return true; |
|
90 }), |
|
91 |
|
92 startUpdateService: function() { |
|
93 let manifestURL = WebappRT.config.app.manifestURL; |
|
94 // We used to install apps without storing their manifest URL. |
|
95 // Now we can't update them. |
|
96 if (!manifestURL) { |
|
97 return; |
|
98 } |
|
99 |
|
100 // Check for updates once a day. |
|
101 let timerManager = Cc["@mozilla.org/updates/timer-manager;1"]. |
|
102 getService(Ci.nsIUpdateTimerManager); |
|
103 timerManager.registerTimer("updateTimer", () => { |
|
104 let window = Services.wm.getMostRecentWindow("webapprt:webapp"); |
|
105 window.navigator.mozApps.mgmt.getAll().onsuccess = function() { |
|
106 let thisApp = null; |
|
107 for (let app of this.result) { |
|
108 if (app.manifestURL == manifestURL) { |
|
109 thisApp = app; |
|
110 break; |
|
111 } |
|
112 } |
|
113 |
|
114 // This shouldn't happen if the app is installed. |
|
115 if (!thisApp) { |
|
116 Cu.reportError("Couldn't find the app in the webapps registry"); |
|
117 return; |
|
118 } |
|
119 |
|
120 thisApp.ondownloadavailable = () => { |
|
121 // Download available, download it! |
|
122 thisApp.download(); |
|
123 }; |
|
124 |
|
125 thisApp.ondownloadsuccess = () => { |
|
126 // Update downloaded, apply it! |
|
127 window.navigator.mozApps.mgmt.applyDownload(thisApp); |
|
128 }; |
|
129 |
|
130 thisApp.ondownloadapplied = () => { |
|
131 // Application updated, nothing to do. |
|
132 }; |
|
133 |
|
134 thisApp.checkForUpdate(); |
|
135 } |
|
136 }, 24 * 60 * 60); |
|
137 }, |
|
138 }; |