tor-browser

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

head.js (5112B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 let gContentPrefs = Cc["@mozilla.org/content-pref/service;1"].getService(
      5  Ci.nsIContentPrefService2
      6 );
      7 
      8 let gLoadContext = Cu.createLoadContext();
      9 
     10 registerCleanupFunction(async function () {
     11  await new Promise(resolve => {
     12    gContentPrefs.removeByName(window.FullZoom.name, gLoadContext, {
     13      handleResult() {},
     14      handleCompletion() {
     15        resolve();
     16      },
     17    });
     18  });
     19 });
     20 
     21 var FullZoomHelper = {
     22  async changeDefaultZoom(newZoom) {
     23    let nonPrivateLoadContext = Cu.createLoadContext();
     24    /* Because our setGlobal function takes in a browsing context, and
     25     * because we want to keep this property consistent across both private
     26     * and non-private contexts, we crate a non-private context and use that
     27     * to set the property, regardless of our actual context.
     28     */
     29 
     30    let parsedZoomValue = parseFloat((parseInt(newZoom) / 100).toFixed(2));
     31    await new Promise(resolve => {
     32      gContentPrefs.setGlobal(
     33        FullZoom.name,
     34        parsedZoomValue,
     35        nonPrivateLoadContext,
     36        {
     37          handleCompletion() {
     38            resolve();
     39          },
     40        }
     41      );
     42    });
     43    // The zoom level is used to update the commands associated with
     44    // increasing, decreasing or resetting the Zoom levels. There are
     45    // a series of async things we need to wait for (writing the content
     46    // pref to the database, and then reading that content pref back out
     47    // again and reacting to it), so waiting for the zoom level to reach
     48    // the expected level is actually simplest to make sure we're okay to
     49    // proceed.
     50    await TestUtils.waitForCondition(() => {
     51      return ZoomManager.zoom == parsedZoomValue;
     52    });
     53  },
     54 
     55  async getGlobalValue() {
     56    return new Promise(resolve => {
     57      let cachedVal = parseFloat(
     58        gContentPrefs.getCachedGlobal(FullZoom.name, gLoadContext)
     59      );
     60      if (cachedVal) {
     61        // We've got cached information, though it may be we've cached
     62        // an undefined value, or the cached info is invalid. To ensure
     63        // a valid return, we opt to return the default 1.0 in the
     64        // undefined and invalid cases.
     65        resolve(parseFloat(cachedVal.value) || 1.0);
     66        return;
     67      }
     68      let value = 1.0;
     69      gContentPrefs.getGlobal(FullZoom.name, gLoadContext, {
     70        handleResult(pref) {
     71          if (pref.value) {
     72            value = parseFloat(pref.value);
     73          }
     74        },
     75        handleCompletion() {
     76          resolve(value);
     77        },
     78        handleError(error) {
     79          console.error(error);
     80        },
     81      });
     82    });
     83  },
     84 
     85  waitForLocationChange: function waitForLocationChange() {
     86    return new Promise(resolve => {
     87      Services.obs.addObserver(function obs(subj, topic) {
     88        Services.obs.removeObserver(obs, topic);
     89        resolve();
     90      }, "browser-fullZoom:location-change");
     91    });
     92  },
     93 
     94  selectTabAndWaitForLocationChange: function selectTabAndWaitForLocationChange(
     95    tab
     96  ) {
     97    if (!tab) {
     98      throw new Error("tab must be given.");
     99    }
    100    if (gBrowser.selectedTab == tab) {
    101      return Promise.resolve();
    102    }
    103 
    104    return Promise.all([
    105      BrowserTestUtils.switchTab(gBrowser, tab),
    106      this.waitForLocationChange(),
    107    ]);
    108  },
    109 
    110  removeTabAndWaitForLocationChange: function removeTabAndWaitForLocationChange(
    111    tab
    112  ) {
    113    tab = tab || gBrowser.selectedTab;
    114    let selected = gBrowser.selectedTab == tab;
    115    gBrowser.removeTab(tab);
    116    if (selected) {
    117      return this.waitForLocationChange();
    118    }
    119    return Promise.resolve();
    120  },
    121 
    122  load: function load(tab, url) {
    123    return new Promise(resolve => {
    124      let didLoad = false;
    125      let didZoom = false;
    126 
    127      BrowserTestUtils.loadURIString({
    128        browser: tab.linkedBrowser,
    129        uriString: url,
    130      }).then(() => {
    131        didLoad = true;
    132        if (didZoom) {
    133          resolve();
    134        }
    135      }, true);
    136 
    137      this.waitForLocationChange().then(function () {
    138        didZoom = true;
    139        if (didLoad) {
    140          resolve();
    141        }
    142      });
    143    });
    144  },
    145 
    146  zoomTest: function zoomTest(tab, val, msg) {
    147    is(ZoomManager.getZoomForBrowser(tab.linkedBrowser), val, msg);
    148  },
    149 
    150  BACK: 0,
    151  FORWARD: 1,
    152  navigate: function navigate(direction) {
    153    return new Promise(resolve => {
    154      let didPs = false;
    155      let didZoom = false;
    156 
    157      BrowserTestUtils.waitForContentEvent(
    158        gBrowser.selectedBrowser,
    159        "pageshow",
    160        true
    161      ).then(() => {
    162        didPs = true;
    163        if (didZoom) {
    164          resolve();
    165        }
    166      });
    167 
    168      if (direction == this.BACK) {
    169        gBrowser.goBack(false);
    170      } else if (direction == this.FORWARD) {
    171        gBrowser.goForward();
    172      }
    173 
    174      this.waitForLocationChange().then(function () {
    175        didZoom = true;
    176        if (didPs) {
    177          resolve();
    178        }
    179      });
    180    });
    181  },
    182 
    183  failAndContinue: function failAndContinue(func) {
    184    return function (err) {
    185      console.error(err);
    186      ok(false, err);
    187      func();
    188    };
    189  },
    190 };