Line data Source code
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 : #ifndef nsSHistory_h
8 : #define nsSHistory_h
9 :
10 : #include "nsCOMPtr.h"
11 : #include "nsExpirationTracker.h"
12 : #include "nsIPartialSHistoryListener.h"
13 : #include "nsISHistory.h"
14 : #include "nsISHistoryInternal.h"
15 : #include "nsISimpleEnumerator.h"
16 : #include "nsIWebNavigation.h"
17 : #include "nsSHEntryShared.h"
18 : #include "nsTObserverArray.h"
19 : #include "nsWeakReference.h"
20 :
21 : #include "mozilla/LinkedList.h"
22 : #include "mozilla/UniquePtr.h"
23 :
24 : class nsIDocShell;
25 : class nsSHEnumerator;
26 : class nsSHistoryObserver;
27 : class nsISHEntry;
28 : class nsISHTransaction;
29 :
30 : class nsSHistory final : public mozilla::LinkedListElement<nsSHistory>,
31 : public nsISHistory,
32 : public nsISHistoryInternal,
33 : public nsIWebNavigation,
34 : public nsSupportsWeakReference
35 : {
36 : public:
37 :
38 : // The timer based history tracker is used to evict bfcache on expiration.
39 0 : class HistoryTracker final : public nsExpirationTracker<nsSHEntryShared, 3>
40 : {
41 : public:
42 2 : explicit HistoryTracker(nsSHistory* aSHistory,
43 : uint32_t aTimeout,
44 : nsIEventTarget* aEventTarget)
45 2 : : nsExpirationTracker(1000 * aTimeout / 2, "HistoryTracker", aEventTarget)
46 : {
47 2 : MOZ_ASSERT(aSHistory);
48 2 : mSHistory = aSHistory;
49 2 : }
50 :
51 : protected:
52 0 : virtual void NotifyExpired(nsSHEntryShared* aObj)
53 : {
54 0 : RemoveObject(aObj);
55 0 : mSHistory->EvictExpiredContentViewerForEntry(aObj);
56 0 : }
57 :
58 : private:
59 : // HistoryTracker is owned by nsSHistory; it always outlives HistoryTracker
60 : // so it's safe to use raw pointer here.
61 : nsSHistory* mSHistory;
62 : };
63 :
64 : nsSHistory();
65 : NS_DECL_ISUPPORTS
66 : NS_DECL_NSISHISTORY
67 : NS_DECL_NSISHISTORYINTERNAL
68 : NS_DECL_NSIWEBNAVIGATION
69 :
70 : // One time initialization method called upon docshell module construction
71 : static nsresult Startup();
72 : static void Shutdown();
73 : static void UpdatePrefs();
74 :
75 : // Max number of total cached content viewers. If the pref
76 : // browser.sessionhistory.max_total_viewers is negative, then
77 : // this value is calculated based on the total amount of memory.
78 : // Otherwise, it comes straight from the pref.
79 0 : static uint32_t GetMaxTotalViewers() { return sHistoryMaxTotalViewers; }
80 :
81 : private:
82 : virtual ~nsSHistory();
83 : friend class nsSHEnumerator;
84 : friend class nsSHistoryObserver;
85 :
86 : // Could become part of nsIWebNavigation
87 : NS_IMETHOD GetTransactionAtIndex(int32_t aIndex, nsISHTransaction** aResult);
88 : nsresult LoadDifferingEntries(nsISHEntry* aPrevEntry, nsISHEntry* aNextEntry,
89 : nsIDocShell* aRootDocShell, long aLoadType,
90 : bool& aDifferenceFound);
91 : nsresult InitiateLoad(nsISHEntry* aFrameEntry, nsIDocShell* aFrameDS,
92 : long aLoadType);
93 :
94 : NS_IMETHOD LoadEntry(int32_t aIndex, long aLoadType, uint32_t aHistCmd);
95 :
96 : #ifdef DEBUG
97 : nsresult PrintHistory();
98 : #endif
99 :
100 : // Evict content viewers in this window which don't lie in the "safe" range
101 : // around aIndex.
102 : void EvictOutOfRangeWindowContentViewers(int32_t aIndex);
103 : void EvictContentViewerForTransaction(nsISHTransaction* aTrans);
104 : static void GloballyEvictContentViewers();
105 : static void GloballyEvictAllContentViewers();
106 :
107 : // Calculates a max number of total
108 : // content viewers to cache, based on amount of total memory
109 : static uint32_t CalcMaxTotalViewers();
110 :
111 : nsresult LoadNextPossibleEntry(int32_t aNewIndex, long aLoadType,
112 : uint32_t aHistCmd);
113 :
114 : // aIndex is the index of the transaction which may be removed.
115 : // If aKeepNext is true, aIndex is compared to aIndex + 1,
116 : // otherwise comparison is done to aIndex - 1.
117 : bool RemoveDuplicate(int32_t aIndex, bool aKeepNext);
118 :
119 : // Track all bfcache entries and evict on expiration.
120 : mozilla::UniquePtr<HistoryTracker> mHistoryTracker;
121 :
122 : nsCOMPtr<nsISHTransaction> mListRoot;
123 : int32_t mIndex;
124 : int32_t mLength;
125 : int32_t mRequestedIndex;
126 :
127 : // The number of entries before this session history object.
128 : int32_t mGlobalIndexOffset;
129 :
130 : // The number of entries after this session history object.
131 : int32_t mEntriesInFollowingPartialHistories;
132 :
133 : // Session History listeners
134 : nsAutoTObserverArray<nsWeakPtr, 2> mListeners;
135 :
136 : // Partial session history listener
137 : nsWeakPtr mPartialHistoryListener;
138 :
139 : // Weak reference. Do not refcount this.
140 : nsIDocShell* mRootDocShell;
141 :
142 : // Set to true if attached to a grouped session history.
143 : bool mIsPartial;
144 :
145 : // Max viewers allowed total, across all SHistory objects
146 : static int32_t sHistoryMaxTotalViewers;
147 : };
148 :
149 : class nsSHEnumerator : public nsISimpleEnumerator
150 : {
151 : public:
152 : NS_DECL_ISUPPORTS
153 : NS_DECL_NSISIMPLEENUMERATOR
154 :
155 : explicit nsSHEnumerator(nsSHistory* aHistory);
156 :
157 : protected:
158 : friend class nsSHistory;
159 : virtual ~nsSHEnumerator();
160 :
161 : private:
162 : int32_t mIndex;
163 : nsSHistory* mSHistory;
164 : };
165 :
166 : #endif /* nsSHistory */
|