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/. */
8 ChromeUtils.defineESModuleGetters(lazy, {
9 E10SUtils: "resource://gre/modules/E10SUtils.sys.mjs",
10 PlacesUIUtils: "resource:///modules/PlacesUIUtils.sys.mjs",
11 PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
12 WebNavigationFrames: "resource://gre/modules/WebNavigationFrames.sys.mjs",
15 let gContentClickListeners = new Set();
17 // Fill in fields which are not sent by the content process for the click event
18 // based on known data in the parent process.
19 function fillInClickEvent(actor, data) {
20 const wgp = actor.manager;
21 data.frameID = lazy.WebNavigationFrames.getFrameId(wgp.browsingContext);
22 data.triggeringPrincipal = wgp.documentPrincipal;
23 data.originPrincipal = wgp.documentPrincipal;
24 data.originStoragePrincipal = wgp.documentStoragePrincipal;
25 data.originAttributes = wgp.documentPrincipal?.originAttributes ?? {};
26 data.isContentWindowPrivate = wgp.browsingContext.usePrivateBrowsing;
29 export class MiddleMousePasteHandlerParent extends JSWindowActorParent {
30 receiveMessage(message) {
31 if (message.name == "MiddleClickPaste") {
32 // This is heavily based on contentAreaClick from browser.js (Bug 903016)
33 // The data is set up in a way to look like an Event.
34 let browser = this.manager.browsingContext.top.embedderElement;
36 // Can be null if the tab disappeared by the time we got the message.
40 fillInClickEvent(this, message.data);
41 browser.ownerGlobal.middleMousePaste(message.data);
46 export class ClickHandlerParent extends JSWindowActorParent {
47 static addContentClickListener(listener) {
48 gContentClickListeners.add(listener);
51 static removeContentClickListener(listener) {
52 gContentClickListeners.delete(listener);
55 receiveMessage(message) {
56 switch (message.name) {
58 fillInClickEvent(this, message.data);
59 this.contentAreaClick(message.data);
60 this.notifyClickListeners(message.data);
66 * Handles clicks in the content area.
68 * @param data {Object} object that looks like an Event
69 * @param browser {Element<browser>}
71 contentAreaClick(data) {
72 // This is heavily based on contentAreaClick from browser.js (Bug 903016)
73 // The data is set up in a way to look like an Event.
74 let browser = this.manager.browsingContext.top.embedderElement;
76 // Can be null if the tab disappeared by the time we got the message.
80 let window = browser.ownerGlobal;
82 // If the browser is not in a place where we can open links, bail out.
83 // This can happen in osx sheets, dialogs, etc. that are not browser
84 // windows. Specifically the payments UI is in an osx sheet.
85 if (window.openLinkIn === undefined) {
89 // Mark the page as a user followed link. This is done so that history can
90 // distinguish automatic embed visits from user activated ones. For example
91 // pages loaded in frames are embed visits and lost with the session, while
92 // visits across frames should be preserved.
94 if (!lazy.PrivateBrowsingUtils.isWindowPrivate(window)) {
95 lazy.PlacesUIUtils.markPageAsFollowedLink(data.href);
98 /* Skip invalid URIs. */
101 // This part is based on handleLinkClick.
102 var where = window.whereToOpenLink(data);
103 if (where == "current") {
107 // Todo(903022): code for where == save
110 charset: browser.characterSet,
111 referrerInfo: lazy.E10SUtils.deserializeReferrerInfo(data.referrerInfo),
112 isContentWindowPrivate: data.isContentWindowPrivate,
113 originPrincipal: data.originPrincipal,
114 originStoragePrincipal: data.originStoragePrincipal,
115 triggeringPrincipal: data.triggeringPrincipal,
116 csp: data.csp ? lazy.E10SUtils.deserializeCSP(data.csp) : null,
117 frameID: data.frameID,
118 openerBrowser: browser,
119 // The child ensures that untrusted events have a valid user activation.
120 hasValidUserGestureActivation: true,
121 triggeringRemoteType: this.manager.domProcess?.remoteType,
124 if (data.globalHistoryOptions) {
125 params.globalHistoryOptions = data.globalHistoryOptions;
127 params.globalHistoryOptions = {
128 triggeringSponsoredURL: browser.getAttribute("triggeringSponsoredURL"),
129 triggeringSponsoredURLVisitTimeMS: browser.getAttribute(
130 "triggeringSponsoredURLVisitTimeMS"
135 // The new tab/window must use the same userContextId.
136 if (data.originAttributes.userContextId) {
137 params.userContextId = data.originAttributes.userContextId;
140 params.allowInheritPrincipal = true;
142 window.openLinkIn(data.href, where, params);
145 notifyClickListeners(data) {
146 for (let listener of gContentClickListeners) {
148 let browser = this.browsingContext.top.embedderElement;
150 listener.onContentClick(browser, data);