#include "components/optimization_guide/core/prediction_model_download_manager.h"
#include "base/containers/flat_set.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/observer_list.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "components/crx_file/crx_verifier.h"
#include "components/download/public/background_service/background_download_service.h"
#include "components/optimization_guide/core/model_util.h"
#include "components/optimization_guide/core/optimization_guide_enums.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_switches.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/core/prediction_model_download_observer.h"
#include "components/optimization_guide/core/prediction_model_store.h"
#include "components/services/unzip/public/cpp/unzip.h"
#include "crypto/sha2.h"
#include "google_apis/common/api_key_request_util.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#if BUILDFLAG(IS_IOS)
#include "components/services/unzip/in_process_unzipper.h"
#else
#include "components/services/unzip/content/unzip_service.h"
#endif
namespace optimization_guide {
namespace {
constexpr uint8_t kPublisherKeyHash[] = …;
const net::NetworkTrafficAnnotationTag
kOptimizationGuidePredictionModelsTrafficAnnotation = …;
void RecordPredictionModelDownloadStatus(PredictionModelDownloadStatus status) { … }
bool WriteModelInfoProtoToFile(const proto::ModelInfo& model_info,
const base::FilePath& file_path) { … }
}
const char kPredictionModelOptimizationTargetCustomDataKey[] = …;
PredictionModelDownloadManager::PredictionModelDownloadManager(
download::BackgroundDownloadService* download_service,
GetBaseModelDirForDownloadCallback get_base_model_dir_for_download_callback,
scoped_refptr<base::SequencedTaskRunner> background_task_runner)
: … { … }
PredictionModelDownloadManager::~PredictionModelDownloadManager() = default;
void PredictionModelDownloadManager::StartDownload(
const GURL& download_url,
proto::OptimizationTarget optimization_target) { … }
void PredictionModelDownloadManager::CancelAllPendingDownloads() { … }
bool PredictionModelDownloadManager::IsAvailableForDownloads() const { … }
void PredictionModelDownloadManager::AddObserver(
PredictionModelDownloadObserver* observer) { … }
void PredictionModelDownloadManager::RemoveObserver(
PredictionModelDownloadObserver* observer) { … }
void PredictionModelDownloadManager::OnDownloadServiceReady(
const std::set<std::string>& pending_download_guids,
const std::map<std::string, base::FilePath>& successful_downloads) { … }
void PredictionModelDownloadManager::OnDownloadServiceUnavailable() { … }
void PredictionModelDownloadManager::OnDownloadStarted(
proto::OptimizationTarget optimization_target,
base::TimeTicks download_requested_time,
const std::string& guid,
download::DownloadParams::StartResult start_result) { … }
void PredictionModelDownloadManager::OnDownloadSucceeded(
std::optional<proto::OptimizationTarget> optimization_target,
const std::string& guid,
const base::FilePath& download_file_path) { … }
void PredictionModelDownloadManager::OnDownloadFailed(
std::optional<proto::OptimizationTarget> optimization_target,
const std::string& guid) { … }
bool PredictionModelDownloadManager::VerifyDownload(
const base::FilePath& download_file_path,
const base::FilePath& base_model_dir,
bool delete_file_on_error) { … }
void PredictionModelDownloadManager::StartUnzipping(
proto::OptimizationTarget optimization_target,
const base::FilePath& download_file_path,
const base::FilePath& base_model_dir,
bool is_verify_success) { … }
void PredictionModelDownloadManager::OnDownloadUnzipped(
proto::OptimizationTarget optimization_target,
const base::FilePath& original_file_path,
const base::FilePath& base_model_dir,
bool success) { … }
std::optional<proto::PredictionModel>
PredictionModelDownloadManager::ProcessUnzippedContents(
const base::FilePath& base_model_dir) { … }
void PredictionModelDownloadManager::NotifyModelReady(
proto::OptimizationTarget optimization_target,
const base::FilePath& base_model_dir,
const std::optional<proto::PredictionModel>& model) { … }
void PredictionModelDownloadManager::NotifyModelDownloadFailed(
proto::OptimizationTarget optimization_target) { … }
}