#include "components/safe_browsing/content/browser/client_side_phishing_model.h"
#include <stdint.h>
#include <memory>
#include <optional>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "components/optimization_guide/core/optimization_guide_model_provider.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/proto/client_side_phishing_model_metadata.pb.h"
#include "components/optimization_guide/proto/models.pb.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 "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace safe_browsing {
namespace {
const char kOverrideCsdModelFlag[] = …;
void ReturnModelOverrideFailure(
base::OnceCallback<void(std::pair<std::string, base::File>)> callback) { … }
void ReadOverridenModel(
base::FilePath path,
base::OnceCallback<void(std::pair<std::string, base::File>)> callback) { … }
base::File LoadImageEmbeddingModelFile(const base::FilePath& model_file_path) { … }
std::pair<std::string, base::File> LoadModelAndVisualTfLiteFile(
const base::FilePath& model_file_path,
base::flat_set<base::FilePath> additional_files) { … }
void CloseModelFile(base::File model_file) { … }
void RecordImageEmbeddingModelUpdateSuccess(bool success) { … }
}
ClientSidePhishingModel::ClientSidePhishingModel(
optimization_guide::OptimizationGuideModelProvider* opt_guide,
const scoped_refptr<base::SequencedTaskRunner>& background_task_runner)
: … { … }
void ClientSidePhishingModel::OnModelUpdated(
optimization_guide::proto::OptimizationTarget optimization_target,
base::optional_ref<const optimization_guide::ModelInfo> model_info) { … }
void ClientSidePhishingModel::SubscribeToImageEmbedderOptimizationGuide() { … }
bool ClientSidePhishingModel::IsSubscribedToImageEmbeddingModelUpdates() { … }
void ClientSidePhishingModel::OnModelAndVisualTfLiteFileLoaded(
std::optional<optimization_guide::proto::Any> model_metadata,
std::pair<std::string, base::File> model_and_tflite) { … }
void ClientSidePhishingModel::OnImageEmbeddingModelLoaded(
std::optional<optimization_guide::proto::Any> model_metadata,
base::File image_embedding_model) { … }
bool ClientSidePhishingModel::IsModelMetadataImageEmbeddingVersionMatching() { … }
int ClientSidePhishingModel::GetTriggerModelVersion() { … }
ClientSidePhishingModel::~ClientSidePhishingModel() { … }
base::CallbackListSubscription ClientSidePhishingModel::RegisterCallback(
base::RepeatingCallback<void()> callback) { … }
bool ClientSidePhishingModel::IsEnabled() const { … }
bool ClientSidePhishingModel::VerifyCSDFlatBufferIndicesAndFields(
const flat::ClientSideModel* model) { … }
const base::flat_map<std::string, TfLiteModelMetadata::Threshold>&
ClientSidePhishingModel::GetVisualTfLiteModelThresholds() const { … }
const base::File& ClientSidePhishingModel::GetVisualTfLiteModel() const { … }
const base::File& ClientSidePhishingModel::GetImageEmbeddingModel() const { … }
bool ClientSidePhishingModel::HasImageEmbeddingModel() { … }
CSDModelType ClientSidePhishingModel::GetModelType() const { … }
base::ReadOnlySharedMemoryRegion
ClientSidePhishingModel::GetModelSharedMemoryRegion() const { … }
void ClientSidePhishingModel::SetModelStringForTesting(
const std::string& model_str,
base::File visual_tflite_model) { … }
void ClientSidePhishingModel::NotifyCallbacksOnUI() { … }
void ClientSidePhishingModel::SetVisualTfLiteModelForTesting(base::File file) { … }
void ClientSidePhishingModel::SetModelTypeForTesting(CSDModelType model_type) { … }
void ClientSidePhishingModel::ClearMappedRegionForTesting() { … }
void* ClientSidePhishingModel::GetFlatBufferMemoryAddressForTesting() { … }
void ClientSidePhishingModel::MaybeOverrideModel() { … }
void ClientSidePhishingModel::OnGetOverridenModelData(
CSDModelType model_type,
std::pair<std::string, base::File> model_and_tflite) { … }
void ClientSidePhishingModel::SetModelAndVisualTfLiteForTesting(
const base::FilePath& model_file_path,
const base::FilePath& visual_tf_lite_model_path) { … }
}