#include "components/safe_browsing/content/browser/client_side_detection_service.h"
#include <algorithm>
#include <memory>
#include "base/callback_list.h"
#include "base/containers/contains.h"
#include "base/containers/queue.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/escape.h"
#include "base/strings/strcat.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/optimization_guide/core/optimization_guide_model_provider.h"
#include "components/optimization_guide/proto/models.pb.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/content/browser/client_side_phishing_model.h"
#include "components/safe_browsing/content/browser/web_ui/safe_browsing_ui.h"
#include "components/safe_browsing/content/common/safe_browsing.mojom.h"
#include "components/safe_browsing/core/common/fbs/client_model_generated.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/proto/client_model.pb.h"
#include "components/safe_browsing/core/common/proto/csd.pb.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 "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "crypto/sha2.h"
#include "google_apis/google_api_keys.h"
#include "ipc/ipc_channel_proxy.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/ip_address.h"
#include "net/base/load_flags.h"
#include "net/http/http_request_headers.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"
#include "url/gurl.h"
BrowserThread;
namespace safe_browsing {
const int ClientSideDetectionService::kReportsIntervalDays = …;
const int ClientSideDetectionService::kMaxReportsPerInterval = …;
const int ClientSideDetectionService::kNegativeCacheIntervalDays = …;
const int ClientSideDetectionService::kPositiveCacheIntervalMinutes = …;
const char ClientSideDetectionService::kClientReportPhishingUrl[] = …;
struct ClientSideDetectionService::ClientPhishingReportInfo { … };
ClientSideDetectionService::CacheState::CacheState(bool phish, base::Time time)
: … { … }
ClientSideDetectionService::ClientSideDetectionService(
std::unique_ptr<Delegate> delegate,
optimization_guide::OptimizationGuideModelProvider* opt_guide,
const scoped_refptr<base::SequencedTaskRunner>& background_task_runner)
: … { … }
ClientSideDetectionService::~ClientSideDetectionService() { … }
void ClientSideDetectionService::Shutdown() { … }
void ClientSideDetectionService::OnPrefsUpdated() { … }
void ClientSideDetectionService::SendClientReportPhishingRequest(
std::unique_ptr<ClientPhishingRequest> verdict,
ClientReportPhishingRequestCallback callback,
const std::string& access_token) { … }
bool ClientSideDetectionService::IsPrivateIPAddress(
const net::IPAddress& address) const { … }
bool ClientSideDetectionService::IsLocalResource(
const net::IPAddress& address) const { … }
void ClientSideDetectionService::OnURLLoaderComplete(
network::SimpleURLLoader* url_loader,
base::Time start_time,
std::unique_ptr<std::string> response_body) { … }
void ClientSideDetectionService::SendModelToRenderers() { … }
void ClientSideDetectionService::StartClientReportPhishingRequest(
std::unique_ptr<ClientPhishingRequest> request,
ClientReportPhishingRequestCallback callback,
const std::string& access_token) { … }
void ClientSideDetectionService::HandlePhishingVerdict(
network::SimpleURLLoader* source,
const GURL& url,
int net_error,
std::optional<net::HttpStatusCode> response_code,
const std::string& data) { … }
bool ClientSideDetectionService::GetValidCachedResult(const GURL& url,
bool* is_phishing) { … }
void ClientSideDetectionService::UpdateCache() { … }
bool ClientSideDetectionService::AtPhishingReportLimit() { … }
int ClientSideDetectionService::GetPhishingNumReports() { … }
bool ClientSideDetectionService::AddPhishingReport(base::Time timestamp) { … }
bool ClientSideDetectionService::LoadPhishingReportTimesFromPrefs() { … }
GURL ClientSideDetectionService::GetClientReportUrl(
const std::string& report_url) { … }
CSDModelType ClientSideDetectionService::GetModelType() { … }
base::ReadOnlySharedMemoryRegion
ClientSideDetectionService::GetModelSharedMemoryRegion() { … }
const base::File& ClientSideDetectionService::GetVisualTfLiteModel() { … }
const base::File& ClientSideDetectionService::GetImageEmbeddingModel() { … }
bool ClientSideDetectionService::
IsModelMetadataImageEmbeddingVersionMatching() { … }
void ClientSideDetectionService::SetURLLoaderFactoryForTesting(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) { … }
void ClientSideDetectionService::OnRenderProcessHostCreated(
content::RenderProcessHost* rph) { … }
void ClientSideDetectionService::SetPhishingModel(
content::RenderProcessHost* rph,
bool new_renderer_process_host) { … }
const base::flat_map<std::string, TfLiteModelMetadata::Threshold>&
ClientSideDetectionService::GetVisualTfLiteModelThresholds() { … }
void ClientSideDetectionService::ClassifyPhishingThroughThresholds(
ClientPhishingRequest* verdict) { … }
base::WeakPtr<ClientSideDetectionService>
ClientSideDetectionService::GetWeakPtr() { … }
bool ClientSideDetectionService::IsModelAvailable() { … }
int ClientSideDetectionService::GetTriggerModelVersion() { … }
bool ClientSideDetectionService::HasImageEmbeddingModel() { … }
bool ClientSideDetectionService::IsSubscribedToImageEmbeddingModelUpdates() { … }
base::CallbackListSubscription
ClientSideDetectionService::RegisterCallbackForModelUpdates(
base::RepeatingCallback<void()> callback) { … }
void ClientSideDetectionService::SetModelAndVisualTfLiteForTesting(
const base::FilePath& model,
const base::FilePath& visual_tf_lite) { … }
}