Skip to content

Commit

Permalink
Shared Storage: Add non-live dataOption to createWorklet for use counter
Browse files Browse the repository at this point in the history
We proposed a breaking change to sharedStorage.createWorklet() in
WICG/shared-storage#158.

We add a use counter for how frequently createWorklet is called in a
non-forward-compatible way according to the proposal (i.e. with a
cross-origin script but without the option dataOrigin: "script-
origin"). This will help us determine what the impact of this change
would be.

Bug: 348445878
Change-Id: I3b882a9ec859beb5265ba31d3169fb5e1239cac4
  • Loading branch information
pythagoraskitty authored and chromium-wpt-export-bot committed Jun 21, 2024
1 parent 9228c24 commit cf9cea1
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/shared-storage/resources/util.js"></script>
<script src="/shared-storage/resources/util.sub.js"></script>
<script src="/fenced-frame/resources/utils.js"></script>

<body>
<script>
'use strict';

async_test(t => {
testCreateWorkletWithDataOption(
t, "context-origin", "key0", "value0", /*is_same_origin_script=*/false,
/*expect_success=*/true);
}, 'For cross-origin createWorklet with dataOrigin option "context-origin", '
+ 'there is no error.');

async_test(t => {
testCreateWorkletWithDataOption(
t, "script-origin", "key0", "value0", /*is_same_origin_script=*/false,
/*expect_success=*/true);
}, 'For cross-origin createWorklet with dataOrigin option "script-origin", '
+ 'there is no error.');

async_test(t => {
testCreateWorkletWithDataOption(
t, "invalid", "key0", "value0", /*is_same_origin_script=*/false,
/*expect_success=*/false);
}, 'For cross-origin createWorklet with dataOrigin option "invalid", '
+ 'there is a TypeError thrown.');

</script>
</body>
42 changes: 42 additions & 0 deletions shared-storage/resources/util.sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,45 @@ async function loadNestedSharedStorageFrameInNewFrame(data) {
await windowPromise;
return {frame: frame, nestedFrame: nestedFrame, nestedFrameUrl: nestedSrc};
}

async function testCreateWorkletWithDataOption(
test, data_origin, key, value, is_same_origin_script, expect_success) {
const sameOrigin = location.origin;
const crossOrigin = 'https://{{domains[www]}}:{{ports[https][0]}}';
const sameOriginScriptUrl = `/shared-storage/resources/simple-module.js`;
const scriptOrigin = is_same_origin_script ? sameOrigin : crossOrigin;
const scriptUrl = is_same_origin_script ? sameOriginScriptUrl :
crossOrigin + sameOriginScriptUrl;

try {
// Currently the `dataOrigin` option is not hooked up.
const worklet = await sharedStorage.createWorklet(
scriptUrl, {credentials: 'omit', dataOrigin: data_origin});

const ancestor_key = token();
let url0 =
generateURL('/shared-storage/resources/frame0.html', [ancestor_key]);

let select_url_result =
await worklet.selectURL('test-url-selection-operation', [{url: url0}], {
data: {'mockResult': 0, 'setKey': key, 'setValue': value},
resolveToConfig: true,
keepAlive: true
});

assert_true(validateSelectURLResult(select_url_result, true));
attachFencedFrame(select_url_result, 'opaque-ads');
const result0 = await nextValueFromServer(ancestor_key);
assert_equals(result0, 'frame0_loaded');

await verifyKeyValueForOrigin(key, value, scriptOrigin);
await deleteKeyForOrigin(key, scriptOrigin);
assert_true(expect_success, 'no error caught even though one was expected');
} catch (e) {
assert_false(
expect_success, 'expected success but error thrown: ' + e.toString());
assert_equals(e.name, 'TypeError');
} finally {
test.done();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/shared-storage/resources/util.js"></script>
<script src="/shared-storage/resources/util.sub.js"></script>
<script src="/fenced-frame/resources/utils.js"></script>

<body>
<script>
'use strict';

async_test(t => {
testCreateWorkletWithDataOption(
t, "context-origin", "key0", "value0", /*is_same_origin_script=*/true,
/*expect_success=*/true);
}, 'For same-origin createWorklet with dataOrigin option "context-origin", '
+ 'there is no error.');

async_test(t => {
testCreateWorkletWithDataOption(
t, "script-origin", "key0", "value0", /*is_same_origin_script=*/true,
/*expect_success=*/true);
}, 'For same-origin createWorklet with dataOrigin option "script-origin", '
+ 'there is no error.');

async_test(t => {
testCreateWorkletWithDataOption(
t, "invalid", "key0", "value0", /*is_same_origin_script=*/true,
/*expect_success=*/false);
}, 'For same-origin createWorklet with dataOrigin option "invalid", '
+ 'there is a TypeError thrown.');

</script>
</body>

0 comments on commit cf9cea1

Please sign in to comment.