#ifndef CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_DOCUMENT_MANAGER_H_
#define CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_DOCUMENT_MANAGER_H_
#include <map>
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "content/common/content_export.h"
#include "content/public/browser/document_user_data.h"
#include "content/public/browser/prefetch_metrics.h"
#include "third_party/blink/public/mojom/speculation_rules/speculation_rules.mojom.h"
#include "third_party/blink/public/mojom/tokens/tokens.mojom.h"
#include "url/gurl.h"
namespace content {
class PrefetchContainer;
class PrefetchHandle;
class PrefetchService;
class PrefetchType;
class PreloadPipelineInfo;
class PreloadingPredictor;
class SpeculationRulesTags;
enum class PreloadingType;
class CONTENT_EXPORT PrefetchDocumentManager
: public DocumentUserData<PrefetchDocumentManager> {
public:
using PrefetchDestructionCallback =
base::RepeatingCallback<void(const GURL&)>;
~PrefetchDocumentManager() override;
PrefetchDocumentManager(const PrefetchDocumentManager&) = delete;
const PrefetchDocumentManager operator=(const PrefetchDocumentManager&) =
delete;
static PrefetchDocumentManager* FromDocumentToken(
int process_id,
const blink::DocumentToken& document_token);
void ProcessCandidates(
std::vector<blink::mojom::SpeculationCandidatePtr>& candidates);
bool MaybePrefetch(blink::mojom::SpeculationCandidatePtr candidate,
const PreloadingPredictor& enacting_predictor);
void PrefetchAheadOfPrerender(
scoped_refptr<PreloadPipelineInfo> preload_pipeline_info,
blink::mojom::SpeculationCandidatePtr candidate,
const PreloadingPredictor& enacting_predictor);
void PrefetchUrl(const GURL& url,
const PrefetchType& prefetch_type,
const PreloadingPredictor& enacting_predictor,
const blink::mojom::Referrer& referrer,
std::optional<SpeculationRulesTags> speculation_rules_tags,
const network::mojom::NoVarySearchPtr& no_vary_search_hint,
scoped_refptr<PreloadPipelineInfo> preload_pipeline_info);
bool HaveCanaryChecksStarted() const { return have_canary_checks_started_; }
void OnCanaryChecksStarted() { have_canary_checks_started_ = true; }
PrefetchReferringPageMetrics& GetReferringPageMetrics() {
return referring_page_metrics_;
}
void OnEligibilityCheckComplete(bool is_eligible);
void OnPrefetchSuccessful(PrefetchContainer* prefetch);
bool IsPrefetchAttemptFailedOrDiscarded(const GURL& url);
std::tuple<bool, base::WeakPtr<PrefetchContainer>> CanPrefetchNow(
PrefetchContainer* next_prefetch);
void SetPrefetchDestructionCallback(PrefetchDestructionCallback callback);
void PrefetchWillBeDestroyed(PrefetchContainer* prefetch);
base::WeakPtr<PrefetchDocumentManager> GetWeakPtr() {
return weak_method_factory_.GetWeakPtr();
}
static void SetPrefetchServiceForTesting(PrefetchService* prefetch_service);
void ResetPrefetchAheadOfPrerenderIfExist(PreloadingType preloading_type,
const GURL& url);
private:
explicit PrefetchDocumentManager(RenderFrameHost* rfh);
friend DocumentUserData;
PrefetchService* GetPrefetchService() const;
bool IsPrefetchAttemptFailedOrDiscardedInternal(
const GURL& url,
PreloadingType planned_max_preloading_type);
blink::DocumentToken document_token_;
std::map<std::pair<GURL, PreloadingType>, std::unique_ptr<PrefetchHandle>>
all_prefetches_;
bool have_canary_checks_started_{false};
std::vector<base::WeakPtr<PrefetchContainer>> completed_immediate_prefetches_;
std::vector<base::WeakPtr<PrefetchContainer>>
completed_non_immediate_prefetches_;
PrefetchReferringPageMetrics referring_page_metrics_;
PrefetchDestructionCallback prefetch_destruction_callback_;
base::WeakPtrFactory<PrefetchDocumentManager> weak_method_factory_{this};
DOCUMENT_USER_DATA_KEY_DECL();
};
}
#endif