1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/* vim: set ts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const EXPORTED_SYMBOLS = [
"LinkClickHandlerChild",
"StrictLinkClickHandlerChild",
];
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
const lazy = {};
XPCOMUtils.defineLazyServiceGetter(
lazy,
"protocolSvc",
"@mozilla.org/uriloader/external-protocol-service;1",
"nsIExternalProtocolService"
);
/**
* Extract the href from the link click event.
* We look for HTMLAnchorElement, HTMLAreaElement, HTMLLinkElement,
* HTMLInputElement.form.action, and nested anchor tags.
* If the clicked element was a HTMLInputElement or HTMLButtonElement
* we return the form action.
*
* @returns the url and the text for the link being clicked.
*/
function hRefForClickEvent(aEvent) {
let target = aEvent.target;
if (
HTMLImageElement.isInstance(target) &&
target.hasAttribute("overflowing")
) {
// Click on zoomed image.
return [null, null];
}
let href = null;
if (
HTMLAnchorElement.isInstance(target) ||
HTMLAreaElement.isInstance(target) ||
HTMLLinkElement.isInstance(target)
) {
if (target.hasAttribute("href") && !target.download) {
href = target.href;
}
} else {
// We may be nested inside of a link node.
let linkNode = aEvent.target;
while (linkNode && !HTMLAnchorElement.isInstance(linkNode)) {
linkNode = linkNode.parentNode;
}
if (linkNode && !linkNode.download) {
href = linkNode.href;
}
}
return href;
}
/**
* Listens for click events and, if the click would result in loading a page
* on a different base domain from the current page, cancels the click event,
* redirecting the URI to an external browser, effectively creating a
* single-site browser.
*
* This actor applies to browsers in the "single-site" message manager group.
*/
class LinkClickHandlerChild extends JSWindowActorChild {
handleEvent(event) {
// Don't handle events that:
// a) are in the parent process (handled by onclick),
// b) aren't trusted,
// c) have already been handled or
// d) aren't left-click.
if (
this.manager.isInProcess ||
!event.isTrusted ||
event.defaultPrevented ||
event.button
) {
return;
}
let eventHRef = hRefForClickEvent(event);
if (!eventHRef) {
return;
}
let pageURI = Services.io.newURI(this.document.location.href);
let eventURI = Services.io.newURI(eventHRef);
try {
if (pageURI.host == eventURI.host) {
// Avoid using the eTLD service, and this also works for IP addresses.
return;
}
try {
if (
Services.eTLD.getBaseDomain(eventURI) ==
Services.eTLD.getBaseDomain(pageURI)
) {
return;
}
} catch (ex) {
if (ex.result != Cr.NS_ERROR_HOST_IS_IP_ADDRESS) {
console.error(ex);
}
}
} catch (ex) {
// The page or link might be from a host-less URL scheme such as about,
// blob, or data. The host is never going to match, carry on.
}
if (
!lazy.protocolSvc.isExposedProtocol(eventURI.scheme) ||
eventURI.schemeIs("http") ||
eventURI.schemeIs("https")
) {
event.preventDefault();
this.sendAsyncMessage("openLinkExternally", eventHRef);
}
}
}
/**
* Listens for click events and, if the click would result in loading a
* different page from the current page, cancels the click event, redirecting
* the URI to an external browser, effectively creating a single-page browser.
*
* This actor applies to browsers in the "single-page" message manager group.
*/
class StrictLinkClickHandlerChild extends JSWindowActorChild {
handleEvent(event) {
// Don't handle events that:
// a) are in the parent process (handled by onclick),
// b) aren't trusted,
// c) have already been handled or
// d) aren't left-click.
if (
this.manager.isInProcess ||
!event.isTrusted ||
event.defaultPrevented ||
event.button
) {
return;
}
let eventHRef = hRefForClickEvent(event);
if (!eventHRef) {
return;
}
let pageURI = Services.io.newURI(this.document.location.href);
let eventURI = Services.io.newURI(eventHRef);
if (eventURI.specIgnoringRef == pageURI.specIgnoringRef) {
return;
}
if (
!lazy.protocolSvc.isExposedProtocol(eventURI.scheme) ||
eventURI.schemeIs("http") ||
eventURI.schemeIs("https")
) {
event.preventDefault();
this.sendAsyncMessage("openLinkExternally", eventHRef);
}
}
}
|