From ffc8a3adcc67f3df7126ba674d8a8e6ebf5ca0d1 Mon Sep 17 00:00:00 2001 From: Blink WPT Bot Date: Sat, 14 Sep 2024 08:30:56 -0700 Subject: [PATCH] Allow RemoteContextHelper to open windows from remote contexts (#48159) There will be multiple tests in followup CLs that will need to open windows from existing windows and frames. This will allow to simplify those tests. Example usage: https://crrev.com/c/5841474 Bug: 340606651 Bug: b/365144247 Change-Id: I47f4d8c473fe69bc72e2c9345c0d5825451431e4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5838815 Commit-Queue: Ari Chivukula Reviewed-by: Fergal Daly Reviewed-by: Ari Chivukula Cr-Commit-Position: refs/heads/main@{#1355331} Co-authored-by: Sandor Major --- .../addWindow-from-window.window.js | 16 ++++++++ .../resources/remote-context-helper.js | 39 ++++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 html/browsers/browsing-the-web/remote-context-helper-tests/addWindow-from-window.window.js diff --git a/html/browsers/browsing-the-web/remote-context-helper-tests/addWindow-from-window.window.js b/html/browsers/browsing-the-web/remote-context-helper-tests/addWindow-from-window.window.js new file mode 100644 index 00000000000000..85509a15b980bc --- /dev/null +++ b/html/browsers/browsing-the-web/remote-context-helper-tests/addWindow-from-window.window.js @@ -0,0 +1,16 @@ +// META: title=RemoteContextHelper with defaults +// META: script=/common/dispatcher/dispatcher.js +// META: script=/common/get-host-info.sub.js +// META: script=/common/utils.js +// META: script=/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js +// META: script=./resources/test-helper.js + +'use strict'; + +promise_test(async t => { + const rcHelper = new RemoteContextHelper(); + const main = await rcHelper.addWindow(); + const childWindow = await main.addWindow(); + await assertSimplestScriptRuns(childWindow); + await assertOriginIsAsExpected(childWindow, location.origin); +}); diff --git a/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js b/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js index aca83720254308..6c88b49fd4a0fd 100644 --- a/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js +++ b/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js @@ -320,12 +320,9 @@ url.searchParams.append('pipe', formattedHeaders.join('|')); } - function windowExecutorCreator({target = '_blank', features} = {}) { - return (url, documentContent) => { - if (url && url.substring(0, 5) == 'data:') { - throw new TypeError('Windows cannot use data: URLs.'); - } - + function windowExecutorCreator( + { target = '_blank', features } = {}, remoteContextWrapper) { + let openWindow = (url, target, features, documentContent) => { const w = window.open(url, target, features); if (documentContent) { w.document.open(); @@ -333,6 +330,19 @@ w.document.close(); } }; + + return (url, documentContent) => { + if (url && url.substring(0, 5) == 'data:') { + throw new TypeError('Windows cannot use data: URLs.'); + } + + if (remoteContextWrapper) { + return remoteContextWrapper.executeScript( + openWindow, [url, target, features, documentContent]); + } else { + openWindow(url, target, features, documentContent); + } + }; } function elementExecutorCreator( @@ -527,6 +537,23 @@ }); } + /** + * Opens a window from the remote context. @see createContext for + * @param {RemoteContextConfig|object} [extraConfig] + * @param {Object} [options] + * @param {string} [options.target] Passed to `window.open` as the + * 2nd argument + * @param {string} [options.features] Passed to `window.open` as the + * 3rd argument + * @returns {Promise} The remote context. + */ + addWindow(extraConfig, options) { + return this.helper.createContext({ + executorCreator: windowExecutorCreator(options, this), + extraConfig, + }); + } + /** * Adds a dedicated worker to the current document. * @param {string|null} [globalVariable] The name of the global variable to