Bug 1535487 - determine rootUrl directly in buglist creator r=tomprince
[gecko.git] / toolkit / actors / WebNavigationChild.jsm
blobcdec098183f8f27473cc8a27c99b4a8ce4d8463d
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/. */
5 "use strict";
7 var EXPORTED_SYMBOLS = ["WebNavigationChild"];
9 const {ActorChild} = ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
10 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
11 const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
13 ChromeUtils.defineModuleGetter(this, "AppConstants",
14                                "resource://gre/modules/AppConstants.jsm");
15 ChromeUtils.defineModuleGetter(this, "E10SUtils",
16                                "resource://gre/modules/E10SUtils.jsm");
18 XPCOMUtils.defineLazyServiceGetter(this, "CrashReporter",
19                                    "@mozilla.org/xre/app-info;1",
20                                    "nsICrashReporter");
22 class WebNavigationChild extends ActorChild {
23   get webNavigation() {
24     return this.mm.docShell.QueryInterface(Ci.nsIWebNavigation);
25   }
27   receiveMessage(message) {
28     switch (message.name) {
29       case "WebNavigation:GoBack":
30         this.goBack();
31         break;
32       case "WebNavigation:GoForward":
33         this.goForward();
34         break;
35       case "WebNavigation:GotoIndex":
36         this.gotoIndex(message.data.index);
37         break;
38       case "WebNavigation:LoadURI":
39         let histogram = Services.telemetry.getKeyedHistogramById("FX_TAB_REMOTE_NAVIGATION_DELAY_MS");
40         histogram.add("WebNavigation:LoadURI",
41                       Services.telemetry.msSystemNow() - message.data.requestTime);
43         this.loadURI(message.data);
45         break;
46       case "WebNavigation:SetOriginAttributes":
47         this.setOriginAttributes(message.data.originAttributes);
48         break;
49       case "WebNavigation:Reload":
50         this.reload(message.data.flags);
51         break;
52       case "WebNavigation:Stop":
53         this.stop(message.data.flags);
54         break;
55     }
56   }
58   _wrapURIChangeCall(fn) {
59     this.mm.WebProgress.inLoadURI = true;
60     try {
61       fn();
62     } finally {
63       this.mm.WebProgress.inLoadURI = false;
64       this.mm.WebProgress.sendLoadCallResult();
65     }
66   }
68   goBack() {
69     if (this.webNavigation.canGoBack) {
70       this._wrapURIChangeCall(() => this.webNavigation.goBack());
71     }
72   }
74   goForward() {
75     if (this.webNavigation.canGoForward) {
76       this._wrapURIChangeCall(() => this.webNavigation.goForward());
77     }
78   }
80   gotoIndex(index) {
81     this._wrapURIChangeCall(() => this.webNavigation.gotoIndex(index));
82   }
84   loadURI(params) {
85     let {
86       uri,
87       flags,
88       referrerInfo,
89       postData,
90       headers,
91       baseURI,
92       triggeringPrincipal,
93       csp,
94     } = params || {};
96     if (AppConstants.MOZ_CRASHREPORTER && CrashReporter.enabled) {
97       let annotation = uri;
98       try {
99         let url = Services.io.newURI(uri);
100         // If the current URI contains a username/password, remove it.
101         url = url.mutate()
102                  .setUserPass("")
103                  .finalize();
104         annotation = url.spec;
105       } catch (ex) { /* Ignore failures to parse and failures
106                       on about: URIs. */ }
107       CrashReporter.annotateCrashReport("URL", annotation);
108     }
109     if (postData)
110       postData = E10SUtils.makeInputStream(postData);
111     if (headers)
112       headers = E10SUtils.makeInputStream(headers);
113     if (baseURI)
114       baseURI = Services.io.newURI(baseURI);
115     this._assert(triggeringPrincipal, "We need a triggering principal to continue loading", new Error().lineNumber);
117     triggeringPrincipal = E10SUtils.deserializePrincipal(triggeringPrincipal, () => {
118       this._assert(false, "Unable to deserialize passed triggering principal", new Error().lineNumber);
119       return Services.scriptSecurityManager.getSystemPrincipal({});
120     });
121     if (csp) {
122       csp = E10SUtils.deserializeCSP(csp);
123     }
125     let loadURIOptions = {
126       triggeringPrincipal,
127       csp,
128       loadFlags: flags,
129       referrerInfo: E10SUtils.deserializeReferrerInfo(referrerInfo),
130       postData,
131       headers,
132       baseURI,
133     };
134     this._wrapURIChangeCall(() => {
135       return this.webNavigation.loadURI(uri, loadURIOptions);
136     });
137   }
139   _assert(condition, msg, line = 0) {
140     let debug = Cc["@mozilla.org/xpcom/debug;1"].getService(Ci.nsIDebug2);
141     if (!condition && debug.isDebugBuild) {
142       debug.warning(`${msg} - ${new Error().stack}`, "WebNavigationChild.js", line);
143       debug.abort("WebNavigationChild.js", line);
144     }
145   }
147   setOriginAttributes(originAttributes) {
148     if (originAttributes) {
149       this.webNavigation.setOriginAttributesBeforeLoading(originAttributes);
150     }
151   }
153   reload(flags) {
154     this.webNavigation.reload(flags);
155   }
157   stop(flags) {
158     this.webNavigation.stop(flags);
159   }