Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / toolkit / actors / NetErrorChild.sys.mjs
blob671eb22baa56798f3f8576ab3373e23a6e35a645
1 /* -*- 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   AppInfo: "chrome://remote/content/shared/AppInfo.sys.mjs",
10 });
12 import { RemotePageChild } from "resource://gre/actors/RemotePageChild.sys.mjs";
14 export class NetErrorChild extends RemotePageChild {
15   actorCreated() {
16     super.actorCreated();
18     // If you add a new function, remember to add it to RemotePageAccessManager.sys.mjs
19     // to allow content-privileged about:neterror or about:certerror to use it.
20     const exportableFunctions = [
21       "RPMGetAppBuildID",
22       "RPMGetInnerMostURI",
23       "RPMAddToHistogram",
24       "RPMRecordTelemetryEvent",
25       "RPMCheckAlternateHostAvailable",
26       "RPMGetHttpResponseHeader",
27       "RPMIsTRROnlyFailure",
28       "RPMIsFirefox",
29       "RPMIsNativeFallbackFailure",
30       "RPMOpenPreferences",
31       "RPMGetTRRSkipReason",
32       "RPMGetTRRDomain",
33       "RPMIsSiteSpecificTRRError",
34       "RPMSetTRRDisabledLoadFlags",
35       "RPMGetCurrentTRRMode",
36     ];
37     this.exportFunctions(exportableFunctions);
38   }
40   getFailedCertChain(docShell) {
41     let securityInfo =
42       docShell.failedChannel && docShell.failedChannel.securityInfo;
43     if (!securityInfo) {
44       return [];
45     }
46     return securityInfo.failedCertChain.map(cert => cert.getBase64DERString());
47   }
49   handleEvent(aEvent) {
50     // Documents have a null ownerDocument.
51     let doc = aEvent.originalTarget.ownerDocument || aEvent.originalTarget;
53     switch (aEvent.type) {
54       case "click":
55         let elem = aEvent.originalTarget;
56         if (elem.id == "viewCertificate") {
57           // Call through the superclass to avoid the security check.
58           this.sendAsyncMessage("Browser:CertExceptionError", {
59             location: doc.location.href,
60             elementId: elem.id,
61             failedCertChain: this.getFailedCertChain(doc.defaultView.docShell),
62           });
63         }
64         break;
65     }
66   }
68   RPMGetInnerMostURI(uriString) {
69     let uri = Services.io.newURI(uriString);
70     if (uri instanceof Ci.nsINestedURI) {
71       uri = uri.QueryInterface(Ci.nsINestedURI).innermostURI;
72     }
74     return uri.spec;
75   }
77   RPMGetAppBuildID() {
78     return Services.appinfo.appBuildID;
79   }
81   RPMAddToHistogram(histID, bin) {
82     Services.telemetry.getHistogramById(histID).add(bin);
83   }
85   RPMRecordTelemetryEvent(category, event, object, value, extra) {
86     Services.telemetry.recordEvent(category, event, object, value, extra);
87   }
89   RPMCheckAlternateHostAvailable() {
90     const host = this.contentWindow.location.host.trim();
92     // Adapted from UrlbarUtils::looksLikeSingleWordHost
93     // https://searchfox.org/mozilla-central/rev/a26af613a476fafe6c3eba05a81bef63dff3c9f1/browser/components/urlbar/UrlbarUtils.sys.mjs#893
94     const REGEXP_SINGLE_WORD = /^[^\s@:/?#]+(:\d+)?$/;
95     if (!REGEXP_SINGLE_WORD.test(host)) {
96       return;
97     }
99     let info = Services.uriFixup.forceHttpFixup(
100       this.contentWindow.location.href
101     );
103     if (!info.fixupCreatedAlternateURI && !info.fixupChangedProtocol) {
104       return;
105     }
107     let { displayHost, displaySpec, pathQueryRef } = info.fixedURI;
109     if (pathQueryRef.endsWith("/")) {
110       pathQueryRef = pathQueryRef.slice(0, pathQueryRef.length - 1);
111     }
113     let weakDoc = Cu.getWeakReference(this.contentWindow.document);
114     let onLookupCompleteListener = {
115       onLookupComplete(request, record, status) {
116         let doc = weakDoc.get();
117         if (!doc || !Components.isSuccessCode(status)) {
118           return;
119         }
121         let link = doc.createElement("a");
122         link.href = displaySpec;
123         link.setAttribute("data-l10n-name", "website");
125         let span = doc.createElement("span");
126         span.appendChild(link);
127         doc.l10n.setAttributes(span, "neterror-dns-not-found-with-suggestion", {
128           hostAndPath: displayHost + pathQueryRef,
129         });
131         const shortDesc = doc.getElementById("errorShortDesc");
132         shortDesc.textContent += " ";
133         shortDesc.appendChild(span);
134       },
135     };
137     Services.uriFixup.checkHost(
138       info.fixedURI,
139       onLookupCompleteListener,
140       this.document.nodePrincipal.originAttributes
141     );
142   }
144   // Get the header from the http response of the failed channel. This function
145   // is used in the 'about:neterror' page.
146   RPMGetHttpResponseHeader(responseHeader) {
147     let channel = this.contentWindow.docShell.failedChannel;
148     if (!channel) {
149       return "";
150     }
152     let httpChannel = channel.QueryInterface(Ci.nsIHttpChannel);
153     if (!httpChannel) {
154       return "";
155     }
157     try {
158       return httpChannel.getResponseHeader(responseHeader);
159     } catch (e) {}
161     return "";
162   }
164   RPMIsTRROnlyFailure() {
165     // We will only show this in Firefox because the options may direct users to settings only available on Firefox Desktop
166     let channel = this.contentWindow?.docShell?.failedChannel?.QueryInterface(
167       Ci.nsIHttpChannelInternal
168     );
169     if (!channel) {
170       return false;
171     }
172     return channel.effectiveTRRMode == Ci.nsIRequest.TRR_ONLY_MODE;
173   }
175   RPMIsFirefox() {
176     return lazy.AppInfo.isFirefox;
177   }
179   _getTRRSkipReason() {
180     let channel = this.contentWindow?.docShell?.failedChannel?.QueryInterface(
181       Ci.nsIHttpChannelInternal
182     );
183     return channel?.trrSkipReason ?? Ci.nsITRRSkipReason.TRR_UNSET;
184   }
186   RPMIsNativeFallbackFailure() {
187     if (!this.contentWindow?.navigator.onLine) {
188       return false;
189     }
191     let skipReason = this._getTRRSkipReason();
193     if (
194       Services.dns.currentTrrMode === Ci.nsIDNSService.MODE_TRRFIRST &&
195       skipReason === Ci.nsITRRSkipReason.TRR_NOT_CONFIRMED
196     ) {
197       return true;
198     }
200     const warningReasons = new Set([
201       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_GOOGLE_SAFESEARCH,
202       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_YOUTUBE_SAFESEARCH,
203       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_ZSCALER_CANARY,
204       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_CANARY,
205       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_MODIFIED_ROOTS,
206       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_PARENTAL_CONTROLS,
207       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_THIRD_PARTY_ROOTS,
208       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_ENTERPRISE_POLICY,
209       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_VPN,
210       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_PROXY,
211       Ci.nsITRRSkipReason.TRR_HEURISTIC_TRIPPED_NRPT,
212     ]);
214     return (
215       Services.dns.currentTrrMode === Ci.nsIDNSService.MODE_NATIVEONLY &&
216       warningReasons.has(skipReason)
217     );
218   }
220   RPMGetTRRSkipReason() {
221     let skipReason = this._getTRRSkipReason();
222     return Services.dns.getTRRSkipReasonName(skipReason);
223   }
225   RPMGetTRRDomain() {
226     return Services.dns.trrDomain;
227   }
229   RPMIsSiteSpecificTRRError() {
230     let skipReason = this._getTRRSkipReason();
231     switch (skipReason) {
232       case Ci.nsITRRSkipReason.TRR_NXDOMAIN:
233       case Ci.nsITRRSkipReason.TRR_RCODE_FAIL:
234       case Ci.nsITRRSkipReason.TRR_NO_ANSWERS:
235         return true;
236     }
237     return false;
238   }
240   RPMSetTRRDisabledLoadFlags() {
241     this.contentWindow.docShell.browsingContext.defaultLoadFlags |=
242       Ci.nsIRequest.LOAD_TRR_DISABLED_MODE;
243   }