From 1c8872c7e26018e3543c86b9ce0d636eb57e6066 Mon Sep 17 00:00:00 2001 From: Peter Van der Beken Date: Mon, 19 Apr 2021 14:50:55 +0000 Subject: [PATCH] Bug 1696158 - Move CanSavePresentation to the parent process. Move some code. r=smaug Differential Revision: https://phabricator.services.mozilla.com/D111606 --- docshell/shistory/nsSHistory.cpp | 141 ++++++++++++++++--------------- 1 file changed, 73 insertions(+), 68 deletions(-) diff --git a/docshell/shistory/nsSHistory.cpp b/docshell/shistory/nsSHistory.cpp index aedc0cb8e6ae..2b53eb2f6a34 100644 --- a/docshell/shistory/nsSHistory.cpp +++ b/docshell/shistory/nsSHistory.cpp @@ -1182,6 +1182,60 @@ nsSHistory::EvictAllContentViewers() { return NS_OK; } +static void FinishRestore(CanonicalBrowsingContext* aBrowsingContext, + nsDocShellLoadState* aLoadState, + SessionHistoryEntry* aEntry, + nsFrameLoader* aFrameLoader, bool aCanSave) { + MOZ_ASSERT(aEntry); + MOZ_ASSERT(aFrameLoader); + + nsCOMPtr frameLoaderOwner = + do_QueryInterface(aBrowsingContext->GetEmbedderElement()); + if (frameLoaderOwner) { + aEntry->SetFrameLoader(nullptr); + if (aFrameLoader->GetMaybePendingBrowsingContext()) { + RefPtr loadingBC = + aFrameLoader->GetMaybePendingBrowsingContext()->Canonical(); + RefPtr currentFrameLoader = + frameLoaderOwner->GetFrameLoader(); + // The current page can be bfcached, store the + // nsFrameLoader in the current SessionHistoryEntry. + if (aCanSave && aBrowsingContext->GetActiveSessionHistoryEntry()) { + aBrowsingContext->GetActiveSessionHistoryEntry()->SetFrameLoader( + currentFrameLoader); + Unused << aBrowsingContext->SetIsInBFCache(true); + } + + // ReplacedBy will swap the entry back. + aBrowsingContext->SetActiveSessionHistoryEntry(aEntry); + loadingBC->SetActiveSessionHistoryEntry(nullptr); + RemotenessChangeOptions options; + aBrowsingContext->ReplacedBy(loadingBC, options); + frameLoaderOwner->ReplaceFrameLoader(aFrameLoader); + + // The old page can't be stored in the bfcache, + // destroy the nsFrameLoader. + if (!aCanSave && currentFrameLoader) { + currentFrameLoader->Destroy(); + } + + // Assuming we still have the session history, update the index. + if (loadingBC->GetSessionHistory()) { + loadingBC->GetSessionHistory()->UpdateIndex(); + } + loadingBC->HistoryCommitIndexAndLength(); + Unused << loadingBC->SetIsInBFCache(false); + // ResetSHEntryHasUserInteractionCache(); ? + // browser.navigation.requireUserInteraction is still + // disabled everywhere. + return; + } + } + + // Fall back to do a normal load. + aBrowsingContext->LoadURI(aLoadState, false); +} + /* static */ void nsSHistory::LoadURIOrBFCache(LoadEntryResult& aLoadEntry) { if (mozilla::BFCacheInParent() && aLoadEntry.mBrowsingContext->IsTop()) { @@ -1196,64 +1250,14 @@ void nsSHistory::LoadURIOrBFCache(LoadEntryResult& aLoadEntry) { RefPtr frameLoader = she->GetFrameLoader(); if (canonicalBC->Group()->Toplevels().Length() == 1 && frameLoader && (!currentShe || she->SharedInfo() != currentShe->SharedInfo())) { - auto restoreInitialStep = [canonicalBC, loadState, - she](const nsTArray aCanSaves) { + auto restoreInitialStep = [canonicalBC, loadState, she, + frameLoader](const nsTArray aCanSaves) { bool canSave = !aCanSaves.Contains(false); MOZ_LOG(gSHIPBFCacheLog, LogLevel::Debug, ("nsSHistory::LoadURIOrBFCache " "saving presentation=%i", canSave)); - auto restoreFinalStep = [canonicalBC, loadState, she](bool aCanSave) { - nsCOMPtr frameLoaderOwner = - do_QueryInterface(canonicalBC->GetEmbedderElement()); - if (frameLoaderOwner) { - RefPtr fl = she->GetFrameLoader(); - if (fl) { - she->SetFrameLoader(nullptr); - RefPtr loadingBC = - fl->GetMaybePendingBrowsingContext(); - if (loadingBC) { - RefPtr currentFrameLoader = - frameLoaderOwner->GetFrameLoader(); - // The current page can be bfcached, store the - // nsFrameLoader in the current SessionHistoryEntry. - if (aCanSave && canonicalBC->GetActiveSessionHistoryEntry()) { - canonicalBC->GetActiveSessionHistoryEntry()->SetFrameLoader( - currentFrameLoader); - Unused << canonicalBC->SetIsInBFCache(true); - } - - // ReplacedBy will swap the entry back. - canonicalBC->SetActiveSessionHistoryEntry(she); - loadingBC->Canonical()->SetActiveSessionHistoryEntry(nullptr); - RemotenessChangeOptions options; - canonicalBC->ReplacedBy(loadingBC->Canonical(), options); - frameLoaderOwner->ReplaceFrameLoader(fl); - - // The old page can't be stored in the bfcache, - // destroy the nsFrameLoader. - if (!aCanSave && currentFrameLoader) { - currentFrameLoader->Destroy(); - } - - // Assuming we still have the session history, update the index. - if (loadingBC->Canonical()->GetSessionHistory()) { - loadingBC->Canonical()->GetSessionHistory()->UpdateIndex(); - } - loadingBC->Canonical()->HistoryCommitIndexAndLength(); - Unused << loadingBC->SetIsInBFCache(false); - // ResetSHEntryHasUserInteractionCache(); ? - // browser.navigation.requireUserInteraction is still - // disabled everywhere. - return; - } - } - } - // Fall back to do a normal load. - canonicalBC->LoadURI(loadState, false); - }; - if (!canSave) { nsCOMPtr frameLoaderOwner = do_QueryInterface(canonicalBC->GetEmbedderElement()); @@ -1267,28 +1271,29 @@ void nsSHistory::LoadURIOrBFCache(LoadEntryResult& aLoadEntry) { ->Canonical() ->GetCurrentWindowGlobal(); if (wgp) { - wgp->PermitUnload( - [restoreFinalStep, currentFrameLoader](bool aAllow) { - if (aAllow) { - restoreFinalStep(false); - } else if (currentFrameLoader - ->GetMaybePendingBrowsingContext()) { - nsISHistory* shistory = - currentFrameLoader->GetMaybePendingBrowsingContext() - ->Canonical() - ->GetSessionHistory(); - if (shistory) { - shistory->InternalSetRequestedIndex(-1); - } - } - }); + wgp->PermitUnload([canonicalBC, loadState, she, frameLoader, + currentFrameLoader](bool aAllow) { + if (aAllow) { + FinishRestore(canonicalBC, loadState, she, frameLoader, + false); + } else if (currentFrameLoader + ->GetMaybePendingBrowsingContext()) { + nsISHistory* shistory = + currentFrameLoader->GetMaybePendingBrowsingContext() + ->Canonical() + ->GetSessionHistory(); + if (shistory) { + shistory->InternalSetRequestedIndex(-1); + } + } + }); return; } } } } - restoreFinalStep(canSave); + FinishRestore(canonicalBC, loadState, she, frameLoader, canSave); }; if (currentShe && !currentShe->GetSaveLayoutStateFlag()) {