// Copyright 2023 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CHROME_BROWSER_PREDICTORS_LCP_CRITICAL_PATH_PREDICTOR_LCP_CRITICAL_PATH_PREDICTOR_UTIL_H_ #define CHROME_BROWSER_PREDICTORS_LCP_CRITICAL_PATH_PREDICTOR_LCP_CRITICAL_PATH_PREDICTOR_UTIL_H_ #include <optional> #include "chrome/browser/predictors/lcp_critical_path_predictor/lcp_critical_path_predictor.pb.h" #include "chrome/browser/predictors/loading_predictor_config.h" #include "components/sqlite_proto/key_value_data.h" #include "components/sqlite_proto/key_value_table.h" #include "third_party/blink/public/mojom/lcp_critical_path_predictor/lcp_critical_path_predictor.mojom.h" namespace url { class Origin; } // namespace url namespace predictors { namespace lcpp { struct LastVisitTimeCompare { … }; } // namespace lcpp // Converts LcppStat to LCPCriticalPathPredictorNavigationTimeHint // so that it can be passed to the renderer via the navigation handle. std::optional<blink::mojom::LCPCriticalPathPredictorNavigationTimeHint> ConvertLcppStatToLCPCriticalPathPredictorNavigationTimeHint( const LcppStat& data); // Returns possible fonts from past loads for a given `stat`. // The returned urls are ordered by descending frequency (the most // frequent one comes first). If there is no data, it returns an empty // vector. std::vector<GURL> PredictFetchedFontUrls(const LcppStat& stat); // Returns possible preconnects based on past loads for a given `stat`. // The returned origins are ordered by descending frequency (the most // frequent one comes first). If there is no data, it returns an empty // vector. std::vector<GURL> PredictPreconnectableOrigins(const LcppStat& stat); // Returns possible subresource URLs from past loads for a given `stat`. // The returned URLs are ordered by descending frequency (the most // frequent one comes first). If there is no data, it returns an empty // vector. std::vector<GURL> PredictFetchedSubresourceUrls(const LcppStat& stat); // Returns possible unused preload URLs from past loads for a given `stat`. // The returned URLs are ordered by descending frequency (the most // frequent one comes first). If there is no data, it returns an empty // vector. std::vector<GURL> PredictUnusedPreloads(const LcppStat& stat); // An input to update LcppData. struct LcppDataInputs { … }; bool UpdateLcppStatWithLcppDataInputs(const LoadingPredictorConfig& config, const LcppDataInputs& inputs, LcppStat& stat); // Update `lcpp_stat_data` adding `new_entry` with `sliding_window_size` and // `max_histogram_buckets` parameters by the top-k algorithm. // See lcp_critical_path_predictor_util.cc for detail. // `dropped_entry` is assigned if this updating dropped an existing entry. void UpdateLcppStringFrequencyStatData( size_t sliding_window_size, size_t max_histogram_buckets, const std::string& new_entry, LcppStringFrequencyStatData& lcpp_stat_data, std::optional<std::string>& dropped_entry); // Update `lcpp_stat_data` adding `new_entry` with `sliding_window_size` and // `max_histogram_buckets` parameters by the top-k algorithm while // keeping `map` have same keys in `lcpp_stat_data`. // See lcp_critical_path_predictor_util.cc for detail. template <typename T> T* UpdateFrequencyStatAndTryGetEntry( size_t sliding_window_size, size_t max_histogram_buckets, const std::string& new_entry, LcppStringFrequencyStatData& frequency_stat, google::protobuf::Map<std::string, T>& map) { … } // Aligns `frequency_stat` elements and `map` elements. // Clears both if `frequency_stat` has invalid parameters too. template <typename T> bool CanonicalizeFrequencyData(size_t max_histogram_buckets, LcppStringFrequencyStatData& frequency_stat, google::protobuf::Map<std::string, T>& map) { … } // Returns true if the LcppData is valid. i.e. looks not corrupted. // Otherwise, data might be corrupted. bool IsValidLcppStat(const LcppStat& lcpp_stat); // Returns true if the url is valid for learning. bool IsURLValidForLcpp(const GURL& url); // Returns the first level path of the url. The url should be true for // the above IsURLValidForLcpp(url). // This function can return empty string if the URL doesn't have // the first level path or it length exceeds kLCPPMultipleKeyMaxPathLength. std::string GetFirstLevelPath(const GURL& url); class LcppDataMap { … }; } // namespace predictors #endif // CHROME_BROWSER_PREDICTORS_LCP_CRITICAL_PATH_PREDICTOR_LCP_CRITICAL_PATH_PREDICTOR_UTIL_H_