Blame browser/modules/ContentClick.jsm

Packit f0b94e
/* -*- mode: js; indent-tabs-mode: nil; js-indent-level: 2 -*- */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
"use strict";
Packit f0b94e
Packit f0b94e
var EXPORTED_SYMBOLS = [ "ContentClick" ];
Packit f0b94e
Packit f0b94e
ChromeUtils.import("resource://gre/modules/Services.jsm");
Packit f0b94e
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
Packit f0b94e
Packit f0b94e
ChromeUtils.defineModuleGetter(this, "PlacesUIUtils",
Packit f0b94e
                               "resource:///modules/PlacesUIUtils.jsm");
Packit f0b94e
ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
Packit f0b94e
                               "resource://gre/modules/PrivateBrowsingUtils.jsm");
Packit f0b94e
Packit f0b94e
var ContentClick = {
Packit f0b94e
  // Listeners are added in nsBrowserGlue.js
Packit f0b94e
  receiveMessage(message) {
Packit f0b94e
    switch (message.name) {
Packit f0b94e
      case "Content:Click":
Packit f0b94e
        // Bug 1446913 - Ensure this happens in the next tick so TabOpen/TabMove
Packit f0b94e
        // events are processed correctly in webextensions.
Packit f0b94e
        Promise.resolve().then(() => {
Packit f0b94e
          this.contentAreaClick(message.json, message.target);
Packit f0b94e
        });
Packit f0b94e
        break;
Packit f0b94e
    }
Packit f0b94e
  },
Packit f0b94e
Packit f0b94e
  /**
Packit f0b94e
   * Handles clicks in the content area.
Packit f0b94e
   *
Packit f0b94e
   * @param json {Object} JSON object that looks like an Event
Packit f0b94e
   * @param browser {Element<browser>}
Packit f0b94e
   */
Packit f0b94e
  contentAreaClick(json, browser) {
Packit f0b94e
    // This is heavily based on contentAreaClick from browser.js (Bug 903016)
Packit f0b94e
    // The json is set up in a way to look like an Event.
Packit f0b94e
    let window = browser.ownerGlobal;
Packit f0b94e
Packit f0b94e
    if (!json.href) {
Packit f0b94e
      // Might be middle mouse navigation.
Packit f0b94e
      if (Services.prefs.getBoolPref("middlemouse.contentLoadURL") &&
Packit f0b94e
          !Services.prefs.getBoolPref("general.autoScroll")) {
Packit f0b94e
        window.middleMousePaste(json);
Packit f0b94e
      }
Packit f0b94e
      return;
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    if (json.bookmark) {
Packit f0b94e
      // This is the Opera convention for a special link that, when clicked,
Packit f0b94e
      // allows to add a sidebar panel.  The link's title attribute contains
Packit f0b94e
      // the title that should be used for the sidebar panel.
Packit f0b94e
      PlacesUIUtils.showBookmarkDialog({ action: "add",
Packit f0b94e
                                         type: "bookmark",
Packit f0b94e
                                         uri: Services.io.newURI(json.href),
Packit f0b94e
                                         title: json.title,
Packit f0b94e
                                         loadBookmarkInSidebar: true,
Packit f0b94e
                                         hiddenRows: [ "description",
Packit f0b94e
                                                        "location",
Packit f0b94e
                                                        "keyword" ]
Packit f0b94e
                                       }, window);
Packit f0b94e
      return;
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    // Note: We don't need the sidebar code here.
Packit f0b94e
Packit f0b94e
    // Mark the page as a user followed link.  This is done so that history can
Packit f0b94e
    // distinguish automatic embed visits from user activated ones.  For example
Packit f0b94e
    // pages loaded in frames are embed visits and lost with the session, while
Packit f0b94e
    // visits across frames should be preserved.
Packit f0b94e
    try {
Packit f0b94e
      if (!PrivateBrowsingUtils.isWindowPrivate(window))
Packit f0b94e
        PlacesUIUtils.markPageAsFollowedLink(json.href);
Packit f0b94e
    } catch (ex) { /* Skip invalid URIs. */ }
Packit f0b94e
Packit f0b94e
    // This part is based on handleLinkClick.
Packit f0b94e
    var where = window.whereToOpenLink(json);
Packit f0b94e
    if (where == "current")
Packit f0b94e
      return;
Packit f0b94e
Packit f0b94e
    // Todo(903022): code for where == save
Packit f0b94e
Packit f0b94e
    let params = {
Packit f0b94e
      charset: browser.characterSet,
Packit f0b94e
      referrerURI: browser.documentURI,
Packit f0b94e
      referrerPolicy: json.referrerPolicy,
Packit f0b94e
      noReferrer: json.noReferrer,
Packit f0b94e
      allowMixedContent: json.allowMixedContent,
Packit f0b94e
      isContentWindowPrivate: json.isContentWindowPrivate,
Packit f0b94e
      originPrincipal: json.originPrincipal,
Packit f0b94e
      triggeringPrincipal: json.triggeringPrincipal,
Packit f0b94e
      frameOuterWindowID: json.frameOuterWindowID,
Packit f0b94e
    };
Packit f0b94e
Packit f0b94e
    // The new tab/window must use the same userContextId.
Packit f0b94e
    if (json.originAttributes.userContextId) {
Packit f0b94e
      params.userContextId = json.originAttributes.userContextId;
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    window.openLinkIn(json.href, where, params);
Packit f0b94e
  }
Packit f0b94e
};