#include "components/safe_browsing/core/browser/realtime/url_lookup_service_base.h"
#include <memory>
#include "base/base64url.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/not_fatal_until.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/enterprise/common/proto/connectors.pb.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/core/browser/referrer_chain_provider.h"
#include "components/safe_browsing/core/browser/verdict_cache_manager.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/safe_browsing/core/common/safebrowsing_constants.h"
#include "components/safe_browsing/core/common/utils.h"
#include "net/base/ip_address.h"
#include "net/base/load_flags.h"
#include "net/base/url_util.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/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
namespace safe_browsing {
namespace {
const size_t kNumFailuresToEnforceBackoff = …;
const size_t kMinBackOffResetDurationInSeconds = …;
const size_t kMaxBackOffResetDurationInSeconds = …;
const size_t kURLLookupTimeoutDurationInSeconds = …;
const int kRTLookupRequestVersion = …;
void RecordBooleanWithAndWithoutSuffix(const std::string& metric,
const std::string& suffix,
bool value) { … }
void RecordSparseWithAndWithoutSuffix(const std::string& metric,
const std::string& suffix,
int32_t value) { … }
void RecordTimesWithAndWithoutSuffix(const std::string& metric,
const std::string& suffix,
base::TimeDelta value) { … }
void RecordCount100WithAndWithoutSuffix(const std::string& metric,
const std::string& suffix,
int value) { … }
void RecordCount1MWithAndWithoutSuffix(const std::string& metric,
const std::string& suffix,
int value) { … }
void RecordRequestPopulationWithAndWithoutSuffix(
const std::string& metric,
const std::string& suffix,
ChromeUserPopulation::UserPopulation population) { … }
void RecordNetworkResultWithAndWithoutSuffix(const std::string& metric,
const std::string& suffix,
int net_error,
int response_code) { … }
RTLookupRequest::OSType GetRTLookupRequestOSType() { … }
void InvokeLookupResponseCallbacks(
std::vector<RTLookupResponseCallback> callbacks,
bool is_rt_lookup_successful,
bool is_cached_response,
std::unique_ptr<RTLookupResponse> response) { … }
}
RealTimeUrlLookupServiceBase::PendingRTLookupRequestData::
PendingRTLookupRequestData(std::unique_ptr<network::SimpleURLLoader> loader)
: … { … }
RealTimeUrlLookupServiceBase::PendingRTLookupRequestData::
PendingRTLookupRequestData(PendingRTLookupRequestData&&) = default;
RealTimeUrlLookupServiceBase::PendingRTLookupRequestData&
RealTimeUrlLookupServiceBase::PendingRTLookupRequestData::operator=(
PendingRTLookupRequestData&&) = default;
RealTimeUrlLookupServiceBase::PendingRTLookupRequestData::
~PendingRTLookupRequestData() = default;
void RealTimeUrlLookupServiceBase::PendingRTLookupRequestData::AddCallback(
RTLookupResponseCallback callback) { … }
RealTimeUrlLookupServiceBase::RealTimeUrlLookupServiceBase(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
VerdictCacheManager* cache_manager,
base::RepeatingCallback<ChromeUserPopulation()>
get_user_population_callback,
ReferrerChainProvider* referrer_chain_provider,
PrefService* pref_service,
WebUIDelegate* delegate)
: … { … }
RealTimeUrlLookupServiceBase::~RealTimeUrlLookupServiceBase() = default;
bool RealTimeUrlLookupServiceBase::CanCheckUrl(const GURL& url) { … }
SBThreatType RealTimeUrlLookupServiceBase::GetSBThreatTypeForRTThreatType(
RTLookupResponse::ThreatInfo::ThreatType rt_threat_type,
RTLookupResponse::ThreatInfo::VerdictType rt_verdict_type) { … }
GURL RealTimeUrlLookupServiceBase::SanitizeURL(const GURL& url) { … }
void RealTimeUrlLookupServiceBase::SanitizeReferrerChainEntries(
ReferrerChain* referrer_chain,
std::optional<base::Time> min_allowed_timestamp,
bool should_remove_subresource_url) { … }
base::WeakPtr<RealTimeUrlLookupServiceBase>
RealTimeUrlLookupServiceBase::GetWeakPtr() { … }
bool RealTimeUrlLookupServiceBase::IsInBackoffMode() const { … }
std::unique_ptr<RTLookupResponse>
RealTimeUrlLookupServiceBase::GetCachedRealTimeUrlVerdict(const GURL& url) { … }
void RealTimeUrlLookupServiceBase::MayBeCacheRealTimeUrlVerdict(
RTLookupResponse response) { … }
void RealTimeUrlLookupServiceBase::SendSampledRequest(
const GURL& url,
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
SessionID tab_id) { … }
void RealTimeUrlLookupServiceBase::StartLookup(
const GURL& url,
RTLookupResponseCallback response_callback,
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
SessionID tab_id) { … }
void RealTimeUrlLookupServiceBase::MaybeSendRequest(
const GURL& url,
const std::string& access_token_string,
RTLookupResponseCallback response_callback,
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
bool is_sampled_report,
SessionID tab_id) { … }
void RealTimeUrlLookupServiceBase::SendRequestInternal(
const GURL& url,
std::unique_ptr<network::ResourceRequest> resource_request,
const std::string& req_data,
std::optional<std::string> access_token_string,
RTLookupResponseCallback response_callback,
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
ChromeUserPopulation::UserPopulation user_population,
bool is_sampled_report,
std::optional<int> webui_token) { … }
void RealTimeUrlLookupServiceBase::OnURLLoaderComplete(
const GURL& url,
std::optional<std::string> access_token_string,
ChromeUserPopulation::UserPopulation user_population,
base::TimeTicks request_start_time,
bool is_sampled_report,
scoped_refptr<base::SequencedTaskRunner> response_callback_task_runner,
std::optional<int> webui_token,
std::unique_ptr<std::string> response_body) { … }
std::unique_ptr<network::ResourceRequest>
RealTimeUrlLookupServiceBase::GetResourceRequest() { … }
std::unique_ptr<RTLookupRequest> RealTimeUrlLookupServiceBase::FillRequestProto(
const GURL& url,
bool is_sampled_report,
SessionID tab_id) { … }
std::optional<int> RealTimeUrlLookupServiceBase::LogLookupRequest(
const RTLookupRequest& request,
const std::string& oauth_token) { … }
void RealTimeUrlLookupServiceBase::LogLookupResponseForToken(
std::optional<int> token,
const RTLookupResponse& response) { … }
void RealTimeUrlLookupServiceBase::OnResponseUnauthorized(
const std::string& invalid_access_token) { … }
void RealTimeUrlLookupServiceBase::Shutdown() { … }
}