2016-01-05 13:41:49 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-05-09 19:28:57 +03:00
|
|
|
/* global content, APP_SHUTDOWN */
|
2016-05-10 01:15:03 +03:00
|
|
|
/* exported startup, shutdown, install, uninstall */
|
|
|
|
|
2016-01-05 13:41:49 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
|
2017-03-21 18:19:55 +03:00
|
|
|
const {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {});
|
2017-06-06 15:36:44 +03:00
|
|
|
const {AppConstants} = Cu.import("resource://gre/modules/AppConstants.jsm", {});
|
2017-03-21 18:19:55 +03:00
|
|
|
|
|
|
|
// MultiWindowKeyListener instance for Ctrl+Alt+R key
|
|
|
|
let listener;
|
|
|
|
// nsIURI to the addon root folder
|
|
|
|
let resourceURI;
|
2016-01-05 13:41:49 +03:00
|
|
|
|
2016-03-02 13:32:05 +03:00
|
|
|
function actionOccurred(id) {
|
|
|
|
let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
|
2016-05-10 01:15:03 +03:00
|
|
|
let Telemetry = require("devtools/client/shared/telemetry");
|
2016-03-02 13:32:05 +03:00
|
|
|
let telemetry = new Telemetry();
|
|
|
|
telemetry.actionOccurred(id);
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:19:55 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-06-06 15:36:44 +03:00
|
|
|
/**
|
|
|
|
* Interpret the processing instructions contained in a preferences file, based on a
|
|
|
|
* limited set of supported #if statements. After we ship as an addon, we don't want to
|
|
|
|
* introduce anymore processing instructions, so all unrecognized preprocessing
|
|
|
|
* instructions will be treated as an error.
|
|
|
|
*
|
|
|
|
* This function is mostly copied from devtools/client/inspector/webpack/prefs-loader.js
|
|
|
|
*
|
|
|
|
* @param {String} content
|
|
|
|
* The string content of a preferences file.
|
|
|
|
* @return {String} the content stripped of preprocessing instructions.
|
|
|
|
*/
|
|
|
|
function interpretPreprocessingInstructions(content) {
|
|
|
|
const ifMap = {
|
|
|
|
"#if MOZ_UPDATE_CHANNEL == beta": AppConstants.MOZ_UPDATE_CHANNEL === "beta",
|
|
|
|
"#if defined(NIGHTLY_BUILD)": AppConstants.NIGHTLY_BUILD,
|
|
|
|
"#ifdef MOZ_DEV_EDITION": AppConstants.MOZ_DEV_EDITION,
|
|
|
|
"#ifdef RELEASE_OR_BETA": AppConstants.RELEASE_OR_BETA,
|
|
|
|
};
|
|
|
|
|
|
|
|
let lines = content.split("\n");
|
|
|
|
let ignoring = false;
|
|
|
|
let newLines = [];
|
|
|
|
let continuation = false;
|
|
|
|
for (let line of lines) {
|
|
|
|
if (line.startsWith("#if")) {
|
|
|
|
if (!(line in ifMap)) {
|
|
|
|
throw new Error("missing line in ifMap: " + line);
|
|
|
|
}
|
|
|
|
ignoring = !ifMap[line];
|
|
|
|
} else if (line.startsWith("#else")) {
|
|
|
|
ignoring = !ignoring;
|
2017-06-08 18:05:52 +03:00
|
|
|
} else if (line.startsWith("#endif")) {
|
|
|
|
ignoring = false;
|
2017-06-06 15:36:44 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 19:19:51 +03:00
|
|
|
let isPrefLine = /^ *(sticky_)?pref\("([^"]+)"/.test(line);
|
2017-06-06 15:36:44 +03:00
|
|
|
if (continuation || (!ignoring && isPrefLine)) {
|
|
|
|
newLines.push(line);
|
|
|
|
|
|
|
|
// The call to pref(...); might span more than one line.
|
|
|
|
continuation = !/\);/.test(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newLines.join("\n");
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:19:55 +03:00
|
|
|
// Read a preference file and set all of its defined pref as default values
|
2017-05-11 16:02:41 +03:00
|
|
|
// (This replicates the behavior of preferences files from mozilla-central)
|
2017-03-21 18:19:55 +03:00
|
|
|
function processPrefFile(url) {
|
|
|
|
let content = readURI(url);
|
2017-06-06 15:36:44 +03:00
|
|
|
content = interpretPreprocessingInstructions(content);
|
2017-03-21 18:19:55 +03:00
|
|
|
content.match(/pref\("[^"]+",\s*.+\s*\)/g).forEach(item => {
|
|
|
|
let m = item.match(/pref\("([^"]+)",\s*(.+)\s*\)/);
|
|
|
|
let name = m[1];
|
2017-06-08 19:16:04 +03:00
|
|
|
let val = m[2].trim();
|
2017-03-21 18:19:55 +03:00
|
|
|
|
|
|
|
// 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("'"))) {
|
2017-05-15 15:57:31 +03:00
|
|
|
val = val.substr(1, val.length - 2);
|
|
|
|
val = val.replace(/\\"/g, '"');
|
|
|
|
defaultBranch.setCharPref(name, val);
|
2017-03-21 18:19:55 +03:00
|
|
|
} 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");
|
2017-04-12 15:57:22 +03:00
|
|
|
processPrefFile(resourceURI.spec + "./client/webide/webide-prefs.js");
|
2017-03-21 18:19:55 +03:00
|
|
|
}
|
|
|
|
|
2016-01-05 13:41:49 +03:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Call preventDefault to avoid duplicated events when
|
|
|
|
// doing the key stroke within a tab.
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let observer = function (window, topic, data) {
|
|
|
|
// Listen on keyup to call keyListener only once per stroke
|
|
|
|
if (topic === "domwindowopened") {
|
|
|
|
window.addEventListener("keyup", keyListener);
|
|
|
|
} else {
|
|
|
|
window.removeEventListener("keyup", keyListener);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2017-01-12 01:21:56 +03:00
|
|
|
start: function () {
|
2016-01-05 13:41:49 +03:00
|
|
|
// Automatically process already opened windows
|
|
|
|
let e = Services.ww.getWindowEnumerator();
|
|
|
|
while (e.hasMoreElements()) {
|
|
|
|
let window = e.getNext();
|
|
|
|
observer(window, "domwindowopened", null);
|
|
|
|
}
|
|
|
|
// And listen for new ones to come
|
|
|
|
Services.ww.registerNotification(observer);
|
|
|
|
},
|
|
|
|
|
2017-01-12 01:21:56 +03:00
|
|
|
stop: function () {
|
2016-01-05 13:41:49 +03:00
|
|
|
Services.ww.unregisterNotification(observer);
|
|
|
|
let e = Services.ww.getWindowEnumerator();
|
|
|
|
while (e.hasMoreElements()) {
|
|
|
|
let window = e.getNext();
|
|
|
|
observer(window, "domwindowclosed", null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-05-10 01:15:03 +03:00
|
|
|
}
|
2016-01-05 13:41:49 +03:00
|
|
|
|
|
|
|
let getTopLevelWindow = function (window) {
|
|
|
|
return window.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShellTreeItem)
|
|
|
|
.rootTreeItem
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindow);
|
|
|
|
};
|
|
|
|
|
2017-05-09 19:28:57 +03:00
|
|
|
function unload(reason) {
|
|
|
|
// This frame script is going to be executed in all processes:
|
|
|
|
// parent and child
|
|
|
|
Services.ppmm.loadProcessScript("data:,(" + function (scriptReason) {
|
|
|
|
/* Flush message manager cached frame scripts as well as chrome locales */
|
|
|
|
let obs = Components.classes["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIObserverService);
|
|
|
|
obs.notifyObservers(null, "message-manager-flush-caches");
|
|
|
|
|
|
|
|
/* Also purge cached modules in child processes, we do it a few lines after
|
|
|
|
in the parent process */
|
|
|
|
if (Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT) {
|
|
|
|
Services.obs.notifyObservers(null, "devtools-unload", scriptReason);
|
|
|
|
}
|
|
|
|
} + ")(\"" + reason.replace(/"/g, '\\"') + "\")", false);
|
|
|
|
|
|
|
|
// As we can't get a reference to existing Loader.jsm instances, we send them
|
|
|
|
// an observer service notification to unload them.
|
|
|
|
Services.obs.notifyObservers(null, "devtools-unload", reason);
|
|
|
|
|
|
|
|
// Then spawn a brand new Loader.jsm instance and start the main module
|
|
|
|
Cu.unload("resource://devtools/shared/Loader.jsm");
|
|
|
|
// Also unload all resources loaded as jsm, hopefully all of them are going
|
|
|
|
// to be converted into regular modules
|
|
|
|
Cu.unload("resource://devtools/client/shared/browser-loader.js");
|
|
|
|
Cu.unload("resource://devtools/client/framework/ToolboxProcess.jsm");
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-01-05 13:41:49 +03:00
|
|
|
function reload(event) {
|
|
|
|
// We automatically reload the toolbox if we are on a browser tab
|
|
|
|
// with a toolbox already opened
|
|
|
|
let reloadToolbox = false;
|
2016-11-02 17:55:00 +03:00
|
|
|
if (event) {
|
|
|
|
let top = getTopLevelWindow(event.view);
|
|
|
|
let isBrowser = top.location.href.includes("/browser.xul");
|
|
|
|
if (isBrowser && top.gBrowser) {
|
|
|
|
// We do not use any devtools code before the call to Loader.jsm reload as
|
|
|
|
// any attempt to use Loader.jsm to load a module will instanciate a new
|
|
|
|
// Loader.
|
|
|
|
let nbox = top.gBrowser.getNotificationBox();
|
|
|
|
reloadToolbox =
|
|
|
|
top.document.getAnonymousElementByAttribute(nbox, "class",
|
|
|
|
"devtools-toolbox-bottom-iframe") ||
|
|
|
|
top.document.getAnonymousElementByAttribute(nbox, "class",
|
|
|
|
"devtools-toolbox-side-iframe") ||
|
|
|
|
Services.wm.getMostRecentWindow("devtools:toolbox");
|
|
|
|
}
|
2016-01-05 13:41:49 +03:00
|
|
|
}
|
2016-04-14 11:40:44 +03:00
|
|
|
let browserConsole = Services.wm.getMostRecentWindow("devtools:webconsole");
|
|
|
|
let reopenBrowserConsole = false;
|
|
|
|
if (browserConsole) {
|
|
|
|
browserConsole.close();
|
|
|
|
reopenBrowserConsole = true;
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:15:03 +03:00
|
|
|
dump("Reload DevTools. (reload-toolbox:" + reloadToolbox + ")\n");
|
2016-01-05 13:41:49 +03:00
|
|
|
|
|
|
|
// Invalidate xul cache in order to see changes made to chrome:// files
|
2017-04-14 22:51:39 +03:00
|
|
|
Services.obs.notifyObservers(null, "startupcache-invalidate");
|
2016-01-05 13:41:49 +03:00
|
|
|
|
2017-05-09 19:28:57 +03:00
|
|
|
unload("reload");
|
2017-03-21 18:19:55 +03:00
|
|
|
|
|
|
|
// Update the preferences before starting new code
|
|
|
|
setPrefs();
|
|
|
|
|
2016-04-14 11:40:43 +03:00
|
|
|
const {devtools} = Cu.import("resource://devtools/shared/Loader.jsm", {});
|
|
|
|
devtools.require("devtools/client/framework/devtools-browser");
|
2016-01-13 13:55:33 +03:00
|
|
|
|
2016-02-16 18:23:56 +03:00
|
|
|
// 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");
|
|
|
|
if (windowtype == "navigator:browser" && window.gBrowser) {
|
|
|
|
// Enumerate tabs on firefox windows
|
|
|
|
for (let tab of window.gBrowser.tabs) {
|
|
|
|
let browser = tab.linkedBrowser;
|
|
|
|
let location = browser.documentURI.spec;
|
|
|
|
let mm = browser.messageManager;
|
|
|
|
// To reload JSON-View tabs and any devtools document
|
|
|
|
if (location.startsWith("about:debugging") ||
|
|
|
|
location.startsWith("chrome://devtools/")) {
|
|
|
|
browser.reload();
|
|
|
|
}
|
|
|
|
// We have to use a frame script to query "baseURI"
|
|
|
|
mm.loadFrameScript("data:text/javascript,new " + function () {
|
2016-05-10 01:15:03 +03:00
|
|
|
let isJSONView =
|
|
|
|
content.document.baseURI.startsWith("resource://devtools/");
|
2016-02-16 18:23:56 +03:00
|
|
|
if (isJSONView) {
|
|
|
|
content.location.reload();
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
} else if (windowtype === "devtools:webide") {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reloadToolbox) {
|
2016-05-10 01:15:03 +03:00
|
|
|
// Reopen the toolbox automatically if we are reloading from toolbox
|
|
|
|
// shortcut and are on a browser window.
|
2016-02-16 18:23:56 +03:00
|
|
|
// Wait for a second before opening the toolbox to avoid races
|
|
|
|
// between the old and the new one.
|
|
|
|
let {setTimeout} = Cu.import("resource://gre/modules/Timer.jsm", {});
|
|
|
|
setTimeout(() => {
|
|
|
|
let { TargetFactory } = devtools.require("devtools/client/framework/target");
|
|
|
|
let { gDevTools } = devtools.require("devtools/client/framework/devtools");
|
2016-11-02 17:55:00 +03:00
|
|
|
let top = getTopLevelWindow(event.view);
|
2016-02-16 18:23:56 +03:00
|
|
|
let target = TargetFactory.forTab(top.gBrowser.selectedTab);
|
|
|
|
gDevTools.showToolbox(target);
|
|
|
|
}, 1000);
|
|
|
|
}
|
2016-03-02 13:32:05 +03:00
|
|
|
|
2016-04-14 11:40:44 +03:00
|
|
|
// Browser console document can't just be reloaded.
|
|
|
|
// HUDService is going to close it on unload.
|
|
|
|
// Instead we have to manually toggle it.
|
|
|
|
if (reopenBrowserConsole) {
|
|
|
|
let HUDService = devtools.require("devtools/client/webconsole/hudservice");
|
|
|
|
HUDService.toggleBrowserConsole();
|
|
|
|
}
|
|
|
|
|
2016-03-02 13:32:05 +03:00
|
|
|
actionOccurred("reloadAddonReload");
|
2016-01-05 13:41:49 +03:00
|
|
|
}
|
|
|
|
|
2017-03-21 18:19:55 +03:00
|
|
|
function startup(data) {
|
2016-01-05 13:41:49 +03:00
|
|
|
dump("DevTools addon started.\n");
|
2017-03-21 18:19:55 +03:00
|
|
|
|
|
|
|
resourceURI = data.resourceURI;
|
|
|
|
|
2016-01-05 13:41:49 +03:00
|
|
|
listener = new MultiWindowKeyListener({
|
|
|
|
keyCode: Ci.nsIDOMKeyEvent.DOM_VK_R, ctrlKey: true, altKey: true,
|
|
|
|
callback: reload
|
|
|
|
});
|
|
|
|
listener.start();
|
2016-03-29 16:28:17 +03:00
|
|
|
|
2016-11-02 17:55:00 +03:00
|
|
|
reload();
|
2016-01-05 13:41:49 +03:00
|
|
|
}
|
2017-05-09 19:28:57 +03:00
|
|
|
function shutdown(data, reason) {
|
|
|
|
// On browser shutdown, do not try to cleanup anything
|
|
|
|
if (reason == APP_SHUTDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-05 13:41:49 +03:00
|
|
|
listener.stop();
|
|
|
|
listener = null;
|
2016-03-29 16:28:17 +03:00
|
|
|
|
2017-05-09 19:28:57 +03:00
|
|
|
unload("disable");
|
2016-01-05 13:41:49 +03:00
|
|
|
}
|
2016-03-02 13:32:05 +03:00
|
|
|
function install() {
|
2016-11-02 17:55:00 +03:00
|
|
|
try {
|
|
|
|
actionOccurred("reloadAddonInstalled");
|
|
|
|
} catch (e) {
|
|
|
|
// When installing on Firefox builds without devtools, telemetry doesn't
|
|
|
|
// work yet and throws.
|
|
|
|
}
|
2016-03-02 13:32:05 +03:00
|
|
|
}
|
2016-01-05 13:41:49 +03:00
|
|
|
function uninstall() {}
|