1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_Navigation_h___
#define mozilla_dom_Navigation_h___
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/Maybe.h"
#include "mozilla/RefPtr.h"
#include "mozilla/dom/NavigateEvent.h"
#include "mozilla/dom/NavigationBinding.h"
#include "nsHashtablesFwd.h"
#include "nsStringFwd.h"
class nsIDHashKey;
namespace mozilla::dom {
class FormData;
class NavigationActivation;
class NavigationDestination;
class NavigationHistoryEntry;
struct NavigationNavigateOptions;
struct NavigationOptions;
class NavigationTransition;
struct NavigationUpdateCurrentEntryOptions;
struct NavigationReloadOptions;
struct NavigationResult;
class SessionHistoryInfo;
struct NavigationAPIMethodTracker;
class Navigation final : public DOMEventTargetHelper {
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Navigation, DOMEventTargetHelper)
explicit Navigation(nsPIDOMWindowInner* aWindow);
using EventTarget::EventListenerAdded;
virtual void EventListenerAdded(nsAtom* aType) override;
using EventTarget::EventListenerRemoved;
virtual void EventListenerRemoved(nsAtom* aType) override;
// Navigation.webidl
void Entries(nsTArray<RefPtr<NavigationHistoryEntry>>& aResult) const;
already_AddRefed<NavigationHistoryEntry> GetCurrentEntry() const;
MOZ_CAN_RUN_SCRIPT
void UpdateCurrentEntry(JSContext* aCx,
const NavigationUpdateCurrentEntryOptions& aOptions,
ErrorResult& aRv);
NavigationTransition* GetTransition() const;
NavigationActivation* GetActivation() const;
bool CanGoBack() {
return !HasEntriesAndEventsDisabled() && mCurrentEntryIndex &&
*mCurrentEntryIndex != 0;
}
bool CanGoForward() {
return !HasEntriesAndEventsDisabled() && mCurrentEntryIndex &&
*mCurrentEntryIndex != mEntries.Length() - 1;
}
void Navigate(JSContext* aCx, const nsAString& aUrl,
const NavigationNavigateOptions& aOptions,
NavigationResult& aResult);
MOZ_CAN_RUN_SCRIPT void Reload(JSContext* aCx,
const NavigationReloadOptions& aOptions,
NavigationResult& aResult);
void TraverseTo(JSContext* aCx, const nsAString& aKey,
const NavigationOptions& aOptions, NavigationResult& aResult);
void Back(JSContext* aCx, const NavigationOptions& aOptions,
NavigationResult& aResult);
void Forward(JSContext* aCx, const NavigationOptions& aOptions,
NavigationResult& aResult);
IMPL_EVENT_HANDLER(navigate);
IMPL_EVENT_HANDLER(navigatesuccess);
IMPL_EVENT_HANDLER(navigateerror);
IMPL_EVENT_HANDLER(currententrychange);
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#initialize-the-navigation-api-entries-for-a-new-document
void InitializeHistoryEntries(
mozilla::Span<const SessionHistoryInfo> aNewSHInfos,
const SessionHistoryInfo* aInitialSHInfo);
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#update-the-navigation-api-entries-for-reactivation
MOZ_CAN_RUN_SCRIPT
void UpdateForReactivation(SessionHistoryInfo* aReactivatedEntry);
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#update-the-navigation-api-entries-for-a-same-document-navigation
void UpdateEntriesForSameDocumentNavigation(
SessionHistoryInfo* aDestinationSHE, NavigationType aNavigationType);
JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
// The Navigation API is only enabled if both SessionHistoryInParent and
// the dom.navigation.webidl.enabled pref are set.
static bool IsAPIEnabled(JSContext* /* unused */ = nullptr,
JSObject* /* unused */ = nullptr);
// Wrapper algorithms for firing the navigate event.
// https://html.spec.whatwg.org/#navigate-event-firing
MOZ_CAN_RUN_SCRIPT bool FireTraverseNavigateEvent(
JSContext* aCx, const SessionHistoryInfo& aDestinationSessionHistoryInfo,
Maybe<UserNavigationInvolvement> aUserInvolvement);
MOZ_CAN_RUN_SCRIPT bool FirePushReplaceReloadNavigateEvent(
JSContext* aCx, NavigationType aNavigationType, nsIURI* aDestinationURL,
bool aIsSameDocument, bool aIsSync,
Maybe<UserNavigationInvolvement> aUserInvolvement,
Element* aSourceElement, already_AddRefed<FormData> aFormDataEntryList,
nsIStructuredCloneContainer* aNavigationAPIState,
nsIStructuredCloneContainer* aClassicHistoryAPIState);
MOZ_CAN_RUN_SCRIPT bool FireDownloadRequestNavigateEvent(
JSContext* aCx, nsIURI* aDestinationURL,
UserNavigationInvolvement aUserInvolvement, Element* aSourceElement,
const nsAString& aFilename);
bool FocusedChangedDuringOngoingNavigation() const;
void SetFocusedChangedDuringOngoingNavigation(
bool aFocusChangedDuringOngoingNavigation);
bool HasOngoingNavigateEvent() const;
MOZ_CAN_RUN_SCRIPT
void AbortOngoingNavigation(
JSContext* aCx, JS::Handle<JS::Value> aError = JS::UndefinedHandleValue);
MOZ_CAN_RUN_SCRIPT
void InformAboutChildNavigableDestruction(JSContext* aCx);
void CreateNavigationActivationFrom(
SessionHistoryInfo* aPreviousEntryForActivation,
NavigationType aNavigationType);
private:
friend struct NavigationAPIMethodTracker;
using UpcomingTraverseAPIMethodTrackers =
nsTHashMap<nsIDHashKey, RefPtr<NavigationAPIMethodTracker>>;
~Navigation() = default;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#has-entries-and-events-disabled
bool HasEntriesAndEventsDisabled() const;
MOZ_CAN_RUN_SCRIPT
nsresult FireEvent(const nsAString& aName);
MOZ_CAN_RUN_SCRIPT
nsresult FireErrorEvent(const nsAString& aName,
const ErrorEventInit& aEventInitDict);
// https://html.spec.whatwg.org/#inner-navigate-event-firing-algorithm
MOZ_CAN_RUN_SCRIPT bool InnerFireNavigateEvent(
JSContext* aCx, NavigationType aNavigationType,
NavigationDestination* aDestination,
UserNavigationInvolvement aUserInvolvement, Element* aSourceElement,
already_AddRefed<FormData> aFormDataEntryList,
nsIStructuredCloneContainer* aClassicHistoryAPIState,
const nsAString& aDownloadRequestFilename);
NavigationHistoryEntry* FindNavigationHistoryEntry(
const SessionHistoryInfo& aSessionHistoryInfo) const;
void PromoteUpcomingAPIMethodTrackerToOngoing(Maybe<nsID>&& aDestinationKey);
RefPtr<NavigationAPIMethodTracker>
MaybeSetUpcomingNonTraverseAPIMethodTracker(
JS::Handle<JS::Value> aInfo,
nsIStructuredCloneContainer* aSerializedState);
RefPtr<NavigationAPIMethodTracker> AddUpcomingTraverseAPIMethodTracker(
const nsID& aKey, JS::Handle<JS::Value> aInfo);
void SetEarlyErrorResult(JSContext* aCx, NavigationResult& aResult,
ErrorResult&& aRv) const;
void SetEarlyStateErrorResult(JSContext* aCx, NavigationResult& aResult,
const nsACString& aMessage) const;
bool CheckIfDocumentIsFullyActiveAndMaybeSetEarlyErrorResult(
JSContext* aCx, const Document* aDocument,
NavigationResult& aResult) const;
bool CheckDocumentUnloadCounterAndMaybeSetEarlyErrorResult(
JSContext* aCx, const Document* aDocument,
NavigationResult& aResult) const;
already_AddRefed<nsIStructuredCloneContainer>
CreateSerializedStateAndMaybeSetEarlyErrorResult(
JSContext* aCx, const JS::Value& aState, NavigationResult& aResult) const;
static void CleanUp(NavigationAPIMethodTracker* aNavigationAPIMethodTracker);
Document* GetAssociatedDocument() const;
// Update the state managing if we need to dispatch the traverse event or not.
void UpdateNeedsTraverse();
void LogHistory() const;
void PerformNavigationTraversal(JSContext* aCx, const nsID& aKey,
const NavigationOptions& aOptions,
NavigationResult& aResult);
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-entry-list
nsTArray<RefPtr<NavigationHistoryEntry>> mEntries;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry
Maybe<uint64_t> mCurrentEntryIndex;
// https://html.spec.whatwg.org/#ongoing-navigation-tracking:navigateevent-2
RefPtr<NavigateEvent> mOngoingNavigateEvent;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#focus-changed-during-ongoing-navigation
bool mFocusChangedDuringOngoingNavigation = false;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#suppress-normal-scroll-restoration-during-ongoing-navigation
bool mSuppressNormalScrollRestorationDuringOngoingNavigation = false;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-api-method-tracker
RefPtr<NavigationAPIMethodTracker> mOngoingAPIMethodTracker;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
RefPtr<NavigationAPIMethodTracker> mUpcomingNonTraverseAPIMethodTracker;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-traverse-api-method-trackers
UpcomingTraverseAPIMethodTrackers mUpcomingTraverseAPIMethodTrackers;
// https://html.spec.whatwg.org/#concept-navigation-transition
RefPtr<NavigationTransition> mTransition;
// https://html.spec.whatwg.org/#navigation-activation
RefPtr<NavigationActivation> mActivation;
};
} // namespace mozilla::dom
template <>
struct fmt::formatter<mozilla::dom::NavigationType, char>
: public formatter<nsLiteralCString> {
template <typename FmtContext>
constexpr auto format(const mozilla::dom::NavigationType& aNavigationType,
FmtContext& aCtx) const {
return formatter<nsLiteralCString>::format(
mozilla::dom::GetEnumString(aNavigationType), aCtx);
}
};
#endif // mozilla_dom_Navigation_h___
|