Blame docshell/shistory/nsSHistory.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
#ifndef nsSHistory_h
Packit f0b94e
#define nsSHistory_h
Packit f0b94e
Packit f0b94e
#include "nsCOMPtr.h"
Packit f0b94e
#include "nsExpirationTracker.h"
Packit f0b94e
#include "nsISHistory.h"
Packit f0b94e
#include "nsISHistoryInternal.h"
Packit f0b94e
#include "nsISimpleEnumerator.h"
Packit f0b94e
#include "nsIWebNavigation.h"
Packit f0b94e
#include "nsSHEntryShared.h"
Packit f0b94e
#include "nsTObserverArray.h"
Packit f0b94e
#include "nsWeakReference.h"
Packit f0b94e
Packit f0b94e
#include "mozilla/LinkedList.h"
Packit f0b94e
#include "mozilla/UniquePtr.h"
Packit f0b94e
Packit f0b94e
class nsIDocShell;
Packit f0b94e
class nsSHEnumerator;
Packit f0b94e
class nsSHistoryObserver;
Packit f0b94e
class nsISHEntry;
Packit f0b94e
class nsISHTransaction;
Packit f0b94e
Packit f0b94e
class nsSHistory final : public mozilla::LinkedListElement<nsSHistory>,
Packit f0b94e
                         public nsISHistory,
Packit f0b94e
                         public nsISHistoryInternal,
Packit f0b94e
                         public nsIWebNavigation,
Packit f0b94e
                         public nsSupportsWeakReference {
Packit f0b94e
 public:
Packit f0b94e
  // The timer based history tracker is used to evict bfcache on expiration.
Packit f0b94e
  class HistoryTracker final : public nsExpirationTracker<nsSHEntryShared, 3> {
Packit f0b94e
   public:
Packit f0b94e
    explicit HistoryTracker(nsSHistory* aSHistory, uint32_t aTimeout,
Packit f0b94e
                            nsIEventTarget* aEventTarget)
Packit f0b94e
        : nsExpirationTracker(1000 * aTimeout / 2, "HistoryTracker",
Packit f0b94e
                              aEventTarget) {
Packit f0b94e
      MOZ_ASSERT(aSHistory);
Packit f0b94e
      mSHistory = aSHistory;
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
   protected:
Packit f0b94e
    virtual void NotifyExpired(nsSHEntryShared* aObj) override {
Packit f0b94e
      RemoveObject(aObj);
Packit f0b94e
      mSHistory->EvictExpiredContentViewerForEntry(aObj);
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
   private:
Packit f0b94e
    // HistoryTracker is owned by nsSHistory; it always outlives HistoryTracker
Packit f0b94e
    // so it's safe to use raw pointer here.
Packit f0b94e
    nsSHistory* mSHistory;
Packit f0b94e
  };
Packit f0b94e
Packit f0b94e
  // Structure used in SetChildHistoryEntry
Packit f0b94e
  struct SwapEntriesData {
Packit f0b94e
    nsDocShell* ignoreShell;     // constant; the shell to ignore
Packit f0b94e
    nsISHEntry* destTreeRoot;    // constant; the root of the dest tree
Packit f0b94e
    nsISHEntry* destTreeParent;  // constant; the node under destTreeRoot
Packit f0b94e
                                 // whose children will correspond to aEntry
Packit f0b94e
  };
Packit f0b94e
Packit f0b94e
  nsSHistory();
Packit f0b94e
  NS_DECL_ISUPPORTS
Packit f0b94e
  NS_DECL_NSISHISTORY
Packit f0b94e
  NS_DECL_NSISHISTORYINTERNAL
Packit f0b94e
  NS_DECL_NSIWEBNAVIGATION
Packit f0b94e
Packit f0b94e
  // One time initialization method called upon docshell module construction
Packit f0b94e
  static nsresult Startup();
Packit f0b94e
  static void Shutdown();
Packit f0b94e
  static void UpdatePrefs();
Packit f0b94e
Packit f0b94e
  // Max number of total cached content viewers.  If the pref
Packit f0b94e
  // browser.sessionhistory.max_total_viewers is negative, then
Packit f0b94e
  // this value is calculated based on the total amount of memory.
Packit f0b94e
  // Otherwise, it comes straight from the pref.
Packit f0b94e
  static uint32_t GetMaxTotalViewers() { return sHistoryMaxTotalViewers; }
Packit f0b94e
Packit f0b94e
  // Get the root SHEntry from a given entry.
Packit f0b94e
  static nsISHEntry* GetRootSHEntry(nsISHEntry* aEntry);
Packit f0b94e
Packit f0b94e
  // Callback prototype for WalkHistoryEntries.
Packit f0b94e
  // aEntry is the child history entry, aShell is its corresponding docshell,
Packit f0b94e
  // aChildIndex is the child's index in its parent entry, and aData is
Packit f0b94e
  // the opaque pointer passed to WalkHistoryEntries.
Packit f0b94e
  typedef nsresult (*WalkHistoryEntriesFunc)(nsISHEntry* aEntry,
Packit f0b94e
                                             nsDocShell* aShell,
Packit f0b94e
                                             int32_t aChildIndex, void* aData);
Packit f0b94e
Packit f0b94e
  // Clone a session history tree for subframe navigation.
Packit f0b94e
  // The tree rooted at |aSrcEntry| will be cloned into |aDestEntry|, except
Packit f0b94e
  // for the entry with id |aCloneID|, which will be replaced with
Packit f0b94e
  // |aReplaceEntry|. |aSrcShell| is a (possibly null) docshell which
Packit f0b94e
  // corresponds to |aSrcEntry| via its mLSHE or mOHE pointers, and will
Packit f0b94e
  // have that pointer updated to point to the cloned history entry.
Packit f0b94e
  // If aCloneChildren is true then the children of the entry with id
Packit f0b94e
  // |aCloneID| will be cloned into |aReplaceEntry|.
Packit f0b94e
  static nsresult CloneAndReplace(nsISHEntry* aSrcEntry, nsDocShell* aSrcShell,
Packit f0b94e
                                  uint32_t aCloneID, nsISHEntry* aReplaceEntry,
Packit f0b94e
                                  bool aCloneChildren, nsISHEntry** aDestEntry);
Packit f0b94e
Packit f0b94e
  // Child-walking callback for CloneAndReplace
Packit f0b94e
  static nsresult CloneAndReplaceChild(nsISHEntry* aEntry, nsDocShell* aShell,
Packit f0b94e
                                       int32_t aChildIndex, void* aData);
Packit f0b94e
Packit f0b94e
  // Child-walking callback for SetHistoryEntry
Packit f0b94e
  static nsresult SetChildHistoryEntry(nsISHEntry* aEntry, nsDocShell* aShell,
Packit f0b94e
                                       int32_t aEntryIndex, void* aData);
Packit f0b94e
Packit f0b94e
  // For each child of aRootEntry, find the corresponding docshell which is
Packit f0b94e
  // a child of aRootShell, and call aCallback. The opaque pointer aData
Packit f0b94e
  // is passed to the callback.
Packit f0b94e
  static nsresult WalkHistoryEntries(nsISHEntry* aRootEntry,
Packit f0b94e
                                     nsDocShell* aRootShell,
Packit f0b94e
                                     WalkHistoryEntriesFunc aCallback,
Packit f0b94e
                                     void* aData);
Packit f0b94e
Packit f0b94e
 private:
Packit f0b94e
  virtual ~nsSHistory();
Packit f0b94e
  friend class nsSHEnumerator;
Packit f0b94e
  friend class nsSHistoryObserver;
Packit f0b94e
Packit f0b94e
  nsresult GetTransactionAtIndex(int32_t aIndex, nsISHTransaction** aResult);
Packit f0b94e
  nsresult LoadDifferingEntries(nsISHEntry* aPrevEntry, nsISHEntry* aNextEntry,
Packit f0b94e
                                nsIDocShell* aRootDocShell, long aLoadType,
Packit f0b94e
                                bool& aDifferenceFound);
Packit f0b94e
  nsresult InitiateLoad(nsISHEntry* aFrameEntry, nsIDocShell* aFrameDS,
Packit f0b94e
                        long aLoadType);
Packit f0b94e
Packit f0b94e
  nsresult LoadEntry(int32_t aIndex, long aLoadType, uint32_t aHistCmd);
Packit f0b94e
Packit f0b94e
#ifdef DEBUG
Packit f0b94e
  nsresult PrintHistory();
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
  // Find the transaction for a given bfcache entry. It only looks up between
Packit f0b94e
  // the range where alive viewers may exist (i.e nsISHistory::VIEWER_WINDOW).
Packit f0b94e
  nsresult FindTransactionForBFCache(nsIBFCacheEntry* aEntry,
Packit f0b94e
                                     nsISHTransaction** aResult,
Packit f0b94e
                                     int32_t* aResultIndex);
Packit f0b94e
Packit f0b94e
  // Evict content viewers in this window which don't lie in the "safe" range
Packit f0b94e
  // around aIndex.
Packit f0b94e
  void EvictOutOfRangeWindowContentViewers(int32_t aIndex);
Packit f0b94e
  void EvictContentViewerForTransaction(nsISHTransaction* aTrans);
Packit f0b94e
  static void GloballyEvictContentViewers();
Packit f0b94e
  static void GloballyEvictAllContentViewers();
Packit f0b94e
Packit f0b94e
  // Calculates a max number of total
Packit f0b94e
  // content viewers to cache, based on amount of total memory
Packit f0b94e
  static uint32_t CalcMaxTotalViewers();
Packit f0b94e
Packit f0b94e
  nsresult LoadNextPossibleEntry(int32_t aNewIndex, long aLoadType,
Packit f0b94e
                                 uint32_t aHistCmd);
Packit f0b94e
Packit f0b94e
  // aIndex is the index of the transaction which may be removed.
Packit f0b94e
  // If aKeepNext is true, aIndex is compared to aIndex + 1,
Packit f0b94e
  // otherwise comparison is done to aIndex - 1.
Packit f0b94e
  bool RemoveDuplicate(int32_t aIndex, bool aKeepNext);
Packit f0b94e
Packit f0b94e
  // Track all bfcache entries and evict on expiration.
Packit f0b94e
  mozilla::UniquePtr<HistoryTracker> mHistoryTracker;
Packit f0b94e
Packit f0b94e
  nsCOMPtr<nsISHTransaction> mListRoot;
Packit f0b94e
  int32_t mIndex;
Packit f0b94e
  int32_t mLength;
Packit f0b94e
  int32_t mRequestedIndex;
Packit f0b94e
Packit f0b94e
  // Session History listeners
Packit f0b94e
  nsAutoTObserverArray<nsWeakPtr, 2> mListeners;
Packit f0b94e
Packit f0b94e
  // Weak reference. Do not refcount this.
Packit f0b94e
  nsIDocShell* mRootDocShell;
Packit f0b94e
Packit f0b94e
  // Max viewers allowed total, across all SHistory objects
Packit f0b94e
  static int32_t sHistoryMaxTotalViewers;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
class nsSHEnumerator : public nsISimpleEnumerator {
Packit f0b94e
 public:
Packit f0b94e
  NS_DECL_ISUPPORTS
Packit f0b94e
  NS_DECL_NSISIMPLEENUMERATOR
Packit f0b94e
Packit f0b94e
  explicit nsSHEnumerator(nsSHistory* aHistory);
Packit f0b94e
Packit f0b94e
 protected:
Packit f0b94e
  friend class nsSHistory;
Packit f0b94e
  virtual ~nsSHEnumerator();
Packit f0b94e
Packit f0b94e
 private:
Packit f0b94e
  int32_t mIndex;
Packit f0b94e
  nsSHistory* mSHistory;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
#endif /* nsSHistory */