browser/devtools/framework/toolbox-process-window.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/framework/toolbox-process-window.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
    1.10 +
    1.11 +let { gDevTools } = Cu.import("resource:///modules/devtools/gDevTools.jsm", {});
    1.12 +let { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
    1.13 +let { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
    1.14 +let { debuggerSocketConnect, DebuggerClient } =
    1.15 +  Cu.import("resource://gre/modules/devtools/dbg-client.jsm", {});
    1.16 +let { ViewHelpers } =
    1.17 +  Cu.import("resource:///modules/devtools/ViewHelpers.jsm", {});
    1.18 +
    1.19 +/**
    1.20 + * Shortcuts for accessing various debugger preferences.
    1.21 + */
    1.22 +let Prefs = new ViewHelpers.Prefs("devtools.debugger", {
    1.23 +  chromeDebuggingHost: ["Char", "chrome-debugging-host"],
    1.24 +  chromeDebuggingPort: ["Int", "chrome-debugging-port"]
    1.25 +});
    1.26 +
    1.27 +let gToolbox, gClient;
    1.28 +
    1.29 +function connect() {
    1.30 +  window.removeEventListener("load", connect);
    1.31 +  // Initiate the connection
    1.32 +  let transport = debuggerSocketConnect(
    1.33 +    Prefs.chromeDebuggingHost,
    1.34 +    Prefs.chromeDebuggingPort
    1.35 +  );
    1.36 +  gClient = new DebuggerClient(transport);
    1.37 +  gClient.connect(() => {
    1.38 +    let addonID = getParameterByName("addonID");
    1.39 +
    1.40 +    if (addonID) {
    1.41 +      gClient.listAddons(({addons}) => {
    1.42 +        let addonActor = addons.filter(addon => addon.id === addonID).pop();
    1.43 +        openToolbox({ addonActor: addonActor.actor, title: addonActor.name });
    1.44 +      });
    1.45 +    } else {
    1.46 +      gClient.listTabs(openToolbox);
    1.47 +    }
    1.48 +  });
    1.49 +}
    1.50 +
    1.51 +window.addEventListener("load", connect);
    1.52 +
    1.53 +function openToolbox(form) {
    1.54 +  let options = {
    1.55 +    form: form,
    1.56 +    client: gClient,
    1.57 +    chrome: true
    1.58 +  };
    1.59 +  devtools.TargetFactory.forRemoteTab(options).then(target => {
    1.60 +    let frame = document.getElementById("toolbox-iframe");
    1.61 +    let options = { customIframe: frame };
    1.62 +    gDevTools.showToolbox(target,
    1.63 +                          "jsdebugger",
    1.64 +                          devtools.Toolbox.HostType.CUSTOM,
    1.65 +                          options)
    1.66 +             .then(onNewToolbox);
    1.67 +  });
    1.68 +}
    1.69 +
    1.70 +function onNewToolbox(toolbox) {
    1.71 +   gToolbox = toolbox;
    1.72 +   bindToolboxHandlers();
    1.73 +   raise();
    1.74 +}
    1.75 +
    1.76 +function bindToolboxHandlers() {
    1.77 +  gToolbox.once("destroyed", quitApp);
    1.78 +  window.addEventListener("unload", onUnload);
    1.79 +}
    1.80 +
    1.81 +function onUnload() {
    1.82 +  window.removeEventListener("unload", onUnload);
    1.83 +  window.removeEventListener("message", onMessage);
    1.84 +  gToolbox.destroy();
    1.85 +}
    1.86 +
    1.87 +function onMessage(event) {
    1.88 +  try {
    1.89 +    let json = JSON.parse(event.data);
    1.90 +    switch (json.name) {
    1.91 +      case "toolbox-raise":
    1.92 +        raise();
    1.93 +        break;
    1.94 +      case "toolbox-title":
    1.95 +        setTitle(json.data.value);
    1.96 +        break;
    1.97 +    }
    1.98 +  } catch(e) { Cu.reportError(e); }
    1.99 +}
   1.100 +
   1.101 +window.addEventListener("message", onMessage);
   1.102 +
   1.103 +function raise() {
   1.104 +  window.focus();
   1.105 +}
   1.106 +
   1.107 +function setTitle(title) {
   1.108 +  document.title = title;
   1.109 +}
   1.110 +
   1.111 +function quitApp() {
   1.112 +  let quit = Cc["@mozilla.org/supports-PRBool;1"]
   1.113 +             .createInstance(Ci.nsISupportsPRBool);
   1.114 +  Services.obs.notifyObservers(quit, "quit-application-requested", null);
   1.115 +
   1.116 +  let shouldProceed = !quit.data;
   1.117 +  if (shouldProceed) {
   1.118 +    Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
   1.119 +  }
   1.120 +}
   1.121 +
   1.122 +function getParameterByName (name) {
   1.123 +  let name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
   1.124 +  let regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
   1.125 +  let results = regex.exec(window.location.search);
   1.126 +  return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
   1.127 +}

mercurial