ChildSHistory.h (6168B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /** 8 * ChildSHistory represents a view of session history from a child process. It 9 * exposes getters for some cached history state, and mutators which are 10 * implemented by communicating with the actual history storage. 11 * 12 * NOTE: Currently session history is in transition, meaning that we're still 13 * using the legacy nsSHistory class internally. The API exposed from this class 14 * should be only the API which we expect to expose when this transition is 15 * complete, and special cases will need to call through the LegacySHistory() 16 * getters. 17 */ 18 19 #ifndef mozilla_dom_ChildSHistory_h 20 #define mozilla_dom_ChildSHistory_h 21 22 #include "nsCOMPtr.h" 23 #include "mozilla/dom/BindingDeclarations.h" 24 #include "nsWrapperCache.h" 25 #include "nsThreadUtils.h" 26 #include "mozilla/ErrorResult.h" 27 #include "mozilla/LinkedList.h" 28 #include "nsID.h" 29 30 class nsISHEntry; 31 class nsISHistory; 32 33 namespace mozilla::dom { 34 35 class BrowsingContext; 36 37 class ChildSHistory : public nsISupports, public nsWrapperCache { 38 public: 39 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 40 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ChildSHistory) 41 nsISupports* GetParentObject() const; 42 JSObject* WrapObject(JSContext* cx, 43 JS::Handle<JSObject*> aGivenProto) override; 44 45 explicit ChildSHistory(BrowsingContext* aBrowsingContext); 46 47 void SetBrowsingContext(BrowsingContext* aBrowsingContext); 48 49 // Create or destroy the session history implementation in the child process. 50 // This can be removed once session history is stored exclusively in the 51 // parent process. 52 void SetIsInProcess(bool aIsInProcess); 53 bool IsInProcess() { return !!mHistory; } 54 55 int32_t Count(); 56 int32_t Index(); 57 58 /** Reload the current entry in the session history. */ 59 MOZ_CAN_RUN_SCRIPT 60 void Reload(uint32_t aReloadFlags, ErrorResult& aRv); 61 62 /** 63 * The CanGo and Go methods are called with an offset from the current index. 64 * Positive numbers go forward in history, while negative numbers go 65 * backwards. 66 * aRequireUserInteraction is used in order to enable the back-button 67 * intervention. This causes an additional check that there must be a previous 68 * entry that has been user-interacted. This check is unnecessary when going 69 * forwards as the latest entry is always available, whether it has been 70 * interacted with or not. This feature is gated by the 71 * browser.navigation.requireUserInteraction pref. 72 */ 73 bool CanGo(int32_t aOffset, bool aRequireUserInteraction); 74 MOZ_CAN_RUN_SCRIPT 75 void Go(int32_t aOffset, bool aRequireUserInteraction, bool aUserActivation, 76 ErrorResult& aRv); 77 void AsyncGo(int32_t aOffset, bool aRequireUserInteraction, 78 bool aUserActivation); 79 void AsyncGo(const nsID& aKey, BrowsingContext* aNavigable, 80 bool aRequireUserInteraction, bool aUserActivation, 81 bool aCheckForCancelation, 82 std::function<void(nsresult)>&& aResolver); 83 84 // aIndex is the new index, and aOffset is the offset between new and current. 85 MOZ_CAN_RUN_SCRIPT 86 void GotoIndex(int32_t aIndex, int32_t aOffset, bool aRequireUserInteraction, 87 bool aUserActivation, ErrorResult& aRv); 88 MOZ_CAN_RUN_SCRIPT 89 void GotoKey(const nsID& aKey, BrowsingContext* aNavigable, 90 bool aRequireUserInteraction, bool aUserActivation, 91 bool aCheckForCancelation, 92 const std::function<void(nsresult)>& aResolver, 93 ErrorResult& aRv); 94 95 void RemovePendingHistoryNavigations(); 96 97 /** 98 * Evicts all content viewers within the current process. 99 */ 100 void EvictLocalDocumentViewers(); 101 102 // GetLegacySHistory and LegacySHistory have been deprecated. Don't 103 // use these, but instead handle the interaction with nsISHistory in 104 // the parent process. 105 nsISHistory* GetLegacySHistory(ErrorResult& aError); 106 nsISHistory* LegacySHistory(); 107 108 void SetIndexAndLength(uint32_t aIndex, uint32_t aLength, 109 const nsID& aChangeId); 110 nsID AddPendingHistoryChange(); 111 nsID AddPendingHistoryChange(int32_t aIndexDelta, int32_t aLengthDelta); 112 113 private: 114 virtual ~ChildSHistory(); 115 116 class PendingAsyncHistoryNavigation 117 : public Runnable, 118 public mozilla::LinkedListElement<PendingAsyncHistoryNavigation> { 119 public: 120 PendingAsyncHistoryNavigation(ChildSHistory* aHistory, int32_t aOffset, 121 bool aRequireUserInteraction, 122 bool aUserActivation); 123 124 PendingAsyncHistoryNavigation(ChildSHistory* aHistory, const nsID& aKey, 125 BrowsingContext* aBrowsingContext, 126 bool aRequireUserInteraction, 127 bool aUserActivation, 128 bool aCheckForCancelation, 129 std::function<void(nsresult)>&& aResolver); 130 131 MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD Run() override; 132 133 private: 134 const RefPtr<ChildSHistory> mHistory; 135 bool mRequireUserInteraction; 136 bool mUserActivation; 137 bool mCheckForCancelation; 138 int32_t mOffset; 139 Maybe<nsID> mKey; 140 RefPtr<BrowsingContext> mBrowsingContext; 141 Maybe<std::function<void(nsresult)>> mResolver; 142 }; 143 144 RefPtr<BrowsingContext> mBrowsingContext; 145 nsCOMPtr<nsISHistory> mHistory; 146 // Can be removed once history-in-parent is the only way 147 mozilla::LinkedList<PendingAsyncHistoryNavigation> mPendingNavigations; 148 int32_t mIndex = -1; 149 int32_t mLength = 0; 150 151 struct PendingSHistoryChange { 152 nsID mChangeID; 153 int32_t mIndexDelta; 154 int32_t mLengthDelta; 155 }; 156 AutoTArray<PendingSHistoryChange, 2> mPendingSHistoryChanges; 157 158 // Needs to start 1 above default epoch in parent 159 uint64_t mHistoryEpoch = 1; 160 bool mPendingEpoch = false; 161 }; 162 163 } // namespace mozilla::dom 164 165 #endif /* mozilla_dom_ChildSHistory_h */