#ifndef CONTENT_BROWSER_PRELOADING_PRELOADING_DATA_IMPL_H_
#define CONTENT_BROWSER_PRELOADING_PRELOADING_DATA_IMPL_H_
#include <memory>
#include <string_view>
#include <tuple>
#include <vector>
#include "content/browser/preloading/preloading_confidence.h"
#include "content/browser/preloading/preloading_prediction.h"
#include "content/public/browser/preloading_data.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "url/gurl.h"
namespace content {
class PrefetchKey;
class PrefetchService;
class PreloadingAttemptImpl;
enum class PredictorConfusionMatrix {
kTruePositive = 0,
kFalsePositive = 1,
kTrueNegative = 2,
kFalseNegative = 3,
kMaxValue = kFalseNegative
};
class CONTENT_EXPORT PreloadingDataImpl
: public PreloadingData,
public WebContentsUserData<PreloadingDataImpl>,
public WebContentsObserver {
public:
~PreloadingDataImpl() override;
static PreloadingDataImpl* GetOrCreateForWebContents(
WebContents* web_contents);
static PreloadingURLMatchCallback GetPrefetchServiceMatcher(
PrefetchService& prefetch_service,
const PrefetchKey& predicted);
PreloadingDataImpl(const PreloadingDataImpl& other) = delete;
PreloadingDataImpl& operator=(const PreloadingDataImpl& other) = delete;
PreloadingAttempt* AddPreloadingAttempt(
PreloadingPredictor predictor,
PreloadingType preloading_type,
PreloadingURLMatchCallback url_match_predicate,
ukm::SourceId triggering_primary_page_source_id) override;
void AddPreloadingPrediction(
PreloadingPredictor predictor,
int confidence,
PreloadingURLMatchCallback url_match_predicate,
ukm::SourceId triggering_primary_page_source_id) override;
void SetIsNavigationInDomainCallback(
PreloadingPredictor predictor,
PredictorDomainCallback is_navigation_in_domain_callback) override;
void SetHasSpeculationRulesPrerender();
bool HasSpeculationRulesPrerender() override;
void OnPreloadingHeuristicsModelInput(
const GURL& url,
ModelPredictionTrainingData::OutcomeCallback on_record_outcome) override;
void AddPreloadingPrediction(const PreloadingPredictor& predictor,
PreloadingConfidence confidence,
PreloadingURLMatchCallback url_match_predicate,
ukm::SourceId triggering_primary_page_source_id);
PreloadingAttemptImpl* AddPreloadingAttempt(
const PreloadingPredictor& creating_predictor,
const PreloadingPredictor& enacting_predictor,
PreloadingType preloading_type,
PreloadingURLMatchCallback url_match_predicate,
ukm::SourceId triggering_primary_page_source_id);
void CopyPredictorDomains(const PreloadingDataImpl& other,
const std::vector<PreloadingPredictor>& predictors);
void AddExperimentalPreloadingPrediction(
std::string_view name,
PreloadingURLMatchCallback url_match_predicate,
float score,
float min_score,
float max_score,
size_t buckets);
void DidStartNavigation(NavigationHandle* navigation_handle) override;
void DidFinishNavigation(NavigationHandle* navigation_handle) override;
void WebContentsDestroyed() override;
static bool IsLinkClickNavigation(NavigationHandle* navigation_handle);
size_t GetPredictionsSizeForTesting() const;
void SetMaxPredictionsToTenForTesting();
private:
explicit PreloadingDataImpl(WebContents* web_contents);
friend class WebContentsUserData<PreloadingDataImpl>;
WEB_CONTENTS_USER_DATA_KEY_DECL();
void RecordMetricsForPreloadingAttempts(
ukm::SourceId navigated_page_source_id);
void RecordUKMForPreloadingPredictions(
ukm::SourceId navigated_page_source_id);
void SetIsAccurateTriggeringAndPrediction(const GURL& navigated_url);
void RecordPreloadingAttemptPrecisionToUMA(
const PreloadingAttemptImpl& attempt);
void RecordPredictionPrecisionToUMA(const PreloadingPrediction& prediction);
void UpdatePreloadingAttemptRecallStats(const PreloadingAttemptImpl& attempt);
void UpdatePredictionRecallStats(const PreloadingPrediction& prediction);
void ResetRecallStats();
void RecordRecallStatsToUMA(NavigationHandle* navigation_handle);
base::flat_map<PreloadingPredictor, PredictorDomainCallback>
is_navigation_in_predictor_domain_callbacks_;
base::flat_set<PreloadingPredictor> predictions_recall_stats_;
base::flat_set<std::pair<PreloadingPredictor, PreloadingType>>
preloading_attempt_recall_stats_;
std::vector<ExperimentalPreloadingPrediction> experimental_predictions_;
size_t total_seen_experimental_predictions_ = 0;
std::vector<ModelPredictionTrainingData> ml_predictions_;
size_t total_seen_ml_predictions_ = 0;
std::vector<std::unique_ptr<PreloadingAttemptImpl>> preloading_attempts_;
std::vector<PreloadingPrediction> preloading_predictions_;
size_t total_seen_preloading_predictions_ = 0;
bool has_speculation_rules_prerender_ = false;
uint32_t sampling_seed_;
bool max_predictions_is_ten_for_testing_ = false;
};
}
#endif