michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: const { DebuggerServer } = Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); michael@0: const { DebuggerClient } = Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); michael@0: michael@0: const { FileUtils } = Cu.import("resource://gre/modules/FileUtils.jsm"); michael@0: const { Services } = Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: let gClient, gActor; michael@0: michael@0: function connect(onDone) { michael@0: michael@0: if (Services.appinfo.name == "B2G") { michael@0: // On b2g, we try to exercice the code that launches the production debugger server michael@0: let settingsService = Cc["@mozilla.org/settingsService;1"].getService(Ci.nsISettingsService); michael@0: settingsService.createLock().set("devtools.debugger.remote-enabled", true, null); michael@0: // We can't use `set` callback as it is fired before shell.js code listening for this setting michael@0: // is actually called. Same thing applies to mozsettings-changed obs notification. michael@0: // So listen to a custom event until bug 942756 lands michael@0: let observer = { michael@0: observe: function (subject, topic, data) { michael@0: Services.obs.removeObserver(observer, "debugger-server-started"); michael@0: let transport = debuggerSocketConnect("127.0.0.1", 6000); michael@0: startClient(transport, onDone); michael@0: } michael@0: }; michael@0: Services.obs.addObserver(observer, "debugger-server-started", false); michael@0: } else { michael@0: // Initialize a loopback remote protocol connection michael@0: DebuggerServer.init(function () { return true; }); michael@0: // We need to register browser actors to have `listTabs` working michael@0: // and also have a root actor michael@0: DebuggerServer.addBrowserActors(); michael@0: let transport = DebuggerServer.connectPipe(); michael@0: startClient(transport, onDone); michael@0: } michael@0: } michael@0: michael@0: function startClient(transport, onDone) { michael@0: // Setup client and actor used in all tests michael@0: gClient = new DebuggerClient(transport); michael@0: gClient.connect(function onConnect() { michael@0: gClient.listTabs(function onListTabs(aResponse) { michael@0: gActor = aResponse.webappsActor; michael@0: if (gActor) michael@0: webappActorRequest({type: "watchApps"}, onDone); michael@0: }); michael@0: }); michael@0: michael@0: gClient.addListener("appInstall", function (aState, aType, aPacket) { michael@0: sendAsyncMessage("installed-event", { manifestURL: aType.manifestURL }); michael@0: }); michael@0: michael@0: gClient.addListener("appUninstall", function (aState, aType, aPacket) { michael@0: sendAsyncMessage("uninstalled-event", { manifestURL: aType.manifestURL }); michael@0: }); michael@0: michael@0: addMessageListener("appActorRequest", request => { michael@0: webappActorRequest(request, response => { michael@0: sendAsyncMessage("appActorResponse", response); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function webappActorRequest(request, onResponse) { michael@0: if (!gActor) { michael@0: connect(webappActorRequest.bind(null, request, onResponse)); michael@0: return; michael@0: } michael@0: michael@0: request.to = gActor; michael@0: gClient.request(request, onResponse); michael@0: } michael@0: michael@0: michael@0: function downloadURL(url, file) { michael@0: let channel = Services.io.newChannel(url, null, null); michael@0: let istream = channel.open(); michael@0: let bstream = Cc["@mozilla.org/binaryinputstream;1"] michael@0: .createInstance(Ci.nsIBinaryInputStream); michael@0: bstream.setInputStream(istream); michael@0: let data = bstream.readBytes(bstream.available()); michael@0: michael@0: let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"] michael@0: .createInstance(Ci.nsIFileOutputStream); michael@0: ostream.init(file, 0x04 | 0x08 | 0x20, 0600, 0); michael@0: ostream.write(data, data.length); michael@0: ostream.QueryInterface(Ci.nsISafeOutputStream).finish(); michael@0: } michael@0: michael@0: // Install a test packaged webapp from data folder michael@0: addMessageListener("install", function (aMessage) { michael@0: let url = aMessage.url; michael@0: let appId = aMessage.appId; michael@0: michael@0: try { michael@0: // Download its content from mochitest http server michael@0: // Copy our package to tmp folder, where the actor retrieves it michael@0: let zip = FileUtils.getDir("TmpD", ["b2g", appId], true, true); michael@0: zip.append("application.zip"); michael@0: downloadURL(url, zip); michael@0: michael@0: let request = {type: "install", appId: appId}; michael@0: webappActorRequest(request, function (aResponse) { michael@0: sendAsyncMessage("installed", aResponse); michael@0: }); michael@0: } catch(e) { michael@0: dump("installTestApp exception: " + e + "\n"); michael@0: } michael@0: }); michael@0: michael@0: addMessageListener("cleanup", function () { michael@0: webappActorRequest({type: "unwatchApps"}, function () { michael@0: gClient.close(); michael@0: }); michael@0: }); michael@0: