tor-browser

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

background.js (4169B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 const port = browser.runtime.connectNative("browser");
      6 
      7 const APIS = {
      8  AddHistogram({ id, value }) {
      9    browser.test.addHistogram(id, value);
     10  },
     11  Eval({ code }) {
     12    // eslint-disable-next-line no-eval
     13    return eval(`(async () => {
     14      ${code}
     15    })()`);
     16  },
     17  GetRequestedLocales() {
     18    return browser.test.getRequestedLocales();
     19  },
     20  ClearUserPref({ pref }) {
     21    return browser.test.clearUserPref(pref);
     22  },
     23  GetLinkColor({ tab, selector }) {
     24    return browser.test.getLinkColor(tab.id, selector);
     25  },
     26  GetPidForTab({ tab }) {
     27    return browser.test.getPidForTab(tab.id);
     28  },
     29  WaitForContentTransformsReceived({ tab }) {
     30    return browser.test.waitForContentTransformsReceived(tab.id);
     31  },
     32  GetProfilePath() {
     33    return browser.test.getProfilePath();
     34  },
     35  GetAllBrowserPids() {
     36    return browser.test.getAllBrowserPids();
     37  },
     38  KillContentProcess({ pid }) {
     39    return browser.test.killContentProcess(pid);
     40  },
     41  GetPrefs({ prefs }) {
     42    return browser.test.getPrefs(prefs);
     43  },
     44  GetActive({ tab }) {
     45    return browser.test.getActive(tab.id);
     46  },
     47  RemoveAllCertOverrides() {
     48    browser.test.removeAllCertOverrides();
     49  },
     50  RestorePrefs({ oldPrefs }) {
     51    return browser.test.restorePrefs(oldPrefs);
     52  },
     53  SetPrefs({ oldPrefs, newPrefs }) {
     54    return browser.test.setPrefs(oldPrefs, newPrefs);
     55  },
     56  SetResolutionAndScaleTo({ tab, resolution }) {
     57    return browser.test.setResolutionAndScaleTo(tab.id, resolution);
     58  },
     59  FlushApzRepaints({ tab }) {
     60    return browser.test.flushApzRepaints(tab.id);
     61  },
     62  ZoomToFocusedInput({ tab }) {
     63    return browser.test.zoomToFocusedInput(tab.id);
     64  },
     65  PromiseAllPaintsDone({ tab }) {
     66    return browser.test.promiseAllPaintsDone(tab.id);
     67  },
     68  UsingGpuProcess() {
     69    return browser.test.usingGpuProcess();
     70  },
     71  KillGpuProcess() {
     72    return browser.test.killGpuProcess();
     73  },
     74  CrashGpuProcess() {
     75    return browser.test.crashGpuProcess();
     76  },
     77  ClearHSTSState() {
     78    return browser.test.clearHSTSState();
     79  },
     80  IsSessionHistoryInParentRunning() {
     81    return browser.test.isSessionHistoryInParentRunning();
     82  },
     83  IsFissionRunning() {
     84    return browser.test.isFissionRunning();
     85  },
     86  TriggerCookieBannerDetected({ tab }) {
     87    return browser.test.triggerCookieBannerDetected(tab.id);
     88  },
     89  TriggerCookieBannerHandled({ tab }) {
     90    return browser.test.triggerCookieBannerHandled(tab.id);
     91  },
     92  TriggerTranslationsOffer({ tab }) {
     93    return browser.test.triggerTranslationsOffer(tab.id);
     94  },
     95  TriggerLanguageStateChange({ tab, languageState }) {
     96    return browser.test.triggerLanguageStateChange(tab.id, languageState);
     97  },
     98  SetHandlingUserInput({ tab, handlingUserInput }) {
     99    return browser.test.setHandlingUserInput(tab.id, handlingUserInput);
    100  },
    101  GetWebExtensionSchemaPermissionNames({ typeNames }) {
    102    return browser.test.getWebExtensionsSchemaPermissionNames(typeNames);
    103  },
    104  TeardownAlertsService() {
    105    return browser.test.teardownAlertsService();
    106  },
    107  NotifyUserGestureActivation({ tab }) {
    108    return browser.test.notifyUserGestureActivation(tab.id);
    109  },
    110 };
    111 
    112 port.onMessage.addListener(async message => {
    113  const impl = APIS[message.type];
    114  apiCall(message, impl);
    115 });
    116 
    117 browser.runtime.onConnect.addListener(contentPort => {
    118  contentPort.onMessage.addListener(message => {
    119    message.args.tab = contentPort.sender.tab;
    120 
    121    const impl = APIS[message.type];
    122    apiCall(message, impl);
    123  });
    124 });
    125 
    126 function apiCall(message, impl) {
    127  const { id, args } = message;
    128  try {
    129    sendResponse(id, impl(args));
    130  } catch (error) {
    131    sendResponse(id, Promise.reject(error));
    132  }
    133 }
    134 
    135 function sendResponse(id, response) {
    136  Promise.resolve(response).then(
    137    value => sendSyncResponse(id, value),
    138    reason => sendSyncResponse(id, null, reason)
    139  );
    140 }
    141 
    142 function sendSyncResponse(id, response, exception) {
    143  port.postMessage({
    144    id,
    145    response: JSON.stringify(response),
    146    exception: exception && exception.toString(),
    147  });
    148 }