Skip to content

Commit

Permalink
Change behavior of window.open w.r.t. windowPreferences and popups
Browse files Browse the repository at this point in the history
See [1] and [2] for more context, but this CL implements new behavior
for how window.open() interprets the windowPreferences argument when
deciding whether to open the window as a new tab or as a "popup",
which is a separate window with minimal UI (toolbars, onmibox,
etc.), and also what to return from the BarProp visible properties,
e.g. window.toolbar.visible.

The existing "trigger" behavior for popups will be retained by this
CL, namely that a popup will be opened instead of a tab if:
 1. the windowFeatures parameter is *not* empty, and
 2. one of the following conditions is true:
  * both `location` and `toolbar` are false or missing
  * `menubar` is false or missing
  * `resizable is false or missing
  * `scrollbar` is false or missing
  * `status` is false or missing

With this CL, an additional windowFeature called 'popup' is added,
so that if 'popup' is present and truthy.

Additionally, all BarProp properties (locationbar,menubar,
personalbar,scrollbars,statusbar, and toolbar) will always return
the same values, either false if a popup was opened, or true if
a tab/window was opened.

The spec for this behavior is part of the HTML spec:
https://html.spec.whatwg.org/multipage/window-object.html#popup-window-is-requested

The intent to ship is here:
https://groups.google.com/a/chromium.org/g/blink-dev/c/q6ybnmxxvpE

[1] whatwg/html#5872
[2] whatwg/html#6530

Fixed: 1192701
Change-Id: I50e745b1000d460c49085edd57d13f420b875ff3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2950386
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Mason Freed <masonf@chromium.org>
Cr-Commit-Position: refs/heads/main@{#943716}
  • Loading branch information
mfreed7 authored and Chromium LUCI CQ committed Nov 19, 2021
1 parent 1440265 commit cdad240
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 200 deletions.
2 changes: 2 additions & 0 deletions content/child/runtime_features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ void SetRuntimeFeaturesFromChromiumFeatures() {
{"WebAppWindowControlsOverlay",
features::kWebAppWindowControlsOverlay},
{"WebAuthenticationConditionalUI", features::kWebAuthConditionalUI},
{"WindowOpenNewPopupBehavior",
blink::features::kWindowOpenNewPopupBehavior},
{"SyncLoadDataUrlFonts", blink::features::kSyncLoadDataUrlFonts},
{"CSSCascadeLayers", blink::features::kCSSCascadeLayers},
// TODO(crbug.com/1185950): Remove this flag when the feature is fully
Expand Down
4 changes: 4 additions & 0 deletions third_party/blink/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ const base::Feature kPurgeRendererMemoryWhenBackgrounded {
#endif
};

// Enables new behavior for window.open() and BarProp properties.
const base::Feature kWindowOpenNewPopupBehavior{
"WindowOpenNewPopupBehavior", base::FEATURE_ENABLED_BY_DEFAULT};

// Changes the default RTCPeerConnection constructor behavior to use Unified
// Plan as the SDP semantics. When the feature is enabled, Unified Plan is used
// unless the default is overridden (by passing {sdpSemantics:'plan-b'} as the
Expand Down
1 change: 1 addition & 0 deletions third_party/blink/public/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ BLINK_COMMON_EXPORT extern const base::Feature
kPreviewsResourceLoadingHintsSpecificResourceTypes;
BLINK_COMMON_EXPORT extern const base::Feature
kPurgeRendererMemoryWhenBackgrounded;
BLINK_COMMON_EXPORT extern const base::Feature kWindowOpenNewPopupBehavior;
BLINK_COMMON_EXPORT extern const base::Feature kRTCUnifiedPlanByDefault;
BLINK_COMMON_EXPORT extern const base::Feature
kRTCDisallowPlanBOutsideDeprecationTrial;
Expand Down
27 changes: 20 additions & 7 deletions third_party/blink/renderer/core/loader/navigation_policy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ TEST_F(NavigationPolicyTest, NoOpener) {
NavigationPolicy policy;
} kCases[] = {
{"", kNavigationPolicyNewForegroundTab},
{"location,menubar,resizable,scrollbars,status",
kNavigationPolicyNewForegroundTab},
{"popup,location,menubar,resizable,scrollbars,status",
kNavigationPolicyNewPopup},
{"PoPuP,location,menubar,resizable,scrollbars,status",
kNavigationPolicyNewPopup},
{"popupFoo,location,menubar,resizable,scrollbars,status",
kNavigationPolicyNewForegroundTab},
{"something", kNavigationPolicyNewPopup},
{"something, something", kNavigationPolicyNewPopup},
{"notnoopener", kNavigationPolicyNewPopup},
Expand All @@ -249,10 +257,13 @@ TEST_F(NavigationPolicyTest, NoOpenerAndNoReferrer) {
{"", kNavigationPolicyNewForegroundTab},
{"noopener, noreferrer", kNavigationPolicyNewForegroundTab},
{"noopener, notreferrer", kNavigationPolicyNewPopup},
{"noopener, notreferrer, popup", kNavigationPolicyNewPopup},
{"notopener, noreferrer", kNavigationPolicyNewPopup},
{"something, noopener, noreferrer", kNavigationPolicyNewPopup},
{"noopener, noreferrer, something", kNavigationPolicyNewPopup},
{"noopener, something, noreferrer", kNavigationPolicyNewPopup},
{"notopener, noreferrer, popup", kNavigationPolicyNewPopup},
{"notopener, noreferrer, popup=0", kNavigationPolicyNewForegroundTab},
{"popup, noopener, noreferrer", kNavigationPolicyNewPopup},
{"noopener, noreferrer, popup", kNavigationPolicyNewPopup},
{"noopener, popup, noreferrer", kNavigationPolicyNewPopup},
{"NoOpEnEr, NoReFeRrEr", kNavigationPolicyNewForegroundTab},
};

Expand All @@ -270,12 +281,14 @@ TEST_F(NavigationPolicyTest, NoReferrer) {
NavigationPolicy policy;
} kCases[] = {
{"", kNavigationPolicyNewForegroundTab},
{"something", kNavigationPolicyNewPopup},
{"something, something", kNavigationPolicyNewPopup},
{"popup", kNavigationPolicyNewPopup},
{"popup, something", kNavigationPolicyNewPopup},
{"notreferrer", kNavigationPolicyNewPopup},
{"notreferrer,popup", kNavigationPolicyNewPopup},
{"notreferrer,popup=0", kNavigationPolicyNewForegroundTab},
{"noreferrer", kNavigationPolicyNewForegroundTab},
{"something, noreferrer", kNavigationPolicyNewPopup},
{"noreferrer, something", kNavigationPolicyNewPopup},
{"popup, noreferrer", kNavigationPolicyNewPopup},
{"noreferrer, popup", kNavigationPolicyNewPopup},
{"NoReFeRrEr", kNavigationPolicyNewForegroundTab},
};

Expand Down
36 changes: 18 additions & 18 deletions third_party/blink/renderer/core/page/create_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,8 @@ WebWindowFeatures GetWindowFeaturesFromString(const String& feature_string,
return window_features;

bool ui_features_were_disabled = false;

// See crbug.com/1192701 for details, but we're working on changing the
// popup-triggering conditions for window.open. This bool represents the "new"
// state after this change.
bool is_popup_with_new_behavior = false;

enum class PopupState { kUnknown, kPopup, kWindow };
PopupState popup_state = PopupState::kUnknown;
unsigned key_begin, key_end;
unsigned value_begin, value_end;

Expand Down Expand Up @@ -186,8 +182,9 @@ WebWindowFeatures GetWindowFeaturesFromString(const String& feature_string,
} else if (key_string == "width" || key_string == "innerwidth") {
window_features.width_set = true;
window_features.width = value;
// Width will be the only trigger for a popup.
is_popup_with_new_behavior = true;
} else if (key_string == "popup") {
// The 'popup' property explicitly triggers a popup.
popup_state = value ? PopupState::kPopup : PopupState::kWindow;
} else if (key_string == "height" || key_string == "innerheight") {
window_features.height_set = true;
window_features.height = value;
Expand Down Expand Up @@ -224,17 +221,20 @@ WebWindowFeatures GetWindowFeaturesFromString(const String& feature_string,
}
}

// Existing logic from NavigationPolicy::NavigationPolicyForCreateWindow():
if (dom_window && dom_window->document()) {
bool is_popup_with_current_behavior = !window_features.tool_bar_visible ||
!window_features.status_bar_visible ||
!window_features.scrollbars_visible ||
!window_features.menu_bar_visible ||
!window_features.resizable;
if (is_popup_with_current_behavior != is_popup_with_new_behavior) {
UseCounter::Count(dom_window->document(),
WebFeature::kWindowOpenNewPopupBehaviorMismatch);
if (RuntimeEnabledFeatures::WindowOpenNewPopupBehaviorEnabled()) {
bool is_popup = popup_state == PopupState::kPopup;
if (popup_state == PopupState::kUnknown) {
is_popup = !window_features.tool_bar_visible ||
!window_features.menu_bar_visible ||
!window_features.resizable ||
!window_features.scrollbars_visible ||
!window_features.status_bar_visible;
}
// If this is a popup, set all BarProps to false, and vice versa.
window_features.tool_bar_visible = !is_popup;
window_features.menu_bar_visible = !is_popup;
window_features.scrollbars_visible = !is_popup;
window_features.status_bar_visible = !is_popup;
}

if (window_features.noreferrer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,11 @@
depends_on: ["WebXRARModule"],
status: "stable",
},
// New behavior for window.open's interpretation of the windowFeatures parameter.
{
name: "WindowOpenNewPopupBehavior",
status: "stable",
},
// Extends window placement functionality for multi-screen devices. Also
// exposes requisite information about displays connected to the device.
{
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<script>
var channelName = window.name;
var channel = new BroadcastChannel(channelName);
const allBarProps = [
window.locationbar.visible,
window.menubar.visible,
window.personalbar.visible,
window.scrollbars.visible,
window.statusbar.visible,
window.toolbar.visible
];
const allTrue = allBarProps.every(x=>x);
const allFalse = allBarProps.every(x=>!x);
channel.postMessage({isPopup: allFalse, mixedState: !allTrue && !allFalse});

// Because messages are not delivered synchronously and because closing a
// browsing context prompts the eventual clearing of all task sources, this
// document should not be closed until the opener document has confirmed
// receipt.
channel.onmessage = function() {
window.close();
};
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title>Window.open popup behavior</title>
<link rel="author" href="masonf@chromium.org">
<link rel="help" href="https://html.spec.whatwg.org/multipage/window-object.html#window-open-steps">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
function testOne(windowFeatures, expectPopup) {
const windowName = Math.round(Math.random()*1e12);
const channel = new BroadcastChannel(windowName);
var w;
promise_test(() => {
return new Promise(resolve => {
w = window.open("support/window-open-popup-target.html", windowName, windowFeatures);
channel.addEventListener('message', resolve);
}).then(e => {
// Send message first so if asserts throw the popup is still closed
channel.postMessage(null);
assert_false(e.data.mixedState, "No mixed state");
assert_equals(e.data.isPopup, expectPopup, "Popup state");
});
},`${windowFeatures} (expect ${expectPopup ? "popup" : "tab"})`);
}

// No windowpreferences at all - tab.
testOne(undefined, /*expectPopup=*/false);

// Test all permutations of these properties:
const features = ["location","toolbar","menubar","resizable","scrollbars","status"];
const nProps = features.length;
const skip = 7; // To speed up the test, don't test all values. Skip 7 to pseudo-randomize.
for(let i=0;i<2**nProps;i+=skip) {
const enableVec = Number(i).toString(2).padStart(nProps,'0').split('').map(s => (s==="1"));
let windowFeatures = [];
for(let i=0;i<nProps;++i) {
if (enableVec[i])
windowFeatures.push(features[i] + "=yes");
}
windowFeatures = windowFeatures.join(',');
// We get a popup we got windowFeatures, and any of them are false:
const expectPopup = !!windowFeatures.length && (!(enableVec[0] || enableVec[1]) || !enableVec[2] || !enableVec[3] || !enableVec[4] || !enableVec[5]);
testOne(windowFeatures, expectPopup);
testOne(windowFeatures + ",noopener", /*expectPopup=*/false);
testOne(windowFeatures + ",noreferrer", /*expectPopup=*/false);
testOne(windowFeatures + ",popup", /*expectPopup=*/true); // "popup" feature = popup
testOne(windowFeatures + ",noopener,noreferrer,popup", /*expectPopup=*/false);
}
</script>
Loading

0 comments on commit cdad240

Please sign in to comment.