Merge pull request #1406 from brendandahl/altlistener

Use Different Approach to add Event Listener
This commit is contained in:
notmasteryet 2012-03-26 18:51:11 -07:00
commit 981427dddb

View 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,7 @@ ChromeActions.prototype = {
}
};
// Event listener to trigger chrome privedged code.
function RequestListener(actions) {
this.actions = actions;
@ -163,38 +152,32 @@ PdfStreamConverter.prototype = {
var channel = ioService.newChannel(
'resource://pdf.js/web/viewer.html', null, null);
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);
// Double check the url is still the correct one.
if (domWindow.document.documentURIObject.equals(aRequest.URI)) {
let requestListener = new RequestListener(new ChromeActions);
domWindow.addEventListener(PDFJS_EVENT_ID, function(event) {
requestListener.receive(event);
}, false, true);
}
listener.onStopRequest.apply(listener, arguments);
}
};
// 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;
let requestListener = new RequestListener(new ChromeActions);
win.addEventListener(PDFJS_EVENT_ID, function(event) {
requestListener.receive(event);
}, false, true);
} else if (!getWindow(top, id)) {
top.removeEventListener('DOMWindowCreated', onDOMWinCreated, true);
}
}, true);
channel.asyncOpen(proxy, aContext);
},
// nsIRequestObserver::onStopRequest