-
Notifications
You must be signed in to change notification settings - Fork 4
/
gaia_apps.js
executable file
·236 lines (209 loc) · 7.11 KB
/
gaia_apps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
var GaiaApps = {
normalizeName: function(name) {
return name.replace(/[- ]+/g, '').toLowerCase();
},
getRunningApps: function() {
let runningApps = window.wrappedJSObject.WindowManager.getRunningApps();
// Return a simplified version of the runningApps object which can be
// JSON-serialized.
let apps = {};
for (let app in runningApps) {
let anApp = {};
for (let key in runningApps[app]) {
if (["name", "origin", "manifest"].indexOf(key) > -1) {
anApp[key] = runningApps[app][key];
}
}
apps[app] = anApp;
}
return apps;
},
getRunningAppOrigin: function(name) {
let runningApps = window.wrappedJSObject.WindowManager.getRunningApps();
let origin;
for (let property in runningApps) {
if (runningApps[property].name == name) {
origin = property;
}
}
return origin;
},
getPermission: function(appName, permissionName) {
GaiaApps.locateWithName(appName, function(app) {
console.log("Getting permission '" + permissionName + "' for " + appName);
var mozPerms = navigator.mozPermissionSettings;
var result = mozPerms.get(
permissionName, app.manifestURL, app.origin, false
);
marionetteScriptFinished(result);
});
},
setPermission: function(appName, permissionName, value) {
GaiaApps.locateWithName(appName, function(app) {
console.log("Setting permission '" + permissionName + "' for " +
appName + "to '" + value + "'");
var mozPerms = navigator.mozPermissionSettings;
mozPerms.set(
permissionName, value, app.manifestURL, app.origin, false
);
marionetteScriptFinished();
});
},
locateWithName: function(name, aCallback) {
var callback = aCallback || marionetteScriptFinished;
function sendResponse(app, appName, entryPoint) {
if (callback === marionetteScriptFinished) {
if (typeof(app) === 'object') {
var result = {
name: app.manifest.name,
origin: app.origin,
entryPoint: entryPoint || null,
normalizedName: appName
};
callback(result);
} else {
callback(false);
}
} else {
callback(app, appName, entryPoint);
}
}
let appsReq = navigator.mozApps.mgmt.getAll();
appsReq.onsuccess = function() {
let apps = appsReq.result;
let normalizedSearchName = GaiaApps.normalizeName(name);
for (let i = 0; i < apps.length; i++) {
let app = apps[i];
let origin = null;
let entryPoints = app.manifest.entry_points;
if (entryPoints) {
for (let ep in entryPoints) {
let currentEntryPoint = entryPoints[ep];
let appName = currentEntryPoint.name;
if (normalizedSearchName === GaiaApps.normalizeName(appName)) {
return sendResponse(app, appName, ep);
}
}
} else {
let appName = app.manifest.name;
if (normalizedSearchName === GaiaApps.normalizeName(appName)) {
return sendResponse(app, appName);
}
}
}
callback(false);
}
},
// Returns the number of running apps.
numRunningApps: function() {
let count = 0;
let runningApps = window.wrappedJSObject.WindowManager.getRunningApps();
for (let origin in runningApps) {
count++;
}
return count;
},
// Kills the specified app.
kill: function(aOrigin, aCallback) {
var callback = aCallback || marionetteScriptFinished;
let runningApps = window.wrappedJSObject.WindowManager.getRunningApps();
if (!runningApps.hasOwnProperty(aOrigin)) {
callback(false);
}
else {
window.addEventListener('appterminated', function gt_onAppTerminated() {
window.removeEventListener('appterminated', gt_onAppTerminated);
waitFor(
function() {
console.log("app with origin '" + aOrigin + "' has terminated");
callback(true);
},
function() {
let runningApps =
window.wrappedJSObject.WindowManager.getRunningApps();
return !runningApps.hasOwnProperty(aOrigin);
}
);
});
console.log("terminating app with origin '" + aOrigin + "'");
window.wrappedJSObject.WindowManager.kill(aOrigin);
}
},
// Kills all running apps, except the homescreen.
killAll: function() {
let originsToClose = [];
let that = this;
let runningApps = window.wrappedJSObject.WindowManager.getRunningApps();
for (let origin in runningApps) {
if (origin.indexOf('homescreen') == -1) {
originsToClose.push(origin);
}
}
if (!originsToClose.length) {
marionetteScriptFinished(true);
return;
}
originsToClose.slice(0).forEach(function(origin) {
GaiaApps.kill(origin, function() {});
});
// Even after the 'appterminated' event has been fired for an app,
// it can still exist in the apps list, so wait until 1 or fewer
// apps are running (since we don't close the homescreen app).
waitFor(
function() { marionetteScriptFinished(true); },
function() { return that.numRunningApps() <= 1; }
);
},
// Launches app with the specified name (e.g., 'Calculator'); returns the
// app frame's id if successful, false if the app can't be found, or times
// out if the app frame can't be found after launching the app.
launchWithName: function(name) {
GaiaApps.locateWithName(name, function(app, appName, entryPoint) {
if (app) {
let runningApps = window.wrappedJSObject.WindowManager.getRunningApps();
let origin = GaiaApps.getRunningAppOrigin(appName);
let alreadyRunning = !!origin;
app.launch(entryPoint || null);
waitFor(
function() {
let app = runningApps[origin];
let result = {frame: app.frame.firstChild,
src: app.iframe.src,
name: app.name,
origin: origin};
if (alreadyRunning) {
// return the app's frame id
marionetteScriptFinished(result);
}
else {
window.addEventListener('apploadtime', function apploadtime() {
window.removeEventListener('apploadtime', apploadtime);
marionetteScriptFinished(result);
});
}
},
// wait until the app is found in the running apps list
function() {
origin = GaiaApps.getRunningAppOrigin(appName);
return !!origin;
}
);
} else {
marionetteScriptFinished(false);
}
});
},
/**
* Uninstalls the app with the specified name.
*/
uninstallWithName: function(name) {
GaiaApps.locateWithName(name, function uninstall(app) {
navigator.mozApps.mgmt.uninstall(app);
marionetteScriptFinished(false);
});
}
};