[Pkg-mozext-commits] [adblock-plus-element-hiding-helper] 16/28: Issue 2879 - Restructure existing process script, split it up into multiple modules

David Prévot taffit at moszumanska.debian.org
Fri Aug 4 21:15:15 UTC 2017


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus-element-hiding-helper.

commit 96cf756b9e3905269ce5c5052bfb17b1d2ffd338
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Thu Dec 1 10:36:44 2016 +0100

    Issue 2879 - Restructure existing process script, split it up into multiple modules
---
 chrome/content/processScript.js | 206 ----------------------------------------
 lib/child/actor.js              |  68 +++++++++++++
 lib/child/bootstrap.js          |  92 ++++++++++++++++++
 lib/child/main.js               |  11 +++
 lib/child/nodeInfo.js           | 113 ++++++++++++++++++++++
 lib/child/preview.js            |  58 +++++++++++
 lib/main.js                     |  10 +-
 7 files changed, 349 insertions(+), 209 deletions(-)

diff --git a/chrome/content/processScript.js b/chrome/content/processScript.js
deleted file mode 100644
index c2feedc..0000000
--- a/chrome/content/processScript.js
+++ /dev/null
@@ -1,206 +0,0 @@
-/*
- * This Source Code is subject to the terms of the Mozilla Public License
- * version 2.0 (the "License"). You can obtain a copy of the License at
- * http://mozilla.org/MPL/2.0/.
- */
-
-const Ci = Components.interfaces;
-const Cu = Components.utils;
-
-let console;
-try
-{
-  // Gecko 44+
-  ({console} = Cu.import("resource://gre/modules/Console.jsm", {}));
-}
-catch (e)
-{
-  ({console} = Cu.import("resource://gre/modules/devtools/Console.jsm", {}));
-}
-
-let DebuggerServer;
-try
-{
-  // Firefox 44+
-  let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
-  ({DebuggerServer} = require("devtools/server/main"));
-}
-catch (e)
-{
-  ({DebuggerServer} = Cu.import("resource://gre/modules/devtools/dbg-server.jsm", {}));
-}
-
-let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
-
-let processID = Services.appinfo.processID;
-let maxNodeID = 0;
-let nodes = new Map();
-
-let name = "elemhidehelper";
-let actor = {
-  constructorFun: Actor,
-  constructorName: name,
-  name: name
-};
-
-addMessageListener("ElemHideHelper:Shutdown", onShutdown);
-addMessageListener("ElemHideHelper:GetNodeInfo", onGetNodeInfo);
-addMessageListener("ElemHideHelper:Preview", onTogglePreview);
-
-DebuggerServer.addTabActor(actor, name);
-
-function onShutdown()
-{
-  removeMessageListener("ElemHideHelper:Shutdown", onShutdown);
-  removeMessageListener("ElemHideHelper:GetNodeInfo", onGetNodeInfo);
-  removeMessageListener("ElemHideHelper:Preview", onTogglePreview);
-
-  try
-  {
-    DebuggerServer.removeTabActor(actor);
-  }
-  catch (e)
-  {
-    // The call above will throw in the content process despite succeeding,
-    // see https://bugzilla.mozilla.org/show_bug.cgi?id=1189780.
-    Cu.reportError(e);
-  }
-}
-
-function onGetNodeInfo(message)
-{
-  if (Cu.isCrossProcessWrapper(message.objects.element))
-    return;
-
-  let nodeInfo = getNodeInfo(message.objects.element);
-  nodeInfo.messageId = message.data;
-  sendAsyncMessage("ElemHideHelper:GetNodeInfo:Response", nodeInfo);
-}
-
-function onTogglePreview(message)
-{
-  togglePreview(message.data.nodeID, message.data.stylesheetData);
-  if (message.data.forgetNode)
-    forgetNode(message.data.nodeID);
-}
-
-function Actor(connection, tabActor)
-{
-}
-
-Actor.prototype = {
-  requestTypes: {
-    nodeinfo: function(request, connection)
-    {
-      let nodeActor = connection.getActor(request.nodeActor);
-      return getNodeInfo(nodeActor ? nodeActor.rawNode: null);
-    }
-  }
-};
-
-function getNodeInfo(node)
-{
-  let nodeData = getNodeData(node);
-  if (nodeData)
-  {
-    let nodeID = processID + "-" + (++maxNodeID);
-    nodes.set(nodeID, {document: node.ownerDocument, style: null});
-    return {
-      host: node.ownerDocument.defaultView.location.hostname,
-      nodeData: nodeData,
-      nodeID: nodeID
-    };
-  }
-
-  return {};
-}
-
-function getNodeData(node, parentNode)
-{
-  if (!node || node.nodeType != Ci.nsIDOMNode.ELEMENT_NODE)
-    return null;
-
-  let result = {};
-  result.tagName = {value: node.tagName, checked: false};
-
-  if (typeof parentNode != "undefined")
-    result.parentNode = parentNode;
-  else
-    result.parentNode = getNodeData(node.parentElement);
-
-  let prevSibling = node.previousElementSibling;
-  result.prevSibling = getNodeData(prevSibling, result.parentNode);
-
-  if (result.parentNode && !prevSibling)
-    result.firstChild = {checked: false};
-
-  let nextSibling = node.nextElementSibling;
-  if (result.parentNode && !nextSibling)
-    result.lastChild = {checked: false};
-
-  result.attributes = [];
-  for (let attribute of node.attributes)
-  {
-    let data = {
-      name: attribute.name,
-      value: attribute.value,
-      selected: attribute.value,
-      checked: false
-    };
-    if (data.name == "id" || data.name == "class")
-      result.attributes.unshift(data);
-    else
-      result.attributes.push(data);
-  }
-
-  if (result.attributes.length >= 2 && result.attributes[1].name == "id")
-  {
-    // Make sure ID attribute comes first
-    let tmp = result.attributes[1];
-    result.attributes[1] = result.attributes[0];
-    result.attributes[0] = tmp;
-  }
-
-  result.customCSS = {selected: "", checked: false};
-  return result;
-}
-
-function togglePreview(nodeID, stylesheetData)
-{
-  let context = nodes.get(nodeID);
-  if (!context)
-    return;
-
-  if (stylesheetData)
-  {
-    if (!context.style || !context.style.parentNode)
-    {
-      context.style = context.document.createElementNS(
-          "http://www.w3.org/1999/xhtml", "style");
-      context.style.setAttribute("type", "text/css");
-      context.document.documentElement.appendChild(context.style);
-    }
-    context.style.textContent = stylesheetData;
-  }
-  else
-  {
-    try
-    {
-      if (context.style && context.style.parentNode)
-        context.style.parentNode.removeChild(context.style);
-      context.style = null;
-    }
-    catch (e)
-    {
-      // If the window was closed (reloaded) we end up with a dead object
-      // reference (https://bugzilla.mozilla.org/show_bug.cgi?id=695480). Just
-      // forget this node then.
-      forgetNode(nodeID);
-    }
-  }
-}
-
-function forgetNode(nodeID)
-{
-  nodes.delete(nodeID);
-}
diff --git a/lib/child/actor.js b/lib/child/actor.js
new file mode 100644
index 0000000..6b048e6
--- /dev/null
+++ b/lib/child/actor.js
@@ -0,0 +1,68 @@
+/*
+ * This Source Code is subject to the terms of the Mozilla Public License
+ * version 2.0 (the "License"). You can obtain a copy of the License at
+ * http://mozilla.org/MPL/2.0/.
+ */
+
+"use strict";
+
+let console;
+try
+{
+  // Gecko 44+
+  ({console} = Cu.import("resource://gre/modules/Console.jsm", {}));
+}
+catch (e)
+{
+  ({console} = Cu.import("resource://gre/modules/devtools/Console.jsm", {}));
+}
+
+let DebuggerServer;
+try
+{
+  // Firefox 44+
+  let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
+  ({DebuggerServer} = require("devtools/server/main"));
+}
+catch (e)
+{
+  ({DebuggerServer} = Cu.import("resource://gre/modules/devtools/dbg-server.jsm", {}));
+}
+
+let {getNodeInfo} = require("./nodeInfo");
+
+function Actor(connection, tabActor)
+{
+}
+
+Actor.prototype = {
+  requestTypes: {
+    nodeinfo: function(request, connection)
+    {
+      let nodeActor = connection.getActor(request.nodeActor);
+      return getNodeInfo(nodeActor ? nodeActor.rawNode : null);
+    }
+  }
+};
+
+let name = "elemhidehelper";
+let actor = {
+  constructorFun: Actor,
+  constructorName: name,
+  name: name
+};
+
+DebuggerServer.addTabActor(actor, name);
+onShutdown.add(() =>
+{
+  try
+  {
+    DebuggerServer.removeTabActor(actor);
+  }
+  catch (e)
+  {
+    // The call above will throw in the content process despite succeeding,
+    // see https://bugzilla.mozilla.org/show_bug.cgi?id=1189780.
+    Cu.reportError(e);
+  }
+});
diff --git a/lib/child/bootstrap.js b/lib/child/bootstrap.js
new file mode 100644
index 0000000..6987fd9
--- /dev/null
+++ b/lib/child/bootstrap.js
@@ -0,0 +1,92 @@
+/*
+ * This Source Code is subject to the terms of the Mozilla Public License
+ * version 2.0 (the "License"). You can obtain a copy of the License at
+ * http://mozilla.org/MPL/2.0/.
+ */
+
+"use strict";
+
+(function(messageManager)
+{
+  const Cc = Components.classes;
+  const Ci = Components.interfaces;
+  const Cr = Components.results;
+  const Cu = Components.utils;
+
+  let {Loader, main, unload} = Cu.import(
+    "resource://gre/modules/commonjs/toolkit/loader.js", {}
+  );
+  let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
+
+  Cu.importGlobalProperties(["atob", "btoa", "File", "URL", "URLSearchParams",
+      "TextDecoder", "TextEncoder"]);
+
+  let shutdownHandlers = [];
+  let onShutdown =
+  {
+    done: false,
+    add: function(handler)
+    {
+      if (shutdownHandlers.indexOf(handler) < 0)
+        shutdownHandlers.push(handler);
+    },
+    remove: function(handler)
+    {
+      let index = shutdownHandlers.indexOf(handler);
+      if (index >= 0)
+        shutdownHandlers.splice(index, 1);
+    }
+  };
+
+  function init()
+  {
+    let url = new URL(Components.stack.filename);
+    let params = new URLSearchParams(url.search.substr(1));
+    let info = JSON.parse(params.get("info"));
+
+    let loader = Loader({
+      paths: {
+        "": info.addonRoot + "lib/"
+      },
+      globals: {
+        Components, Cc, Ci, Cu, Cr, atob, btoa, File, URL, URLSearchParams,
+        TextDecoder, TextEncoder, onShutdown
+      },
+      modules: {
+        info, messageManager
+      },
+      id: info.addonID
+    });
+    onShutdown.add(() => unload(loader, "disable"))
+
+    main(loader, "child/main");
+  }
+
+  function shutdown(message)
+  {
+    if (onShutdown.done)
+      return;
+
+    onShutdown.done = true;
+    for (let i = shutdownHandlers.length - 1; i >= 0; i --)
+    {
+      try
+      {
+        shutdownHandlers[i]();
+      }
+      catch (e)
+      {
+        Cu.reportError(e);
+      }
+    }
+    shutdownHandlers = null;
+  }
+
+  messageManager.addMessageListener("ElemHideHelper:Shutdown", shutdown);
+  onShutdown.add(() =>
+  {
+    messageManager.removeMessageListener("ElemHideHelper:Shutdown", shutdown);
+  });
+
+  init();
+})(this);
diff --git a/lib/child/main.js b/lib/child/main.js
new file mode 100644
index 0000000..bb52cff
--- /dev/null
+++ b/lib/child/main.js
@@ -0,0 +1,11 @@
+/*
+ * This Source Code is subject to the terms of the Mozilla Public License
+ * version 2.0 (the "License"). You can obtain a copy of the License at
+ * http://mozilla.org/MPL/2.0/.
+ */
+
+"use strict";
+
+require("./actor");
+require("./nodeInfo");
+require("./preview");
diff --git a/lib/child/nodeInfo.js b/lib/child/nodeInfo.js
new file mode 100644
index 0000000..0a835cf
--- /dev/null
+++ b/lib/child/nodeInfo.js
@@ -0,0 +1,113 @@
+/*
+ * This Source Code is subject to the terms of the Mozilla Public License
+ * version 2.0 (the "License"). You can obtain a copy of the License at
+ * http://mozilla.org/MPL/2.0/.
+ */
+
+"use strict";
+
+let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
+
+let messageManager = require("messageManager");
+
+let processID = Services.appinfo.processID;
+let maxNodeID = 0;
+let nodes = new Map();
+
+messageManager.addMessageListener("ElemHideHelper:GetNodeInfo", onGetNodeInfo);
+onShutdown.add(() =>
+{
+  messageManager.removeMessageListener("ElemHideHelper:GetNodeInfo",
+                                       onGetNodeInfo);
+});
+
+function onGetNodeInfo(message)
+{
+  if (Cu.isCrossProcessWrapper(message.objects.element))
+    return;
+
+  let nodeInfo = getNodeInfo(message.objects.element);
+  nodeInfo.messageId = message.data;
+  messageManager.sendAsyncMessage("ElemHideHelper:GetNodeInfo:Response",
+                                  nodeInfo);
+}
+
+function getNodeInfo(node)
+{
+  let nodeData = getNodeData(node);
+  if (nodeData)
+  {
+    let nodeID = processID + "-" + (++maxNodeID);
+    nodes.set(nodeID, {document: node.ownerDocument, style: null});
+    return {
+      host: node.ownerDocument.defaultView.location.hostname,
+      nodeData: nodeData,
+      nodeID: nodeID
+    };
+  }
+
+  return {};
+}
+exports.getNodeInfo = getNodeInfo;
+
+function getNodeById(nodeId)
+{
+  return nodes.get(nodeId);
+}
+exports.getNodeById = getNodeById;
+
+function getNodeData(node, parentNode)
+{
+  if (!node || node.nodeType != Ci.nsIDOMNode.ELEMENT_NODE)
+    return null;
+
+  let result = {};
+  result.tagName = {value: node.tagName, checked: false};
+
+  if (typeof parentNode != "undefined")
+    result.parentNode = parentNode;
+  else
+    result.parentNode = getNodeData(node.parentElement);
+
+  let prevSibling = node.previousElementSibling;
+  result.prevSibling = getNodeData(prevSibling, result.parentNode);
+
+  if (result.parentNode && !prevSibling)
+    result.firstChild = {checked: false};
+
+  let nextSibling = node.nextElementSibling;
+  if (result.parentNode && !nextSibling)
+    result.lastChild = {checked: false};
+
+  result.attributes = [];
+  for (let attribute of node.attributes)
+  {
+    let data = {
+      name: attribute.name,
+      value: attribute.value,
+      selected: attribute.value,
+      checked: false
+    };
+    if (data.name == "id" || data.name == "class")
+      result.attributes.unshift(data);
+    else
+      result.attributes.push(data);
+  }
+
+  if (result.attributes.length >= 2 && result.attributes[1].name == "id")
+  {
+    // Make sure ID attribute comes first
+    let tmp = result.attributes[1];
+    result.attributes[1] = result.attributes[0];
+    result.attributes[0] = tmp;
+  }
+
+  result.customCSS = {selected: "", checked: false};
+  return result;
+}
+
+function forgetNode(nodeID)
+{
+  nodes.delete(nodeID);
+}
+exports.forgetNode = forgetNode;
diff --git a/lib/child/preview.js b/lib/child/preview.js
new file mode 100644
index 0000000..dac83d5
--- /dev/null
+++ b/lib/child/preview.js
@@ -0,0 +1,58 @@
+/*
+ * This Source Code is subject to the terms of the Mozilla Public License
+ * version 2.0 (the "License"). You can obtain a copy of the License at
+ * http://mozilla.org/MPL/2.0/.
+ */
+
+"use strict";
+
+let messageManager = require("messageManager");
+let {forgetNode, getNodeById} = require("./nodeInfo");
+
+messageManager.addMessageListener("ElemHideHelper:Preview", onTogglePreview);
+onShutdown.add(() =>
+{
+  messageManager.removeMessageListener("ElemHideHelper:Preview", onTogglePreview);
+});
+
+function onTogglePreview(message)
+{
+  togglePreview(message.data.nodeID, message.data.stylesheetData);
+  if (message.data.forgetNode)
+    forgetNode(message.data.nodeID);
+}
+
+function togglePreview(nodeID, stylesheetData)
+{
+  let context = getNodeById(nodeID);
+  if (!context)
+    return;
+
+  if (stylesheetData)
+  {
+    if (!context.style || !context.style.parentNode)
+    {
+      context.style = context.document.createElementNS(
+          "http://www.w3.org/1999/xhtml", "style");
+      context.style.setAttribute("type", "text/css");
+      context.document.documentElement.appendChild(context.style);
+    }
+    context.style.textContent = stylesheetData;
+  }
+  else
+  {
+    try
+    {
+      if (context.style && context.style.parentNode)
+        context.style.parentNode.removeChild(context.style);
+      context.style = null;
+    }
+    catch (e)
+    {
+      // If the window was closed (reloaded) we end up with a dead object
+      // reference (https://bugzilla.mozilla.org/show_bug.cgi?id=695480). Just
+      // forget this node then.
+      forgetNode(nodeID);
+    }
+  }
+}
diff --git a/lib/main.js b/lib/main.js
index 9a4ee67..eb053f8 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -46,14 +46,18 @@ request.addEventListener("load", function(event)
 }, false);
 request.send(null);
 
-// Load our developer tools actor
-let processScript = "chrome://elemhidehelper/content/processScript.js?" + elementMarkerClass;
+// Load our process script
+let info = require("info");
+let processScript = info.addonRoot + "lib/child/bootstrap.js?" +
+    elementMarkerClass + "&" +
+    "info=" + encodeURIComponent(JSON.stringify(info));
 let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"]
                        .getService(Ci.nsIProcessScriptLoader);
 messageManager.loadProcessScript(processScript, true);
 onShutdown.add(() => {
   messageManager.removeDelayedProcessScript(processScript);
-  messageManager.QueryInterface(Ci.nsIMessageBroadcaster).broadcastAsyncMessage("ElemHideHelper:Shutdown");
+  messageManager.QueryInterface(Ci.nsIMessageBroadcaster)
+                .broadcastAsyncMessage("ElemHideHelper:Shutdown");
 });
 
 // Load overlay asynchonously and start attaching to windows once done

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus-element-hiding-helper.git



More information about the Pkg-mozext-commits mailing list