Bug 1314608 - Install preferences via devtools addon and start devtools on addon install. r=jryans draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 21 Mar 2017 16:19:55 +0100
changeset 503826 0df1cc2cd662be10fb8a4a47f908a4ff7fc3012e
parent 503825 439ee2e4fc4ed4a82102608639d9751e376d5536
child 503830 21a2cc5e3e14b73f65e7b7e34ff10c34af4bb151
push id50664
push userbmo:poirot.alex@gmail.com
push dateThu, 23 Mar 2017 15:53:31 +0000
reviewersjryans
bugs1314608
milestone55.0a1
Bug 1314608 - Install preferences via devtools addon and start devtools on addon install. r=jryans MozReview-Commit-ID: 2LLfmoDPLt
devtools/bootstrap.js
--- a/devtools/bootstrap.js
+++ b/devtools/bootstrap.js
@@ -5,24 +5,90 @@
 /* global content */
 /* exported startup, shutdown, install, uninstall */
 
 "use strict";
 
 const Cu = Components.utils;
 const Ci = Components.interfaces;
 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
+const {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {});
+
+let prefs = {
+  // Enable dump as some errors are only printed on the stdout
+  "browser.dom.window.dump.enabled": true,
+  // Enable the browser toolbox and various chrome-only features
+  "devtools.chrome.enabled": true,
+  "devtools.debugger.remote-enabled": true,
+  // Disable the prompt to ease usage of the browser toolbox
+  "devtools.debugger.prompt-connection": false,
+};
+
+// Values of debug pref before overriding them
+let originalPrefValues = {};
+// MultiWindowKeyListener instance for Ctrl+Alt+R key
+let listener;
+// nsIURI to the addon root folder
+let resourceURI;
 
 function actionOccurred(id) {
   let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
   let Telemetry = require("devtools/client/shared/telemetry");
   let telemetry = new Telemetry();
   telemetry.actionOccurred(id);
 }
 
+// Synchronously fetch the content of a given URL
+function readURI(uri) {
+  let stream = NetUtil.newChannel({
+    uri: NetUtil.newURI(uri, "UTF-8"),
+    loadUsingSystemPrincipal: true}
+  ).open2();
+  let count = stream.available();
+  let data = NetUtil.readInputStreamToString(stream, count, {
+    charset: "UTF-8"
+  });
+
+  stream.close();
+
+  return data;
+}
+
+// Read a preference file and set all of its defined pref as default values
+// (This replicate the behavior of preferences files from mozilla-central)
+function processPrefFile(url) {
+  let content = readURI(url);
+  content.match(/pref\("[^"]+",\s*.+\s*\)/g).forEach(item => {
+    let m = item.match(/pref\("([^"]+)",\s*(.+)\s*\)/);
+    let name = m[1];
+    let val = m[2];
+
+    // Prevent overriding prefs that have been changed by the user
+    if (Services.prefs.prefHasUserValue(name)) {
+      return;
+    }
+    let defaultBranch = Services.prefs.getDefaultBranch("");
+    if ((val.startsWith("\"") && val.endsWith("\"")) ||
+        (val.startsWith("'") && val.endsWith("'"))) {
+      defaultBranch.setCharPref(name, val.substr(1, val.length - 2));
+    } else if (val.match(/[0-9]+/)) {
+      defaultBranch.setIntPref(name, parseInt(val, 10));
+    } else if (val == "true" || val == "false") {
+      defaultBranch.setBoolPref(name, val == "true");
+    } else {
+      console.log("Unable to match preference type for value:", val);
+    }
+  });
+}
+
+function setPrefs() {
+  processPrefFile(resourceURI.spec + "./client/preferences/devtools.js");
+  processPrefFile(resourceURI.spec + "./client/preferences/debugger.js");
+}
+
 // Helper to listen to a key on all windows
 function MultiWindowKeyListener({ keyCode, ctrlKey, altKey, callback }) {
   let keyListener = function (event) {
     if (event.ctrlKey == !!ctrlKey &&
         event.altKey == !!altKey &&
         event.keyCode === keyCode) {
       callback(event);
 
@@ -131,16 +197,20 @@ function reload(event) {
   Cu.unload("resource://devtools/shared/apps/Devices.jsm");
   Cu.unload("resource://devtools/client/scratchpad/scratchpad-manager.jsm");
   Cu.unload("resource://devtools/shared/Parser.jsm");
   Cu.unload("resource://devtools/client/shared/DOMHelpers.jsm");
   Cu.unload("resource://devtools/client/shared/widgets/VariablesView.jsm");
   Cu.unload("resource://devtools/client/responsivedesign/responsivedesign.jsm");
   Cu.unload("resource://devtools/client/shared/widgets/AbstractTreeItem.jsm");
   Cu.unload("resource://devtools/shared/deprecated-sync-thenables.js");
+
+  // Update the preferences before starting new code
+  setPrefs();
+
   const {devtools} = Cu.import("resource://devtools/shared/Loader.jsm", {});
   devtools.require("devtools/client/framework/devtools-browser");
 
   // Go over all top level windows to reload all devtools related things
   let windowsEnum = Services.wm.getEnumerator(null);
   while (windowsEnum.hasMoreElements()) {
     let window = windowsEnum.getNext();
     let windowtype = window.document.documentElement.getAttribute("windowtype");
@@ -189,30 +259,21 @@ function reload(event) {
   if (reopenBrowserConsole) {
     let HUDService = devtools.require("devtools/client/webconsole/hudservice");
     HUDService.toggleBrowserConsole();
   }
 
   actionOccurred("reloadAddonReload");
 }
 
-let prefs = {
-  // Enable dump as some errors are only printed on the stdout
-  "browser.dom.window.dump.enabled": true,
-  // Enable the browser toolbox and various chrome-only features
-  "devtools.chrome.enabled": true,
-  "devtools.debugger.remote-enabled": true,
-  // Disable the prompt to ease usage of the browser toolbox
-  "devtools.debugger.prompt-connection": false,
-};
-let originalPrefValues = {};
+function startup(data) {
+  dump("DevTools addon started.\n");
 
-let listener;
-function startup() {
-  dump("DevTools addon started.\n");
+  resourceURI = data.resourceURI;
+
   listener = new MultiWindowKeyListener({
     keyCode: Ci.nsIDOMKeyEvent.DOM_VK_R, ctrlKey: true, altKey: true,
     callback: reload
   });
   listener.start();
 
   // Toggle development prefs and save original values
   originalPrefValues = {};