tor-browser

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

ToolbarDropHandler.sys.mjs (4686B)


      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 let lazy = {};
      6 
      7 ChromeUtils.defineLazyGetter(lazy, "FluentStrings", function () {
      8  return new Localization(["browser/toolbarDropHandler.ftl"], true);
      9 });
     10 
     11 ChromeUtils.defineESModuleGetters(lazy, {
     12  HomePage: "resource:///modules/HomePage.sys.mjs",
     13  OpenInTabsUtils:
     14    "moz-src:///browser/components/tabbrowser/OpenInTabsUtils.sys.mjs",
     15  URILoadingHelper: "resource:///modules/URILoadingHelper.sys.mjs",
     16  UrlbarUtils: "moz-src:///browser/components/urlbar/UrlbarUtils.sys.mjs",
     17 });
     18 
     19 export var ToolbarDropHandler = {
     20  _canDropLink: aEvent => Services.droppedLinkHandler.canDropLink(aEvent, true),
     21 
     22  onDragOver(aEvent) {
     23    if (this._canDropLink(aEvent)) {
     24      aEvent.preventDefault();
     25    }
     26  },
     27 
     28  async _openHomeDialog(aURL, win) {
     29    const [promptTitle, promptMsgSingle, promptMsgMultiple] =
     30      await lazy.FluentStrings.formatValues([
     31        "toolbar-drop-on-home-title",
     32        "toolbar-drop-on-home-msg",
     33        "toolbar-drop-on-home-msg-multiple",
     34      ]);
     35 
     36    var promptMsg;
     37    if (aURL.includes("|")) {
     38      promptMsg = promptMsgMultiple;
     39    } else {
     40      promptMsg = promptMsgSingle;
     41    }
     42 
     43    var pressedVal = Services.prompt.confirmEx(
     44      win,
     45      promptTitle,
     46      promptMsg,
     47      Services.prompt.STD_YES_NO_BUTTONS,
     48      null,
     49      null,
     50      null,
     51      null,
     52      { value: 0 }
     53    );
     54 
     55    if (pressedVal == 0) {
     56      lazy.HomePage.set(aURL).catch(console.error);
     57    }
     58  },
     59 
     60  onDropHomeButtonObserver(aEvent) {
     61    // disallow setting home pages that inherit the principal
     62    let links = Services.droppedLinkHandler.dropLinks(aEvent, true);
     63    if (links.length) {
     64      let urls = [];
     65      for (let link of links) {
     66        if (link.url.includes("|")) {
     67          urls.push(...link.url.split("|"));
     68        } else {
     69          urls.push(link.url);
     70        }
     71      }
     72 
     73      try {
     74        Services.droppedLinkHandler.validateURIsForDrop(aEvent, urls);
     75      } catch (e) {
     76        return;
     77      }
     78 
     79      const win = aEvent.target.ownerGlobal;
     80      win.setTimeout(this._openHomeDialog, 0, urls.join("|"), win);
     81    }
     82  },
     83 
     84  async onDropNewTabButtonObserver(aEvent) {
     85    const win = aEvent.target.ownerGlobal;
     86    let links = Services.droppedLinkHandler.dropLinks(aEvent);
     87    if (
     88      links.length >=
     89      Services.prefs.getIntPref("browser.tabs.maxOpenBeforeWarn")
     90    ) {
     91      // Sync dialog cannot be used inside drop event handler.
     92      let answer = await lazy.OpenInTabsUtils.promiseConfirmOpenInTabs(
     93        links.length,
     94        win
     95      );
     96      if (!answer) {
     97        return;
     98      }
     99    }
    100 
    101    let where = aEvent.shiftKey ? "tabshifted" : "tab";
    102    let triggeringPrincipal =
    103      Services.droppedLinkHandler.getTriggeringPrincipal(aEvent);
    104    let policyContainer =
    105      Services.droppedLinkHandler.getPolicyContainer(aEvent);
    106    for (let link of links) {
    107      if (link.url) {
    108        let data = await lazy.UrlbarUtils.getShortcutOrURIAndPostData(link.url);
    109        // Allow third-party services to fixup this URL.
    110        lazy.URILoadingHelper.openLinkIn(win, data.url, where, {
    111          postData: data.postData,
    112          allowThirdPartyFixup: true,
    113          triggeringPrincipal,
    114          policyContainer,
    115        });
    116      }
    117    }
    118  },
    119 
    120  async onDropNewWindowButtonObserver(aEvent) {
    121    const win = aEvent.target.ownerGlobal;
    122    let links = Services.droppedLinkHandler.dropLinks(aEvent);
    123    if (
    124      links.length >=
    125      Services.prefs.getIntPref("browser.tabs.maxOpenBeforeWarn")
    126    ) {
    127      // Sync dialog cannot be used inside drop event handler.
    128      let answer = await lazy.OpenInTabsUtils.promiseConfirmOpenInTabs(
    129        links.length,
    130        win
    131      );
    132      if (!answer) {
    133        return;
    134      }
    135    }
    136 
    137    let triggeringPrincipal =
    138      Services.droppedLinkHandler.getTriggeringPrincipal(aEvent);
    139    let policyContainer =
    140      Services.droppedLinkHandler.getPolicyContainer(aEvent);
    141    for (let link of links) {
    142      if (link.url) {
    143        let data = await lazy.UrlbarUtils.getShortcutOrURIAndPostData(link.url);
    144        // Allow third-party services to fixup this URL.
    145        lazy.URILoadingHelper.openLinkIn(win, data.url, "window", {
    146          // TODO fix allowInheritPrincipal
    147          // (this is required by javascript: drop to the new window) Bug 1475201
    148          allowInheritPrincipal: true,
    149          postData: data.postData,
    150          allowThirdPartyFixup: true,
    151          triggeringPrincipal,
    152          policyContainer,
    153        });
    154      }
    155    }
    156  },
    157 };