[Pkg-mozext-commits] [greasemonkey] 11/62: Use a single shared observer

David Prévot taffit at moszumanska.debian.org
Sun Sep 13 22:10:20 UTC 2015


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

taffit pushed a commit to branch master
in repository greasemonkey.

commit 9066b675be988605f60eb7511649c4f9696b32dd
Author: The8472 <git at infinite-source.de>
Date:   Mon Aug 17 19:59:24 2015 +0200

    Use a single shared observer
    
    Framescripts get loaded for each tab, this means each tab registered a
    callback with the global observer service.
    
    Move the observer registration to a module and dispatch from a hash
    table instead, that reduces the number of callbacks called per
    document-element-inserted from N to 1.
---
 content/framescript.js      | 49 ++++++++++++++++++++-------------------------
 modules/documentObserver.js | 41 +++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 27 deletions(-)

diff --git a/content/framescript.js b/content/framescript.js
index 807a8bf..c96bed2 100644
--- a/content/framescript.js
+++ b/content/framescript.js
@@ -5,7 +5,6 @@ var Cc = Components.classes;
 var Ci = Components.interfaces;
 var Cu = Components.utils;
 
-Cu.import('resource://gre/modules/Services.jsm');
 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
 
 Cu.import('chrome://greasemonkey-modules/content/GM_setClipboard.js');
@@ -15,6 +14,7 @@ Cu.import('chrome://greasemonkey-modules/content/menucommand.js');
 Cu.import('chrome://greasemonkey-modules/content/miscapis.js');
 Cu.import('chrome://greasemonkey-modules/content/sandbox.js');
 Cu.import('chrome://greasemonkey-modules/content/scriptProtocol.js');
+Cu.import('chrome://greasemonkey-modules/content/documentObserver.js');
 Cu.import('chrome://greasemonkey-modules/content/util.js');
 
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
@@ -24,30 +24,19 @@ var gStripUserPassRegexp = new RegExp('(://)([^:/]+)(:[^@/]+)?@');
 
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
 
-var contentObserver = {
-  observe: function (aSubject, aTopic, aData) {
+function contentObserver(win) {
     if (!GM_util.getEnabled()) return;
 
-    switch (aTopic) {
-      case 'document-element-inserted':
-        var doc = aSubject;
-        var win = doc && doc.defaultView;
-        if (!doc || !win) return;
-        if (win.top !== content) return;
-
-        var url = doc.documentURI;
-        if (!GM_util.isGreasemonkeyable(url)) return;
-
-        // Listen for whichever kind of load event arrives first.
-        win.addEventListener('DOMContentLoaded', contentLoad, true);
-        win.addEventListener('load', contentLoad, true);
-
-        runScripts('document-start', win);
-        break;
-      default:
-        dump('Content frame observed unknown topic: ' + aTopic + '\n');
-    }
-  }
+    var doc = win.document;
+    var url = doc.documentURI;
+    if (!GM_util.isGreasemonkeyable(url)) return;
+
+    // Listen for whichever kind of load event arrives first.
+    win.addEventListener('DOMContentLoaded', contentLoad, true);
+    win.addEventListener('load', contentLoad, true);
+
+    runScripts('document-start', win);
+  
 };
 
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
@@ -179,9 +168,19 @@ function windowIsTop(aContentWin) {
   return true;
 };
 
+function windowCreated() {
+  OnNewDocument(content, contentObserver);
+}
+
+
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
 
 addEventListener('DOMContentLoaded', blankLoad);
+addEventListener('DOMWindowCreated', windowCreated);
+
+if(content) windowCreated();
+
+
 
 addMessageListener('greasemonkey:inject-delayed-script', injectDelayedScript);
 addMessageListener('greasemonkey:load-failed-script', loadFailedScript);
@@ -192,10 +191,6 @@ addMessageListener('greasemonkey:menu-command-run', function(aMessage) {
   MenuCommandRun(content, aMessage);
 });
 
-Services.obs.addObserver(contentObserver, 'document-element-inserted', false);
-addEventListener('unload', function() {
-  Services.obs.removeObserver(contentObserver, 'document-element-inserted');
-}, false);
 
 (function() {
   initInstallPolicy();
diff --git a/modules/documentObserver.js b/modules/documentObserver.js
new file mode 100644
index 0000000..288b69c
--- /dev/null
+++ b/modules/documentObserver.js
@@ -0,0 +1,41 @@
+'use strict';
+
+var EXPORTED_SYMBOLS = ['OnNewDocument'];
+
+var Cu = Components.utils;
+
+Cu.import('resource://gre/modules/Services.jsm');
+Cu.import('chrome://greasemonkey-modules/content/util.js');
+
+
+var callbacks = new WeakMap()
+
+function OnNewDocument(topWindow, callback) {
+	callbacks.set(topWindow, callback)
+}
+
+var contentObserver = {
+  observe: function (aSubject, aTopic, aData) {
+    if (!GM_util.getEnabled()) return;
+
+    switch (aTopic) {
+      case 'document-element-inserted':
+        var doc = aSubject;
+        var win = doc && doc.defaultView;
+        if (!doc || !win) return;
+        var topWin = win.top;
+
+        var frameCallback = callbacks.get(topWin);
+
+        if(!frameCallback) return;
+
+        frameCallback(win)
+
+        break;
+      default:
+        dump('Content frame observed unknown topic: ' + aTopic + '\n');
+    }
+  }
+};
+
+Services.obs.addObserver(contentObserver, 'document-element-inserted', false);
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/greasemonkey.git



More information about the Pkg-mozext-commits mailing list