tor-browser

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

web-locks.tentative.https.sub.html (2447B)


      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 worklet1 = await sharedStorage.createWorklet('resources/simple-module.js');
     14  let worklet2 = await sharedStorage.createWorklet('resources/simple-module.js');
     15 
     16  const ancestor_key1 = token();
     17  let url1_0 = generateURL("/shared-storage/resources/frame0.html",
     18                         [ancestor_key1]);
     19  let url1_1 = generateURL("/shared-storage/resources/frame1.html",
     20                         [ancestor_key1]);
     21 
     22  const ancestor_key2 = token();
     23  let url2_0 = generateURL("/shared-storage/resources/frame0.html",
     24                         [ancestor_key2]);
     25  let url2_1 = generateURL("/shared-storage/resources/frame1.html",
     26                         [ancestor_key2]);
     27 
     28  // Two `selectURL()`s run in parallel. Each performs the following steps:
     29  // 1. Acquires the lock.
     30  // 2. Reads the current value of the given key.
     31  // 3. Waits for 100ms.
     32  // 4. Increments the value.
     33  // 5. Releases the lock.
     34  //
     35  // Expected behavior: After both operations finish, the value of the given key
     36  // should be 2.
     37  //
     38  // This demonstrates that the lock is effective, preventing the
     39  // "get and increment" operations from interleaving. If the lock were not
     40  // used, both worklets would likely read 0 and set the value to 1.
     41 
     42  let select_url_result1 = await worklet1.selectURL(
     43      "get-wait-increment-within-lock", [{url: url1_0}, {url: url1_1}],
     44      {data: {'key': 'key'}, resolveToConfig: true});
     45 
     46  let select_url_result2 = await worklet2.selectURL(
     47      "get-wait-increment-within-lock", [{url: url2_0}, {url: url2_1}],
     48      {data: {'key': 'key'}, resolveToConfig: true});
     49 
     50  attachFencedFrame(select_url_result1, 'opaque-ads');
     51  const result1 = await nextValueFromServer(ancestor_key1);
     52  assert_equals(result1, "frame1_loaded");
     53 
     54  attachFencedFrame(select_url_result2, 'opaque-ads');
     55  const result2 = await nextValueFromServer(ancestor_key2);
     56  assert_equals(result2, "frame1_loaded");
     57 
     58  await verifyKeyValueForOrigin('key', '2', location.origin);
     59 
     60  await deleteKeyForOrigin('key', location.origin);
     61 }, 'Basic test for Web Locks API in the shared storage worklet');
     62 
     63 </script>
     64 </body>