]> git.parisson.com Git - pdf.js.git/commitdiff
Use a different way to add the event listener.
authorBrendan Dahl <brendan.dahl@gmail.com>
Fri, 23 Mar 2012 21:28:09 +0000 (14:28 -0700)
committerBrendan Dahl <brendan.dahl@gmail.com>
Fri, 23 Mar 2012 21:28:09 +0000 (14:28 -0700)
extensions/firefox/components/PdfStreamConverter.js

index 9375a26909c0747a45e72204181e86b809645654..da7748f32ec65a24859e7c2eee49d6397baac64c 100644 (file)
@@ -30,23 +30,11 @@ function log(aMsg) {
   Services.console.logStringMessage(msg);
   dump(msg + '\n');
 }
-function getWindow(top, id) {
-  return top.QueryInterface(Ci.nsIInterfaceRequestor)
-            .getInterface(Ci.nsIDOMWindowUtils)
-            .getOuterWindowWithId(id);
-}
-function windowID(win) {
-  return win.QueryInterface(Ci.nsIInterfaceRequestor)
-            .getInterface(Ci.nsIDOMWindowUtils)
-            .outerWindowID;
-}
-function topWindow(win) {
-  return win.QueryInterface(Ci.nsIInterfaceRequestor)
-            .getInterface(Ci.nsIWebNavigation)
-            .QueryInterface(Ci.nsIDocShellTreeItem)
-            .rootTreeItem
-            .QueryInterface(Ci.nsIInterfaceRequestor)
-            .getInterface(Ci.nsIDOMWindow);
+
+function getDOMWindow(aChannel) {
+  var requestor = aChannel.notificationCallbacks;
+  var win = requestor.getInterface(Components.interfaces.nsIDOMWindow);
+  return win;
 }
 
 // All the priviledged actions.
@@ -75,6 +63,12 @@ ChromeActions.prototype = {
   }
 };
 
+function getDOMWindow(aChannel) {
+  var requestor = aChannel.notificationCallbacks;
+  var win = requestor.getInterface(Components.interfaces.nsIDOMWindow);
+  return win;
+}
+
 // Event listener to trigger chrome privedged code.
 function RequestListener(actions) {
   this.actions = actions;
@@ -163,38 +157,29 @@ PdfStreamConverter.prototype = {
     var channel = ioService.newChannel(
                     'resource://pdf.js/web/viewer.html', null, null);
 
-    // Keep the URL the same so the browser sees it as the same.
-    channel.originalURI = aRequest.URI;
-    channel.asyncOpen(this.listener, aContext);
-
-    // Setup a global listener waiting for the next DOM to be created and verfiy
-    // that its the one we want by its URL. When the correct DOM is found create
-    // an event listener on that window for the pdf.js events that require
-    // chrome priviledges. Code snippet from John Galt.
-    let window = aRequest.loadGroup.groupObserver
-                         .QueryInterface(Ci.nsIWebProgress)
-                         .DOMWindow;
-    let top = topWindow(window);
-    let id = windowID(window);
-    window = null;
-
-    top.addEventListener('DOMWindowCreated', function onDOMWinCreated(event) {
-      let doc = event.originalTarget;
-      let win = doc.defaultView;
-
-      if (id == windowID(win)) {
-        top.removeEventListener('DOMWindowCreated', onDOMWinCreated, true);
-        if (!doc.documentURIObject.equals(aRequest.URI))
-          return;
-
+    var listener = this.listener;
+    // Proxy all the requst observer calls, when it gets to onStopRequst
+    // we can get the dom window.
+    var proxy = {
+      onStartRequest: function() {
+        listener.onStartRequest.apply(listener, arguments);
+      },
+      onDataAvailable: function() {
+        listener.onDataAvailable.apply(listener, arguments);
+      },
+      onStopRequest: function() {
+        var domWindow = getDOMWindow(channel);
         let requestListener = new RequestListener(new ChromeActions);
-        win.addEventListener(PDFJS_EVENT_ID, function(event) {
+        domWindow.addEventListener(PDFJS_EVENT_ID, function(event) {
           requestListener.receive(event);
         }, false, true);
-      } else if (!getWindow(top, id)) {
-        top.removeEventListener('DOMWindowCreated', onDOMWinCreated, true);
+        listener.onStopRequest.apply(listener, arguments);
       }
-    }, true);
+    };
+
+    // Keep the URL the same so the browser sees it as the same.
+    channel.originalURI = aRequest.URI;
+    channel.asyncOpen(proxy, aContext);
   },
 
   // nsIRequestObserver::onStopRequest