Bug 789392 - window.close() failed to trigger mozbrowserclose event on the parent frame if the child frame is not created with window.open. r=bz

This commit is contained in:
Justin Lebar 2012-09-27 23:06:01 -04:00
parent a06e299cf2
commit 7032cf24b5
6 changed files with 112 additions and 3 deletions

View File

@ -6518,9 +6518,10 @@ nsGlobalWindow::Close()
return NS_OK;
}
// Don't allow scripts from content to close windows
// that were not opened by script
if (!mHadOriginalOpener && !nsContentUtils::IsCallerTrustedForWrite()) {
// Don't allow scripts from content to close non-app windows that were not
// opened by script.
if (!mDocShell->GetIsApp() &&
!mHadOriginalOpener && !nsContentUtils::IsCallerTrustedForWrite()) {
bool allowClose =
Preferences::GetBool("dom.allow_scripts_to_close_windows", true);
if (!allowClose) {

View File

@ -93,6 +93,9 @@ MOCHITEST_FILES = \
browserElement_CloseFromOpener.js \
test_browserElement_inproc_CloseFromOpener.html \
file_browserElement_CloseFromOpener.html \
browserElement_CloseApp.js \
test_browserElement_inproc_CloseApp.html \
file_browserElement_CloseApp.html \
browserElement_OpenWindow.js \
test_browserElement_inproc_OpenWindow.html \
file_browserElement_Open1.html \
@ -178,6 +181,7 @@ MOCHITEST_FILES += \
test_browserElement_oop_PromptConfirm.html \
test_browserElement_oop_Close.html \
test_browserElement_oop_CloseFromOpener.html \
test_browserElement_oop_CloseApp.html \
test_browserElement_oop_OpenWindow.html \
test_browserElement_oop_OpenWindowInFrame.html \
test_browserElement_oop_OpenWindowRejected.html \

View File

@ -0,0 +1,66 @@
/* Any copyright is dedicated to the public domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Bug 789392 - Test that apps frames can trigger mozbrowserclose by calling
// window.close(), but browser frames cannot.
"use strict";
SimpleTest.waitForExplicitFinish();
function runTest() {
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.addPermission();
// Our app frame and browser frame load the same content. That content calls
// window.close() and then alert(). We should get a mozbrowserclose event on
// the app frame before the mozbrowsershowmodalprompt, but not on the browser
// frame.
var appFrame = document.createElement('iframe');
appFrame.mozbrowser = true;
appFrame.setAttribute('mozapp', 'http://example.org/manifest.webapp');
var browserFrame = document.createElement('iframe');
browserFrame.mozbrowser = true;
var gotAppFrameClose = false;
appFrame.addEventListener('mozbrowserclose', function() {
ok(true, "Got close from app frame.");
gotAppFrameClose = true;
});
var gotAppFrameAlert = false;
appFrame.addEventListener('mozbrowsershowmodalprompt', function() {
ok(gotAppFrameClose, "Should have gotten app frame close by now.");
ok(!gotAppFrameAlert, "Just one alert from the app frame.");
gotAppFrameAlert = true;
if (gotBrowserFrameAlert && gotAppFrameAlert) {
SimpleTest.finish();
}
});
browserFrame.addEventListener('mozbrowserclose', function() {
ok(false, "Got close from browser frame.");
});
var gotBrowserFrameAlert = false;
browserFrame.addEventListener('mozbrowsershowmodalprompt', function() {
ok(!gotBrowserFrameAlert, "Just one browser frame alert.");
gotBrowserFrameAlert = true;
if (gotBrowserFrameAlert && gotAppFrameAlert) {
SimpleTest.finish();
}
});
document.body.appendChild(appFrame);
document.body.appendChild(browserFrame);
appFrame.src = 'http://example.org/tests/dom/browser-element/mochitest/file_browserElement_CloseApp.html';
browserFrame.src = 'http://example.org/tests/dom/browser-element/mochitest/file_browserElement_CloseApp.html';
}
// The test harness sets dom.allow_scripts_to_close_windows to true (as of
// writing, anyway). But that means that browser tabs can close themselves,
// which is what we want to test /can't/ happen! For the purposes of this
// test (and normal browser operation), this pref should be false.
SpecialPowers.pushPrefEnv({'set': [['dom.allow_scripts_to_close_windows', false]]}, runTest);

View File

@ -0,0 +1,12 @@
<html>
<body>
file_browserElement_CloseApp.html
<script>
window.close();
alert('called close');
</script>
</body>
</html>

View File

@ -0,0 +1,13 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for Bug 789392</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="browserElementTestHelpers.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="application/javascript;version=1.7" src="browserElement_CloseApp.js">
</script>
</body>
</html>

View File

@ -0,0 +1,13 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for Bug 789392</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="browserElementTestHelpers.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="application/javascript;version=1.7" src="browserElement_CloseApp.js">
</script>
</body>
</html>