#include "components/optimization_guide/core/hints_fetcher.h"
#include <memory>
#include <utility>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/strcat.h"
#include "base/time/default_clock.h"
#include "components/optimization_guide/core/hints_processing_util.h"
#include "components/optimization_guide/core/optimization_guide_common.mojom.h"
#include "components/optimization_guide/core/optimization_guide_constants.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_logger.h"
#include "components/optimization_guide/core/optimization_guide_prefs.h"
#include "components/optimization_guide/core/optimization_guide_switches.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/variations/net/variations_http_headers.h"
#include "net/base/load_flags.h"
#include "net/base/url_util.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
namespace optimization_guide {
namespace {
std::string GetStringNameForRequestContext(
proto::RequestContext request_context) { … }
void RecordRequestStatusHistogram(proto::RequestContext request_context,
FetcherRequestStatus status) { … }
void AppendOverrideHeadersIfNeeded(network::ResourceRequest& request) { … }
}
HintsFetcher::HintsFetcher(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const GURL& optimization_guide_service_url,
PrefService* pref_service,
OptimizationGuideLogger* optimization_guide_logger)
: … { … }
HintsFetcher::~HintsFetcher() { … }
void HintsFetcher::ClearHostsSuccessfullyFetched(PrefService* pref_service) { … }
void HintsFetcher::SetTimeClockForTesting(const base::Clock* time_clock) { … }
bool HintsFetcher::WasHostCoveredByFetch(PrefService* pref_service,
const std::string& host) { … }
bool HintsFetcher::WasHostCoveredByFetch(PrefService* pref_service,
const std::string& host,
const base::Clock* time_clock) { … }
void HintsFetcher::ClearSingleFetchedHost(PrefService* pref_service,
const std::string& host) { … }
void HintsFetcher::AddFetchedHostForTesting(PrefService* pref_service,
const std::string& host,
base::Time time) { … }
bool HintsFetcher::FetchOptimizationGuideServiceHints(
const std::vector<std::string>& hosts,
const std::vector<GURL>& urls,
const base::flat_set<optimization_guide::proto::OptimizationType>&
optimization_types,
optimization_guide::proto::RequestContext request_context,
const std::string& locale,
const std::string& access_token,
bool skip_cache,
HintsFetchedCallback hints_fetched_callback,
std::optional<proto::RequestContextMetadata> request_context_metadata) { … }
void HintsFetcher::HandleResponse(const std::string& get_hints_response_data,
int net_status,
int response_code,
bool skip_cache) { … }
void HintsFetcher::UpdateHostsSuccessfullyFetched(
base::TimeDelta valid_duration) { … }
void HintsFetcher::OnURLLoadComplete(
bool skip_cache,
std::unique_ptr<std::string> response_body) { … }
std::vector<GURL> HintsFetcher::GetSizeLimitedURLsForFetching(
const std::vector<GURL>& urls) const { … }
std::vector<std::string> HintsFetcher::GetSizeLimitedHostsDueForHintsRefresh(
const std::vector<std::string>& hosts,
bool skip_cache) const { … }
}