2015-05-08 14:37:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2005-08-18 15:15:08 +04:00
|
|
|
|
|
|
|
#ifndef nsSHistory_h
|
|
|
|
#define nsSHistory_h
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2017-04-27 13:16:46 +03:00
|
|
|
#include "nsExpirationTracker.h"
|
2005-08-18 15:15:33 +04:00
|
|
|
#include "nsISHistory.h"
|
2017-04-27 13:16:46 +03:00
|
|
|
#include "nsSHEntryShared.h"
|
2018-08-19 00:22:47 +03:00
|
|
|
#include "nsSimpleEnumerator.h"
|
2012-09-20 18:41:18 +04:00
|
|
|
#include "nsTObserverArray.h"
|
2017-04-27 13:59:11 +03:00
|
|
|
#include "nsWeakReference.h"
|
2005-09-23 22:16:40 +04:00
|
|
|
|
2017-04-19 20:05:00 +03:00
|
|
|
#include "mozilla/LinkedList.h"
|
2017-04-27 13:59:11 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2005-08-18 15:15:08 +04:00
|
|
|
|
2005-08-18 15:15:34 +04:00
|
|
|
class nsIDocShell;
|
2018-02-02 01:38:07 +03:00
|
|
|
class nsDocShell;
|
2005-09-30 00:24:27 +04:00
|
|
|
class nsSHistoryObserver;
|
2013-09-04 19:26:20 +04:00
|
|
|
class nsISHEntry;
|
|
|
|
|
2017-04-19 20:05:00 +03:00
|
|
|
class nsSHistory final : public mozilla::LinkedListElement<nsSHistory>,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsISHistory,
|
2017-04-27 13:59:11 +03:00
|
|
|
public nsSupportsWeakReference {
|
2005-08-18 15:15:08 +04:00
|
|
|
public:
|
2017-04-27 13:59:11 +03:00
|
|
|
// The timer based history tracker is used to evict bfcache on expiration.
|
|
|
|
class HistoryTracker final : public nsExpirationTracker<nsSHEntryShared, 3> {
|
|
|
|
public:
|
|
|
|
explicit HistoryTracker(nsSHistory* aSHistory, uint32_t aTimeout,
|
|
|
|
nsIEventTarget* aEventTarget)
|
|
|
|
: nsExpirationTracker(1000 * aTimeout / 2, "HistoryTracker",
|
|
|
|
aEventTarget) {
|
|
|
|
MOZ_ASSERT(aSHistory);
|
|
|
|
mSHistory = aSHistory;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual void NotifyExpired(nsSHEntryShared* aObj) override {
|
2017-04-27 13:59:11 +03:00
|
|
|
RemoveObject(aObj);
|
|
|
|
mSHistory->EvictExpiredContentViewerForEntry(aObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// HistoryTracker is owned by nsSHistory; it always outlives HistoryTracker
|
|
|
|
// so it's safe to use raw pointer here.
|
|
|
|
nsSHistory* mSHistory;
|
|
|
|
};
|
|
|
|
|
2017-12-19 13:26:36 +03:00
|
|
|
// Structure used in SetChildHistoryEntry
|
|
|
|
struct SwapEntriesData {
|
|
|
|
nsDocShell* ignoreShell; // constant; the shell to ignore
|
|
|
|
nsISHEntry* destTreeRoot; // constant; the root of the dest tree
|
|
|
|
nsISHEntry* destTreeParent; // constant; the node under destTreeRoot
|
|
|
|
// whose children will correspond to aEntry
|
|
|
|
};
|
|
|
|
|
2019-04-04 19:20:19 +03:00
|
|
|
explicit nsSHistory(nsDocShell* aRootDocShell);
|
2005-08-18 15:16:01 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISHISTORY
|
2018-09-03 02:27:37 +03:00
|
|
|
|
|
|
|
nsresult Reload(uint32_t aReloadFlags);
|
2005-08-18 15:15:08 +04:00
|
|
|
|
2005-09-23 22:16:40 +04:00
|
|
|
// One time initialization method called upon docshell module construction
|
|
|
|
static nsresult Startup();
|
2011-06-25 05:55:46 +04:00
|
|
|
static void Shutdown();
|
|
|
|
static void UpdatePrefs();
|
2005-09-23 22:16:40 +04:00
|
|
|
|
2005-10-02 00:38:47 +04:00
|
|
|
// Max number of total cached content viewers. If the pref
|
|
|
|
// browser.sessionhistory.max_total_viewers is negative, then
|
|
|
|
// this value is calculated based on the total amount of memory.
|
|
|
|
// Otherwise, it comes straight from the pref.
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t GetMaxTotalViewers() { return sHistoryMaxTotalViewers; }
|
2005-08-18 15:15:59 +04:00
|
|
|
|
2017-12-19 13:26:36 +03:00
|
|
|
// Get the root SHEntry from a given entry.
|
|
|
|
static nsISHEntry* GetRootSHEntry(nsISHEntry* aEntry);
|
|
|
|
|
|
|
|
// Callback prototype for WalkHistoryEntries.
|
|
|
|
// aEntry is the child history entry, aShell is its corresponding docshell,
|
|
|
|
// aChildIndex is the child's index in its parent entry, and aData is
|
|
|
|
// the opaque pointer passed to WalkHistoryEntries.
|
|
|
|
typedef nsresult (*WalkHistoryEntriesFunc)(nsISHEntry* aEntry,
|
|
|
|
nsDocShell* aShell,
|
|
|
|
int32_t aChildIndex, void* aData);
|
|
|
|
|
|
|
|
// Clone a session history tree for subframe navigation.
|
|
|
|
// The tree rooted at |aSrcEntry| will be cloned into |aDestEntry|, except
|
|
|
|
// for the entry with id |aCloneID|, which will be replaced with
|
|
|
|
// |aReplaceEntry|. |aSrcShell| is a (possibly null) docshell which
|
|
|
|
// corresponds to |aSrcEntry| via its mLSHE or mOHE pointers, and will
|
|
|
|
// have that pointer updated to point to the cloned history entry.
|
|
|
|
// If aCloneChildren is true then the children of the entry with id
|
|
|
|
// |aCloneID| will be cloned into |aReplaceEntry|.
|
|
|
|
static nsresult CloneAndReplace(nsISHEntry* aSrcEntry, nsDocShell* aSrcShell,
|
|
|
|
uint32_t aCloneID, nsISHEntry* aReplaceEntry,
|
|
|
|
bool aCloneChildren, nsISHEntry** aDestEntry);
|
|
|
|
|
|
|
|
// Child-walking callback for CloneAndReplace
|
|
|
|
static nsresult CloneAndReplaceChild(nsISHEntry* aEntry, nsDocShell* aShell,
|
|
|
|
int32_t aChildIndex, void* aData);
|
|
|
|
|
|
|
|
// Child-walking callback for SetHistoryEntry
|
|
|
|
static nsresult SetChildHistoryEntry(nsISHEntry* aEntry, nsDocShell* aShell,
|
|
|
|
int32_t aEntryIndex, void* aData);
|
|
|
|
|
|
|
|
// For each child of aRootEntry, find the corresponding docshell which is
|
|
|
|
// a child of aRootShell, and call aCallback. The opaque pointer aData
|
|
|
|
// is passed to the callback.
|
|
|
|
static nsresult WalkHistoryEntries(nsISHEntry* aRootEntry,
|
|
|
|
nsDocShell* aRootShell,
|
|
|
|
WalkHistoryEntriesFunc aCallback,
|
|
|
|
void* aData);
|
|
|
|
|
2016-10-14 10:18:29 +03:00
|
|
|
private:
|
2005-08-18 15:16:01 +04:00
|
|
|
virtual ~nsSHistory();
|
2005-09-30 00:24:27 +04:00
|
|
|
friend class nsSHistoryObserver;
|
2005-08-18 15:15:59 +04:00
|
|
|
|
2018-09-11 09:08:39 +03:00
|
|
|
// The size of the window of SHEntries which can have alive viewers in the
|
|
|
|
// bfcache around the currently active SHEntry.
|
|
|
|
//
|
|
|
|
// We try to keep viewers for SHEntries between index - VIEWER_WINDOW and
|
|
|
|
// index + VIEWER_WINDOW alive.
|
|
|
|
static const int32_t VIEWER_WINDOW = 3;
|
|
|
|
|
2016-10-20 12:33:22 +03:00
|
|
|
nsresult LoadDifferingEntries(nsISHEntry* aPrevEntry, nsISHEntry* aNextEntry,
|
|
|
|
nsIDocShell* aRootDocShell, long aLoadType,
|
|
|
|
bool& aDifferenceFound);
|
2015-05-06 20:57:23 +03:00
|
|
|
nsresult InitiateLoad(nsISHEntry* aFrameEntry, nsIDocShell* aFrameDS,
|
|
|
|
long aLoadType);
|
2005-08-18 15:16:33 +04:00
|
|
|
|
2017-08-24 06:27:11 +03:00
|
|
|
nsresult LoadEntry(int32_t aIndex, long aLoadType, uint32_t aHistCmd);
|
2009-09-01 20:45:05 +04:00
|
|
|
|
2005-08-18 15:16:52 +04:00
|
|
|
#ifdef DEBUG
|
2015-05-06 20:57:23 +03:00
|
|
|
nsresult PrintHistory();
|
2005-08-18 15:16:52 +04:00
|
|
|
#endif
|
|
|
|
|
2018-09-05 02:03:22 +03:00
|
|
|
// Find the history entry for a given bfcache entry. It only looks up between
|
2018-09-11 09:08:39 +03:00
|
|
|
// the range where alive viewers may exist (i.e nsSHistory::VIEWER_WINDOW).
|
2018-09-05 02:03:22 +03:00
|
|
|
nsresult FindEntryForBFCache(nsIBFCacheEntry* aBFEntry, nsISHEntry** aResult,
|
|
|
|
int32_t* aResultIndex);
|
2017-08-24 09:32:23 +03:00
|
|
|
|
2011-10-21 19:26:34 +04:00
|
|
|
// Evict content viewers in this window which don't lie in the "safe" range
|
|
|
|
// around aIndex.
|
2012-08-22 19:56:38 +04:00
|
|
|
void EvictOutOfRangeWindowContentViewers(int32_t aIndex);
|
2018-09-05 02:03:22 +03:00
|
|
|
void EvictContentViewerForEntry(nsISHEntry* aEntry);
|
2011-10-21 19:26:34 +04:00
|
|
|
static void GloballyEvictContentViewers();
|
|
|
|
static void GloballyEvictAllContentViewers();
|
2005-08-18 15:17:00 +04:00
|
|
|
|
2005-10-02 00:38:47 +04:00
|
|
|
// Calculates a max number of total
|
|
|
|
// content viewers to cache, based on amount of total memory
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t CalcMaxTotalViewers();
|
2005-10-02 00:38:47 +04:00
|
|
|
|
2015-05-06 20:57:23 +03:00
|
|
|
nsresult LoadNextPossibleEntry(int32_t aNewIndex, long aLoadType,
|
|
|
|
uint32_t aHistCmd);
|
|
|
|
|
2018-09-05 02:03:22 +03:00
|
|
|
// aIndex is the index of the entry which may be removed.
|
2011-10-17 18:59:28 +04:00
|
|
|
// If aKeepNext is true, aIndex is compared to aIndex + 1,
|
2010-11-15 13:11:51 +03:00
|
|
|
// otherwise comparison is done to aIndex - 1.
|
2012-08-22 19:56:38 +04:00
|
|
|
bool RemoveDuplicate(int32_t aIndex, bool aKeepNext);
|
2010-09-05 22:10:35 +04:00
|
|
|
|
2017-04-27 13:59:11 +03:00
|
|
|
// Track all bfcache entries and evict on expiration.
|
|
|
|
mozilla::UniquePtr<HistoryTracker> mHistoryTracker;
|
|
|
|
|
2018-09-05 02:03:22 +03:00
|
|
|
nsTArray<nsCOMPtr<nsISHEntry>> mEntries; // entries are never null
|
2018-08-27 07:28:46 +03:00
|
|
|
int32_t mIndex; // -1 means "no index"
|
|
|
|
int32_t mRequestedIndex; // -1 means "no requested index"
|
|
|
|
|
2018-08-28 05:00:41 +03:00
|
|
|
void WindowIndices(int32_t aIndex, int32_t* aOutStartIndex,
|
|
|
|
int32_t* aOutEndIndex);
|
|
|
|
|
2018-09-05 02:03:22 +03:00
|
|
|
// Length of mEntries.
|
|
|
|
int32_t Length() { return int32_t(mEntries.Length()); }
|
2016-10-14 10:18:29 +03:00
|
|
|
|
2012-09-20 18:41:18 +04:00
|
|
|
// Session History listeners
|
|
|
|
nsAutoTObserverArray<nsWeakPtr, 2> mListeners;
|
2016-10-14 10:18:29 +03:00
|
|
|
|
2005-08-18 15:15:59 +04:00
|
|
|
// Weak reference. Do not refcount this.
|
2015-05-06 20:57:23 +03:00
|
|
|
nsIDocShell* mRootDocShell;
|
2005-09-23 22:16:40 +04:00
|
|
|
|
|
|
|
// Max viewers allowed total, across all SHistory objects
|
2015-05-06 20:57:23 +03:00
|
|
|
static int32_t sHistoryMaxTotalViewers;
|
2005-08-18 15:15:08 +04:00
|
|
|
};
|
2015-05-06 20:57:23 +03:00
|
|
|
|
2018-02-02 01:38:07 +03:00
|
|
|
inline nsISupports* ToSupports(nsSHistory* aObj) {
|
|
|
|
return static_cast<nsISHistory*>(aObj);
|
|
|
|
}
|
|
|
|
|
2015-05-06 20:57:23 +03:00
|
|
|
#endif /* nsSHistory */
|