Skip to content

Commit

Permalink
Allow RemoteContextHelper to open windows from remote contexts (#48159)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Fergal Daly <[email protected]>
Reviewed-by: Ari Chivukula <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1355331}

Co-authored-by: Sandor Major <[email protected]>
  • Loading branch information
chromium-wpt-export-bot and sandormajor authored Sep 14, 2024
1 parent 307a251 commit ffc8a3a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,29 @@
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();
w.document.write(documentContent);
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(
Expand Down Expand Up @@ -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<RemoteContextWrapper>} 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
Expand Down

0 comments on commit ffc8a3a

Please sign in to comment.