[Pkg-mozext-commits] [greasemonkey] 16/43: Restore ScriptRunner functionality etc.

David Prévot taffit at moszumanska.debian.org
Sun Feb 22 21:56:10 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 ba1a2e1711ade6914ed0b329743f36ba00f2c766
Author: Ventero <ventero at ventero.de>
Date:   Tue Nov 4 21:34:27 2014 +0100

    Restore ScriptRunner functionality etc.
    
    Reverts parts of f82a7e813a3280f89cba16c60ccb340b688b468a.
---
 content/framescript.js               |  47 +----------
 modules/contentObserver.js           | 149 ++++++++++++++++++++---------------
 modules/sandbox.js                   |  38 +++++----
 modules/util/messageManagerForWin.js |  21 +++++
 4 files changed, 131 insertions(+), 124 deletions(-)

diff --git a/content/framescript.js b/content/framescript.js
index fa9ada0..960e5a1 100644
--- a/content/framescript.js
+++ b/content/framescript.js
@@ -6,8 +6,6 @@ var Ci = Components.interfaces;
 var Cu = Components.utils;
 
 
-//var gScriptRunners = {};
-
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
 
 // For every frame/process, make sure the content observer is running.
@@ -16,47 +14,10 @@ Cu.import('resource://greasemonkey/contentObserver.js');
 
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
 
-function ScriptRunner(aWindow, aUrl) {
-  this.menuCommands = [];
-  this.window = aWindow;
-  this.windowId = GM_util.windowId(this.window);
-  this.url = aUrl;
-}
-
-
-ScriptRunner.prototype.openInTab = function(aUrl, aInBackground) {
-  var response = sendSyncMessage('greasemonkey:open-in-tab', {
-    inBackground: aInBackground,
-    url: aUrl
-  });
-
-  return response ? response[0] : null;
-};
-
-
-ScriptRunner.prototype.registeredMenuCommand = function(aCommand) {
-  var length = this.menuCommands.push(aCommand);
-
-  sendAsyncMessage("greasemonkey:menu-command-registered", {
-    accessKey: aCommand.accessKey,
-    frozen: aCommand.frozen,
-    index: length - 1,
-    name: aCommand.name,
-    windowId: aCommand.contentWindowId
-  });
-};
-
-// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
-
-/*
-addEventListener("pagehide", observer.pagehide.bind(observer));
-addEventListener("pageshow", observer.pageshow.bind(observer));
-*/
-
+addEventListener("pagehide", contentObserver.pagehide.bind(contentObserver));
+addEventListener("pageshow", contentObserver.pageshow.bind(contentObserver));
 
-/*
 addMessageListener("greasemonkey:inject-script",
-    observer.runDelayedScript.bind(observer));
+    contentObserver.runDelayedScript.bind(contentObserver));
 addMessageListener("greasemonkey:menu-command-clicked",
-    observer.runMenuCommand.bind(observer));
-*/
+    contentObserver.runMenuCommand.bind(contentObserver));
diff --git a/modules/contentObserver.js b/modules/contentObserver.js
index 66cad99..c65a4ca 100644
--- a/modules/contentObserver.js
+++ b/modules/contentObserver.js
@@ -17,10 +17,77 @@ Cu.import('resource://greasemonkey/util.js');
 
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
 
+var gScriptRunners = {};
 var gStripUserPassRegexp = new RegExp('(://)([^:/]+)(:[^@/]+)?@');
 
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
 
+function ScriptRunner(aWindow, aUrl) {
+  this.menuCommands = [];
+  this.window = aWindow;
+  this.windowId = GM_util.windowId(this.window);
+  this.url = aUrl;
+}
+
+ScriptRunner.prototype.injectScripts = function(aScripts) {
+  try {
+    this.window.QueryInterface(Ci.nsIDOMChromeWindow);
+    // Never ever inject scripts into a chrome context window.
+    return;
+  } catch(e) {
+    // Ignore, it's good if we can't QI to a chrome window.
+  }
+
+  var winIsTop = this.windowIsTop(this.window);
+
+  for (var i = 0, script = null; script = aScripts[i]; i++) {
+    if (script.noframes && !winIsTop) continue;
+    var sandbox = createSandbox(script, this);
+    runScriptInSandbox(script, sandbox);
+  }
+}
+
+ScriptRunner.prototype.openInTab = function(aUrl, aInBackground) {
+  var mm = GM_util.messageManagerForWin(this.window);
+  var response = mm.sendSyncMessage('greasemonkey:open-in-tab', {
+    inBackground: aInBackground,
+    url: aUrl
+  });
+
+  return response ? response[0] : null;
+};
+
+
+ScriptRunner.prototype.registeredMenuCommand = function(aCommand) {
+  var length = this.menuCommands.push(aCommand);
+
+  var mm = GM_util.messageManagerForWin(this.window);
+  mm.sendAsyncMessage("greasemonkey:menu-command-registered", {
+    accessKey: aCommand.accessKey,
+    frozen: aCommand.frozen,
+    index: length - 1,
+    name: aCommand.name,
+    windowId: aCommand.contentWindowId
+  });
+};
+
+ScriptRunner.prototype.windowIsTop = function(aContentWin) {
+  try {
+    aContentWin.QueryInterface(Ci.nsIDOMWindow);
+    if (aContentWin.frameElement) return false;
+  } catch (e) {
+    var url = 'unknown';
+    try {
+      url = aContentWin.location.href;
+    } catch (e) { }
+    // Ignore non-DOM-windows.
+    dump('Could not QI this.window to nsIDOMWindow at\n' + url + ' ?!\n');
+  }
+  return true;
+};
+
+// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
+
 function ContentObserver() {
 }
 
@@ -68,7 +135,7 @@ ContentObserver.prototype.observe = function(aSubject, aTopic, aData) {
       // twice, though we'd expect once.  One observes broken events, the other
       // works.
       try {
-        this.contentFrameMessageManager(win);
+        GM_util.messageManagerForWin(win);
       } catch (e) {
         dump('ignoring observe of win with no contentFrameMessageManager\n');
         return;
@@ -78,12 +145,6 @@ ContentObserver.prototype.observe = function(aSubject, aTopic, aData) {
       win.addEventListener("DOMContentLoaded", gContentLoad, true);
       win.addEventListener("load", gContentLoad, true);
 
-      // TODO:
-      // Sometimes we get this notification twice with different windows but
-      // identical documentURI/location.href. In one of those cases, the call
-      // to sendSyncMessage will throw, and I can't find a way to detect which
-      // notification is the correct one. if (win !== content) would also
-      // exclude iframes.
       this.runScripts('document-start', win);
       break;
     default:
@@ -95,8 +156,7 @@ ContentObserver.prototype.observe = function(aSubject, aTopic, aData) {
 ContentObserver.prototype.pagehide = function(aEvent) {
   var contentWin = aEvent.target.defaultView;
   var windowId = GM_util.windowId(contentWin);
-  // TODO: Fix.
-  /*
+
   if (!windowId || !gScriptRunners[windowId]) return;
 
   // Small optimization: only send a notification if there's a menu command
@@ -104,33 +164,33 @@ ContentObserver.prototype.pagehide = function(aEvent) {
   if (!gScriptRunners[windowId].menuCommands.length) return;
 
   if (aEvent.persisted) {
-    sendAsyncMessage("greasemonkey:toggle-menu-commands", {
+    var mm = GM_util.messageManagerForWin(contentWin);
+    mm.sendAsyncMessage("greasemonkey:toggle-menu-commands", {
       frozen: true,
       windowId: windowId
     });
   } else {
-    sendAsyncMessage("greasemonkey:clear-menu-commands", {
+    var mm = GM_util.messageManagerForWin(contentWin);
+    mm.sendAsyncMessage("greasemonkey:clear-menu-commands", {
       windowId: windowId
     });
   }
-  */
 };
 
 
 ContentObserver.prototype.pageshow = function(aEvent) {
   var contentWin = aEvent.target.defaultView;
   var windowId = GM_util.windowId(contentWin);
-  // TODO: Fix.
-  /*
+
   if (!windowId || !gScriptRunners[windowId]) return;
 
   if (!gScriptRunners[windowId].menuCommands.length) return;
 
-  sendAsyncMessage("greasemonkey:toggle-menu-commands", {
+  var mm = GM_util.messageManagerForWin(contentWin);
+  mm.sendAsyncMessage("greasemonkey:toggle-menu-commands", {
     frozen: false,
     windowId: windowId
   });
-  */
 };
 
 
@@ -157,14 +217,6 @@ ContentObserver.prototype.runMenuCommand = function(aMessage) {
 
 
 ContentObserver.prototype.runScripts = function(aRunWhen, aContentWin) {
-  try {
-    this.window.QueryInterface(Ci.nsIDOMChromeWindow);
-    // Never ever inject scripts into a chrome context window.
-    return;
-  } catch(e) {
-    // Ignore, it's good if we can't QI to a chrome window.
-  }
-
   // See #1970
   // When content does (e.g.) history.replacestate() in an inline script,
   // the location.href changes between document-start and document-end time.
@@ -178,17 +230,17 @@ ContentObserver.prototype.runScripts = function(aRunWhen, aContentWin) {
   if (!GM_util.isGreasemonkeyable(url)) return;
 
   var windowId = GM_util.windowId(aContentWin);
-  // TODO: Fix.
-  /*
-  if (gScriptRunners[windowId]) {
-    // Update the window in case it changed, see the comment in observe().
-    gScriptRunners[windowId].window = aContentWin;
-  } else {
+  if (!gScriptRunners[windowId]) {
     gScriptRunners[windowId] = new ScriptRunner(aContentWin, url);
+  } else if (gScriptRunners[windowId].window !== aContentWin) {
+    // Sanity check, shouldn't be necessary.
+    // TODO: remove
+    dump("Script runner window changed for " + url + " at " + aRunWhen + "\n");
+    gScriptRunners[windowId].window = aContentWin;
   }
-  */
 
-  var response = this.contentFrameMessageManager(aContentWin).sendSyncMessage(
+  var mm = GM_util.messageManagerForWin(aContentWin);
+  var response = mm.sendSyncMessage(
     'greasemonkey:scripts-for-url', {
       'url': url,
       'when': aRunWhen,
@@ -197,37 +249,7 @@ ContentObserver.prototype.runScripts = function(aRunWhen, aContentWin) {
   if (!response || !response[0]) return;
 
   var scripts = response[0].map(this.createScriptFromObject);
-  var winIsTop = this.windowIsTop(aContentWin);
-  for (var i = 0, script = null; script = scripts[i]; i++) {
-    if (script.noframes && !winIsTop) continue;
-    var sandbox = createSandbox(script, url, aContentWin);
-    runScriptInSandbox(script, sandbox);
-  }
-};
-
-
-ContentObserver.prototype.windowIsTop = function(aContentWin) {
-  try {
-    aContentWin.QueryInterface(Ci.nsIDOMWindow);
-    if (aContentWin.frameElement) return false;
-  } catch (e) {
-    var url = 'unknown';
-    try {
-      url = aContentWin.location.href;
-    } catch (e) { }
-    // Ignore non-DOM-windows.
-    dump('Could not QI this.window to nsIDOMWindow at\n' + url + ' ?!\n');
-  }
-  return true;
-};
-
-ContentObserver.prototype.contentFrameMessageManager = function(aContentWin) {
-  return aContentWin.QueryInterface(Ci.nsIInterfaceRequestor)
-      .getInterface(Ci.nsIWebNavigation)
-      .QueryInterface(Ci.nsIDocShellTreeItem)
-      .rootTreeItem
-      .QueryInterface(Ci.nsIInterfaceRequestor)
-      .getInterface(Ci.nsIContentFrameMessageManager);
+  gScriptRunners[windowId].injectScripts(scripts);
 };
 
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
@@ -235,7 +257,6 @@ ContentObserver.prototype.contentFrameMessageManager = function(aContentWin) {
 // Since observers are process-global, create this observer singleton in the
 // process-global JSM scope.
 var contentObserver = new ContentObserver();
-dump('ADD OBSERVER\n');
 Services.obs.addObserver(contentObserver, 'document-element-inserted', false);
 
 // This single global function reference can easily be both attached as
diff --git a/modules/sandbox.js b/modules/sandbox.js
index f36dea9..8e6440a 100644
--- a/modules/sandbox.js
+++ b/modules/sandbox.js
@@ -24,15 +24,18 @@ function alert(msg) {
       .alert(null, "Greasemonkey alert", msg);
 }
 
-function createSandbox(aScript, aUrl, aWindow) {
+function createSandbox(aScript, aScriptRunner) {
+  var contentWin = aScriptRunner.window;
+  var url = aScriptRunner.url;
+
   if (GM_util.inArray(aScript.grants, 'none')) {
     // If there is an explicit none grant, use a plain unwrapped sandbox
     // with no other content.
     var contentSandbox = new Components.utils.Sandbox(
-        aWindow,
+        contentWin,
         {
           'sandboxName': aScript.id,
-          'sandboxPrototype': aWindow,
+          'sandboxPrototype': contentWin,
           'wantXrays': false,
         });
     // GM_info is always provided.
@@ -53,10 +56,10 @@ function createSandbox(aScript, aUrl, aWindow) {
   }
 
   var sandbox = new Components.utils.Sandbox(
-      [aWindow],
+      [contentWin],
       {
         'sandboxName': aScript.id,
-        'sandboxPrototype': aWindow,
+        'sandboxPrototype': contentWin,
         'wantXrays': true,
       });
 
@@ -72,16 +75,16 @@ function createSandbox(aScript, aUrl, aWindow) {
   sandbox.exportFunction = Cu.exportFunction;
 
   if (GM_util.inArray(aScript.grants, 'GM_addStyle')) {
-    sandbox.GM_addStyle = GM_util.hitch(null, GM_addStyle, aWindow.document);
+    sandbox.GM_addStyle = GM_util.hitch(null, GM_addStyle, contentWin.document);
   }
   if (GM_util.inArray(aScript.grants, 'GM_log')) {
     sandbox.GM_log = GM_util.hitch(new GM_ScriptLogger(aScript), 'log');
   }
-  // TODO: Fix.
-//  if (GM_util.inArray(aScript.grants, 'GM_registerMenuCommand')) {
-//    var gmrmc = GM_util.hitch(null, registerMenuCommand, aScriptRunner);
-//    sandbox.GM_registerMenuCommand = gmrmc;
-//  }
+
+  if (GM_util.inArray(aScript.grants, 'GM_registerMenuCommand')) {
+   var gmrmc = GM_util.hitch(null, registerMenuCommand, aScriptRunner);
+   sandbox.GM_registerMenuCommand = gmrmc;
+  }
 
   var scriptStorage = new GM_ScriptStorage(aScript);
   if (GM_util.inArray(aScript.grants, 'GM_deleteValue')) {
@@ -109,14 +112,15 @@ function createSandbox(aScript, aUrl, aWindow) {
   if (GM_util.inArray(aScript.grants, 'GM_listValues')) {
     sandbox.GM_listValues = GM_util.hitch(scriptStorage, 'listValues');
   }
-  // TODO: Fix.
-//  if (GM_util.inArray(aScript.grants, 'GM_openInTab')) {
-//    sandbox.GM_openInTab = GM_util.hitch(
-//        null, GM_openInTab, aScriptRunner);
-//  }
+
+  if (GM_util.inArray(aScript.grants, 'GM_openInTab')) {
+   sandbox.GM_openInTab = GM_util.hitch(
+       null, GM_openInTab, aScriptRunner);
+  }
+
   if (GM_util.inArray(aScript.grants, 'GM_xmlhttpRequest')) {
     sandbox.GM_xmlhttpRequest = GM_util.hitch(
-        new GM_xmlhttpRequester(aWindow, aUrl, sandbox),
+        new GM_xmlhttpRequester(contentWin, url, sandbox),
         'contentStartRequest');
   }
 
diff --git a/modules/util/messageManagerForWin.js b/modules/util/messageManagerForWin.js
new file mode 100644
index 0000000..3033f22
--- /dev/null
+++ b/modules/util/messageManagerForWin.js
@@ -0,0 +1,21 @@
+var EXPORTED_SYMBOLS = ['messageManagerForWin'];
+
+var Ci = Components.interfaces;
+
+function messageManagerForWin(aContentWin) {
+  var rti = aContentWin.QueryInterface(Ci.nsIInterfaceRequestor)
+      .getInterface(Ci.nsIWebNavigation)
+      .QueryInterface(Ci.nsIDocShellTreeItem)
+      .rootTreeItem;
+
+  // dump(rti + "\n");
+  // dump(rti.itemType + "\n");
+  // dump(rti.name + "\n");
+
+  return aContentWin.QueryInterface(Ci.nsIInterfaceRequestor)
+      .getInterface(Ci.nsIWebNavigation)
+      .QueryInterface(Ci.nsIDocShellTreeItem)
+      .rootTreeItem
+      .QueryInterface(Ci.nsIInterfaceRequestor)
+      .getInterface(Ci.nsIContentFrameMessageManager);
+};

-- 
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