From c91bbe8c6d1aad1fc892dbee8da6ae0a59eaaede Mon Sep 17 00:00:00 2001 From: Kashav Madan Date: Tue, 23 Mar 2021 18:01:20 +0000 Subject: [PATCH] Bug 1597499 - Move nsIDocShell::ChildOffset to BrowsingContext, r=nika Differential Revision: https://phabricator.services.mozilla.com/D89969 --- docshell/base/BrowsingContext.cpp | 10 +++++-- docshell/base/BrowsingContext.h | 7 +++++ docshell/base/nsDocShell.cpp | 27 +++---------------- docshell/base/nsDocShell.h | 4 --- docshell/base/nsIDocShell.idl | 6 ----- dom/chrome-webidl/BrowsingContext.webidl | 2 ++ .../sessionstore/SessionStoreUtils.cpp | 14 +++------- 7 files changed, 24 insertions(+), 46 deletions(-) diff --git a/docshell/base/BrowsingContext.cpp b/docshell/base/BrowsingContext.cpp index 24daf0c64b33..aa41a66f3fe9 100644 --- a/docshell/base/BrowsingContext.cpp +++ b/docshell/base/BrowsingContext.cpp @@ -489,6 +489,7 @@ void BrowsingContext::CreateFromIPC(BrowsingContext::IPCInitializer&& aInit, context->mWindowless = aInit.mWindowless; context->mCreatedDynamically = aInit.mCreatedDynamically; + context->mChildOffset = aInit.mChildOffset; if (context->GetHasSessionHistory()) { context->CreateChildSHistory(); if (mozilla::SessionHistoryInParent()) { @@ -528,7 +529,8 @@ BrowsingContext::BrowsingContext(WindowContext* aParentWindow, mEmbeddedByThisProcess(false), mUseRemoteTabs(false), mUseRemoteSubframes(false), - mCreatedDynamically(false) { + mCreatedDynamically(false), + mChildOffset(0) { MOZ_RELEASE_ASSERT(!mParentWindow || mParentWindow->Group() == mGroup); MOZ_RELEASE_ASSERT(mBrowsingContextId != 0); MOZ_RELEASE_ASSERT(mGroup); @@ -721,7 +723,8 @@ void BrowsingContext::Attach(bool aFromIPC, ContentParent* aOriginProcess) { MOZ_DIAGNOSTIC_ASSERT(mParentWindow->GetWindowGlobalChild()->CanSend(), "local attach call with dead parent window"); } - + mChildOffset = + mCreatedDynamically ? -1 : mParentWindow->Children().Length(); mParentWindow->AppendChildBrowsingContext(this); } else { mGroup->Toplevels().AppendElement(this); @@ -2379,6 +2382,7 @@ BrowsingContext::IPCInitializer BrowsingContext::GetIPCInitializer() { init.mUseRemoteTabs = mUseRemoteTabs; init.mUseRemoteSubframes = mUseRemoteSubframes; init.mCreatedDynamically = mCreatedDynamically; + init.mChildOffset = mChildOffset; init.mOriginAttributes = mOriginAttributes; if (mChildSessionHistory && mozilla::SessionHistoryInParent()) { init.mSessionHistoryIndex = mChildSessionHistory->Index(); @@ -3397,6 +3401,7 @@ void IPDLParamTraits::Write( WriteIPDLParam(aMessage, aActor, aInit.mUseRemoteTabs); WriteIPDLParam(aMessage, aActor, aInit.mUseRemoteSubframes); WriteIPDLParam(aMessage, aActor, aInit.mCreatedDynamically); + WriteIPDLParam(aMessage, aActor, aInit.mChildOffset); WriteIPDLParam(aMessage, aActor, aInit.mOriginAttributes); WriteIPDLParam(aMessage, aActor, aInit.mRequestContextId); WriteIPDLParam(aMessage, aActor, aInit.mSessionHistoryIndex); @@ -3416,6 +3421,7 @@ bool IPDLParamTraits::Read( &aInit->mUseRemoteSubframes) || !ReadIPDLParam(aMessage, aIterator, aActor, &aInit->mCreatedDynamically) || + !ReadIPDLParam(aMessage, aIterator, aActor, &aInit->mChildOffset) || !ReadIPDLParam(aMessage, aIterator, aActor, &aInit->mOriginAttributes) || !ReadIPDLParam(aMessage, aIterator, aActor, &aInit->mRequestContextId) || !ReadIPDLParam(aMessage, aIterator, aActor, diff --git a/docshell/base/BrowsingContext.h b/docshell/base/BrowsingContext.h index fdd1fd88d2bb..99b9fc2dc6d5 100644 --- a/docshell/base/BrowsingContext.h +++ b/docshell/base/BrowsingContext.h @@ -677,6 +677,7 @@ class BrowsingContext : public nsILoadContext, public nsWrapperCache { bool mUseRemoteTabs = false; bool mUseRemoteSubframes = false; bool mCreatedDynamically = false; + int32_t mChildOffset = 0; int32_t mSessionHistoryIndex = -1; int32_t mSessionHistoryCount = 0; OriginAttributes mOriginAttributes; @@ -715,6 +716,8 @@ class BrowsingContext : public nsILoadContext, public nsWrapperCache { bool CreatedDynamically() const { return mCreatedDynamically; } + int32_t ChildOffset() const { return mChildOffset; } + const OriginAttributes& OriginAttributesRef() { return mOriginAttributes; } nsresult SetOriginAttributes(const OriginAttributes& aAttrs); @@ -1133,6 +1136,10 @@ class BrowsingContext : public nsILoadContext, public nsWrapperCache { // True if this BrowsingContext is for a frame that was added dynamically. bool mCreatedDynamically : 1; + // The original offset of this context in its container. This property is -1 + // if this BrowsingContext is for a frame that was added dynamically. + int32_t mChildOffset; + // The start time of user gesture, this is only available if the browsing // context is in process. TimeStamp mUserGestureStart; diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index e7fb16d25229..a781bc6277de 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -376,7 +376,6 @@ nsDocShell::nsDocShell(BrowsingContext* aBrowsingContext, mItemType(aBrowsingContext->IsContent() ? typeContent : typeChrome), mPreviousEntryIndex(-1), mLoadedEntryIndex(-1), - mChildOffset(0), mBusyFlags(BUSY_FLAGS_NONE), mAppType(nsIDocShell::APP_TYPE_UNKNOWN), mLoadType(0), @@ -1023,7 +1022,7 @@ bool nsDocShell::MaybeHandleSubframeHistory( // it wasn't originally for some other frame. nsCOMPtr shEntry; currentSH->GetChildSHEntryIfHasNoDynamicallyAddedChild( - mChildOffset, getter_AddRefs(shEntry)); + mBrowsingContext->ChildOffset(), getter_AddRefs(shEntry)); if (shEntry) { aLoadState->SetSHEntry(shEntry); } @@ -3011,12 +3010,6 @@ nsDocShell::SetTreeOwner(nsIDocShellTreeOwner* aTreeOwner) { return NS_OK; } -void nsDocShell::SetChildOffset(int32_t aChildOffset) { - mChildOffset = aChildOffset; -} - -int32_t nsDocShell::GetChildOffset() { return mChildOffset; } - NS_IMETHODIMP nsDocShell::GetHistoryID(nsID& aID) { aID = mBrowsingContext->GetHistoryID(); @@ -3070,24 +3063,11 @@ nsDocShell::AddChild(nsIDocShellTreeItem* aChild) { NS_ASSERTION(!mChildList.IsEmpty(), "child list must not be empty after a successful add"); - nsCOMPtr childDocShell = do_QueryInterface(aChild); - bool dynamic = nsDocShell::Cast(childDocShell)->GetCreatedDynamically(); - if (!dynamic) { - nsCOMPtr currentSH; - bool oshe = false; - GetCurrentSHEntry(getter_AddRefs(currentSH), &oshe); - if (currentSH) { - currentSH->HasDynamicallyAddedChild(&dynamic); - } - } - childDocShell->SetChildOffset(dynamic ? -1 : mChildList.Length() - 1); - /* Set the child's global history if the parent has one */ if (mBrowsingContext->GetUseGlobalHistory()) { // childDocShell->SetUseGlobalHistory(true); // this should be set through BC inherit - MOZ_ASSERT(nsDocShell::Cast(childDocShell) - ->mBrowsingContext->GetUseGlobalHistory()); + MOZ_ASSERT(aChild->GetBrowsingContext()->GetUseGlobalHistory()); } if (aChild->ItemType() != mItemType) { @@ -11865,7 +11845,8 @@ nsresult nsDocShell::AddToSessionHistory( MOZ_ALWAYS_SUCCEEDS(topWc->SetSHEntryHasUserInteraction(false)); } if (!mOSHE || !LOAD_TYPE_HAS_FLAGS(mLoadType, LOAD_FLAGS_REPLACE_HISTORY)) { - rv = AddChildSHEntryToParent(entry, mChildOffset, aCloneChildren); + rv = AddChildSHEntryToParent(entry, mBrowsingContext->ChildOffset(), + aCloneChildren); } } diff --git a/docshell/base/nsDocShell.h b/docshell/base/nsDocShell.h index c29b1e05f362..b414dcdc63cd 100644 --- a/docshell/base/nsDocShell.h +++ b/docshell/base/nsDocShell.h @@ -1202,10 +1202,6 @@ class nsDocShell final : public nsDocLoader, int32_t mPreviousEntryIndex; int32_t mLoadedEntryIndex; - // Offset in the parent's child list. - // -1 if the docshell is added dynamically to the parent shell. - int32_t mChildOffset; - BusyFlags mBusyFlags; AppType mAppType; uint32_t mLoadType; diff --git a/docshell/base/nsIDocShell.idl b/docshell/base/nsIDocShell.idl index d326491a03b8..2f19d82df0ae 100644 --- a/docshell/base/nsIDocShell.idl +++ b/docshell/base/nsIDocShell.idl @@ -449,12 +449,6 @@ interface nsIDocShell : nsIDocShellTreeItem */ readonly attribute nsIChannel currentDocumentChannel; - /** - * The original offset of this child in its container. This property is -1 for - * dynamically added docShells. - */ - [notxpcom,nostdcall] attribute long childOffset; - /** * Find out whether the docshell is currently in the middle of a page * transition. This is set just before the pagehide/unload events fire. diff --git a/dom/chrome-webidl/BrowsingContext.webidl b/dom/chrome-webidl/BrowsingContext.webidl index dfdfe724ccc5..d533498bbe06 100644 --- a/dom/chrome-webidl/BrowsingContext.webidl +++ b/dom/chrome-webidl/BrowsingContext.webidl @@ -201,6 +201,8 @@ interface BrowsingContext { // Resets the location change rate limit. Used for testing. void resetLocationChangeRateLimit(); + + readonly attribute long childOffset; }; BrowsingContext includes LoadContextMixin; diff --git a/toolkit/components/sessionstore/SessionStoreUtils.cpp b/toolkit/components/sessionstore/SessionStoreUtils.cpp index 4bdc191ccb95..25b4a1baf6bd 100644 --- a/toolkit/components/sessionstore/SessionStoreUtils.cpp +++ b/toolkit/components/sessionstore/SessionStoreUtils.cpp @@ -121,18 +121,10 @@ void SessionStoreUtils::ForEachNonDynamicChildFrame( return; } - if (context->CreatedDynamically()) { - continue; + if (!context->CreatedDynamically()) { + int32_t childOffset = context->ChildOffset(); + aCallback.Call(WindowProxyHolder(context.forget()), childOffset); } - - nsCOMPtr childDocShell(do_QueryInterface(item)); - if (!childDocShell) { - aRv.Throw(NS_ERROR_FAILURE); - return; - } - - int32_t childOffset = childDocShell->GetChildOffset(); - aCallback.Call(WindowProxyHolder(context.forget()), childOffset); } }