Bug 1523562 [wpt PR 14802] - [Animation Worklet] Upstream worklet animation with...
[gecko.git] / browser / modules / ContentClick.jsm
blob0ee3bf71c21a35f07044871e1a5e9e6c8fe7a623
1 /* -*- mode: js; indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 "use strict";
8 var EXPORTED_SYMBOLS = [ "ContentClick" ];
10 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
12 ChromeUtils.defineModuleGetter(this, "PlacesUIUtils",
13                                "resource:///modules/PlacesUIUtils.jsm");
14 ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
15                                "resource://gre/modules/PrivateBrowsingUtils.jsm");
17 var ContentClick = {
18   // Listeners are added in nsBrowserGlue.js
19   receiveMessage(message) {
20     switch (message.name) {
21       case "Content:Click":
22         this.contentAreaClick(message.json, message.target);
23         break;
24     }
25   },
27   /**
28    * Handles clicks in the content area.
29    *
30    * @param json {Object} JSON object that looks like an Event
31    * @param browser {Element<browser>}
32    */
33   contentAreaClick(json, browser) {
34     // This is heavily based on contentAreaClick from browser.js (Bug 903016)
35     // The json is set up in a way to look like an Event.
36     let window = browser.ownerGlobal;
38     if (!json.href) {
39       // Might be middle mouse navigation.
40       if (Services.prefs.getBoolPref("middlemouse.contentLoadURL") &&
41           !Services.prefs.getBoolPref("general.autoScroll")) {
42         window.middleMousePaste(json);
43       }
44       return;
45     }
47     // If the browser is not in a place where we can open links, bail out.
48     // This can happen in osx sheets, dialogs, etc. that are not browser
49     // windows.  Specifically the payments UI is in an osx sheet.
50     if (window.openLinkIn === undefined) {
51       return;
52     }
54     // Mark the page as a user followed link.  This is done so that history can
55     // distinguish automatic embed visits from user activated ones.  For example
56     // pages loaded in frames are embed visits and lost with the session, while
57     // visits across frames should be preserved.
58     try {
59       if (!PrivateBrowsingUtils.isWindowPrivate(window))
60         PlacesUIUtils.markPageAsFollowedLink(json.href);
61     } catch (ex) { /* Skip invalid URIs. */ }
63     // This part is based on handleLinkClick.
64     var where = window.whereToOpenLink(json);
65     if (where == "current")
66       return;
68     // Todo(903022): code for where == save
70     let params = {
71       charset: browser.characterSet,
72       referrerURI: browser.documentURI,
73       referrerPolicy: json.referrerPolicy,
74       noReferrer: json.noReferrer,
75       allowMixedContent: json.allowMixedContent,
76       isContentWindowPrivate: json.isContentWindowPrivate,
77       originPrincipal: json.originPrincipal,
78       triggeringPrincipal: json.triggeringPrincipal,
79       frameOuterWindowID: json.frameOuterWindowID,
80     };
82     // The new tab/window must use the same userContextId.
83     if (json.originAttributes.userContextId) {
84       params.userContextId = json.originAttributes.userContextId;
85     }
87     params.allowInheritPrincipal = true;
89     window.openLinkIn(json.href, where, params);
90   },