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
|
|
|
|
2017-04-27 13:16:46 +03:00
|
|
|
#include "nsArray.h"
|
|
|
|
#include "nsDocShellEditorData.h"
|
|
|
|
#include "nsIContentViewer.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIDocument.h"
|
2013-08-22 10:30:55 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
2017-04-27 13:16:46 +03:00
|
|
|
#include "nsILayoutHistoryState.h"
|
2011-10-21 19:26:34 +04:00
|
|
|
#include "nsISHistory.h"
|
|
|
|
#include "nsISHistoryInternal.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2017-04-27 13:16:46 +03:00
|
|
|
|
2012-07-11 07:59:52 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-12-04 22:04:36 +03:00
|
|
|
#include "mozilla/Preferences.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
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::Shutdown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSHEntryShared::nsSHEntryShared()
|
2016-11-17 00:42:35 +03:00
|
|
|
: mDocShellID({0})
|
2017-04-27 13:16:46 +03:00
|
|
|
, mLastTouched(0)
|
|
|
|
, mID(gSHEntrySharedID++)
|
|
|
|
, mViewerBounds(0, 0, 0, 0)
|
2011-10-21 19:26:34 +04:00
|
|
|
, mIsFrameNavigation(false)
|
|
|
|
, mSaveLayoutState(true)
|
|
|
|
, mSticky(true)
|
|
|
|
, mDynamicallyCreated(false)
|
|
|
|
, mExpired(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSHEntryShared::~nsSHEntryShared()
|
|
|
|
{
|
2017-08-24 10:17:39 +03:00
|
|
|
// The destruction can be caused by either the entry is removed from session
|
|
|
|
// history and no one holds the reference, or the whole session history is on
|
|
|
|
// destruction. We want to ensure that we invoke
|
|
|
|
// shistory->RemoveFromExpirationTracker for the former case.
|
2011-10-21 19:26:34 +04:00
|
|
|
RemoveFromExpirationTracker();
|
2017-08-24 10:17:39 +03:00
|
|
|
|
|
|
|
// Calling RemoveDynEntriesForBFCacheEntry on destruction is unnecessary since
|
|
|
|
// there couldn't be any SHEntry holding this shared entry, and we noticed
|
|
|
|
// that calling RemoveDynEntriesForBFCacheEntry in the middle of
|
|
|
|
// nsSHistory::Release can cause a crash, so set mSHistory to null explicitly
|
|
|
|
// before RemoveFromBFCacheSync.
|
|
|
|
mSHistory = nullptr;
|
2011-10-21 19:26:34 +04:00
|
|
|
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
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsSHEntryShared> newEntry = new nsSHEntryShared();
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
newEntry->mDocShellID = aEntry->mDocShellID;
|
|
|
|
newEntry->mChildShells.AppendObjects(aEntry->mChildShells);
|
2016-07-28 10:20:41 +03:00
|
|
|
newEntry->mTriggeringPrincipal = aEntry->mTriggeringPrincipal;
|
2016-09-20 09:36:25 +03:00
|
|
|
newEntry->mPrincipalToInherit = aEntry->mPrincipalToInherit;
|
2011-10-21 19:26:34 +04:00
|
|
|
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
|
|
|
{
|
2017-04-27 13:59:11 +03:00
|
|
|
nsCOMPtr<nsISHistoryInternal> shistory = do_QueryReferent(mSHistory);
|
|
|
|
if (shistory && GetExpirationState()->IsTracked()) {
|
|
|
|
shistory->RemoveFromExpirationTracker(this);
|
2011-10-21 19:26:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsSHEntryShared> kungFuDeathGrip = this;
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-05-16 05:54:01 +03:00
|
|
|
// If we're setting mContentViewer to null, state should already be cleared
|
|
|
|
// in the DropPresentationState() call above; If we're setting it to a
|
|
|
|
// non-null content viewer, the entry shouldn't have been tracked either.
|
2017-05-22 06:25:54 +03:00
|
|
|
MOZ_ASSERT(!GetExpirationState()->IsTracked());
|
2011-10-21 19:26:34 +04:00
|
|
|
mContentViewer = aViewer;
|
|
|
|
|
|
|
|
if (mContentViewer) {
|
2017-04-27 13:59:11 +03:00
|
|
|
// mSHistory is only set for root entries, but in general bfcache only
|
|
|
|
// applies to root entries as well. BFCache for subframe navigation has been
|
|
|
|
// disabled since 2005 in bug 304860.
|
2018-01-20 01:50:13 +03:00
|
|
|
if (nsCOMPtr<nsISHistoryInternal> shistory = do_QueryReferent(mSHistory)) {
|
2017-04-27 13:59:11 +03:00
|
|
|
shistory->AddToExpirationTracker(this);
|
|
|
|
}
|
2011-10-21 19:26:34 +04:00
|
|
|
|
|
|
|
// Store observed document in strong pointer in case it is removed from
|
|
|
|
// the contentviewer
|
2018-01-20 01:50:13 +03:00
|
|
|
mDocument = mContentViewer->GetDocument();
|
2011-10-21 19:26:34 +04:00
|
|
|
if (mDocument) {
|
|
|
|
mDocument->SetBFCacheEntry(this);
|
|
|
|
mDocument->AddMutationObserver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSHEntryShared::RemoveFromBFCacheSync()
|
|
|
|
{
|
2017-03-07 11:58:59 +03:00
|
|
|
MOZ_ASSERT(mContentViewer && mDocument, "we're not in the bfcache!");
|
2011-10-21 19:26:34 +04:00
|
|
|
|
2017-08-24 10:17:39 +03:00
|
|
|
// The call to DropPresentationState could drop the last reference, so hold
|
|
|
|
// |this| until RemoveDynEntriesForBFCacheEntry finishes.
|
|
|
|
RefPtr<nsSHEntryShared> kungFuDeathGrip = this;
|
|
|
|
|
|
|
|
// DropPresentationState would clear mContentViewer.
|
2011-10-21 19:26:34 +04:00
|
|
|
nsCOMPtr<nsIContentViewer> viewer = mContentViewer;
|
|
|
|
DropPresentationState();
|
|
|
|
|
|
|
|
if (viewer) {
|
|
|
|
viewer->Destroy();
|
|
|
|
}
|
|
|
|
|
2017-08-24 10:17:39 +03:00
|
|
|
// Now that we've dropped the viewer, we have to clear associated dynamic
|
|
|
|
// subframe entries.
|
|
|
|
nsCOMPtr<nsISHistoryInternal> shistory = do_QueryReferent(mSHistory);
|
|
|
|
if (shistory) {
|
|
|
|
shistory->RemoveDynEntriesForBFCacheEntry(this);
|
2017-09-12 20:21:17 +03:00
|
|
|
}
|
|
|
|
|
2017-08-24 10:17:39 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-09-12 20:21:17 +03:00
|
|
|
|
2011-10-21 19:26:34 +04:00
|
|
|
nsresult
|
|
|
|
nsSHEntryShared::RemoveFromBFCacheAsync()
|
|
|
|
{
|
2017-03-07 11:58:59 +03:00
|
|
|
MOZ_ASSERT(mContentViewer && mDocument, "we're not in the bfcache!");
|
2011-10-21 19:26:34 +04:00
|
|
|
|
2017-08-24 10:17:39 +03:00
|
|
|
// Check it again to play safe in release builds.
|
2017-03-07 11:58:59 +03:00
|
|
|
if (!mDocument) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2017-08-24 10:17:39 +03:00
|
|
|
|
|
|
|
// DropPresentationState would clear mContentViewer & mDocument. Capture and
|
|
|
|
// release the references asynchronously so that the document doesn't get
|
|
|
|
// nuked mid-mutation.
|
|
|
|
nsCOMPtr<nsIContentViewer> viewer = mContentViewer;
|
|
|
|
nsCOMPtr<nsIDocument> document = mDocument;
|
|
|
|
RefPtr<nsSHEntryShared> self = this;
|
|
|
|
nsresult rv = mDocument->Dispatch(mozilla::TaskCategory::Other,
|
|
|
|
NS_NewRunnableFunction("nsSHEntryShared::RemoveFromBFCacheAsync",
|
|
|
|
[self, viewer, document]() {
|
|
|
|
if (viewer) {
|
|
|
|
viewer->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISHistoryInternal> shistory = do_QueryReferent(self->mSHistory);
|
|
|
|
if (shistory) {
|
|
|
|
shistory->RemoveDynEntriesForBFCacheEntry(self);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2011-10-21 19:26:34 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-08-24 10:17:39 +03:00
|
|
|
NS_WARNING("Failed to dispatch RemoveFromBFCacheAsync runnable.");
|
2011-10-21 19:26:34 +04:00
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* 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,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* 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,
|
2017-07-27 16:49:52 +03:00
|
|
|
nsIContent* aFirstNewContent)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
RemoveFromBFCacheAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
2017-07-27 16:49:52 +03:00
|
|
|
nsIContent* aChild)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
RemoveFromBFCacheAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSHEntryShared::ContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
nsIContent* aPreviousSibling)
|
|
|
|
{
|
|
|
|
RemoveFromBFCacheAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-06 20:57:23 +03:00
|
|
|
nsSHEntryShared::ParentChainChanged(nsIContent* aContent)
|
2011-10-21 19:26:34 +04:00
|
|
|
{
|
|
|
|
}
|