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: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
#include "nsSHEntryShared.h"
|
2013-08-22 10:30:55 +04:00
|
|
|
|
|
|
|
#include "nsIDOMDocument.h"
|
2011-10-21 19:26:34 +04:00
|
|
|
#include "nsISHistory.h"
|
|
|
|
#include "nsISHistoryInternal.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsIContentViewer.h"
|
2013-11-15 11:12:43 +04:00
|
|
|
#include "nsIDocShell.h"
|
2011-10-21 19:26:34 +04:00
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsDocShellEditorData.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsILayoutHistoryState.h"
|
2012-07-11 07:59:52 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-12-04 22:04:36 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-09-04 19:26:20 +04:00
|
|
|
#include "nsISupportsArray.h"
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
namespace dom = mozilla::dom;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t gSHEntrySharedID = 0;
|
2011-10-21 19:26:34 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2011-10-21 19:26:34 +04:00
|
|
|
|
2014-12-04 22:04:36 +03:00
|
|
|
#define CONTENT_VIEWER_TIMEOUT_SECONDS "browser.sessionhistory.contentViewerTimeout"
|
|
|
|
// Default this to time out unused content viewers after 30 minutes
|
2015-05-06 20:57:23 +03:00
|
|
|
#define CONTENT_VIEWER_TIMEOUT_SECONDS_DEFAULT (30 * 60)
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
typedef nsExpirationTracker<nsSHEntryShared, 3> HistoryTrackerBase;
|
2015-05-06 20:57:23 +03:00
|
|
|
class HistoryTracker final : public HistoryTrackerBase
|
|
|
|
{
|
2011-10-21 19:26:34 +04:00
|
|
|
public:
|
2014-12-11 01:49:09 +03:00
|
|
|
explicit HistoryTracker(uint32_t aTimeout)
|
2015-09-10 07:07:07 +03:00
|
|
|
: HistoryTrackerBase(1000 * aTimeout / 2, "HistoryTracker")
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
}
|
2014-12-04 22:04:36 +03:00
|
|
|
|
2011-10-21 19:26:34 +04:00
|
|
|
protected:
|
2015-05-06 20:57:23 +03:00
|
|
|
virtual void NotifyExpired(nsSHEntryShared* aObj)
|
|
|
|
{
|
2011-10-21 19:26:34 +04:00
|
|
|
RemoveObject(aObj);
|
|
|
|
aObj->Expire();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-06 20:57:23 +03:00
|
|
|
static HistoryTracker* gHistoryTracker = nullptr;
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
void
|
2014-12-04 22:04:36 +03:00
|
|
|
nsSHEntryShared::EnsureHistoryTracker()
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
2014-12-04 22:04:36 +03:00
|
|
|
if (!gHistoryTracker) {
|
|
|
|
// nsExpirationTracker doesn't allow one to change the timer period,
|
|
|
|
// so just set it once when the history tracker is used for the first time.
|
|
|
|
gHistoryTracker = new HistoryTracker(
|
|
|
|
mozilla::Preferences::GetUint(CONTENT_VIEWER_TIMEOUT_SECONDS,
|
|
|
|
CONTENT_VIEWER_TIMEOUT_SECONDS_DEFAULT));
|
|
|
|
}
|
2011-10-21 19:26:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::Shutdown()
|
|
|
|
{
|
|
|
|
delete gHistoryTracker;
|
2012-07-30 18:20:58 +04:00
|
|
|
gHistoryTracker = nullptr;
|
2011-10-21 19:26:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSHEntryShared::nsSHEntryShared()
|
|
|
|
: mDocShellID(0)
|
|
|
|
, mIsFrameNavigation(false)
|
|
|
|
, mSaveLayoutState(true)
|
|
|
|
, mSticky(true)
|
|
|
|
, mDynamicallyCreated(false)
|
|
|
|
, mLastTouched(0)
|
|
|
|
, mID(gSHEntrySharedID++)
|
|
|
|
, mExpired(false)
|
|
|
|
, mViewerBounds(0, 0, 0, 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSHEntryShared::~nsSHEntryShared()
|
|
|
|
{
|
|
|
|
RemoveFromExpirationTracker();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-12-04 22:04:36 +03:00
|
|
|
if (gHistoryTracker) {
|
|
|
|
// Check that we're not still on track to expire. We shouldn't be, because
|
|
|
|
// we just removed ourselves!
|
2015-05-06 20:57:23 +03:00
|
|
|
nsExpirationTracker<nsSHEntryShared, 3>::Iterator iterator(gHistoryTracker);
|
2014-12-04 22:04:36 +03:00
|
|
|
|
2015-05-06 20:57:23 +03:00
|
|
|
nsSHEntryShared* elem;
|
2014-12-04 22:04:36 +03:00
|
|
|
while ((elem = iterator.Next()) != nullptr) {
|
|
|
|
NS_ASSERTION(elem != this, "Found dead entry still in the tracker!");
|
|
|
|
}
|
2011-10-21 19:26:34 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mContentViewer) {
|
|
|
|
RemoveFromBFCacheSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsSHEntryShared, nsIBFCacheEntry, nsIMutationObserver)
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
already_AddRefed<nsSHEntryShared>
|
2015-05-06 20:57:23 +03:00
|
|
|
nsSHEntryShared::Duplicate(nsSHEntryShared* aEntry)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
nsRefPtr<nsSHEntryShared> newEntry = new nsSHEntryShared();
|
|
|
|
|
|
|
|
newEntry->mDocShellID = aEntry->mDocShellID;
|
|
|
|
newEntry->mChildShells.AppendObjects(aEntry->mChildShells);
|
|
|
|
newEntry->mOwner = aEntry->mOwner;
|
|
|
|
newEntry->mContentType.Assign(aEntry->mContentType);
|
|
|
|
newEntry->mIsFrameNavigation = aEntry->mIsFrameNavigation;
|
|
|
|
newEntry->mSaveLayoutState = aEntry->mSaveLayoutState;
|
|
|
|
newEntry->mSticky = aEntry->mSticky;
|
|
|
|
newEntry->mDynamicallyCreated = aEntry->mDynamicallyCreated;
|
|
|
|
newEntry->mCacheKey = aEntry->mCacheKey;
|
|
|
|
newEntry->mLastTouched = aEntry->mLastTouched;
|
|
|
|
|
|
|
|
return newEntry.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-06 20:57:23 +03:00
|
|
|
void
|
|
|
|
nsSHEntryShared::RemoveFromExpirationTracker()
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
2014-12-04 22:04:36 +03:00
|
|
|
if (gHistoryTracker && GetExpirationState()->IsTracked()) {
|
2011-10-21 19:26:34 +04:00
|
|
|
gHistoryTracker->RemoveObject(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSHEntryShared::SyncPresentationState()
|
|
|
|
{
|
|
|
|
if (mContentViewer && mWindowState) {
|
|
|
|
// If we have a content viewer and a window state, we should be ok.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DropPresentationState();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::DropPresentationState()
|
|
|
|
{
|
|
|
|
nsRefPtr<nsSHEntryShared> kungFuDeathGrip = this;
|
|
|
|
|
|
|
|
if (mDocument) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mDocument->SetBFCacheEntry(nullptr);
|
2011-10-21 19:26:34 +04:00
|
|
|
mDocument->RemoveMutationObserver(this);
|
2012-07-30 18:20:58 +04:00
|
|
|
mDocument = nullptr;
|
2011-10-21 19:26:34 +04:00
|
|
|
}
|
|
|
|
if (mContentViewer) {
|
|
|
|
mContentViewer->ClearHistoryEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveFromExpirationTracker();
|
2012-07-30 18:20:58 +04:00
|
|
|
mContentViewer = nullptr;
|
2011-10-21 19:26:34 +04:00
|
|
|
mSticky = true;
|
2012-07-30 18:20:58 +04:00
|
|
|
mWindowState = nullptr;
|
2011-10-21 19:26:34 +04:00
|
|
|
mViewerBounds.SetRect(0, 0, 0, 0);
|
|
|
|
mChildShells.Clear();
|
2012-07-30 18:20:58 +04:00
|
|
|
mRefreshURIList = nullptr;
|
|
|
|
mEditorData = nullptr;
|
2011-10-21 19:26:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::Expire()
|
|
|
|
{
|
|
|
|
// This entry has timed out. If we still have a content viewer, we need to
|
|
|
|
// evict it.
|
|
|
|
if (!mContentViewer) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-15 11:12:43 +04:00
|
|
|
nsCOMPtr<nsIDocShell> container;
|
2011-10-21 19:26:34 +04:00
|
|
|
mContentViewer->GetContainer(getter_AddRefs(container));
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> treeItem = do_QueryInterface(container);
|
|
|
|
if (!treeItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We need to find the root DocShell since only that object has an
|
|
|
|
// SHistory and we need the SHistory to evict content viewers
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> root;
|
|
|
|
treeItem->GetSameTypeRootTreeItem(getter_AddRefs(root));
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(root);
|
|
|
|
nsCOMPtr<nsISHistory> history;
|
|
|
|
webNav->GetSessionHistory(getter_AddRefs(history));
|
|
|
|
nsCOMPtr<nsISHistoryInternal> historyInt = do_QueryInterface(history);
|
|
|
|
if (!historyInt) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
historyInt->EvictExpiredContentViewerForEntry(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-06 20:57:23 +03:00
|
|
|
nsSHEntryShared::SetContentViewer(nsIContentViewer* aViewer)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!aViewer || !mContentViewer,
|
|
|
|
"SHEntryShared already contains viewer");
|
|
|
|
|
|
|
|
if (mContentViewer || !aViewer) {
|
|
|
|
DropPresentationState();
|
|
|
|
}
|
|
|
|
|
|
|
|
mContentViewer = aViewer;
|
|
|
|
|
|
|
|
if (mContentViewer) {
|
2014-12-04 22:04:36 +03:00
|
|
|
EnsureHistoryTracker();
|
2011-10-21 19:26:34 +04:00
|
|
|
gHistoryTracker->AddObject(this);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
mContentViewer->GetDOMDocument(getter_AddRefs(domDoc));
|
|
|
|
// Store observed document in strong pointer in case it is removed from
|
|
|
|
// the contentviewer
|
|
|
|
mDocument = do_QueryInterface(domDoc);
|
|
|
|
if (mDocument) {
|
|
|
|
mDocument->SetBFCacheEntry(this);
|
|
|
|
mDocument->AddMutationObserver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSHEntryShared::RemoveFromBFCacheSync()
|
|
|
|
{
|
2015-05-06 20:57:23 +03:00
|
|
|
NS_ASSERTION(mContentViewer && mDocument, "we're not in the bfcache!");
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContentViewer> viewer = mContentViewer;
|
|
|
|
DropPresentationState();
|
|
|
|
|
|
|
|
// Warning! The call to DropPresentationState could have dropped the last
|
|
|
|
// reference to this object, so don't access members beyond here.
|
|
|
|
|
|
|
|
if (viewer) {
|
|
|
|
viewer->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DestroyViewerEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DestroyViewerEvent(nsIContentViewer* aViewer, nsIDocument* aDocument)
|
2015-05-06 20:57:23 +03:00
|
|
|
: mViewer(aViewer)
|
|
|
|
, mDocument(aDocument)
|
|
|
|
{
|
|
|
|
}
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (mViewer) {
|
|
|
|
mViewer->Destroy();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentViewer> mViewer;
|
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSHEntryShared::RemoveFromBFCacheAsync()
|
|
|
|
{
|
2015-05-06 20:57:23 +03:00
|
|
|
NS_ASSERTION(mContentViewer && mDocument, "we're not in the bfcache!");
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
// Release the reference to the contentviewer asynchronously so that the
|
|
|
|
// document doesn't get nuked mid-mutation.
|
|
|
|
|
2015-05-06 20:57:23 +03:00
|
|
|
nsCOMPtr<nsIRunnable> evt = new DestroyViewerEvent(mContentViewer, mDocument);
|
2011-10-21 19:26:34 +04:00
|
|
|
nsresult rv = NS_DispatchToCurrentThread(evt);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to dispatch DestroyViewerEvent");
|
|
|
|
} else {
|
|
|
|
// Drop presentation. Only do this if we succeeded in posting the event
|
|
|
|
// since otherwise the document could be torn down mid-mutation, causing
|
|
|
|
// crashes.
|
|
|
|
DropPresentationState();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Careful! The call to DropPresentationState could have dropped the last
|
|
|
|
// reference to this nsSHEntryShared, so don't access members beyond here.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-06 20:57:23 +03:00
|
|
|
nsSHEntryShared::GetID(uint64_t* aID)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
*aID = mID;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::NodeWillBeDestroyed(const nsINode* aNode)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Document destroyed while we're holding a strong ref to it");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::CharacterDataWillChange(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::CharacterDataChanged(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
|
|
|
RemoveFromBFCacheAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::AttributeWillChange(nsIDocument* aDocument,
|
|
|
|
dom::Element* aContent,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aNameSpaceID,
|
2011-10-21 19:26:34 +04:00
|
|
|
nsIAtom* aAttribute,
|
2015-07-25 09:05:19 +03:00
|
|
|
int32_t aModType,
|
|
|
|
const nsAttrValue* aNewValue)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-24 18:23:32 +03:00
|
|
|
void
|
|
|
|
nsSHEntryShared::NativeAnonymousChildListChange(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
bool aIsRemove)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-21 19:26:34 +04:00
|
|
|
void
|
|
|
|
nsSHEntryShared::AttributeChanged(nsIDocument* aDocument,
|
|
|
|
dom::Element* aElement,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aNameSpaceID,
|
2011-10-21 19:26:34 +04:00
|
|
|
nsIAtom* aAttribute,
|
2015-07-25 09:01:19 +03:00
|
|
|
int32_t aModType,
|
|
|
|
const nsAttrValue* aOldValue)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
RemoveFromBFCacheAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t /* unused */)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
RemoveFromBFCacheAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t /* unused */)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
RemoveFromBFCacheAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::ContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndexInContainer,
|
2011-10-21 19:26:34 +04:00
|
|
|
nsIContent* aPreviousSibling)
|
|
|
|
{
|
|
|
|
RemoveFromBFCacheAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-06 20:57:23 +03:00
|
|
|
nsSHEntryShared::ParentChainChanged(nsIContent* aContent)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
}
|