Bug 936129 - nsGlobalWindow::InnerForSetTimeoutOrInterval is a no-op for bareword setTimeout. r=bz.

--HG--
extra : rebase_source : 3a7875435f6443c67af9030fa4e39cac6aac8a49
This commit is contained in:
Peter Van der Beken 2013-11-26 16:28:34 +01:00
parent e108a063d1
commit b742139fb8
3 changed files with 97 additions and 38 deletions

View File

@ -11287,41 +11287,47 @@ uint32_t sNestingLevel;
nsGlobalWindow*
nsGlobalWindow::InnerForSetTimeoutOrInterval(ErrorResult& aError)
{
// This needs to forward to the inner window, but since the current
// inner may not be the inner in the calling scope, we need to treat
// this specially here as we don't want timeouts registered in a
// dying inner window to get registered and run on the current inner
// window. To get this right, we need to forward this call to the
// inner window that's calling window.setTimeout().
nsGlobalWindow* currentInner;
nsGlobalWindow* forwardTo;
if (IsInnerWindow()) {
nsGlobalWindow* outer = GetOuterWindowInternal();
currentInner = outer ? outer->GetCurrentInnerWindowInternal() : this;
if (!IsOuterWindow()) {
return this;
forwardTo = this;
} else {
currentInner = GetCurrentInnerWindowInternal();
// This needs to forward to the inner window, but since the current
// inner may not be the inner in the calling scope, we need to treat
// this specially here as we don't want timeouts registered in a
// dying inner window to get registered and run on the current inner
// window. To get this right, we need to forward this call to the
// inner window that's calling window.setTimeout().
forwardTo = CallerInnerWindow();
if (!forwardTo) {
aError.Throw(NS_ERROR_NOT_AVAILABLE);
return nullptr;
}
// If the caller and the callee share the same outer window, forward to the
// caller inner. Else, we forward to the current inner (e.g. someone is
// calling setTimeout() on a reference to some other window).
if (forwardTo->GetOuterWindow() != this || !forwardTo->IsInnerWindow()) {
if (!currentInner) {
NS_WARNING("No inner window available!");
aError.Throw(NS_ERROR_NOT_INITIALIZED);
return nullptr;
}
return currentInner;
}
}
nsGlobalWindow* callerInner = CallerInnerWindow();
if (!callerInner) {
aError.Throw(NS_ERROR_NOT_AVAILABLE);
return nullptr;
}
// If the caller and the callee share the same outer window,
// forward to the caller inner. Else, we forward to the current
// inner (e.g. someone is calling setTimeout() on a reference to
// some other window).
if (callerInner->GetOuterWindow() == this &&
callerInner->IsInnerWindow()) {
return callerInner;
}
nsGlobalWindow* currentInner = GetCurrentInnerWindowInternal();
if (!currentInner) {
NS_WARNING("No inner window available!");
aError.Throw(NS_ERROR_NOT_INITIALIZED);
return nullptr;
}
return currentInner;
// If forwardTo is not the window with an active document then we want the
// call to setTimeout/Interval to be a noop, so return null but don't set an
// error.
return forwardTo->HasActiveDocument() ? currentInner : nullptr;
}
int32_t
@ -11381,8 +11387,7 @@ nsGlobalWindow::SetTimeoutOrInterval(nsIScriptTimeoutHandler *aHandler,
int32_t interval,
bool aIsInterval, int32_t *aReturn)
{
FORWARD_TO_INNER(SetTimeoutOrInterval, (aHandler, interval, aIsInterval, aReturn),
NS_ERROR_NOT_INITIALIZED);
MOZ_ASSERT(IsInnerWindow());
// If we don't have a document (we could have been unloaded since
// the call to setTimeout was made), do nothing.
@ -11565,8 +11570,8 @@ nsGlobalWindow::SetTimeoutOrInterval(Function& aFunction, int32_t aTimeout,
bool aIsInterval, ErrorResult& aError)
{
nsGlobalWindow* inner = InnerForSetTimeoutOrInterval(aError);
if (aError.Failed()) {
return 0;
if (!inner) {
return -1;
}
if (inner != this) {
@ -11591,8 +11596,8 @@ nsGlobalWindow::SetTimeoutOrInterval(JSContext* aCx, const nsAString& aHandler,
ErrorResult& aError)
{
nsGlobalWindow* inner = InnerForSetTimeoutOrInterval(aError);
if (aError.Failed()) {
return 0;
if (!inner) {
return -1;
}
if (inner != this) {

View File

@ -31,6 +31,7 @@ support-files =
[test_named_frames.html]
[test_nondomexception.html]
[test_screen_orientation.html]
[test_settimeout_inner.html]
[test_setting_opener.html]
[test_url.html]
[test_window_constructor.html]

View File

@ -0,0 +1,53 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=936129
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 936129</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript">
/** Test for Bug 936129 **/
SimpleTest.waitForExplicitFinish();
function test1Done()
{
ok(true, "Bareword setTimeout should work after calling document.open().");
var iframe = document.getElementById("testFrame");
iframe.onload = function () {
window.runTest2 = iframe.contentWindow.runTest2;
iframe.onload = function () {
window.runTest2();
setTimeout(allDone);
}
iframe.src = "about:blank";
}
iframe.src = "data:text/html,<script>function runTest2() { setTimeout('parent.test2Done()'); };<" + "/script>";
}
window.test2DoneCalled = false;
function test2Done()
{
window.test2DoneCalled = true;
}
function allDone()
{
ok(!window.test2DoneCalled, "Bareword setTimeout should be a noop after the document for the window context that it's called on isn't active anymore.");
SimpleTest.finish();
}
</script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=936129">Mozilla Bug 936129</a>
<p id="display"></p>
<div id="content" style="display: none">
<iframe id="testFrame" src="data:text/html,<script>window.onload = function runTest1() { document.open(); setTimeout('parent.test1Done();'); document.close(); }</script>"></iframe>
</div>
<pre id="test">
</pre>
</body>
</html>