This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
child_window_factory.js
332 lines (300 loc) · 10.7 KB
/
child_window_factory.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'use strict';
/* global AppWindow, PopupWindow, ActivityWindow, SettingsListener, Service,
AttentionWindow, MozActivity, GlobalOverlayWindow, TrustedWindow */
(function(exports) {
var ENABLE_IN_APP_SHEET = false;
SettingsListener.observe('in-app-sheet.enabled', false, function(value) {
ENABLE_IN_APP_SHEET = value;
});
/**
* ChildWindowFactory is a submodule of AppWindow,
* its responsbility is to:
*
* (1) deal with window.open request
* from mozbrowser iframe to open a proper new window.
* (2) deal with launchactivity request to open activity window.
*
* When an app is opening a page within same origin via window.open,
* the generated new app window would be linked to its caller.
* You could refer to the opener via <code>this.previousWindow</code>
* and refer the openee via <code>this.nextWindow</code>.
*
* On the other hand, if <code>"dialog"</code> feature is specified in
* window.open, we will open a front window, which is an instance of
* PopupWindow, on the caller and set it as the rear window of the callee.
* You could access the dialog's opener via <code>this.rearWindow</code>
* and access the openee via <code>this.frontWindow</code> in the opener.
*
* At most an appWindow instance could have one front window and
* one next window.
*
* @param {AppWindow} app The ordering window of this factory.
*/
var ChildWindowFactory = function ChildWindowFactory(app) {
this.app = app;
this.app.element.addEventListener('mozbrowseropenwindow', this);
this.app.element.addEventListener('_launchactivity',
this.createActivityWindow.bind(this));
this.app.element.addEventListener('_launchtrusted',
this.createTrustedWindow.bind(this));
};
ChildWindowFactory.prototype.handleEvent =
function cwf_handleEvent(evt) {
this.app.debug('[cwf] handling ' + evt.type);
// Prevent Gecko's default handler from opening the window.
evt.preventDefault();
// Handle event from child window.
if (evt.detail && evt.detail.instanceID &&
evt.detail.instanceID !== this.app.instanceID) {
if (this['_handle_child_' + evt.type]) {
this['_handle_child_' + evt.type](evt);
}
return;
}
// Skip to wrapperWindowFactory.
if (this.isHomescreen) {
// XXX: Launch wrapper window here.
return;
}
// <a href="" target="_blank"> should never be part of the app
// except while FTU is running: windows must be closable & parented by FTU
if (evt.detail.name == '_blank' &&
!Service.query('isFtuRunning') &&
evt.detail.features !== 'attention' &&
evt.detail.features !== 'global-clickthrough-overlay') {
this.createNewWindow(evt);
evt.stopPropagation();
return;
}
// Check if this is a call to open the url in an known app.
if (evt.detail.isApp) {
this.app.publish('openwindow',
{ manifestURL: evt.detail.name,
url: evt.detail.url,
timestamp: Date.now() });
evt.stopPropagation();
return;
}
var caught = false;
switch (evt.detail.features) {
case 'dialog':
// Only open popupWindow by app/http/https prefix
if (/^(app|http|https):\/\//i.test(evt.detail.url)) {
caught = this.createPopupWindow(evt);
} else {
caught = this.launchActivity(evt);
}
break;
case 'alwaysLowered':
if (this.app.hasPermission('open-hidden-window')) {
caught = this.createPopupWindow(evt);
}
break;
case 'attention':
// Open attentionWindow
if (!this.createAttentionWindow(evt)) {
this.createPopupWindow(evt);
}
break;
case 'global-clickthrough-overlay':
// Open GlobalOverlayWindow.
this.createGlobalOverlayWindow(evt);
break;
case 'mozhaidasheet':
// This feature is for internal usage only
// before we have final API to open an inner sheet.
caught = this.createChildWindow(evt);
break;
default:
if (ENABLE_IN_APP_SHEET) {
caught = this.createChildWindow(evt);
} else {
caught = this.createPopupWindow(evt);
}
break;
}
if (caught) {
evt.stopPropagation();
}
};
ChildWindowFactory.prototype.createPopupWindow = function(evt) {
if (this.app.frontWindow &&
(this.app.frontWindow.isTransitioning() ||
this.app.frontWindow.isActive())) {
return false;
}
var stayBackground = evt.detail.features.indexOf('alwaysLowered') >= 0;
var configObject = {
url: evt.detail.url,
name: this.app.name,
iframe: evt.detail.frameElement,
origin: this.app.origin,
rearWindow: this.app,
stayBackground: stayBackground
};
var childWindow = new PopupWindow(configObject);
if (!stayBackground) {
childWindow.element.addEventListener('_opened', this);
childWindow.element.addEventListener('_closing', this);
}
childWindow.open();
return true;
};
ChildWindowFactory.prototype._sameOrigin = function(url1, url2) {
var a = url1.split('/');
var b = url2.split('/');
return (a[0] === b[0] && a[2] === b[2]);
};
ChildWindowFactory.prototype.createNewWindow = function(evt) {
if (!this.app.isActive() || this.app.isTransitioning()) {
return false;
}
var parentAllowFullscreenAttr =
evt.target.getAttribute('mozallowfullscreen');
var iframe = evt.detail.frameElement;
// This new window should be allowed to go full screen.
if (parentAllowFullscreenAttr) {
iframe.setAttribute('mozallowfullscreen', parentAllowFullscreenAttr);
}
var configObject = {
url: evt.detail.url,
name: evt.detail.name,
iframe: iframe,
isPrivate: this.app.isPrivateBrowser()
};
window.dispatchEvent(new CustomEvent('openwindow', {
detail: configObject
}));
return true;
};
ChildWindowFactory.prototype.createChildWindow = function(evt) {
if (!this.app.isActive() || this.app.isTransitioning()) {
return false;
}
var configObject = {
url: evt.detail.url,
name: this.app.name,
iframe: evt.detail.frameElement,
origin: this.app.origin,
isPrivate: this.app.isPrivateBrowser()
};
if (this._sameOrigin(this.app.origin, evt.detail.url)) {
configObject.manifestURL = this.app.manifestURL;
configObject.previousWindow = this.app;
} else {
configObject.name = '';
configObject.origin = evt.detail.url;
}
var childWindow = new AppWindow(configObject);
childWindow.requestOpen();
return true;
};
ChildWindowFactory.prototype.createAttentionWindow = function(evt) {
if (!this.app || !this.app.hasPermission('attention')) {
console.error('Cannot create attention window. ' +
'Invalid of underprivileged app');
return false;
}
// Canceling any full screen web content
if (document.mozFullScreen) {
document.mozCancelFullScreen();
}
var attentionFrame = evt.detail.frameElement;
var attention = new AttentionWindow({
iframe: attentionFrame,
url: evt.detail.url,
name: evt.detail.name,
manifestURL: this.app.manifestURL,
origin: this.app.origin,
parentWindow: this.app
});
this.app.attentionWindow = attention;
attention.requestOpen();
return true;
};
ChildWindowFactory.prototype.createGlobalOverlayWindow = function(evt) {
if (!this.app || !this.app.hasPermission('global-clickthrough-overlay')) {
console.error('Cannot create global overlay window. ' +
'Invalid of underprivileged app');
return false;
}
var overlayFrame = evt.detail.frameElement;
var overlay = new GlobalOverlayWindow({
iframe: overlayFrame,
url: evt.detail.url,
name: evt.detail.name,
manifestURL: this.app.manifestURL,
origin: this.app.origin,
parentWindow: this.app
});
this.app.overlayWindow = overlay;
overlay.requestOpen();
return true;
};
ChildWindowFactory.prototype._handle_child__opened = function(evt) {
// Do nothing if we are not active or we are being killing.
if (!this.app.isVisible() || this.app._killed) {
return;
}
this.app.setVisible(false, true);
};
ChildWindowFactory.prototype._handle_child__closing = function(evt) {
// Do nothing if we are not active or we are being killing.
if (!this.app.isVisible() || this.app._killed) {
return;
}
this.app.setOrientation();
if (Service.query('getTopMostWindow').getBottomMostWindow() ===
this.app.getBottomMostWindow()) {
this.app.setVisible(true, true);
}
// An activity handled by ActivityWindow is always an inline activity.
// All window activities are handled by AppWindow. All inline
// activities have a rearWindow. Once this inline activity is killed,
// make rear window visible to screen reader and the focus should be
// transfered to its rear window.
evt.detail.rearWindow._setVisibleForScreenReader(true);
evt.detail.rearWindow.focus();
};
ChildWindowFactory.prototype.createActivityWindow = function(evt) {
var configuration = evt.detail;
var top = this.app.getTopMostWindow();
// If the lastActivity is the same as launch request, we don't need to
// create another activity.
if (top.manifestURL == configuration.manifestURL &&
top.url == configuration.url) {
return;
}
// We need to blur the opener before opening the new one.
top.setNFCFocus(false);
var activity = new ActivityWindow(configuration, top);
activity.element.addEventListener('_closing', this);
activity.element.addEventListener('_opened', this);
activity.open();
// Make topmost window browser element invisibile to screen reader.
top._setVisibleForScreenReader(false);
};
ChildWindowFactory.prototype.createTrustedWindow = function(evt) {
var configuration = evt.detail;
var top = this.app.getTopMostWindow();
var trusted = new TrustedWindow(configuration, top);
trusted.element.addEventListener('_opened', this);
trusted.element.addEventListener('_closing', this);
trusted.open();
};
ChildWindowFactory.prototype.launchActivity = function(evt) {
var activity = new MozActivity({
name: 'view',
data: {
type: 'url',
url: evt.detail.url
}
});
activity.onerror = function() {
this.app.debug(
'view activity error:' + activity.error.name + evt.detail.url);
};
return true;
};
exports.ChildWindowFactory = ChildWindowFactory;
}(window));