tor-browser

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

common.js (3059B)


      1 const url_base = "/feature-policy/experimental-features/resources/";
      2 window.messageResponseCallback = null;
      3 
      4 function setFeatureState(iframe, feature, origins) {
      5    iframe.setAttribute("allow", `${feature} ${origins};`);
      6 }
      7 
      8 // Returns a promise which is resolved when the <iframe> is navigated to |url|
      9 // and "load" handler has been called.
     10 function loadUrlInIframe(iframe, url) {
     11  return new Promise((resolve) => {
     12    iframe.addEventListener("load", resolve);
     13    iframe.src = url;
     14  });
     15 }
     16 
     17 // Posts |message| to |target| and resolves the promise with the response coming
     18 // back from |target|.
     19 function sendMessageAndGetResponse(target, message) {
     20  return new Promise((resolve) => {
     21    window.messageResponseCallback = resolve;
     22    target.postMessage(message, "*");
     23  });
     24 }
     25 
     26 
     27 function onMessage(e) {
     28  if (window.messageResponseCallback) {
     29    window.messageResponseCallback(e.data);
     30    window.messageResponseCallback = null;
     31  }
     32 }
     33 
     34 window.addEventListener("message", onMessage);
     35 
     36 // Waits for |load_timeout| before resolving the promise. It will resolve the
     37 // promise sooner if a message event with |e.data.id| of |id| is received.
     38 // In such a case the response is the contents of the message |e.data.contents|.
     39 // Otherwise, returns false (when timeout occurs).
     40 function waitForMessageOrTimeout(t, id, load_timeout) {
     41  return new Promise((resolve) => {
     42      window.addEventListener(
     43        "message",
     44        (e) => {
     45          if (!e.data || e.data.id !== id)
     46            return;
     47          resolve(e.data.contents);
     48        }
     49      );
     50      t.step_timeout(() => { resolve(false); }, load_timeout);
     51  });
     52 }
     53 
     54 function createIframe(container, attributes) {
     55  var new_iframe = document.createElement("iframe");
     56  for (attr_name in attributes)
     57    new_iframe.setAttribute(attr_name, attributes[attr_name]);
     58  container.appendChild(new_iframe);
     59  return new_iframe;
     60 }
     61 
     62 // Returns a promise which is resolved when |load| event is dispatched for |e|.
     63 function wait_for_load(e) {
     64  return new Promise((resolve) => {
     65    e.addEventListener("load", resolve);
     66  });
     67 }
     68 
     69 setup(() => {
     70  window.reporting_observer_instance = new ReportingObserver((reports, observer) => {
     71    if (window.reporting_observer_callback) {
     72      reports.forEach(window.reporting_observer_callback);
     73    }
     74  }, {types: ["permissions-policy-violation"]});
     75  window.reporting_observer_instance.observe();
     76  window.reporting_observer_callback = null;
     77 });
     78 
     79 // Waits for a violation in |feature| and source file containing |file_name|.
     80 function wait_for_violation_in_file(feature, file_name) {
     81  return new Promise( (resolve) => {
     82    assert_equals(null, window.reporting_observer_callback);
     83    window.reporting_observer_callback = (report) => {
     84        var feature_match = (feature === report.body.featureId);
     85        var file_name_match =
     86            !file_name ||
     87            (report.body.sourceFile.indexOf(file_name) !== -1);
     88        if (feature_match && file_name_match) {
     89          window.reporting_observer_callback = null;
     90          resolve(report);
     91        }
     92    };
     93  });
     94 }