Bug 1888590 - Mark some subtests on trusted-types-event-handlers.html as failing...
[gecko.git] / toolkit / actors / AboutHttpsOnlyErrorChild.sys.mjs
blob20313d30c04bdff51f3a767943eb31d4cda494e0
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
7 import { RemotePageChild } from "resource://gre/actors/RemotePageChild.sys.mjs";
9 const lazy = {};
11 XPCOMUtils.defineLazyServiceGetter(
12   lazy,
13   "@mozilla.org/network/serialization-helper;1",
14   "nsISerializationHelper"
17 export class AboutHttpsOnlyErrorChild extends RemotePageChild {
18   actorCreated() {
19     super.actorCreated();
21     // If you add a new function, remember to add it to RemotePageAccessManager.sys.mjs
22     // to allow content-privileged about:httpsonlyerror to use it.
23     const exportableFunctions = [
24       "RPMTryPingSecureWWWLink",
25       "RPMOpenSecureWWWLink",
26     ];
27     this.exportFunctions(exportableFunctions);
28   }
30   RPMTryPingSecureWWWLink() {
31     // try if the page can be reached with www prefix
32     // if so send message to the parent to send message to the error page to display
33     // suggestion button for www
35     const httpsOnlySuggestionPref = Services.prefs.getBoolPref(
36       "dom.security.https_only_mode_error_page_user_suggestions"
37     );
39     // only check if pref is true otherwise return
40     if (!httpsOnlySuggestionPref) {
41       return;
42     }
44     // get the host url without the path with www in front
45     const wwwURL = "https://www." + this.contentWindow.location.host;
46     fetch(wwwURL, {
47       credentials: "omit",
48       cache: "no-store",
49     })
50       .then(data => {
51         if (data.status === 200) {
52           this.contentWindow.dispatchEvent(
53             new this.contentWindow.CustomEvent("pingSecureWWWLinkSuccess")
54           );
55         }
56       })
57       .catch(() => {
58         dump("No secure www suggestion possible for " + wwwURL);
59       });
60   }
62   RPMOpenSecureWWWLink() {
63     // if user wants to visit suggested secure www page: visit page with www prefix and delete errorpage from history
64     const context = this.manager.browsingContext;
65     const docShell = context.docShell;
66     const httpChannel = docShell.failedChannel.QueryInterface(
67       Ci.nsIHttpChannel
68     );
69     const webNav = docShell.QueryInterface(Ci.nsIWebNavigation);
70     const triggeringPrincipal =
71       docShell.failedChannel.loadInfo.triggeringPrincipal;
72     const oldURI = httpChannel.URI;
73     const newWWWURI = oldURI
74       .mutate()
75       .setHost("www." + oldURI.host)
76       .finalize();
78     webNav.loadURI(newWWWURI, {
79       triggeringPrincipal,
80       loadFlags: Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY,
81     });
82   }