This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
bootstrap.js
291 lines (254 loc) · 11.5 KB
/
bootstrap.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
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import('resource://gre/modules/Services.jsm');
function debug(data) {
dump('browser-helper: ' + data + '\n');
}
function initResponsiveDesign(browserWindow) {
// Inject custom controls in responsive view
Cu.import('resource:///modules/devtools/responsivedesign.jsm');
ResponsiveUIManager.once('on', function(event, tab, responsive) {
let document = tab.ownerDocument;
browserWindow.shell = {
sendChromeEvent: sendChromeEvent
};
// Ensure tweaking only the first responsive mode opened
responsive.stack.classList.add('os-mode');
let sleepButton = document.createElement('button');
sleepButton.id = 'os-sleep-button';
sleepButton.setAttribute('top', 0);
sleepButton.setAttribute('right', 0);
sleepButton.addEventListener('mousedown', function() {
sendChromeEvent({type: 'sleep-button-press'});
});
sleepButton.addEventListener('mouseup', function() {
sendChromeEvent({type: 'sleep-button-release'});
});
responsive.stack.appendChild(sleepButton);
let volumeButtons = document.createElement('vbox');
volumeButtons.id = 'os-volume-buttons';
volumeButtons.setAttribute('top', 0);
volumeButtons.setAttribute('left', 0);
let volumeUp = document.createElement('button');
volumeUp.id = 'os-volume-up-button';
volumeUp.addEventListener('mousedown', function() {
sendChromeEvent({type: 'volume-up-button-press'});
});
volumeUp.addEventListener('mouseup', function() {
sendChromeEvent({type: 'volume-up-button-release'});
});
let volumeDown = document.createElement('button');
volumeDown.id = 'os-volume-down-button';
volumeDown.addEventListener('mousedown', function() {
sendChromeEvent({type: 'volume-down-button-press'});
});
volumeDown.addEventListener('mouseup', function() {
sendChromeEvent({type: 'volume-down-button-release'});
});
volumeButtons.appendChild(volumeUp);
volumeButtons.appendChild(volumeDown);
responsive.stack.appendChild(volumeButtons);
// <toolbar id="os-hardware-button">
// <toolbarbutton id="os-home-button" />
// </toolbar>
let bottomToolbar = document.createElement('toolbar');
bottomToolbar.id = 'os-hardware-buttons';
bottomToolbar.setAttribute('align', 'center');
bottomToolbar.setAttribute('pack', 'center');
let homeButton = document.createElement('toolbarbutton');
homeButton.id = 'os-home-button';
homeButton.setAttribute('class', 'devtools-toolbarbutton');
homeButton.addEventListener('mousedown', function() {
sendChromeEvent({type: 'home-button-press'});
});
homeButton.addEventListener('mouseup', function() {
sendChromeEvent({type: 'home-button-release'});
});
bottomToolbar.appendChild(homeButton);
responsive.container.appendChild(bottomToolbar);
// Simulate chrome event.
function sendChromeEvent(detail) {
let contentWindow = responsive.browser.contentWindow;
var contentDetail = Components.utils.createObjectIn(contentWindow);
for (var i in detail) {
contentDetail[i] = detail[i];
}
Components.utils.makeObjectPropsNormal(contentDetail);
var customEvt = contentWindow.document.createEvent('CustomEvent');
customEvt.initCustomEvent('mozChromeEvent', true, true, contentDetail);
contentWindow.dispatchEvent(customEvt);
}
});
// Cleanup responsive mode if it's disabled
ResponsiveUIManager.on('off', function(event, tab, responsive) {
if (responsive.stack.classList.contains('os-mode')) {
responsive.stack.classList.remove('os-mode');
let document = tab.ownerDocument;
let sleepButton = document.getElementById('os-sleep-button');
responsive.stack.removeChild(sleepButton);
let volumeButtons = document.getElementById('os-volume-buttons');
responsive.stack.removeChild(volumeButtons);
let bottomToolbar = document.getElementById('os-hardware-buttons');
responsive.container.removeChild(bottomToolbar);
}
});
// Automatically toggle responsive design mode
let width = 320, height = 480;
// We have to take into account padding and border introduced with the
// device look'n feel:
width += 15*2; // Horizontal padding
width += 1*2; // Vertical border
height += 60; // Top Padding
height += 1; // Top border
let args = {'width': width, 'height': height};
let mgr = browserWindow.ResponsiveUI.ResponsiveUIManager;
mgr.handleGcliCommand(browserWindow,
browserWindow.gBrowser.selectedTab,
'resize to',
args);
// Enable touch events
browserWindow.gBrowser.selectedTab.__responsiveUI.enableTouch();
}
function startup(data, reason) {
try {
// Initialize various JSM instanciated by shell.js
// All of them are usefull even if we don't use them in this file.
Cu.import('resource://gre/modules/ContactService.jsm');
Cu.import('resource://gre/modules/SettingsChangeNotifier.jsm');
Cu.import('resource://gre/modules/ActivitiesService.jsm');
Cu.import('resource://gre/modules/PermissionPromptHelper.jsm');
var mm = Cc['@mozilla.org/globalmessagemanager;1']
.getService(Ci.nsIMessageBroadcaster);
// Allow panning to work in the system app.
mm.loadFrameScript('chrome://global/content/BrowserElementPanning.js', true);
// Enable mozApps.getAll and app.launch to work.
// By default on firefox, apps are consider as not launchable
// and only launchable apps appear in getAll().
Cu.import('resource://gre/modules/Webapps.jsm');
DOMApplicationRegistry.allAppsLaunchable = true;
// Track loading of the system app in order to load our striped down copy
// of shell.xul
Services.obs.addObserver(function(document) {
// Some documents like XBL don't have location and should be ignored
if (!document.location)
return;
let domain = document.location.toString();
if (domain.indexOf('system.gaiamobile.org') != -1) {
let scope = {
content: document.defaultView
};
Services.scriptloader.loadSubScript(
'chrome://browser-helper.js/content/shell.js', scope);
}
}, 'document-element-inserted', false);
// Watch for app load start in firefox tabs.
// We have to set the app id on each docshell where we are trying to load
// an app. That's because we can't set mozapp attribute on firefox iframes
// as Firefox is still using xul:browser.
let nsIWebProgressListener = Ci.nsIWebProgressListener;
let appsService = Cc['@mozilla.org/AppsService;1'].getService(Ci.nsIAppsService);
let docShellListener = {
onStateChange: function onStateChange(webProgress, request, flags, status) {
if (!webProgress.chromeEventHandler ||
webProgress.chromeEventHandler.tagName != "xul:browser") {
// Only set app id for firefox tab <xul:browser> frames
return;
}
// Catch any load start request
if (flags & nsIWebProgressListener.STATE_START &&
flags & nsIWebProgressListener.STATE_IS_DOCUMENT &&
(request instanceof Ci.nsIChannel || 'URI' in request)) {
// Try to compute the manifest URL out of document URL
// We should end up with urls like:
// http://system.gaiamobile.org:8080/manifest.webapp
// app://system.gaiamobile.org/manifest.webapp
let manifestURL = request.URI.prePath + '/manifest.webapp';
let manifest = appsService.getAppByManifestURL(manifestURL);
if (manifest) {
let app = manifest.QueryInterface(Ci.mozIApplication);
// test-agent expects its iframes to load apps without any permissions
// so prevent setting the correct app id to its iframes
if (!webProgress.DOMWindow || !webProgress.DOMWindow.top ||
!webProgress.DOMWindow.top.location.host.startsWith("test-agent.gaiamobile.org")) {
webProgress.QueryInterface(Ci.nsIDocShell).setIsApp(app.localId);
}
}
}
}
};
// Watch for new Firefox top level windows
Services.obs.addObserver(function(win) {
win.addEventListener('DOMContentLoaded', function loaded() {
win.removeEventListener('DOMContentLoaded', loaded);
if (win.gBrowser) {
// Listen for loads happening in all browser windows.
win.gBrowser.addProgressListener(docShellListener);
}
});
}, 'domwindowopened', false);
Services.obs.addObserver(function() {
let browserWindow = Services.wm.getMostRecentWindow('navigator:browser');
// Inject CSS in browser to customize responsive view
let doc = browserWindow.document;
let pi = doc.createProcessingInstruction('xml-stylesheet', 'href="chrome://browser-helper.js/content/browser.css" type="text/css"');
doc.insertBefore(pi, doc.firstChild);
// Start responsive design mode
initResponsiveDesign(browserWindow);
// And devtool panel while maximizing its size according to screen size
Services.prefs.setIntPref('devtools.toolbox.sidebar.width',
browserWindow.outerWidth - 550);
gDevToolsBrowser.selectToolCommand(browserWindow.gBrowser);
let KeyboardGlobal = Cu.import('resource://gre/modules/Keyboard.jsm');
// SystemAppProxy.jsm doesn't ship into Firefox.
// Keyboard.jsm is the only one user of it outside of b2g/ folder.
// So hack it, in order to allow keyboard to send events to the system app
Object.defineProperty(KeyboardGlobal, "SystemAppProxy", {
value: {
dispatchEvent: function (detail) {
let contentWindow = browserWindow.content;
var contentDetail = Components.utils.createObjectIn(contentWindow);
for (var i in detail) {
contentDetail[i] = detail[i];
}
Components.utils.makeObjectPropsNormal(contentDetail);
var customEvt = contentWindow.document.createEvent('CustomEvent');
customEvt.initCustomEvent('mozChromeEvent', true, true, contentDetail);
contentWindow.dispatchEvent(customEvt);
}
}
});
Keyboard.initFormsFrameScript(mm);
mm.loadFrameScript('chrome://global/content/forms.js', true);
}, 'sessionstore-windows-restored', false);
try {
// Register a new devtool panel with various OS controls
Cu.import('resource:///modules/devtools/gDevTools.jsm');
gDevTools.registerTool({
id: 'firefox-os-controls',
key: 'F',
modifiers: 'accel,shift',
icon: 'chrome://browser-helper.js/content/panel/icon.gif',
url: 'chrome://browser-helper.js/content/panel/index.html',
label: 'FFOS Control',
tooltip: 'Set of controls to tune FirefoxOS apps in Desktop browser',
isTargetSupported: function(target) {
return target.isLocalTab;
},
build: function(iframeWindow, toolbox) {
iframeWindow.wrappedJSObject.tab = toolbox.target.window;
return {
destroy: function () {}
};
}
});
} catch(e) {
debug('Can\'t load the devtools panel. Likely because this version of Gecko is too old');
}
} catch (e) {
debug('Something went wrong while trying to start browser-helper: ' + e);
}
}