1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/apps/tests/debugger-protocol-helper.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +const Cc = Components.classes; 1.8 +const Ci = Components.interfaces; 1.9 +const Cu = Components.utils; 1.10 + 1.11 +const { DebuggerServer } = Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); 1.12 +const { DebuggerClient } = Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); 1.13 + 1.14 +const { FileUtils } = Cu.import("resource://gre/modules/FileUtils.jsm"); 1.15 +const { Services } = Cu.import("resource://gre/modules/Services.jsm"); 1.16 + 1.17 +let gClient, gActor; 1.18 + 1.19 +function connect(onDone) { 1.20 + 1.21 + if (Services.appinfo.name == "B2G") { 1.22 + // On b2g, we try to exercice the code that launches the production debugger server 1.23 + let settingsService = Cc["@mozilla.org/settingsService;1"].getService(Ci.nsISettingsService); 1.24 + settingsService.createLock().set("devtools.debugger.remote-enabled", true, null); 1.25 + // We can't use `set` callback as it is fired before shell.js code listening for this setting 1.26 + // is actually called. Same thing applies to mozsettings-changed obs notification. 1.27 + // So listen to a custom event until bug 942756 lands 1.28 + let observer = { 1.29 + observe: function (subject, topic, data) { 1.30 + Services.obs.removeObserver(observer, "debugger-server-started"); 1.31 + let transport = debuggerSocketConnect("127.0.0.1", 6000); 1.32 + startClient(transport, onDone); 1.33 + } 1.34 + }; 1.35 + Services.obs.addObserver(observer, "debugger-server-started", false); 1.36 + } else { 1.37 + // Initialize a loopback remote protocol connection 1.38 + DebuggerServer.init(function () { return true; }); 1.39 + // We need to register browser actors to have `listTabs` working 1.40 + // and also have a root actor 1.41 + DebuggerServer.addBrowserActors(); 1.42 + let transport = DebuggerServer.connectPipe(); 1.43 + startClient(transport, onDone); 1.44 + } 1.45 +} 1.46 + 1.47 +function startClient(transport, onDone) { 1.48 + // Setup client and actor used in all tests 1.49 + gClient = new DebuggerClient(transport); 1.50 + gClient.connect(function onConnect() { 1.51 + gClient.listTabs(function onListTabs(aResponse) { 1.52 + gActor = aResponse.webappsActor; 1.53 + if (gActor) 1.54 + webappActorRequest({type: "watchApps"}, onDone); 1.55 + }); 1.56 + }); 1.57 + 1.58 + gClient.addListener("appInstall", function (aState, aType, aPacket) { 1.59 + sendAsyncMessage("installed-event", { manifestURL: aType.manifestURL }); 1.60 + }); 1.61 + 1.62 + gClient.addListener("appUninstall", function (aState, aType, aPacket) { 1.63 + sendAsyncMessage("uninstalled-event", { manifestURL: aType.manifestURL }); 1.64 + }); 1.65 + 1.66 + addMessageListener("appActorRequest", request => { 1.67 + webappActorRequest(request, response => { 1.68 + sendAsyncMessage("appActorResponse", response); 1.69 + }); 1.70 + }); 1.71 +} 1.72 + 1.73 +function webappActorRequest(request, onResponse) { 1.74 + if (!gActor) { 1.75 + connect(webappActorRequest.bind(null, request, onResponse)); 1.76 + return; 1.77 + } 1.78 + 1.79 + request.to = gActor; 1.80 + gClient.request(request, onResponse); 1.81 +} 1.82 + 1.83 + 1.84 +function downloadURL(url, file) { 1.85 + let channel = Services.io.newChannel(url, null, null); 1.86 + let istream = channel.open(); 1.87 + let bstream = Cc["@mozilla.org/binaryinputstream;1"] 1.88 + .createInstance(Ci.nsIBinaryInputStream); 1.89 + bstream.setInputStream(istream); 1.90 + let data = bstream.readBytes(bstream.available()); 1.91 + 1.92 + let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"] 1.93 + .createInstance(Ci.nsIFileOutputStream); 1.94 + ostream.init(file, 0x04 | 0x08 | 0x20, 0600, 0); 1.95 + ostream.write(data, data.length); 1.96 + ostream.QueryInterface(Ci.nsISafeOutputStream).finish(); 1.97 +} 1.98 + 1.99 +// Install a test packaged webapp from data folder 1.100 +addMessageListener("install", function (aMessage) { 1.101 + let url = aMessage.url; 1.102 + let appId = aMessage.appId; 1.103 + 1.104 + try { 1.105 + // Download its content from mochitest http server 1.106 + // Copy our package to tmp folder, where the actor retrieves it 1.107 + let zip = FileUtils.getDir("TmpD", ["b2g", appId], true, true); 1.108 + zip.append("application.zip"); 1.109 + downloadURL(url, zip); 1.110 + 1.111 + let request = {type: "install", appId: appId}; 1.112 + webappActorRequest(request, function (aResponse) { 1.113 + sendAsyncMessage("installed", aResponse); 1.114 + }); 1.115 + } catch(e) { 1.116 + dump("installTestApp exception: " + e + "\n"); 1.117 + } 1.118 +}); 1.119 + 1.120 +addMessageListener("cleanup", function () { 1.121 + webappActorRequest({type: "unwatchApps"}, function () { 1.122 + gClient.close(); 1.123 + }); 1.124 +}); 1.125 +