add popup blocking context menu item. bug 166442 r=brendan,jag

This commit is contained in:
danm%netscape.com 2006-09-14 06:05:18 +00:00
Родитель a64fef63b3
Коммит b0aafe88a3
1 изменённых файлов: 66 добавлений и 0 удалений

Просмотреть файл

@ -130,6 +130,25 @@ const gHomepagePrefListener =
}
};
// popup window permission change listener
const gPopupPermListener = {
observe: function(subject, topic, data) {
if (topic == "popup-perm-close") {
// close the window if we're a popup and our opener's URI matches
// the URI in the notification
var popupOpenerURI = maybeInitPopupContext();
if (popupOpenerURI) {
const IOS = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
closeURI = IOS.newURI(data, null, null);
if (closeURI.host == popupOpenerURI.host)
window.close();
}
}
}
};
/**
* Pref listener handler functions.
* Both functions assume that observer.domain is set to
@ -155,6 +174,20 @@ function removePrefListener(observer)
}
}
function addPopupPermListener(observer)
{
const OS = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
OS.addObserver(observer, "popup-perm-close", false);
}
function removePopupPermListener(observer)
{
const OS = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
OS.removeObserver(observer, "popup-perm-close");
}
/**
* We can avoid adding multiple load event listeners and save some time by adding
* one listener that calls all real handlers.
@ -378,6 +411,7 @@ function Startup()
addPrefListener(gButtonPrefListener);
addPrefListener(gTabStripPrefListener);
addPrefListener(gHomepagePrefListener);
addPopupPermListener(gPopupPermListener);
window.browserContentListener =
new nsBrowserContentListener(window, getBrowser());
@ -576,6 +610,7 @@ function Shutdown()
removePrefListener(gButtonPrefListener);
removePrefListener(gTabStripPrefListener);
removePrefListener(gHomepagePrefListener);
removePopupPermListener(gPopupPermListener);
window.browserContentListener.close();
// Close the app core.
@ -1953,3 +1988,34 @@ function checkTheme()
}
}
}
// opener may not have been initialized by load time (chrome windows only)
// so call this function some time later.
function maybeInitPopupContext()
{
// it's not a popup with no opener
if (!window.content.opener)
return null;
try {
// are we a popup window?
const CI = Components.interfaces;
var xulwin = window
.QueryInterface(CI.nsIInterfaceRequestor)
.getInterface(CI.nsIWebNavigation)
.QueryInterface(CI.nsIDocShellTreeItem).treeOwner
.QueryInterface(CI.nsIInterfaceRequestor)
.getInterface(CI.nsIXULWindow);
if (xulwin.contextFlags &
CI.nsIWindowCreator2.PARENT_IS_LOADING_OR_RUNNING_TIMEOUT) {
// return our opener's URI
const IOS = Components.classes["@mozilla.org/network/io-service;1"]
.getService(CI.nsIIOService);
var spec = Components.lookupMethod(window.content.opener, "location")
.call();
return IOS.newURI(spec, null, null);
}
} catch(e) {
}
return null;
}