browser/devtools/framework/connect/connect.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 "use strict";
michael@0 8
michael@0 9 const Cu = Components.utils;
michael@0 10 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
michael@0 11 Cu.import("resource://gre/modules/Services.jsm");
michael@0 12 Cu.import("resource://gre/modules/Task.jsm");
michael@0 13 Cu.import("resource://gre/modules/devtools/dbg-client.jsm");
michael@0 14 let {gDevTools} = Cu.import("resource:///modules/devtools/gDevTools.jsm", {});
michael@0 15 let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
michael@0 16 let {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
michael@0 17
michael@0 18 let gClient;
michael@0 19 let gConnectionTimeout;
michael@0 20
michael@0 21 XPCOMUtils.defineLazyGetter(window, 'l10n', function () {
michael@0 22 return Services.strings.createBundle('chrome://browser/locale/devtools/connection-screen.properties');
michael@0 23 });
michael@0 24
michael@0 25 /**
michael@0 26 * Once DOM is ready, we prefil the host/port inputs with
michael@0 27 * pref-stored values.
michael@0 28 */
michael@0 29 window.addEventListener("DOMContentLoaded", function onDOMReady() {
michael@0 30 window.removeEventListener("DOMContentLoaded", onDOMReady, true);
michael@0 31 let host = Services.prefs.getCharPref("devtools.debugger.remote-host");
michael@0 32 let port = Services.prefs.getIntPref("devtools.debugger.remote-port");
michael@0 33
michael@0 34 if (host) {
michael@0 35 document.getElementById("host").value = host;
michael@0 36 }
michael@0 37
michael@0 38 if (port) {
michael@0 39 document.getElementById("port").value = port;
michael@0 40 }
michael@0 41
michael@0 42 let form = document.querySelector("#connection-form form");
michael@0 43 form.addEventListener("submit", function() {
michael@0 44 window.submit();
michael@0 45 });
michael@0 46 }, true);
michael@0 47
michael@0 48 /**
michael@0 49 * Called when the "connect" button is clicked.
michael@0 50 */
michael@0 51 function submit() {
michael@0 52 // Show the "connecting" screen
michael@0 53 document.body.classList.add("connecting");
michael@0 54
michael@0 55 // Save the host/port values
michael@0 56 let host = document.getElementById("host").value;
michael@0 57 Services.prefs.setCharPref("devtools.debugger.remote-host", host);
michael@0 58
michael@0 59 let port = document.getElementById("port").value;
michael@0 60 Services.prefs.setIntPref("devtools.debugger.remote-port", port);
michael@0 61
michael@0 62 // Initiate the connection
michael@0 63 let transport;
michael@0 64 try {
michael@0 65 transport = debuggerSocketConnect(host, port);
michael@0 66 } catch(e) {
michael@0 67 // Bug 921850: catch rare exception from debuggerSocketConnect
michael@0 68 showError("unexpected");
michael@0 69 return;
michael@0 70 }
michael@0 71 gClient = new DebuggerClient(transport);
michael@0 72 let delay = Services.prefs.getIntPref("devtools.debugger.remote-timeout");
michael@0 73 gConnectionTimeout = setTimeout(handleConnectionTimeout, delay);
michael@0 74 gClient.connect(onConnectionReady);
michael@0 75 }
michael@0 76
michael@0 77 /**
michael@0 78 * Connection is ready. List actors and build buttons.
michael@0 79 */
michael@0 80 let onConnectionReady = Task.async(function*(aType, aTraits) {
michael@0 81 clearTimeout(gConnectionTimeout);
michael@0 82
michael@0 83 let deferred = promise.defer();
michael@0 84 gClient.listAddons(deferred.resolve);
michael@0 85 let response = yield deferred.promise;
michael@0 86
michael@0 87 let parent = document.getElementById("addonActors")
michael@0 88 if (!response.error && response.addons.length > 0) {
michael@0 89 // Add one entry for each add-on.
michael@0 90 for (let addon of response.addons) {
michael@0 91 if (!addon.debuggable) {
michael@0 92 continue;
michael@0 93 }
michael@0 94 buildAddonLink(addon, parent);
michael@0 95 }
michael@0 96 }
michael@0 97 else {
michael@0 98 // Hide the section when there are no add-ons
michael@0 99 parent.previousElementSibling.remove();
michael@0 100 parent.remove();
michael@0 101 }
michael@0 102
michael@0 103 deferred = promise.defer();
michael@0 104 gClient.listTabs(deferred.resolve);
michael@0 105 response = yield deferred.promise;
michael@0 106
michael@0 107 parent = document.getElementById("tabActors");
michael@0 108
michael@0 109 // Add Global Process debugging...
michael@0 110 let globals = JSON.parse(JSON.stringify(response));
michael@0 111 delete globals.tabs;
michael@0 112 delete globals.selected;
michael@0 113 // ...only if there are appropriate actors (a 'from' property will always
michael@0 114 // be there).
michael@0 115
michael@0 116 // Add one entry for each open tab.
michael@0 117 for (let i = 0; i < response.tabs.length; i++) {
michael@0 118 buildTabLink(response.tabs[i], parent, i == response.selected);
michael@0 119 }
michael@0 120
michael@0 121 let gParent = document.getElementById("globalActors");
michael@0 122
michael@0 123 // Build the Remote Process button
michael@0 124 if (Object.keys(globals).length > 1) {
michael@0 125 let a = document.createElement("a");
michael@0 126 a.onclick = function() {
michael@0 127 openToolbox(globals, true);
michael@0 128
michael@0 129 }
michael@0 130 a.title = a.textContent = window.l10n.GetStringFromName("mainProcess");
michael@0 131 a.className = "remote-process";
michael@0 132 a.href = "#";
michael@0 133 gParent.appendChild(a);
michael@0 134 }
michael@0 135 // Move the selected tab on top
michael@0 136 let selectedLink = parent.querySelector("a.selected");
michael@0 137 if (selectedLink) {
michael@0 138 parent.insertBefore(selectedLink, parent.firstChild);
michael@0 139 }
michael@0 140
michael@0 141 document.body.classList.remove("connecting");
michael@0 142 document.body.classList.add("actors-mode");
michael@0 143
michael@0 144 // Ensure the first link is focused
michael@0 145 let firstLink = parent.querySelector("a:first-of-type");
michael@0 146 if (firstLink) {
michael@0 147 firstLink.focus();
michael@0 148 }
michael@0 149 });
michael@0 150
michael@0 151 /**
michael@0 152 * Build one button for an add-on actor.
michael@0 153 */
michael@0 154 function buildAddonLink(addon, parent) {
michael@0 155 let a = document.createElement("a");
michael@0 156 a.onclick = function() {
michael@0 157 openToolbox({ addonActor: addon.actor, title: addon.name }, true, "jsdebugger");
michael@0 158 }
michael@0 159
michael@0 160 a.textContent = addon.name;
michael@0 161 a.title = addon.id;
michael@0 162 a.href = "#";
michael@0 163
michael@0 164 parent.appendChild(a);
michael@0 165 }
michael@0 166
michael@0 167 /**
michael@0 168 * Build one button for a tab actor.
michael@0 169 */
michael@0 170 function buildTabLink(tab, parent, selected) {
michael@0 171 let a = document.createElement("a");
michael@0 172 a.onclick = function() {
michael@0 173 openToolbox(tab);
michael@0 174 }
michael@0 175
michael@0 176 a.textContent = tab.title;
michael@0 177 a.title = tab.url;
michael@0 178 if (!a.textContent) {
michael@0 179 a.textContent = tab.url;
michael@0 180 }
michael@0 181 a.href = "#";
michael@0 182
michael@0 183 if (selected) {
michael@0 184 a.classList.add("selected");
michael@0 185 }
michael@0 186
michael@0 187 parent.appendChild(a);
michael@0 188 }
michael@0 189
michael@0 190 /**
michael@0 191 * An error occured. Let's show it and return to the first screen.
michael@0 192 */
michael@0 193 function showError(type) {
michael@0 194 document.body.className = "error";
michael@0 195 let activeError = document.querySelector(".error-message.active");
michael@0 196 if (activeError) {
michael@0 197 activeError.classList.remove("active");
michael@0 198 }
michael@0 199 activeError = document.querySelector(".error-" + type);
michael@0 200 if (activeError) {
michael@0 201 activeError.classList.add("active");
michael@0 202 }
michael@0 203 }
michael@0 204
michael@0 205 /**
michael@0 206 * Connection timeout.
michael@0 207 */
michael@0 208 function handleConnectionTimeout() {
michael@0 209 showError("timeout");
michael@0 210 }
michael@0 211
michael@0 212 /**
michael@0 213 * The user clicked on one of the buttons.
michael@0 214 * Opens the toolbox.
michael@0 215 */
michael@0 216 function openToolbox(form, chrome=false, tool="webconsole") {
michael@0 217 let options = {
michael@0 218 form: form,
michael@0 219 client: gClient,
michael@0 220 chrome: chrome
michael@0 221 };
michael@0 222 devtools.TargetFactory.forRemoteTab(options).then((target) => {
michael@0 223 let hostType = devtools.Toolbox.HostType.WINDOW;
michael@0 224 gDevTools.showToolbox(target, tool, hostType).then((toolbox) => {
michael@0 225 toolbox.once("destroyed", function() {
michael@0 226 gClient.close();
michael@0 227 });
michael@0 228 });
michael@0 229 window.close();
michael@0 230 });
michael@0 231 }

mercurial