diff options
Diffstat (limited to 'chromium/components/optimization_guide/hints_fetcher.h')
-rw-r--r-- | chromium/components/optimization_guide/hints_fetcher.h | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/chromium/components/optimization_guide/hints_fetcher.h b/chromium/components/optimization_guide/hints_fetcher.h index f8bca072c7d..f477d62ae31 100644 --- a/chromium/components/optimization_guide/hints_fetcher.h +++ b/chromium/components/optimization_guide/hints_fetcher.h @@ -14,9 +14,12 @@ #include "base/memory/scoped_refptr.h" #include "base/optional.h" #include "base/sequence_checker.h" +#include "base/time/clock.h" #include "components/optimization_guide/proto/hints.pb.h" #include "url/gurl.h" +class PrefService; + namespace network { class SharedURLLoaderFactory; class SimpleURLLoader; @@ -24,6 +27,12 @@ class SimpleURLLoader; namespace optimization_guide { +// Callback to inform the caller that the remote hints have been fetched and +// to pass back the fetched hints response from the remote Optimization Guide +// Service. +using HintsFetchedCallback = base::OnceCallback<void( + base::Optional<std::unique_ptr<proto::GetHintsResponse>>)>; + // A class to handle requests for optimization hints from a remote Optimization // Guide Service. // @@ -31,16 +40,11 @@ namespace optimization_guide { // Owner must ensure that |hint_cache| remains alive for the lifetime of // |HintsFetcher|. class HintsFetcher { - // Callback to inform the caller that the remote hints have been fetched and - // to pass back the fetched hints response from the remote Optimization Guide - // Service. - using HintsFetchedCallback = base::OnceCallback<void( - base::Optional<std::unique_ptr<proto::GetHintsResponse>>)>; - public: HintsFetcher( scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, - GURL optimization_guide_service_url); + GURL optimization_guide_service_url, + PrefService* pref_service); virtual ~HintsFetcher(); // Requests hints from the Optimization Guide Service if a request for them is @@ -52,6 +56,18 @@ class HintsFetcher { const std::vector<std::string>& hosts, HintsFetchedCallback hints_fetched_callback); + // Set |time_clock_| for testing. + void SetTimeClockForTesting(const base::Clock* time_clock); + + // Clear all the hosts and expiration times from the + // HintsFetcherHostsSuccessfullyFetched dictionary pref. + static void ClearHostsSuccessfullyFetched(PrefService* pref_service); + + // Record whether the host was covered by a hints fetch and any returned + // hints would not have expired. + static void RecordHintsFetcherCoverage(PrefService* pref_serivce, + const std::string& host); + private: // URL loader completion callback. void OnURLLoadComplete(std::unique_ptr<std::string> response_body); @@ -64,6 +80,13 @@ class HintsFetcher { int status, int response_code); + // Stores the hosts in |hosts_in_fetch_| in the + // HintsFetcherHostsSuccessfullyFetched dictionary pref. The value stored for + // each host is the time that the hints fetched for each host will expire. + // |hosts_in_fetch_| is cleared once the hosts are stored + // in the pref. + void UpdateHostsSuccessfullyFetched(); + // Used to hold the GetHintsRequest being constructed and sent as a remote // request. std::unique_ptr<proto::GetHintsRequest> get_hints_request_; @@ -78,6 +101,15 @@ class HintsFetcher { // Holds the |URLLoader| for an active hints request. std::unique_ptr<network::SimpleURLLoader> url_loader_; + // A reference to the PrefService for this profile. Not owned. + PrefService* pref_service_ = nullptr; + + // Holds the hosts being requested by the hints fetcher. + std::vector<std::string> hosts_fetched_; + + // Clock used for recording time that the hints fetch occurred. + const base::Clock* time_clock_; + // Used for creating a |url_loader_| when needed for request hints. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; |