tor-browser

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

partitioned-getRegistrations.tentative.https.html (3881B)


      1 <!DOCTYPE html>
      2 <meta charset="utf-8"/>
      3 <title>Service Worker: Partitioned Service Workers</title>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="resources/test-helpers.sub.js"></script>
      7 <script src="/common/get-host-info.sub.js"></script>
      8 <script src="resources/partitioned-utils.js"></script>
      9 
     10 <body>
     11 This test loads a SW in a first-party context and gets the SW's (randomly)
     12 generated ID. It does the same thing for the SW but in a third-party context
     13 and then confirms that the IDs are different.
     14 
     15 <script>
     16 promise_test(async t => {
     17  const script = './resources/partitioned-storage-sw.js'
     18  const scope = './resources/partitioned-'
     19  const absoluteScope = new URL(scope, window.location).href;
     20 
     21  // Add service worker to this 1P context.
     22  const reg = await service_worker_unregister_and_register(t, script, scope);
     23  t.add_cleanup(() => reg.unregister());
     24  await wait_for_state(t, reg.installing, 'activated');
     25 
     26  // Register the message listener.
     27  self.addEventListener('message', messageEventHandler);
     28 
     29  // Open an iframe that will create a promise within the SW.
     30  // The query param is there to track which request the service worker is
     31  // handling.
     32  //
     33  // This promise is necessary to prevent the service worker from being
     34  // shutdown during the test which would cause a new ID to be generated
     35  // and thus invalidate the test.
     36  const wait_frame_url = new URL(
     37    './resources/partitioned-waitUntilResolved.fakehtml?From1pFrame',
     38    self.location);
     39 
     40  // We don't really need the data the SW sent us from this request
     41  // but we can use the ID to confirm the SW wasn't shut down during the
     42  // test.
     43  const wait_frame_1p_data = await loadAndReturnSwData(t, wait_frame_url,
     44                                                       'iframe');
     45 
     46  // Now we need to create a third-party iframe that will send us its SW's
     47  // ID.
     48  const third_party_iframe_url = new URL(
     49    './resources/partitioned-service-worker-third-party-iframe-getRegistrations.html',
     50    get_host_info().HTTPS_ORIGIN + self.location.pathname);
     51 
     52  // Create the 3p window (which will in turn create the iframe with the SW)
     53  // and await on its data.
     54  const frame_3p_ID = await loadAndReturnSwData(t, third_party_iframe_url,
     55    'window');
     56 
     57  // Now get this frame's SW's ID.
     58  const frame_1p_ID_promise = makeMessagePromise();
     59 
     60  const retrieved_registrations =
     61        await navigator.serviceWorker.getRegistrations();
     62  // It's possible that other tests have left behind other service workers.
     63  // This steps filters those other SWs out.
     64  const filtered_registrations =
     65    retrieved_registrations.filter(reg => reg.scope == absoluteScope);
     66 
     67  // Register a listener on the service worker container and then forward to
     68  // the self event listener so we can reuse the existing message promise
     69  // function.
     70  navigator.serviceWorker.addEventListener('message', evt => {
     71    self.postMessage(evt.data, '*');
     72  });
     73 
     74  filtered_registrations[0].active.postMessage({type: "get-id"});
     75 
     76  const  frame_1p_ID = await frame_1p_ID_promise;
     77 
     78  // First check that the SW didn't shutdown during the run of the test.
     79  // (Note: We're not using assert_equals because random values make it
     80  // difficult to use a test expectations file.)
     81  assert_true(wait_frame_1p_data.ID === frame_1p_ID.ID,
     82    "1p SW didn't shutdown");
     83  // Now check that the 1p and 3p IDs differ.
     84  assert_false(frame_1p_ID.ID === frame_3p_ID.ID,
     85    "1p SW ID matches 3p SW ID");
     86 
     87  // Finally, for clean up, resolve the SW's promise so it stops waiting.
     88  const resolve_frame_url = new URL(
     89    './resources/partitioned-resolve.fakehtml?From1pFrame', self.location);
     90 
     91  // We don't care about the data.
     92  await loadAndReturnSwData(t, resolve_frame_url, 'iframe');
     93 
     94 }, "ServiceWorker's getRegistrations() is partitioned");
     95 
     96 
     97 </script>
     98 
     99 </body>