tor-browser

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

ShareDelegate.sys.mjs (2087B)


      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 import { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs";
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineESModuleGetters(lazy, {
     10  GeckoViewPrompter: "resource://gre/modules/GeckoViewPrompter.sys.mjs",
     11 });
     12 
     13 const domBundle = Services.strings.createBundle(
     14  "chrome://global/locale/dom/dom.properties"
     15 );
     16 
     17 const { debug, warn } = GeckoViewUtils.initLogging("ShareDelegate");
     18 
     19 export class ShareDelegate {
     20  init(aParent) {
     21    this._openerWindow = aParent;
     22  }
     23 
     24  get openerWindow() {
     25    return this._openerWindow;
     26  }
     27 
     28  async share(aTitle, aText, aUri) {
     29    const ABORT = 2;
     30    const FAILURE = 1;
     31    const SUCCESS = 0;
     32 
     33    const msg = {
     34      type: "share",
     35      title: aTitle,
     36      text: aText,
     37      uri: aUri ? aUri.displaySpec : null,
     38    };
     39    const prompt = new lazy.GeckoViewPrompter(this._openerWindow);
     40    const result = await new Promise(resolve => {
     41      prompt.asyncShowPrompt(msg, resolve);
     42    });
     43 
     44    if (!result) {
     45      // A null result is treated as a dismissal in GeckoViewPrompter.
     46      throw new DOMException(
     47        domBundle.GetStringFromName("WebShareAPI_Aborted"),
     48        "AbortError"
     49      );
     50    }
     51 
     52    const res = result && result.response;
     53    switch (res) {
     54      case FAILURE:
     55        throw new DOMException(
     56          domBundle.GetStringFromName("WebShareAPI_Failed"),
     57          "DataError"
     58        );
     59      case ABORT: // Handle aborted attempt and invalid responses the same.
     60        throw new DOMException(
     61          domBundle.GetStringFromName("WebShareAPI_Aborted"),
     62          "AbortError"
     63        );
     64      case SUCCESS:
     65        return;
     66      default:
     67        throw new DOMException("Unknown error.", "UnknownError");
     68    }
     69  }
     70 }
     71 
     72 ShareDelegate.prototype.classID = Components.ID(
     73  "{1201d357-8417-4926-a694-e6408fbedcf8}"
     74 );
     75 ShareDelegate.prototype.QueryInterface = ChromeUtils.generateQI([
     76  "nsISharePicker",
     77 ]);