Skip to content

Commit

Permalink
[shared storage] Implement with_lock option for methods from response…
Browse files Browse the repository at this point in the history
… headers

Explainer PR(s):
- WICG/shared-storage#199
- WICG/shared-storage#205

How:
- Add the parameter parsing logic in the network service, and send the
  final request to the browser process's central
  SharedStorageLockManager component.
- Since SharedStorageLockManager::SharedStorageUpdate() hides the
  exact database result, we update observer's OnMethodFinished()'s
  parameters accordingly. In the test observer, the error message is
  further transformed into a boolean 'success' result. This callback
  is only used in tests and it's not important to assert the detailed
  result anyway.
- A new test, `SharedStorageHeaderObserverTest.Append_NoCapacity`,
  has been added to demonstrate the case where `SharedStorageUpdate`
  encounters an error.

Bug: 373899210
Change-Id: I4588c83174d465e7248cc1f41c9b7a693aa326f4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6042601
Reviewed-by: Gary Kacmarcik <garykac@chromium.org>
Commit-Queue: Yao Xiao <yaoxia@chromium.org>
Reviewed-by: Cammie Smith Barnes <cammie@chromium.org>
Reviewed-by: Kenichi Ishibashi <bashi@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: Joe Mason <joenotcharles@google.com>
Reviewed-by: Rakina Zata Amni <rakina@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1388298}
  • Loading branch information
yaoxiachromium authored and chromium-wpt-export-bot committed Nov 26, 2024
1 parent 7a76ca9 commit 7658fed
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!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="/fenced-frame/resources/utils.js"></script>

<body>
<script>
'use strict';

promise_test(async t => {
let worklet = await sharedStorage.createWorklet('resources/simple-module.js');

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

// Invoke `selectURL()` to perform the following steps:
// 1. Acquires the lock.
// 2. Reads the current value at the given key.
// 3. Waits for 500ms.
// 4. Sets the shared storage value to the read value appended with the given letter.
// 5. Releases the lock.
//
// After 100ms, send a fetch() request. The response triggers the `append`
// method with the same lock and the same letter.
//
// Expected behavior: After both of them finish, the value at the given key
// should contain the letter repeated twice.
//
// This demonstrates that the `withLock` option is effective, preventing the
// `append` method interfering with the "get and set" operation. If the lock
// were not used, the final value would likely be a single letter.
//
// Note: This test remains valid even if the header `append` happens outside
// the critical section protected by the lock within the worklet. The test
// effectively demonstrates mutual exclusion as long as there's a reasonable
// chance for `append` to occur while the worklet is still running.
let select_url_result = await worklet.selectURL(
"get-wait-set-within-lock",
[{url: url0}, {url: url1}],
{data: {'key': 'key',
'lock_name': 'lock1',
'append_letter': 'a'},
resolveToConfig: true});

// Busy wait for 100ms.
const startWaitTime = Date.now();
while (Date.now() - startWaitTime < 100) {}

// Send a fetch() request. The response triggers the `append` method with the
// same lock and the same letter.
const rawUpdatesHeader = 'append;key=key;value=a;with_lock=lock1';
const updatesHeader = encodeURIComponent(rawUpdatesHeader);
const updatesUrl =
`/shared-storage/resources/shared-storage-write.py?write=${updatesHeader}`;

let response = await fetch(updatesUrl, {sharedStorageWritable: true});
let sharedStorageWritableHeader = await response.text();
assert_equals(sharedStorageWritableHeader, "?1");

attachFencedFrame(select_url_result, 'opaque-ads');
const result = await nextValueFromServer(ancestor_key);
assert_equals(result, "frame1_loaded");

await verifyKeyValueForOrigin('key', 'aa', location.origin);

await deleteKeyForOrigin('key', location.origin);
}, 'Test for withLock option in the Shared-Storage-Write response header');

</script>
</body>

0 comments on commit 7658fed

Please sign in to comment.