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 file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 ChromeUtils.defineESModuleGetters(lazy, {
8 error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
10 "chrome://remote/content/marionette/actors/MarionetteEventsParent.sys.mjs",
11 Log: "chrome://remote/content/shared/Log.sys.mjs",
13 "chrome://remote/content/shared/webdriver/Capabilities.sys.mjs",
14 ProgressListener: "chrome://remote/content/shared/Navigate.sys.mjs",
15 TimedPromise: "chrome://remote/content/marionette/sync.sys.mjs",
16 truncate: "chrome://remote/content/shared/Format.sys.mjs",
19 ChromeUtils.defineLazyGetter(lazy, "logger", () =>
20 lazy.Log.get(lazy.Log.TYPES.MARIONETTE)
23 // Timeouts used to check if a new navigation has been initiated.
24 const TIMEOUT_BEFOREUNLOAD_EVENT = 200;
25 const TIMEOUT_UNLOAD_EVENT = 5000;
28 export const navigate = {};
31 * Checks the value of readyState for the current page
32 * load activity, and resolves the command if the load
33 * has been finished. It also takes care of the selected
36 * @param {PageLoadStrategy} pageLoadStrategy
37 * Strategy when navigation is considered as finished.
38 * @param {object} eventData
39 * @param {string} eventData.documentURI
40 * Current document URI of the document.
41 * @param {string} eventData.readyState
42 * Current ready state of the document.
45 * True if the page load has been finished.
47 function checkReadyState(pageLoadStrategy, eventData = {}) {
48 const { documentURI, readyState } = eventData;
50 const result = { error: null, finished: false };
54 if (documentURI.startsWith("about:certerror")) {
55 result.error = new lazy.error.InsecureCertificateError();
56 result.finished = true;
57 } else if (/about:.*(error)\?/.exec(documentURI)) {
58 result.error = new lazy.error.UnknownError(
59 `Reached error page: ${documentURI}`
61 result.finished = true;
63 // Return early with a page load strategy of eager, and also
64 // special-case about:blocked pages which should be treated as
65 // non-error pages but do not raise a pageshow event. about:blank
66 // is also treaded specifically here, because it gets temporary
67 // loaded for new content processes, and we only want to rely on
68 // complete loads for it.
70 (pageLoadStrategy === lazy.PageLoadStrategy.Eager &&
71 documentURI != "about:blank") ||
72 /about:blocked\?/.exec(documentURI)
74 result.finished = true;
79 result.finished = true;
87 * Determines if we expect to get a DOM load event (DOMContentLoaded)
88 * on navigating to the <code>future</code> URL.
90 * @param {URL} current
91 * URL the browser is currently visiting.
92 * @param {object} options
93 * @param {BrowsingContext=} options.browsingContext
94 * The current browsing context. Needed for targets of _parent and _top.
95 * @param {URL=} options.future
96 * Destination URL, if known.
97 * @param {target=} options.target
98 * Link target, if known.
101 * Full page load would be expected if future is followed.
104 * If <code>current</code> is not defined, or any of
105 * <code>current</code> or <code>future</code> are invalid URLs.
107 navigate.isLoadEventExpected = function (current, options = {}) {
108 const { browsingContext, future, target } = options;
110 if (typeof current == "undefined") {
111 throw new TypeError("Expected at least one URL");
114 if (["_parent", "_top"].includes(target) && !browsingContext) {
116 "Expected browsingContext when target is _parent or _top"
120 // Don't wait if the navigation happens in a different browsing context
122 target === "_blank" ||
123 (target === "_parent" && browsingContext.parent) ||
124 (target === "_top" && browsingContext.top != browsingContext)
129 // Assume we will go somewhere exciting
130 if (typeof future == "undefined") {
134 // Assume javascript:<whatever> will modify the current document
135 // but this is not an entirely safe assumption to make,
136 // considering it could be used to set window.location
137 if (future.protocol == "javascript:") {
141 // If hashes are present and identical
143 current.href.includes("#") &&
144 future.href.includes("#") &&
145 current.hash === future.hash
154 * Load the given URL in the specified browsing context.
156 * @param {CanonicalBrowsingContext} browsingContext
157 * Browsing context to load the URL into.
158 * @param {string} url
159 * URL to navigate to.
161 navigate.navigateTo = async function (browsingContext, url) {
163 loadFlags: Ci.nsIWebNavigation.LOAD_FLAGS_IS_LINK,
164 triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
165 // Fake user activation.
166 hasValidUserGestureActivation: true,
168 browsingContext.fixupAndLoadURIString(url, opts);
174 * @param {CanonicalBrowsingContext} browsingContext
175 * Browsing context to refresh.
177 navigate.refresh = async function (browsingContext) {
178 const flags = Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
179 browsingContext.reload(flags);
183 * Execute a callback and wait for a possible navigation to complete
185 * @param {GeckoDriver} driver
186 * Reference to driver instance.
187 * @param {Function} callback
188 * Callback to execute that might trigger a navigation.
189 * @param {object} options
190 * @param {BrowsingContext=} options.browsingContext
191 * Browsing context to observe. Defaults to the current browsing context.
192 * @param {boolean=} options.loadEventExpected
193 * If false, return immediately and don't wait for
194 * the navigation to be completed. Defaults to true.
195 * @param {boolean=} options.requireBeforeUnload
196 * If false and no beforeunload event is fired, abort waiting
197 * for the navigation. Defaults to true.
199 navigate.waitForNavigationCompleted = async function waitForNavigationCompleted(
205 browsingContextFn = driver.getBrowsingContext.bind(driver),
206 loadEventExpected = true,
207 requireBeforeUnload = true,
210 const browsingContext = browsingContextFn();
211 const chromeWindow = browsingContext.topChromeWindow;
212 const pageLoadStrategy = driver.currentSession.pageLoadStrategy;
214 // Return immediately if no load event is expected
215 if (!loadEventExpected) {
217 return Promise.resolve();
220 // When not waiting for page load events, do not return until the navigation has actually started.
221 if (pageLoadStrategy === lazy.PageLoadStrategy.None) {
222 const listener = new lazy.ProgressListener(browsingContext.webProgress, {
223 resolveWhenStarted: true,
224 waitForExplicitStart: true,
226 const navigated = listener.start();
227 navigated.finally(() => {
228 if (listener.isStarted) {
236 return Promise.resolve();
239 let rejectNavigation;
240 let resolveNavigation;
242 let browsingContextChanged = false;
243 let seenBeforeUnload = false;
244 let seenUnload = false;
248 const checkDone = ({ finished, error }) => {
251 rejectNavigation(error);
258 const onPromptOpened = action => {
259 lazy.logger.trace("Canceled page load listener because a dialog opened");
260 checkDone({ finished: true });
263 const onTimer = timer => {
264 // In the case when a document has a beforeunload handler
265 // registered, the currently active command will return immediately
266 // due to the modal dialog observer.
268 // Otherwise the timeout waiting for the document to start
269 // navigating is increased by 5000 ms to ensure a possible load
270 // event is not missed. In the common case such an event should
271 // occur pretty soon after beforeunload, and we optimise for this.
272 if (seenBeforeUnload) {
273 seenBeforeUnload = false;
274 unloadTimer.initWithCallback(
276 TIMEOUT_UNLOAD_EVENT,
277 Ci.nsITimer.TYPE_ONE_SHOT
280 // If no page unload has been detected, ensure to properly stop
281 // the load listener, and return from the currently active command.
282 } else if (!seenUnload) {
284 "Canceled page load listener because no navigation " +
287 checkDone({ finished: true });
291 const onNavigation = (eventName, data) => {
292 const browsingContext = browsingContextFn();
294 // Ignore events from other browsing contexts than the selected one.
295 if (data.browsingContext != browsingContext) {
300 lazy.truncate`[${data.browsingContext.id}] Received event ${data.type} for ${data.documentURI}`
305 seenBeforeUnload = true;
314 checkDone({ finished: true });
317 case "DOMContentLoaded":
319 // Don't require an unload event when a top-level browsing context
321 if (!seenUnload && !browsingContextChanged) {
324 const result = checkReadyState(pageLoadStrategy, data);
330 // In the case when the currently selected frame is closed,
331 // there will be no further load events. Stop listening immediately.
332 const onBrowsingContextDiscarded = (subject, topic, why) => {
333 // If the BrowsingContext is being discarded to be replaced by another
334 // context, we don't want to stop waiting for the pageload to complete, as
335 // we will continue listening to the newly created context.
336 if (subject == browsingContextFn() && why != "replace") {
338 "Canceled page load listener " +
339 `because browsing context with id ${subject.id} has been removed`
341 checkDone({ finished: true });
345 // Detect changes to the top-level browsing context to not
346 // necessarily require an unload event.
347 const onBrowsingContextChanged = event => {
348 if (event.target === driver.curBrowser.contentBrowser) {
349 browsingContextChanged = true;
353 const onUnload = event => {
355 "Canceled page load listener " +
356 "because the top-browsing context has been closed"
358 checkDone({ finished: true });
361 chromeWindow.addEventListener("TabClose", onUnload);
362 chromeWindow.addEventListener("unload", onUnload);
363 driver.curBrowser.tabBrowser?.addEventListener(
364 "XULFrameLoaderCreated",
365 onBrowsingContextChanged
367 driver.promptListener.on("opened", onPromptOpened);
368 Services.obs.addObserver(
369 onBrowsingContextDiscarded,
370 "browsing-context-discarded"
373 lazy.EventDispatcher.on("page-load", onNavigation);
375 return new lazy.TimedPromise(
376 async (resolve, reject) => {
377 rejectNavigation = reject;
378 resolveNavigation = resolve;
383 // Certain commands like clickElement can cause a navigation. Setup a timer
384 // to check if a "beforeunload" event has been emitted within the given
385 // time frame. If not resolve the Promise.
386 if (!requireBeforeUnload) {
387 unloadTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
388 unloadTimer.initWithCallback(
390 TIMEOUT_BEFOREUNLOAD_EVENT,
391 Ci.nsITimer.TYPE_ONE_SHOT
395 // Executing the callback above could destroy the actor pair before the
396 // command returns. Such an error has to be ignored.
397 if (e.name !== "AbortError") {
398 checkDone({ finished: true, error: e });
403 errorMessage: "Navigation timed out",
404 timeout: driver.currentSession.timeouts.pageLoad,
407 // Clean-up all registered listeners and timers
408 Services.obs.removeObserver(
409 onBrowsingContextDiscarded,
410 "browsing-context-discarded"
412 chromeWindow.removeEventListener("TabClose", onUnload);
413 chromeWindow.removeEventListener("unload", onUnload);
414 driver.curBrowser.tabBrowser?.removeEventListener(
415 "XULFrameLoaderCreated",
416 onBrowsingContextChanged
418 driver.promptListener?.off(onPromptOpened);
419 unloadTimer?.cancel();
421 lazy.EventDispatcher.off("page-load", onNavigation);