Bug 1874684 - Part 31: Correctly reject invalid durations in some RoundDuration calls...
[gecko.git] / browser / actors / ClickHandlerParent.sys.mjs
blob4078c6404fcc5bff77f953b4e93b4e450d244ab6
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 const lazy = {};
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",
13 });
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;
35       if (!browser) {
36         // Can be null if the tab disappeared by the time we got the message.
37         // Just bail.
38         return;
39       }
40       fillInClickEvent(this, message.data);
41       browser.ownerGlobal.middleMousePaste(message.data);
42     }
43   }
46 export class ClickHandlerParent extends JSWindowActorParent {
47   static addContentClickListener(listener) {
48     gContentClickListeners.add(listener);
49   }
51   static removeContentClickListener(listener) {
52     gContentClickListeners.delete(listener);
53   }
55   receiveMessage(message) {
56     switch (message.name) {
57       case "Content:Click":
58         fillInClickEvent(this, message.data);
59         this.contentAreaClick(message.data);
60         this.notifyClickListeners(message.data);
61         break;
62     }
63   }
65   /**
66    * Handles clicks in the content area.
67    *
68    * @param data {Object} object that looks like an Event
69    * @param browser {Element<browser>}
70    */
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;
75     if (!browser) {
76       // Can be null if the tab disappeared by the time we got the message.
77       // Just bail.
78       return;
79     }
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) {
86       return;
87     }
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.
93     try {
94       if (!lazy.PrivateBrowsingUtils.isWindowPrivate(window)) {
95         lazy.PlacesUIUtils.markPageAsFollowedLink(data.href);
96       }
97     } catch (ex) {
98       /* Skip invalid URIs. */
99     }
101     // This part is based on handleLinkClick.
102     var where = window.whereToOpenLink(data);
103     if (where == "current") {
104       return;
105     }
107     // Todo(903022): code for where == save
109     let params = {
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,
122     };
124     if (data.globalHistoryOptions) {
125       params.globalHistoryOptions = data.globalHistoryOptions;
126     } else {
127       params.globalHistoryOptions = {
128         triggeringSponsoredURL: browser.getAttribute("triggeringSponsoredURL"),
129         triggeringSponsoredURLVisitTimeMS: browser.getAttribute(
130           "triggeringSponsoredURLVisitTimeMS"
131         ),
132       };
133     }
135     // The new tab/window must use the same userContextId.
136     if (data.originAttributes.userContextId) {
137       params.userContextId = data.originAttributes.userContextId;
138     }
140     params.allowInheritPrincipal = true;
142     window.openLinkIn(data.href, where, params);
143   }
145   notifyClickListeners(data) {
146     for (let listener of gContentClickListeners) {
147       try {
148         let browser = this.browsingContext.top.embedderElement;
150         listener.onContentClick(browser, data);
151       } catch (ex) {
152         console.error(ex);
153       }
154     }
155   }