This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
main.js
308 lines (252 loc) · 8.75 KB
/
main.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
const {Cu} = require("chrome");
const {DebuggerClient} = Cu.import("resource://gre/modules/devtools/dbg-client.jsm", {});
const {Devices} = Cu.import("resource://gre/modules/devtools/Devices.jsm");
let {gDevTools} = Cu.import("resource:///modules/devtools/gDevTools.jsm", {});
let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
Cu.import("resource://gre/modules/Services.jsm");
const gcli = require("./devtools/gcli");
const task = require("./util/task");
const server = require("./chromium/server");
const timers = require("sdk/timers");
const { notify } = require("sdk/notifications");
const {ConnectionManager} = devtools.require("devtools/client/connection-manager");
const {USBRuntime} = devtools.require("devtools/webide/runtimes");
const {AppManager} = devtools.require("devtools/webide/app-manager");
let winObserver = function(win, topic) {
if (topic == "domwindowopened") {
win.addEventListener("load", function onLoadWindow() {
win.removeEventListener("load", onLoadWindow, false);
if (win.document.documentURI == "chrome://webide/content/webide.xul") {
win.setTimeout(() => onWebIDEWindowOpen(win), 0);
}
}, false);
}
}
Services.ww.registerNotification(winObserver);
function onWebIDEWindowOpen(window) {
let promise = window.promise;
/** iOS device **/
// To find iOS devices, we need to use ios_webkit_debug_proxy (works on linux and windows).
// On port 9221, we can find a list of connected devices.
// I believe we should build a static version of ios_webkit_debug_proxy
// and integrate it in the ADB Addon Helper.
//
// At the moment, we just assume that a device is plugged and that ios_webkit_debug_proxy
// is runnning.
let iOSRuntime = {
getName: function() {
return "iOS proxy";
},
connect: function(connection) {
let transport = server.connect("http://localhost:9222");
connection.connect(transport);
return promise.resolve();
},
}
AppManager.runtimeList.custom.push(iOSRuntime);
/** Chrome on Android **/
let baseCustomRuntimes = AppManager.runtimeList.custom;
function AndroidRuntime(id) {
this._usbRuntime = new USBRuntime(id);
this._usbRuntime.updateNameFromADB().then( () => AppManager.update("runtimelist"));
}
AndroidRuntime.prototype = {
getName: function() {
return "Google Chrome: " + this._usbRuntime.getName();
},
connect: function(connection) {
let device = Devices.getByName(this._usbRuntime.id);
let freeLocalPort = ConnectionManager.getFreeTCPPort();
let local = "tcp:" + freeLocalPort;
let remote = "localabstract:chrome_devtools_remote";
return device.forwardPort(local, remote).then(() => {
let transport = server.connect("http://localhost:" + freeLocalPort);
connection.connect(transport);
});
},
}
function scan() {
AppManager.runtimeList.custom = [...baseCustomRuntimes];
AppManager.update("runtimelist");
for (let id of Devices.available()) {
let device = Devices.getByName(id);
device.shell("grep -q chrome_devtools_remote /proc/net/unix && echo OK || echo KO").then(stdout => {
if (stdout.match(/^OK/)) {
AppManager.runtimeList.custom.push(new AndroidRuntime(id));
AppManager.update("runtimelist");
}
});
}
}
Devices.on("unregister", scan);
Devices.on("register", scan);
scan();
}
var openToolbox = task.async(function*(client, form, hostOptions, options={}) {
let tool = options.tool;
let targetOptions = {
client: client,
form: form,
chrome: options.chrome || false
};
let target = yield devtools.TargetFactory.forRemoteTab(targetOptions);
if (options.loadURL && target.url != options.loadURL) {
yield new Promise((resolve, reject) => {
target.activeTab.navigateTo(options.loadURL, () => {
target.once("navigate", resolve);
});
});
}
let hostType = devtools.Toolbox.HostType.WINDOW;
let toolbox = yield gDevTools.showToolbox(target, tool, hostType, hostOptions);
toolbox.once("destroyed", function() {
client.close();
});
});
const ui = require("sdk/ui");
let action_button = ui.ActionButton({
id: "ui-button",
label: "Debug whatever is on port 9222",
icon: "./icon.png",
onClick: task.async(function*(state) {
let response;
try {
response = yield tryConnect("http://localhost:9222");
} catch(ex) {
notify({
title: "No browser found.",
text: "No browser found on port 9222"
});
return;
}
let client = response.client;
let response = response.response;
let tab = response.tabs[response.selected];
try {
yield openToolbox(client, tab, {}, {
tool: "inspector",
});
} catch (e) {
notify({
title: "Can't debug page",
text: "A remote debugging connection is not allowed for page " + tab.url
});
}
})
});
function tryConnect(url) {
console.log("trying to connect...");
let transport = server.connect(url);
let client = new DebuggerClient(transport);
return new Promise((resolve, reject) => {
client.connect((type, traits) => {
client.listTabs(response => {
if (response.error) {
client.close();
reject(response);
} else {
resolve({client: client, response: response});
}
});
});
});
}
const child_process = require("sdk/system/child_process");
function wait(ms) {
return new Promise((resolve, reject) => {
timers.setTimeout(() => {
resolve();
}, ms);
});
}
var connectDebugger = task.async(function*(url, spawn) {
let response = null;
try {
response = yield tryConnect(url);
} catch(ex) {
spawn();
}
let tries = 25;
let delay = 100;
while (!response && tries) {
yield wait(delay);
try {
response = yield tryConnect(url);
} catch(ex) {}
tries--;
}
return response;
});
var debugChrome = task.async(function*(browserWindow, contentWindow) {
let response = yield connectDebugger("http://localhost:9225", () => {
let windowPosition = "--window-position=" + browserWindow.screenX + "," + browserWindow.screenY;
let windowSize = "--window-size=" + browserWindow.outerWidth + "," + (browserWindow.outerHeight - 200);
child_process.spawn("/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary", ["--remote-debugging-port=9225", windowPosition, windowSize]);
});
if (!response) {
// Complain.
return;
}
let top = browserWindow.screenY + browserWindow.outerHeight - 200;
let left = browserWindow.screenX;
let height = 200;
let width = browserWindow.outerWidth;
let positionArgs = "top=" + top + ",left=" + left + ",outerHeight=" + height + ",outerWidth=" + width;
let client = response.client;
response = response.response;
let tab = response.tabs[response.selected];
yield openToolbox(client, response.tabs[response.selected], {
positionArgs: positionArgs
}, {
tool: "inspector",
loadURL: contentWindow.location.toString()
});
});
var debugIOS = task.async(function*(browserWindow, contentWindow) {
let response = yield connectDebugger("http://localhost:9230", () => {
child_process.spawn("/usr/local/bin/ios_webkit_debug_proxy", ["-c", "null:9221,:9230-9240"]);
});
let client = response.client;
response = response.response;
let tab = response.tabs[response.selected];
yield openToolbox(client, response.tabs[response.selected], {}, {
tool: "inspector",
loadURL: contentWindow.location.toString()
});
});
var debugAndroid = task.async(function*(browserWindow, contentWindow) {
let response = yield connectDebugger("http://localhost:9240", () => {
child_process.spawn("/Users/dcamp/moz/android-sdk-macosx/platform-tools/adb", ["forward", "tcp:9240", "localabstract:chrome_devtools_remote"]);
});
let client = response.client;
response = response.response;
let tab = response.tabs[response.selected];
yield openToolbox(client, response.tabs[response.selected], {}, {
tool: "inspector",
loadURL: contentWindow.location.toString()
});
});
// TODO: These commands should eventually be using the new export.items format
gcli.addItems([
{
name: 'chrome',
description: 'Check remotely in Chrome Desktop',
exec: function(args, context) {
return debugChrome(context.environment.chromeWindow, context.environment.window);
}
},
{
name: 'ios',
description: 'Check remotely on Safari (iOS)',
exec: function(args, context) {
return debugIOS(context.environment.chromeWindow, context.environment.window);
}
},
{
name: 'android',
description: 'Check on remotely on Android',
exec: function(args, context) {
return debugAndroid(context.environment.chromeWindow, context.environment.window);
}
}
]);