Bug 1825052 [wpt PR 39246] - Update wpt metadata, a=testonly
[gecko.git] / docshell / shistory / nsSHEntryShared.h
blob6bb6344202a649796f0d270df225e9a1ea9baf40
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/. */
7 #ifndef nsSHEntryShared_h__
8 #define nsSHEntryShared_h__
10 #include "nsCOMArray.h"
11 #include "nsCOMPtr.h"
12 #include "nsExpirationTracker.h"
13 #include "nsIBFCacheEntry.h"
14 #include "nsIWeakReferenceUtils.h"
15 #include "nsRect.h"
16 #include "nsString.h"
17 #include "nsStubMutationObserver.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/UniquePtr.h"
22 class nsSHEntry;
23 class nsISHEntry;
24 class nsISHistory;
25 class nsIContentSecurityPolicy;
26 class nsIContentViewer;
27 class nsIDocShellTreeItem;
28 class nsILayoutHistoryState;
29 class nsIPrincipal;
30 class nsDocShellEditorData;
31 class nsFrameLoader;
32 class nsIMutableArray;
33 class nsSHistory;
35 // A document may have multiple SHEntries, either due to hash navigations or
36 // calls to history.pushState. SHEntries corresponding to the same document
37 // share many members; in particular, they share state related to the
38 // back/forward cache.
40 // The classes defined here are the vehicle for this sharing.
42 // Some of the state can only be stored in the process where we did the actual
43 // load, because that's where the objects live (eg. the content viewer).
45 namespace mozilla {
46 namespace dom {
47 class Document;
49 /**
50 * SHEntrySharedState holds shared state both in the child process and in the
51 * parent process.
53 struct SHEntrySharedState {
54 SHEntrySharedState() : mId(GenerateId()) {}
55 SHEntrySharedState(const SHEntrySharedState& aState) = default;
56 SHEntrySharedState(nsIPrincipal* aTriggeringPrincipal,
57 nsIPrincipal* aPrincipalToInherit,
58 nsIPrincipal* aPartitionedPrincipalToInherit,
59 nsIContentSecurityPolicy* aCsp,
60 const nsACString& aContentType)
61 : mId(GenerateId()),
62 mTriggeringPrincipal(aTriggeringPrincipal),
63 mPrincipalToInherit(aPrincipalToInherit),
64 mPartitionedPrincipalToInherit(aPartitionedPrincipalToInherit),
65 mCsp(aCsp),
66 mContentType(aContentType) {}
68 // These members aren't copied by SHEntrySharedParentState::CopyFrom() because
69 // they're specific to a particular content viewer.
70 uint64_t mId = 0;
72 // These members are copied by SHEntrySharedParentState::CopyFrom(). If you
73 // add a member here, be sure to update the CopyFrom() implementation.
74 nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
75 nsCOMPtr<nsIPrincipal> mPrincipalToInherit;
76 nsCOMPtr<nsIPrincipal> mPartitionedPrincipalToInherit;
77 nsCOMPtr<nsIContentSecurityPolicy> mCsp;
78 nsCString mContentType;
79 // Child side updates layout history state when page is being unloaded or
80 // moved to bfcache.
81 nsCOMPtr<nsILayoutHistoryState> mLayoutHistoryState;
82 uint32_t mCacheKey = 0;
83 bool mIsFrameNavigation = false;
84 bool mSaveLayoutState = true;
86 protected:
87 static uint64_t GenerateId();
90 /**
91 * SHEntrySharedParentState holds the shared state that can live in the parent
92 * process.
94 class SHEntrySharedParentState : public SHEntrySharedState {
95 public:
96 friend class SessionHistoryInfo;
98 uint64_t GetId() const { return mId; }
99 void ChangeId(uint64_t aId);
101 void SetFrameLoader(nsFrameLoader* aFrameLoader);
103 nsFrameLoader* GetFrameLoader();
105 void NotifyListenersContentViewerEvicted();
107 nsExpirationState* GetExpirationState() { return &mExpirationState; }
109 SHEntrySharedParentState();
110 SHEntrySharedParentState(nsIPrincipal* aTriggeringPrincipal,
111 nsIPrincipal* aPrincipalToInherit,
112 nsIPrincipal* aPartitionedPrincipalToInherit,
113 nsIContentSecurityPolicy* aCsp,
114 const nsACString& aContentType);
116 // This returns the existing SHEntrySharedParentState that was registered for
117 // aId, if one exists.
118 static SHEntrySharedParentState* Lookup(uint64_t aId);
120 protected:
121 virtual ~SHEntrySharedParentState();
122 NS_INLINE_DECL_VIRTUAL_REFCOUNTING_WITH_DESTROY(SHEntrySharedParentState,
123 Destroy())
125 virtual void Destroy() { delete this; }
127 void CopyFrom(SHEntrySharedParentState* aSource);
129 // These members are copied by SHEntrySharedParentState::CopyFrom(). If you
130 // add a member here, be sure to update the CopyFrom() implementation.
131 nsID mDocShellID{};
133 nsIntRect mViewerBounds{0, 0, 0, 0};
135 uint32_t mLastTouched = 0;
137 // These members aren't copied by SHEntrySharedParentState::CopyFrom() because
138 // they're specific to a particular content viewer.
139 nsWeakPtr mSHistory;
141 RefPtr<nsFrameLoader> mFrameLoader;
143 nsExpirationState mExpirationState;
145 bool mSticky = true;
146 bool mDynamicallyCreated = false;
148 // This flag is about necko cache, not bfcache.
149 bool mExpired = false;
153 * SHEntrySharedChildState holds the shared state that needs to live in the
154 * process where the document was loaded.
156 class SHEntrySharedChildState {
157 protected:
158 void CopyFrom(SHEntrySharedChildState* aSource);
160 public:
161 // These members are copied by SHEntrySharedChildState::CopyFrom(). If you
162 // add a member here, be sure to update the CopyFrom() implementation.
163 nsCOMArray<nsIDocShellTreeItem> mChildShells;
165 // These members aren't copied by SHEntrySharedChildState::CopyFrom() because
166 // they're specific to a particular content viewer.
167 nsCOMPtr<nsIContentViewer> mContentViewer;
168 RefPtr<mozilla::dom::Document> mDocument;
169 nsCOMPtr<nsISupports> mWindowState;
170 // FIXME Move to parent?
171 nsCOMPtr<nsIMutableArray> mRefreshURIList;
172 UniquePtr<nsDocShellEditorData> mEditorData;
175 } // namespace dom
176 } // namespace mozilla
179 * nsSHEntryShared holds the shared state if the session history is not stored
180 * in the parent process, or if the load itself happens in the parent process.
181 * Note, since nsSHEntryShared inherits both SHEntrySharedParentState and
182 * SHEntrySharedChildState and those have some same member variables,
183 * the ones from SHEntrySharedParentState should be used.
185 class nsSHEntryShared final : public nsIBFCacheEntry,
186 public nsStubMutationObserver,
187 public mozilla::dom::SHEntrySharedParentState,
188 public mozilla::dom::SHEntrySharedChildState {
189 public:
190 static void EnsureHistoryTracker();
191 static void Shutdown();
193 using SHEntrySharedParentState::SHEntrySharedParentState;
195 already_AddRefed<nsSHEntryShared> Duplicate();
197 NS_DECL_ISUPPORTS_INHERITED
198 NS_DECL_NSIBFCACHEENTRY
200 // The nsIMutationObserver bits we actually care about.
201 NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
202 NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
203 NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
204 NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
205 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
207 private:
208 ~nsSHEntryShared();
210 friend class nsSHEntry;
212 void RemoveFromExpirationTracker();
213 void SyncPresentationState();
214 void DropPresentationState();
216 nsresult SetContentViewer(nsIContentViewer* aViewer);
219 #endif