tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

web-locks-window-modifier-method.tentative.https.sub.html (2489B)


      1 <!doctype html>
      2 <script src="/resources/testharness.js"></script>
      3 <script src="/resources/testharnessreport.js"></script>
      4 <script src="/common/utils.js"></script>
      5 <script src="/shared-storage/resources/util.js"></script>
      6 <script src="/fenced-frame/resources/utils.js"></script>
      7 
      8 <body>
      9 <script>
     10 'use strict';
     11 
     12 promise_test(async t => {
     13  let worklet = await sharedStorage.createWorklet('resources/simple-module.js');
     14 
     15  const ancestor_key = token();
     16  let url0 = generateURL("/shared-storage/resources/frame0.html",
     17                         [ancestor_key]);
     18  let url1 = generateURL("/shared-storage/resources/frame1.html",
     19                         [ancestor_key]);
     20 
     21  // Invoke `selectURL()` to perform the following steps:
     22  // 1. Acquires the lock.
     23  // 2. Reads the current value at the given key.
     24  // 3. Waits for 500ms.
     25  // 4. Sets the shared storage value to the read value appended with the given letter.
     26  // 5. Releases the lock.
     27  //
     28  // After 100ms, invoke `sharedStorage.append()` with the same lock and the
     29  // same letter to append.
     30  //
     31  // Expected behavior: After both of them finish, the value at the given key
     32  // should contain the letter repeated twice.
     33  //
     34  // This demonstrates that the `withLock` option is effective, preventing the
     35  // `append()` method interfering with the "get and set" operation. If the lock
     36  // were not used, the final value would likely be a single letter.
     37  //
     38  // Note: This test remains valid even if the `append()` call happens outside
     39  // the critical section protected by the lock within the worklet. The test
     40  // effectively demonstrates mutual exclusion as long as there's a reasonable
     41  // chance for `append()` to occur while the worklet is still running.
     42  let select_url_result = await worklet.selectURL(
     43      "get-wait-set-within-lock",
     44      [{url: url0}, {url: url1}],
     45      {data: {'key': 'key',
     46              'lock_name': 'lock1',
     47              'append_letter': 'a'},
     48      resolveToConfig: true});
     49 
     50  // Busy wait for 100ms.
     51  const startWaitTime = Date.now();
     52  while (Date.now() - startWaitTime < 100) {}
     53 
     54  sharedStorage.append('key', 'a', {withLock: 'lock1'});
     55 
     56  attachFencedFrame(select_url_result, 'opaque-ads');
     57  const result = await nextValueFromServer(ancestor_key);
     58  assert_equals(result, "frame1_loaded");
     59 
     60  await verifyKeyValueForOrigin('key', 'aa', location.origin);
     61 
     62  await deleteKeyForOrigin('key', location.origin);
     63 }, 'Test for withLock option in a Window context');
     64 
     65 </script>
     66 </body>