|
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 module is imported at the startup of an application. It takes care of |
|
6 * permissions installation, application url loading, security settings. Put |
|
7 * stuff here that you want to happen once on startup before the webapp is |
|
8 * loaded. */ |
|
9 |
|
10 this.EXPORTED_SYMBOLS = ["startup"]; |
|
11 |
|
12 const Ci = Components.interfaces; |
|
13 const Cu = Components.utils; |
|
14 |
|
15 Cu.import("resource://gre/modules/Services.jsm"); |
|
16 Cu.import("resource://gre/modules/AppsUtils.jsm"); |
|
17 Cu.import("resource://gre/modules/PermissionsInstaller.jsm"); |
|
18 Cu.import('resource://gre/modules/Payment.jsm'); |
|
19 Cu.import('resource://gre/modules/AlarmService.jsm'); |
|
20 Cu.import("resource://gre/modules/Task.jsm"); |
|
21 Cu.import("resource://gre/modules/Promise.jsm"); |
|
22 Cu.import("resource://gre/modules/osfile.jsm"); |
|
23 |
|
24 Cu.import("resource://webapprt/modules/WebappRT.jsm"); |
|
25 Cu.import("resource://webapprt/modules/WebRTCHandler.jsm"); |
|
26 |
|
27 const PROFILE_DIR = OS.Constants.Path.profileDir; |
|
28 |
|
29 function isFirstRunOrUpdate() { |
|
30 let savedBuildID = null; |
|
31 try { |
|
32 savedBuildID = Services.prefs.getCharPref("webapprt.buildID"); |
|
33 } catch (e) {} |
|
34 |
|
35 let ourBuildID = Services.appinfo.platformBuildID; |
|
36 |
|
37 if (ourBuildID != savedBuildID) { |
|
38 Services.prefs.setCharPref("webapprt.buildID", ourBuildID); |
|
39 return true; |
|
40 } |
|
41 |
|
42 return false; |
|
43 } |
|
44 |
|
45 function writeFile(aPath, aData) { |
|
46 return Task.spawn(function() { |
|
47 let data = new TextEncoder().encode(aData); |
|
48 yield OS.File.writeAtomic(aPath, data, { tmpPath: aPath + ".tmp" }); |
|
49 }); |
|
50 } |
|
51 |
|
52 function createBrandingFiles() { |
|
53 return Task.spawn(function() { |
|
54 let manifest = WebappRT.localeManifest; |
|
55 let name = WebappRT.localeManifest.name; |
|
56 let developer = " "; |
|
57 if (WebappRT.localeManifest.developer) { |
|
58 developer = WebappRT.localeManifest.developer.name; |
|
59 } |
|
60 |
|
61 let brandDTDContent = '<!ENTITY brandShortName "' + name + '">\n\ |
|
62 <!ENTITY brandFullName "' + name + '">\n\ |
|
63 <!ENTITY vendorShortName "' + developer + '">\n\ |
|
64 <!ENTITY trademarkInfo.part1 " ">'; |
|
65 |
|
66 yield writeFile(OS.Path.join(PROFILE_DIR, "brand.dtd"), brandDTDContent); |
|
67 |
|
68 let brandPropertiesContent = 'brandShortName=' + name + '\n\ |
|
69 brandFullName=' + name + '\n\ |
|
70 vendorShortName=' + developer; |
|
71 |
|
72 yield writeFile(OS.Path.join(PROFILE_DIR, "brand.properties"), |
|
73 brandPropertiesContent); |
|
74 }); |
|
75 } |
|
76 |
|
77 // Observes all the events needed to actually launch an application. |
|
78 // It waits for XUL window and webapps registry loading. |
|
79 this.startup = function(window) { |
|
80 return Task.spawn(function () { |
|
81 // Observe XUL window loading. |
|
82 // For tests, it could be already loaded. |
|
83 let deferredWindowLoad = Promise.defer(); |
|
84 if (window.document && window.document.getElementById("content")) { |
|
85 deferredWindowLoad.resolve(); |
|
86 } else { |
|
87 window.addEventListener("DOMContentLoaded", function onLoad() { |
|
88 window.removeEventListener("DOMContentLoaded", onLoad, false); |
|
89 deferredWindowLoad.resolve(); |
|
90 }); |
|
91 } |
|
92 |
|
93 let appUpdated = false; |
|
94 let updatePending = yield WebappRT.isUpdatePending(); |
|
95 if (updatePending) { |
|
96 appUpdated = yield WebappRT.applyUpdate(); |
|
97 } |
|
98 |
|
99 yield WebappRT.loadConfig(); |
|
100 |
|
101 let appData = WebappRT.config.app; |
|
102 |
|
103 // Initialize DOMApplicationRegistry by importing Webapps.jsm. |
|
104 Cu.import("resource://gre/modules/Webapps.jsm"); |
|
105 // Initialize window-independent handling of webapps- notifications. |
|
106 Cu.import("resource://webapprt/modules/WebappManager.jsm"); |
|
107 |
|
108 // Wait for webapps registry loading. |
|
109 yield DOMApplicationRegistry.registryStarted; |
|
110 // Add the currently running app to the registry. |
|
111 yield DOMApplicationRegistry.addInstalledApp(appData, appData.manifest, |
|
112 appData.updateManifest); |
|
113 |
|
114 let manifestURL = appData.manifestURL; |
|
115 if (manifestURL) { |
|
116 // On firstrun, set permissions to their default values. |
|
117 // When the webapp runtime is updated, update the permissions. |
|
118 if (isFirstRunOrUpdate(Services.prefs) || appUpdated) { |
|
119 PermissionsInstaller.installPermissions(appData, true); |
|
120 yield createBrandingFiles(); |
|
121 } |
|
122 } |
|
123 |
|
124 // Branding substitution |
|
125 let aliasFile = Components.classes["@mozilla.org/file/local;1"] |
|
126 .createInstance(Ci.nsIFile); |
|
127 aliasFile.initWithPath(PROFILE_DIR); |
|
128 |
|
129 let aliasURI = Services.io.newFileURI(aliasFile); |
|
130 |
|
131 Services.io.getProtocolHandler("resource") |
|
132 .QueryInterface(Ci.nsIResProtocolHandler) |
|
133 .setSubstitution("webappbranding", aliasURI); |
|
134 |
|
135 // Wait for XUL window loading |
|
136 yield deferredWindowLoad.promise; |
|
137 |
|
138 // Get the <browser> element in the webapp.xul window. |
|
139 let appBrowser = window.document.getElementById("content"); |
|
140 |
|
141 // Set the principal to the correct appID and launch the application. |
|
142 appBrowser.docShell.setIsApp(WebappRT.appID); |
|
143 appBrowser.setAttribute("src", WebappRT.launchURI); |
|
144 |
|
145 if (appData.manifest.fullscreen) { |
|
146 appBrowser.addEventListener("load", function onLoad() { |
|
147 appBrowser.removeEventListener("load", onLoad, true); |
|
148 appBrowser.contentDocument. |
|
149 documentElement.mozRequestFullScreen(); |
|
150 }, true); |
|
151 } |
|
152 |
|
153 WebappRT.startUpdateService(); |
|
154 }); |
|
155 } |