diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index 94d91d352e9c..899e3fb13aac 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -13964,3 +13964,36 @@ bool nsDocShell::IsSameDocumentAsActiveEntry( const mozilla::dom::SessionHistoryInfo& aSHInfo) { return mActiveEntry ? mActiveEntry->SharesDocumentWith(aSHInfo) : false; } + +// https://html.spec.whatwg.org/#nav-window +nsPIDOMWindowInner* nsDocShell::GetActiveWindow() { + nsPIDOMWindowOuter* outer = GetWindow(); + return outer ? outer->GetCurrentInnerWindow() : nullptr; +} + +// https://html.spec.whatwg.org/#inform-the-navigation-api-about-aborting-navigation +void nsDocShell::InformNavigationAPIAboutAbortingNavigation(JSContext* aCx) { + // Step 1 + // This becomes an assert since we have a common event loop. + MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread()); + + // No ongoing navigations if we don't have a window. + RefPtr window = GetActiveWindow(); + if (!window) { + return; + } + + // Step 2 + RefPtr navigation = window->Navigation(); + if (!navigation) { + return; + } + + // Step 3 + if (!navigation->HasOngoingNavigateEvent()) { + return; + } + + // Step 4 + navigation->AbortOngoingNavigation(aCx); +} diff --git a/docshell/base/nsDocShell.h b/docshell/base/nsDocShell.h index 771bdea2cfe0..347233a943d0 100644 --- a/docshell/base/nsDocShell.h +++ b/docshell/base/nsDocShell.h @@ -388,6 +388,8 @@ class nsDocShell final : public nsDocLoader, return mozilla::dom::WindowProxyHolder(mBrowsingContext); } + nsPIDOMWindowInner* GetActiveWindow(); + /** * Loads the given URI. See comments on nsDocShellLoadState members for more * information on information used. @@ -1140,6 +1142,9 @@ class nsDocShell final : public nsDocLoader, mozilla::dom::UserNavigationInvolvement::None); private: + MOZ_CAN_RUN_SCRIPT + void InformNavigationAPIAboutAbortingNavigation(JSContext* aCx); + void SetCurrentURIInternal(nsIURI* aURI); // data members diff --git a/dom/navigation/Navigation.cpp b/dom/navigation/Navigation.cpp index 070bd8c891f9..b9493ac38742 100644 --- a/dom/navigation/Navigation.cpp +++ b/dom/navigation/Navigation.cpp @@ -1097,6 +1097,10 @@ void Navigation::SetFocusedChangedDuringOngoingNavigation( mFocusChangedDuringOngoingNavigation = aFocusChangedDUringOngoingNavigation; } +bool Navigation::HasOngoingNavigateEvent() const { + return mOngoingNavigateEvent; +} + // The associated document of navigation's relevant global object. Document* Navigation::GetAssociatedDocument() const { nsGlobalWindowInner* window = GetOwnerWindow(); diff --git a/dom/navigation/Navigation.h b/dom/navigation/Navigation.h index c0ba24528fb5..e8fb79192e6d 100644 --- a/dom/navigation/Navigation.h +++ b/dom/navigation/Navigation.h @@ -128,6 +128,11 @@ class Navigation final : public DOMEventTargetHelper { void SetFocusedChangedDuringOngoingNavigation( bool aFocusChangedDuringOngoingNavigation); + bool HasOngoingNavigateEvent() const; + + void AbortOngoingNavigation( + JSContext* aCx, JS::Handle aError = JS::UndefinedHandleValue); + private: using UpcomingTraverseAPIMethodTrackers = nsTHashMap>; @@ -183,9 +188,6 @@ class Navigation final : public DOMEventTargetHelper { static void CleanUp(NavigationAPIMethodTracker* aNavigationAPIMethodTracker); - void AbortOngoingNavigation( - JSContext* aCx, JS::Handle aError = JS::UndefinedHandleValue); - Document* GetAssociatedDocument() const; void LogHistory() const;