tor-browser

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

webchannel-favicons.html (4214B)


      1 <!DOCTYPE html>
      2 <!-- This Source Code Form is subject to the terms of the Mozilla Public
      3   - License, v. 2.0. If a copy of the MPL was not distributed with this
      4   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
      5 <html>
      6  <head>
      7    <meta charset="utf-8"/>
      8    <title></title>
      9  </head>
     10  <body>
     11    <script>
     12      "use strict";
     13      // This file is used to test the retrieval of favicons from a front-end.
     14      // Rather than using some kind of complicated message passing scheme to
     15      // talk to the test harness, modify the title of the page. The tests can
     16      // easily read the window title to see if things worked as expected.
     17 
     18      // The following are the titles used to communicate the page's state to the tests.
     19      // Keep these in sync with any tests that read them.
     20      const initialTitle = "Waiting for the favicons";
     21      const successTitle = "Favicons received";
     22      const errorTitle = "Error"
     23 
     24      document.title = initialTitle;
     25 
     26      // A function which requests the favicons from the browser using the GET_PAGE_FAVICONS
     27      // WebChannel message.
     28      function getPageFavicons() {
     29        return new Promise((resolve, reject) => {
     30          const requestId = 0;
     31 
     32 
     33          function listener(event) {
     34            window.removeEventListener(
     35              "WebChannelMessageToContent",
     36              listener,
     37              true
     38            );
     39 
     40            const { id, message } = event.detail;
     41 
     42            if (id !== "profiler.firefox.com" ||
     43              !message ||
     44              typeof message !== "object"
     45            ) {
     46              console.error(message);
     47              reject(new Error("A malformed WebChannel event was received."));
     48              return;
     49            }
     50 
     51            if (!message.type) {
     52              console.error(message);
     53              reject(new Error("The WebChannel event indicates an error."));
     54              return;
     55            }
     56 
     57            if (message.requestId === requestId) {
     58              if (message.type === "SUCCESS_RESPONSE") {
     59                resolve(message.response);
     60              } else {
     61                reject(new Error(message.error));
     62              }
     63            }
     64          }
     65 
     66          window.addEventListener("WebChannelMessageToContent", listener, true);
     67 
     68          window.dispatchEvent(
     69            new CustomEvent("WebChannelMessageToChrome", {
     70              detail: JSON.stringify({
     71                id: "profiler.firefox.com",
     72                message: { type: "GET_PAGE_FAVICONS", requestId, pageUrls: [
     73                  "https://profiler.firefox.com"
     74                ] },
     75              }),
     76            })
     77          );
     78        })
     79      }
     80 
     81      async function runTest() {
     82        try {
     83          // Get the favicons.
     84          const favicons = await getPageFavicons();
     85 
     86          // Check that the favicons are reasonable.
     87          // After the check, modify the title of the document, so the tab title gets
     88          // updated. This is an easy way to pass a message to the test script.
     89          if (
     90            favicons &&
     91            Array.isArray(favicons) &&
     92            favicons[0] &&
     93            favicons[0].data &&
     94            favicons[0].mimeType === "image/png"
     95          ) {
     96            // The favicon looks good!
     97            document.title = successTitle;
     98          } else {
     99            // The favicons don't look right, surface the error to the terminal.
    100            dump('The favicons were malformed in webchannel-favicons.html\n');
    101            dump(`Favicons: ${JSON.stringify(favicons)}\n`);
    102 
    103            // Also to the web console.
    104            console.error(
    105              "The favicons were malformed in webchannel-favicons.html",
    106              favicons
    107            );
    108 
    109            // Report the error to the tab title.
    110            document.title = errorTitle;
    111          }
    112        } catch (error) {
    113          // Catch any error and notify the test.
    114          document.title = errorTitle;
    115          dump('An error was caught in webchannel-favicons.html\n');
    116          dump(`${error}\n`);
    117          console.error(
    118            "An error was caught in webchannel-favicons.html",
    119            error
    120          );
    121        }
    122      }
    123 
    124      runTest();
    125    </script>
    126  </body>
    127 </html>