Bug 801080 - window.close should close social panels. r=felipe

This commit is contained in:
Mark Hammond 2012-10-16 13:53:37 +11:00
parent ab0f807a87
commit f41c26f285
2 changed files with 19 additions and 0 deletions

View File

@ -78,6 +78,15 @@ var tests = {
},
testCloseSelf: function(next) {
// window.close is affected by the pref dom.allow_scripts_to_close_windows,
// which defaults to false, but is set to true by the test harness.
// so temporarily set it back.
const ALLOW_SCRIPTS_TO_CLOSE_PREF = "dom.allow_scripts_to_close_windows";
// note clearUserPref doesn't do what we expect, as the test harness itself
// changes the pref value - so clearUserPref resets it to false rather than
// the true setup by the test harness.
let oldAllowScriptsToClose = Services.prefs.getBoolPref(ALLOW_SCRIPTS_TO_CLOSE_PREF);
Services.prefs.setBoolPref(ALLOW_SCRIPTS_TO_CLOSE_PREF, false);
let panel = document.getElementById("social-flyout-panel");
let port = Social.provider.getWorkerPort();
ok(port, "provider has a port");
@ -92,6 +101,7 @@ var tests = {
iframe.contentDocument.addEventListener("SocialTest-DoneCloseSelf", function _doneHandler() {
iframe.contentDocument.removeEventListener("SocialTest-DoneCloseSelf", _doneHandler, false);
is(panel.state, "closed", "flyout should have closed itself");
Services.prefs.setBoolPref(ALLOW_SCRIPTS_TO_CLOSE_PREF, oldAllowScriptsToClose);
next();
}, false);
is(panel.state, "open", "flyout should be open");

View File

@ -179,6 +179,15 @@ function attachToWindow(provider, targetWindow) {
// set a timer which will fire after the unload events have all fired.
schedule(function () { port.close(); });
});
// We allow window.close() to close the panel, so add an event handler for
// this, then cancel the event (so the window itself doesn't die) and
// close the panel instead.
// However, this is typically affected by the dom.allow_scripts_to_close_windows
// preference, but we can avoid that check by setting a flag on the window.
let dwu = targetWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
dwu.allowScriptsToClose();
targetWindow.addEventListener("DOMWindowClose", function _mozSocialDOMWindowClose(evt) {
let elt = targetWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)