Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/docshell/shistory/nsSHistory.h
Line
Count
Source (jump to first uncovered line)
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 "nsISHistory.h"
13
#include "nsSHEntryShared.h"
14
#include "nsSimpleEnumerator.h"
15
#include "nsTObserverArray.h"
16
#include "nsWeakReference.h"
17
18
#include "mozilla/LinkedList.h"
19
#include "mozilla/UniquePtr.h"
20
21
class nsIDocShell;
22
class nsDocShell;
23
class nsSHistoryObserver;
24
class nsISHEntry;
25
26
class nsSHistory final : public mozilla::LinkedListElement<nsSHistory>,
27
                         public nsISHistory,
28
                         public nsSupportsWeakReference
29
{
30
public:
31
32
  // The timer based history tracker is used to evict bfcache on expiration.
33
  class HistoryTracker final : public nsExpirationTracker<nsSHEntryShared, 3>
34
  {
35
  public:
36
    explicit HistoryTracker(nsSHistory* aSHistory,
37
                            uint32_t aTimeout,
38
                            nsIEventTarget* aEventTarget)
39
      : nsExpirationTracker(1000 * aTimeout / 2, "HistoryTracker", aEventTarget)
40
0
    {
41
0
      MOZ_ASSERT(aSHistory);
42
0
      mSHistory = aSHistory;
43
0
    }
44
45
  protected:
46
    virtual void NotifyExpired(nsSHEntryShared* aObj) override
47
0
    {
48
0
      RemoveObject(aObj);
49
0
      mSHistory->EvictExpiredContentViewerForEntry(aObj);
50
0
    }
51
52
  private:
53
    // HistoryTracker is owned by nsSHistory; it always outlives HistoryTracker
54
    // so it's safe to use raw pointer here.
55
    nsSHistory* mSHistory;
56
  };
57
58
  // Structure used in SetChildHistoryEntry
59
  struct SwapEntriesData
60
  {
61
    nsDocShell* ignoreShell;     // constant; the shell to ignore
62
    nsISHEntry* destTreeRoot;    // constant; the root of the dest tree
63
    nsISHEntry* destTreeParent;  // constant; the node under destTreeRoot
64
                                 // whose children will correspond to aEntry
65
  };
66
67
  nsSHistory();
68
  NS_DECL_ISUPPORTS
69
  NS_DECL_NSISHISTORY
70
71
  nsresult Reload(uint32_t aReloadFlags);
72
  nsresult GetCurrentURI(nsIURI** aResultURI);
73
74
  // One time initialization method called upon docshell module construction
75
  static nsresult Startup();
76
  static void Shutdown();
77
  static void UpdatePrefs();
78
79
  // Max number of total cached content viewers.  If the pref
80
  // browser.sessionhistory.max_total_viewers is negative, then
81
  // this value is calculated based on the total amount of memory.
82
  // Otherwise, it comes straight from the pref.
83
0
  static uint32_t GetMaxTotalViewers() { return sHistoryMaxTotalViewers; }
84
85
  // Get the root SHEntry from a given entry.
86
  static nsISHEntry* GetRootSHEntry(nsISHEntry* aEntry);
87
88
  // Callback prototype for WalkHistoryEntries.
89
  // aEntry is the child history entry, aShell is its corresponding docshell,
90
  // aChildIndex is the child's index in its parent entry, and aData is
91
  // the opaque pointer passed to WalkHistoryEntries.
92
  typedef nsresult(*WalkHistoryEntriesFunc)(nsISHEntry* aEntry,
93
                                            nsDocShell* aShell,
94
                                            int32_t aChildIndex,
95
                                            void* aData);
96
97
  // Clone a session history tree for subframe navigation.
98
  // The tree rooted at |aSrcEntry| will be cloned into |aDestEntry|, except
99
  // for the entry with id |aCloneID|, which will be replaced with
100
  // |aReplaceEntry|. |aSrcShell| is a (possibly null) docshell which
101
  // corresponds to |aSrcEntry| via its mLSHE or mOHE pointers, and will
102
  // have that pointer updated to point to the cloned history entry.
103
  // If aCloneChildren is true then the children of the entry with id
104
  // |aCloneID| will be cloned into |aReplaceEntry|.
105
  static nsresult CloneAndReplace(nsISHEntry* aSrcEntry,
106
                                  nsDocShell* aSrcShell,
107
                                  uint32_t aCloneID,
108
                                  nsISHEntry* aReplaceEntry,
109
                                  bool aCloneChildren,
110
                                  nsISHEntry** aDestEntry);
111
112
  // Child-walking callback for CloneAndReplace
113
  static nsresult CloneAndReplaceChild(nsISHEntry* aEntry, nsDocShell* aShell,
114
                                       int32_t aChildIndex, void* aData);
115
116
117
  // Child-walking callback for SetHistoryEntry
118
  static nsresult SetChildHistoryEntry(nsISHEntry* aEntry, nsDocShell* aShell,
119
                                       int32_t aEntryIndex, void* aData);
120
121
  // For each child of aRootEntry, find the corresponding docshell which is
122
  // a child of aRootShell, and call aCallback. The opaque pointer aData
123
  // is passed to the callback.
124
  static nsresult WalkHistoryEntries(nsISHEntry* aRootEntry,
125
                                     nsDocShell* aRootShell,
126
                                     WalkHistoryEntriesFunc aCallback,
127
                                     void* aData);
128
129
private:
130
  virtual ~nsSHistory();
131
  friend class nsSHistoryObserver;
132
133
  // The size of the window of SHEntries which can have alive viewers in the
134
  // bfcache around the currently active SHEntry.
135
  //
136
  // We try to keep viewers for SHEntries between index - VIEWER_WINDOW and
137
  // index + VIEWER_WINDOW alive.
138
  static const int32_t VIEWER_WINDOW = 3;
139
140
  nsresult LoadDifferingEntries(nsISHEntry* aPrevEntry, nsISHEntry* aNextEntry,
141
                                nsIDocShell* aRootDocShell, long aLoadType,
142
                                bool& aDifferenceFound);
143
  nsresult InitiateLoad(nsISHEntry* aFrameEntry, nsIDocShell* aFrameDS,
144
                        long aLoadType);
145
146
  nsresult LoadEntry(int32_t aIndex, long aLoadType, uint32_t aHistCmd);
147
148
#ifdef DEBUG
149
  nsresult PrintHistory();
150
#endif
151
152
  // Find the history entry for a given bfcache entry. It only looks up between
153
  // the range where alive viewers may exist (i.e nsSHistory::VIEWER_WINDOW).
154
  nsresult FindEntryForBFCache(nsIBFCacheEntry* aBFEntry,
155
                               nsISHEntry** aResult,
156
                               int32_t* aResultIndex);
157
158
  // Evict content viewers in this window which don't lie in the "safe" range
159
  // around aIndex.
160
  void EvictOutOfRangeWindowContentViewers(int32_t aIndex);
161
  void EvictContentViewerForEntry(nsISHEntry* aEntry);
162
  static void GloballyEvictContentViewers();
163
  static void GloballyEvictAllContentViewers();
164
165
  // Calculates a max number of total
166
  // content viewers to cache, based on amount of total memory
167
  static uint32_t CalcMaxTotalViewers();
168
169
  nsresult LoadNextPossibleEntry(int32_t aNewIndex, long aLoadType,
170
                                 uint32_t aHistCmd);
171
172
  // aIndex is the index of the entry which may be removed.
173
  // If aKeepNext is true, aIndex is compared to aIndex + 1,
174
  // otherwise comparison is done to aIndex - 1.
175
  bool RemoveDuplicate(int32_t aIndex, bool aKeepNext);
176
177
  // Track all bfcache entries and evict on expiration.
178
  mozilla::UniquePtr<HistoryTracker> mHistoryTracker;
179
180
  nsTArray<nsCOMPtr<nsISHEntry>> mEntries; // entries are never null
181
  int32_t mIndex;           // -1 means "no index"
182
  int32_t mRequestedIndex;  // -1 means "no requested index"
183
184
  void WindowIndices(int32_t aIndex, int32_t* aOutStartIndex,
185
                     int32_t* aOutEndIndex);
186
187
  // Length of mEntries.
188
0
  int32_t Length() { return int32_t(mEntries.Length()); }
189
190
  // Session History listeners
191
  nsAutoTObserverArray<nsWeakPtr, 2> mListeners;
192
193
  // Weak reference. Do not refcount this.
194
  nsIDocShell* mRootDocShell;
195
196
  // Max viewers allowed total, across all SHistory objects
197
  static int32_t sHistoryMaxTotalViewers;
198
};
199
200
inline nsISupports*
201
ToSupports(nsSHistory* aObj)
202
0
{
203
0
  return static_cast<nsISHistory*>(aObj);
204
0
}
205
206
#endif /* nsSHistory */