tor-browser

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

firebase.js (2343B)


      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 "use strict";
      6 
      7 /**
      8 * Bug 1767407 - Shim Firebase
      9 *
     10 * Sites relying on firebase-messaging.js will break in Private
     11 * browsing mode because it assumes that they require service
     12 * workers and indexedDB, when they generally do not.
     13 */
     14 
     15 /* globals cloneInto */
     16 
     17 (function () {
     18  const win = window.wrappedJSObject;
     19  const emptyObj = new win.Object();
     20  const emptyArr = new win.Array();
     21  const emptyMsg = cloneInto({ message: "" }, window);
     22  const noOpFn = cloneInto(function () {}, window, { cloneFunctions: true });
     23 
     24  if (!win.indexedDB) {
     25    const idb = {
     26      open: () => win.Promise.reject(emptyMsg),
     27    };
     28 
     29    Object.defineProperty(win, "indexedDB", {
     30      value: cloneInto(idb, window, { cloneFunctions: true }),
     31    });
     32  }
     33 
     34  // bug 1778993
     35  for (const name of [
     36    "IDBCursor",
     37    "IDBDatabase",
     38    "IDBIndex",
     39    "IDBOpenDBRequest",
     40    "IDBRequest",
     41    "IDBTransaction",
     42  ]) {
     43    if (!win[name]) {
     44      Object.defineProperty(win, name, { value: emptyObj });
     45    }
     46  }
     47 
     48  if (!win.serviceWorker) {
     49    const sw = {
     50      addEventListener() {},
     51      getRegistrations: () => win.Promise.resolve(emptyArr),
     52      register: () => win.Promise.reject(emptyMsg),
     53    };
     54 
     55    Object.defineProperty(navigator.wrappedJSObject, "serviceWorker", {
     56      value: cloneInto(sw, window, { cloneFunctions: true }),
     57    });
     58 
     59    // bug 1779536
     60    Object.defineProperty(navigator.wrappedJSObject.serviceWorker, "ready", {
     61      value: new win.Promise(noOpFn),
     62    });
     63  }
     64 
     65  // bug 1750699
     66  if (!win.PushManager) {
     67    Object.defineProperty(win, "PushManager", { value: emptyObj });
     68  }
     69 
     70  // bug 1750699
     71  if (!win.PushSubscription) {
     72    const ps = {
     73      prototype: {
     74        getKey() {},
     75      },
     76    };
     77 
     78    Object.defineProperty(win, "PushSubscription", {
     79      value: cloneInto(ps, window, { cloneFunctions: true }),
     80    });
     81  }
     82 
     83  // bug 1750699
     84  if (!win.ServiceWorkerRegistration) {
     85    const swr = {
     86      prototype: {
     87        showNotification() {},
     88      },
     89    };
     90 
     91    Object.defineProperty(win, "ServiceWorkerRegistration", {
     92      value: cloneInto(swr, window, { cloneFunctions: true }),
     93    });
     94  }
     95 })();