Bug 1333990: Part 3d - Split observe() method to fix complexity warnings. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 10 Mar 2017 23:33:06 -0800
changeset 499664 05931f9c7ca84bdf6cfb962ee74826776218f116
parent 499663 e860bd3996c8e8573df10895064d65df712a2605
child 499665 c05921e137786611957cc9585eda16a9ecca11df
push id49469
push usermaglione.k@gmail.com
push dateThu, 16 Mar 2017 02:25:47 +0000
reviewersaswan
bugs1333990
milestone54.0a1
Bug 1333990: Part 3d - Split observe() method to fix complexity warnings. r?aswan MozReview-Commit-ID: 5ACOf2X5Z7I
toolkit/components/extensions/ExtensionContent.jsm
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -619,28 +619,26 @@ DocumentManager = {
           DocumentManager.getExtensionPageContext(extension, window);
         } else if (apiLevel == FULL_PRIVILEGES) {
           ExtensionChild.createExtensionContext(extension, window);
         }
       }
     }
   },
 
-  observe(subject, topic, data) {
+  observers: {
     // For some types of documents (about:blank), we only see the first
     // notification, for others (data: URIs) we only observe the second.
-    if (topic == "content-document-global-created" || topic == "document-element-inserted") {
+    "content-document-global-created"(subject, topic, data) {
+      this.observers["document-element-inserted"].call(this, subject.document, topic, data);
+    },
+    "document-element-inserted"(subject, topic, data) {
       let document = subject;
       let window = document && document.defaultView;
 
-      if (topic == "content-document-global-created") {
-        window = subject;
-        document = window && window.document;
-      }
-
       if (!document || !document.location || !window) {
         return;
       }
 
       // Make sure we only load into frames that ExtensionContent.init
       // was called on (i.e., not frames for social or sidebars).
       let mm = getWindowMessageManager(window);
       if (!mm || !ExtensionContent.globals.has(mm)) {
@@ -653,17 +651,18 @@ DocumentManager = {
         this.loadInto(window);
         this.trigger("document_start", window);
       }
 
       /* eslint-disable mozilla/balanced-listeners */
       window.addEventListener("DOMContentLoaded", this, true);
       window.addEventListener("load", this, true);
       /* eslint-enable mozilla/balanced-listeners */
-    } else if (topic == "inner-window-destroyed") {
+    },
+    "inner-window-destroyed"(subject, topic, data) {
       let windowId = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
 
       MessageChannel.abortResponses({innerWindowID: windowId});
 
       // Close any existent content-script context for the destroyed window.
       if (this.contentScriptWindows.has(windowId)) {
         let extensions = this.contentScriptWindows.get(windowId);
         for (let [, context] of extensions) {
@@ -676,32 +675,38 @@ DocumentManager = {
       // Close any existent iframe extension page context for the destroyed window.
       if (this.extensionPageWindows.has(windowId)) {
         let context = this.extensionPageWindows.get(windowId);
         context.close();
         this.extensionPageWindows.delete(windowId);
       }
 
       ExtensionChild.destroyExtensionContext(windowId);
-    } else if (topic === "http-on-opening-request") {
+    },
+    "http-on-opening-request"(subject, topic, data) {
       let {loadInfo} = subject.QueryInterface(Ci.nsIChannel);
       if (loadInfo) {
         let {externalContentPolicyType: type} = loadInfo;
         if (type === Ci.nsIContentPolicy.TYPE_DOCUMENT ||
             type === Ci.nsIContentPolicy.TYPE_SUBDOCUMENT) {
           this.preloadScripts(subject.URI, loadInfo);
         }
       }
-    } else if (topic === "memory-pressure") {
+    },
+    "memory-pressure"(subject, topic, data) {
       let timeout = data === "heap-minimize" ? 0 : undefined;
 
       for (let cache of ChromeUtils.nondeterministicGetWeakSetKeys(scriptCaches)) {
         cache.clear(timeout);
       }
-    }
+    },
+  },
+
+  observe(subject, topic, data) {
+    this.observers[topic].call(this, subject, topic, data);
   },
 
   handleEvent(event) {
     let window = event.currentTarget;
     if (event.target != window.document) {
       // We use capturing listeners so we have precedence over content script
       // listeners, but only care about events targeted to the element we're
       // listening on.