Skip to content

Commit

Permalink
Bug 1930458 - Always consume user activation when opending a new wind…
Browse files Browse the repository at this point in the history
…ow; r=smaug

Per whatwg/html#10547, we should always consume the
user activation even if the popup blocker is disabled or the window has
permission to open a new window.

Differential Revision: https://phabricator.services.mozilla.com/D230331
  • Loading branch information
EdgarChen committed Dec 3, 2024
1 parent 3621122 commit 68afb9b
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 19 deletions.
7 changes: 7 additions & 0 deletions dom/base/nsGlobalWindowOuter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6838,6 +6838,13 @@ nsresult nsGlobalWindowOuter::OpenInternal(
}
}

// Per https://github.com/whatwg/html/pull/10547, we should always consume
// user activation when opening a new window, even if the popup blocker is
// disabled or the website has popup permission.
if (!windowExists && mDoc) {
mDoc->ConsumeTransientUserGestureActivation();
}

RefPtr<BrowsingContext> domReturn;

nsresult rv = NS_OK;
Expand Down
3 changes: 3 additions & 0 deletions dom/base/test/useractivation/mochitest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ support-files = ["file_self_close.html"]

["test_useractivation_key_events.html"]

["test_useractivation_open_new_window.html"]
support-files = ["file_self_close.html"]

["test_useractivation_sandbox_transient.html"]
support-files = ["file_useractivation_sandbox_transient_popup.html"]

Expand Down
109 changes: 109 additions & 0 deletions dom/base/test/useractivation/test_useractivation_open_new_window.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!DOCTYPE HTML>
<html>
<head>
<title>User activation test: consume user activation when opending a new window</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a href="file_self_close.html" target="_blank">link</a>
<script>

const ALLOW_ACTION = SpecialPowers.Ci.nsIPermissionManager.ALLOW_ACTION;
const DENY_ACTION = SpecialPowers.Ci.nsIPermissionManager.DENY_ACTION;
const UNKNOWN_ACTION = SpecialPowers.Ci.nsIPermissionManager.UNKNOWN_ACTION;

async function test_useractivation_window_open() {
info("Test user activation after calling window.open()")
SpecialPowers.wrap(document).notifyUserGestureActivation();
// Open a new window
ok(navigator.userActivation.isActive, `has user activation`);
let w = window.open("file_empty.html", "testWindow");
ok(!navigator.userActivation.isActive, `user activation should be consumed after opening a new window`);

SpecialPowers.wrap(document).notifyUserGestureActivation();
ok(navigator.userActivation.isActive, `has user activation`);
let w2 = window.open("file_empty.html", "testWindow");
ok(w2 == w, "should use the existing window");
ok(navigator.userActivation.isActive, `user activation should not be consumed if window is existed`);

if (w2 && w2 !== w) {
w2.close();
}
if (w) {
w.close();
}
}

async function test_useractivation_anchor_blank() {
info("Test user activation after calling window.open()")
let link = document.querySelector("a");
link.addEventListener("click", () => {
ok(navigator.userActivation.isActive, `has user activation`);
});
synthesizeMouseAtCenter(link, {});
await new Promise(resolve => SimpleTest.executeSoon(resolve));
ok(!navigator.userActivation.isActive, `user activation should be consumed`);
}

async function test_useractivation() {
await test_useractivation_window_open();
await test_useractivation_anchor_blank();
}

add_task(async function testPopupBlockerDisabled() {
await SpecialPowers.pushPrefEnv({
set: [
// Disable popup blocker
["dom.disable_open_during_load", false],
],
});
await test_useractivation();
await SpecialPowers.popPrefEnv();
});

add_task(async function testPopupBlockerEnabled() {
await SpecialPowers.pushPrefEnv({
set: [
// Enable popup blocker
["dom.disable_open_during_load", true],
],
});

await test_useractivation();
await SpecialPowers.popPrefEnv();
});

add_task(async function testUnknownPermission() {
await SpecialPowers.pushPermissions([
{'type': 'popup', 'remove': true, 'context': document}
]);
ok(await SpecialPowers.testPermission('popup', UNKNOWN_ACTION, document),
'popup value should have UNKNOWN permission');
await test_useractivation();
await SpecialPowers.popPermissions();
});

add_task(async function testAllowPermission() {
await SpecialPowers.pushPermissions([
{'type': 'popup', 'allow': ALLOW_ACTION, 'context': document}
]);
ok(await SpecialPowers.testPermission('popup', ALLOW_ACTION, document),
'popup value should have ALLOW permission');
await test_useractivation();
await SpecialPowers.popPermissions();
});

add_task(async function testDenyPermission() {
await SpecialPowers.pushPermissions([
{'type': 'popup', 'allow': DENY_ACTION, 'context': document}
]);
ok(await SpecialPowers.testPermission('popup', DENY_ACTION, document),
'popup value should have ALLOW permission');
await test_useractivation();
await SpecialPowers.popPermissions();
});

</script>
</body>
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
[sec-fetch-user - POST]
expected:
if (os == "linux") and not fission and not debug: [FAIL, PASS, TIMEOUT, NOTRUN]
if (os == "linux") and fission: [FAIL, NOTRUN]
FAIL

[sec-fetch-user - POST with user activation]
expected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
expected:
if (os == "android") and not debug and sessionHistoryInParent: [OK, ERROR]
if (os == "android") and not debug and not sessionHistoryInParent: [OK, ERROR]
[window.open() and consuming user activation]
expected: FAIL

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# XXX(edgar): Need to enable popup blocker due to bug 1930458.
prefs: [dom.disable_open_during_load:true]
[target_blank_useractivation_multi_globals.html]
expected:
if not sessionHistoryInParent and not debug: [OK, ERROR]

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@
testWin.close();
});
assert_false(navigator.userActivation.isActive, "User activation should be consumed");
});
}, "Opening a new window should consume user activation");

promise_test(async function(t) {
await test_driver.bless("user activation");
const testWin = window.open("/resources/blank.html", "testWindow");
assert_false(navigator.userActivation.isActive, "User activation should be consumed");
t.add_cleanup(() => {
testWin.close();
});

// Open the existing window again
await test_driver.bless("user activation");
const testWin2 = window.open("/resources/blank.html", "testWindow");
assert_true(navigator.userActivation.isActive, "User activation should not be consumed");
}, "Opening an existing window should not consume user activation");
</script>

0 comments on commit 68afb9b

Please sign in to comment.