tor-browser

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

test_multi_sharedWorker_lifetimes_nobfcache.html (4681B)


      1 <!--
      2  Any copyright is dedicated to the Public Domain.
      3  http://creativecommons.org/publicdomain/zero/1.0/
      4 -->
      5 <!DOCTYPE HTML>
      6 <html>
      7  <head>
      8    <title>Test for SharedWorker</title>
      9    <script src="/tests/SimpleTest/SimpleTest.js"></script>
     10    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
     11      <script class="testbody" type="text/javascript">
     12        "use strict";
     13 
     14        function runTest() {
     15          const windowRelativeURL = "multi_sharedWorker_frame_nobfcache.html";
     16          const storedData = "0123456789abcdefghijklmnopqrstuvwxyz";
     17          var bc, bc2;
     18          bc = new BroadcastChannel("bugSharedWorkerLiftetime");
     19          bc.onmessage = (event) => {
     20            var msg = event.data;
     21            var command = msg.command;
     22            if (command == "finished") {
     23              bc.close();
     24              bc2.close();
     25              SimpleTest.finish();
     26            } else if (command == "debug") {
     27              info(msg.message);
     28            } else if (command == "fromWorker" || command == "loaded") {
     29              sendToGenerator(msg.workerMessage);
     30            }
     31          }
     32          bc2 = new BroadcastChannel("navigate");
     33          bc2.onmessage = (event) => {
     34            if (event.data.command == "loaded") {
     35              sendToGenerator();
     36            }
     37          }
     38 
     39          function postToWorker(workerMessage) {
     40            bc.postMessage({command: "postToWorker", workerMessage});
     41          }
     42 
     43          let testGenerator = (function*() {
     44            SimpleTest.waitForExplicitFinish();
     45 
     46            // Open the window
     47            window.open(windowRelativeURL, "testWin", "noopener");
     48            yield undefined;
     49 
     50            // Retrieve data from worker
     51            postToWorker({ command: "retrieve" });
     52 
     53            let msg = yield undefined;
     54 
     55            // Verify there is no data stored
     56            is(msg.type, "result", "Got a result message");
     57            is(msg.data, undefined, "No data stored yet");
     58 
     59            // Store data, and retrieve it
     60            postToWorker({ command: "store", data: storedData });
     61            postToWorker({ command: "retrieve" });
     62 
     63            msg = yield undefined;
     64            // Verify there is data stored
     65            is(msg.type, "result", "Got a result message");
     66            is(msg.data, storedData, "Got stored data");
     67 
     68 
     69            info("Navigating to a different page");
     70            // Current subpage should not go into bfcache because of the Cache-Control
     71            // headers we have set.
     72            bc.postMessage({command: "navigate", location: "navigate.html"});
     73            yield undefined;
     74 
     75            info("Navigating to " + windowRelativeURL);
     76            bc2.postMessage({command: "navigate", location: windowRelativeURL });
     77            yield undefined;
     78 
     79            postToWorker({ command: "retrieve" });
     80 
     81            msg = yield undefined;
     82            is(msg.type, "result", "Got a result message");
     83            is(msg.data, undefined, "No data stored");
     84 
     85            postToWorker({ command: "store", data: storedData });
     86            postToWorker({ command: "retrieve" });
     87 
     88            msg = yield undefined;
     89            is(msg.type, "result", "Got a result message");
     90            is(msg.data, storedData, "Got stored data");
     91 
     92            bc.postMessage({command: "finish"});
     93          })();
     94 
     95          let sendToGenerator = testGenerator.next.bind(testGenerator);
     96          testGenerator.next();
     97        }
     98 
     99        SimpleTest.waitForExplicitFinish();
    100        if (isXOrigin) {
    101          // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
    102          // Acquire storage access permission here so that the BroadcastChannel used to
    103          // communicate with the opened windows works in xorigin tests. Otherwise,
    104          // the iframe containing this page is isolated from first-party storage access,
    105          // which isolates BroadcastChannel communication.
    106          SpecialPowers.wrap(document).notifyUserGestureActivation();
    107          SpecialPowers.pushPrefEnv({
    108            set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
    109          }).then(() => {
    110            SpecialPowers.pushPermissions([{'type': 'storageAccessAPI', 'allow': 1, 'context': document}], () =>{
    111              SpecialPowers.wrap(document).requestStorageAccess().then(() => {
    112                runTest();
    113              }).then(() => {
    114                SpecialPowers.removePermission("3rdPartyStorage^http://mochi.test:8888", "http://mochi.xorigin-test:8888");
    115              });
    116            });
    117          });
    118        } else {
    119          runTest();
    120        }
    121 
    122      </script>
    123  </head>
    124  <body>
    125  </body>
    126 </html>