#include "components/no_state_prefetch/browser/no_state_prefetch_manager.h"
#include <stddef.h>
#include <functional>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/not_fatal_until.h"
#include "base/notreached.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_util.h"
#include "base/system/sys_info.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/default_tick_clock.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/values.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_contents.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_field_trial.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_handle.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_history.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_manager_delegate.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_utils.h"
#include "components/no_state_prefetch/browser/prerender_histograms.h"
#include "components/no_state_prefetch/common/no_state_prefetch_final_status.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/preloading.h"
#include "content/public/browser/preloading_data.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/session_storage_namespace.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "net/http/http_cache.h"
#include "net/http/http_request_headers.h"
#include "ui/gfx/geometry/rect.h"
BrowserThread;
PreloadingAttempt;
PreloadingEligibility;
PreloadingFailureReason;
PreloadingHoldbackStatus;
PreloadingTriggeringOutcome;
RenderViewHost;
SessionStorageNamespace;
WebContents;
namespace prerender {
namespace {
constexpr base::TimeDelta kPeriodicCleanupInterval = …;
constexpr base::TimeDelta kDeleteWithExtremePrejudice = …;
constexpr int kHistoryLength = …;
PreloadingFailureReason ToPreloadingFailureReason(FinalStatus status) { … }
void SetPreloadingTriggeringOutcome(PreloadingAttempt* attempt,
PreloadingTriggeringOutcome outcome) { … }
void SetPreloadingEligibility(PreloadingAttempt* attempt,
PreloadingEligibility eligibility) { … }
}
class NoStatePrefetchManager::OnCloseWebContentsDeleter final
: public content::WebContentsDelegate { … };
NoStatePrefetchManagerObserver::~NoStatePrefetchManagerObserver() = default;
struct NoStatePrefetchManager::NavigationRecord { … };
NoStatePrefetchManager::NoStatePrefetchManager(
content::BrowserContext* browser_context,
std::unique_ptr<NoStatePrefetchManagerDelegate> delegate)
: … { … }
NoStatePrefetchManager::~NoStatePrefetchManager() { … }
void NoStatePrefetchManager::Shutdown() { … }
std::unique_ptr<NoStatePrefetchHandle>
NoStatePrefetchManager::StartPrefetchingFromLinkRelPrerender(
int process_id,
int route_id,
const GURL& url,
blink::mojom::PrerenderTriggerType trigger_type,
const content::Referrer& referrer,
const url::Origin& initiator_origin,
const gfx::Size& size) { … }
std::unique_ptr<NoStatePrefetchHandle>
NoStatePrefetchManager::AddSameOriginSpeculation(
const GURL& url,
content::SessionStorageNamespace* session_storage_namespace,
const gfx::Size& size,
const url::Origin& initiator_origin) { … }
void NoStatePrefetchManager::CancelAllPrerenders() { … }
void NoStatePrefetchManager::DestroyAllContents(FinalStatus final_status) { … }
void NoStatePrefetchManager::MoveEntryToPendingDelete(
NoStatePrefetchContents* entry,
FinalStatus final_status) { … }
bool NoStatePrefetchManager::IsWebContentsPrefetching(
const WebContents* web_contents) const { … }
NoStatePrefetchContents* NoStatePrefetchManager::GetNoStatePrefetchContents(
const content::WebContents* web_contents) const { … }
NoStatePrefetchContents*
NoStatePrefetchManager::GetNoStatePrefetchContentsForRoute(int child_id,
int route_id) const { … }
std::vector<WebContents*>
NoStatePrefetchManager::GetAllNoStatePrefetchingContentsForTesting() const { … }
bool NoStatePrefetchManager::HasRecentlyBeenNavigatedTo(Origin origin,
const GURL& url) { … }
base::Value::Dict NoStatePrefetchManager::CopyAsDict() const { … }
void NoStatePrefetchManager::ClearData(int clear_flags) { … }
void NoStatePrefetchManager::RecordFinalStatus(Origin origin,
FinalStatus final_status) const { … }
void NoStatePrefetchManager::RecordNavigation(const GURL& url) { … }
struct NoStatePrefetchManager::NoStatePrefetchData::OrderByExpiryTime { … };
NoStatePrefetchManager::NoStatePrefetchData::NoStatePrefetchData(
NoStatePrefetchManager* manager,
std::unique_ptr<NoStatePrefetchContents> contents,
base::TimeTicks expiry_time)
: … { … }
NoStatePrefetchManager::NoStatePrefetchData::~NoStatePrefetchData() = default;
void NoStatePrefetchManager::NoStatePrefetchData::OnHandleCreated(
NoStatePrefetchHandle* handle) { … }
void NoStatePrefetchManager::NoStatePrefetchData::OnHandleNavigatedAway(
NoStatePrefetchHandle* handle) { … }
void NoStatePrefetchManager::NoStatePrefetchData::OnHandleCanceled(
NoStatePrefetchHandle* handle) { … }
std::unique_ptr<NoStatePrefetchContents>
NoStatePrefetchManager::NoStatePrefetchData::ReleaseContents() { … }
void NoStatePrefetchManager::SourceNavigatedAway(
NoStatePrefetchData* prefetch_data) { … }
bool NoStatePrefetchManager::IsLowEndDevice() const { … }
bool NoStatePrefetchManager::IsPredictionEnabled(Origin origin) { … }
void NoStatePrefetchManager::MaybePreconnect(Origin origin,
const GURL& url_arg) const { … }
std::unique_ptr<NoStatePrefetchHandle>
NoStatePrefetchManager::StartPrefetchingWithPreconnectFallback(
Origin origin,
const GURL& url_arg,
const content::Referrer& referrer,
const std::optional<url::Origin>& initiator_origin,
const gfx::Rect& bounds,
SessionStorageNamespace* session_storage_namespace,
base::WeakPtr<content::PreloadingAttempt> attempt) { … }
void NoStatePrefetchManager::StartSchedulingPeriodicCleanups() { … }
void NoStatePrefetchManager::StopSchedulingPeriodicCleanups() { … }
void NoStatePrefetchManager::PeriodicCleanup() { … }
void NoStatePrefetchManager::PostCleanupTask() { … }
base::TimeTicks NoStatePrefetchManager::GetExpiryTimeForNewPrerender(
Origin origin) const { … }
base::TimeTicks NoStatePrefetchManager::GetExpiryTimeForNavigatedAwayPrerender()
const { … }
void NoStatePrefetchManager::DeleteOldEntries() { … }
void NoStatePrefetchManager::DeleteToDeletePrerenders() { … }
base::Time NoStatePrefetchManager::GetCurrentTime() const { … }
base::TimeTicks NoStatePrefetchManager::GetCurrentTimeTicks() const { … }
void NoStatePrefetchManager::SetTickClockForTesting(
const base::TickClock* tick_clock) { … }
void NoStatePrefetchManager::AddObserver(
std::unique_ptr<NoStatePrefetchManagerObserver> observer) { … }
std::unique_ptr<NoStatePrefetchContents>
NoStatePrefetchManager::CreateNoStatePrefetchContents(
const GURL& url,
const content::Referrer& referrer,
const std::optional<url::Origin>& initiator_origin,
Origin origin) { … }
void NoStatePrefetchManager::SortActivePrefetches() { … }
NoStatePrefetchManager::NoStatePrefetchData*
NoStatePrefetchManager::FindNoStatePrefetchData(
const GURL& url,
SessionStorageNamespace* session_storage_namespace) { … }
NoStatePrefetchManager::NoStatePrefetchDataVector::iterator
NoStatePrefetchManager::FindIteratorForNoStatePrefetchContents(
NoStatePrefetchContents* no_state_prefetch_contents) { … }
bool NoStatePrefetchManager::DoesRateLimitAllowPrefetch(Origin origin) const { … }
void NoStatePrefetchManager::DeleteOldWebContents() { … }
bool NoStatePrefetchManager::GetPrefetchInformation(
const GURL& url,
base::TimeDelta* prefetch_age,
FinalStatus* final_status,
Origin* origin) { … }
void NoStatePrefetchManager::SetPrefetchFinalStatusForUrl(
const GURL& url,
FinalStatus final_status) { … }
void NoStatePrefetchManager::OnPrefetchUsed(const GURL& url) { … }
void NoStatePrefetchManager::CleanUpOldNavigations(
std::vector<NavigationRecord>* navigations,
base::TimeDelta max_age) { … }
void NoStatePrefetchManager::ScheduleDeleteOldWebContents(
std::unique_ptr<WebContents> tab,
OnCloseWebContentsDeleter* deleter) { … }
void NoStatePrefetchManager::AddToHistory(NoStatePrefetchContents* contents) { … }
base::Value::List NoStatePrefetchManager::GetActivePrerenders() const { … }
void NoStatePrefetchManager::SkipNoStatePrefetchContentsAndMaybePreconnect(
const GURL& url,
Origin origin,
FinalStatus final_status) const { … }
void NoStatePrefetchManager::AddPrerenderProcessHost(
content::RenderProcessHost* process_host) { … }
bool NoStatePrefetchManager::MayReuseProcessHost(
content::RenderProcessHost* process_host) { … }
void NoStatePrefetchManager::RenderProcessHostDestroyed(
content::RenderProcessHost* host) { … }
base::WeakPtr<NoStatePrefetchManager> NoStatePrefetchManager::AsWeakPtr() { … }
void NoStatePrefetchManager::ClearPrefetchInformationForTesting() { … }
std::unique_ptr<NoStatePrefetchHandle>
NoStatePrefetchManager::StartPrefetchingWithPreconnectFallbackForTesting(
Origin origin,
const GURL& url,
const std::optional<url::Origin>& initiator_origin) { … }
void NoStatePrefetchManager::SetNoStatePrefetchContentsFactoryForTest(
NoStatePrefetchContents::Factory* no_state_prefetch_contents_factory) { … }
}