#include "components/optimization_guide/core/model_execution/on_device_model_service_controller.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/task/thread_pool.h"
#include "components/optimization_guide/core/model_execution/feature_keys.h"
#include "components/optimization_guide/core/model_execution/model_execution_features.h"
#include "components/optimization_guide/core/model_execution/model_execution_util.h"
#include "components/optimization_guide/core/model_execution/on_device_model_access_controller.h"
#include "components/optimization_guide/core/model_execution/on_device_model_adaptation_controller.h"
#include "components/optimization_guide/core/model_execution/on_device_model_adaptation_loader.h"
#include "components/optimization_guide/core/model_execution/on_device_model_component.h"
#include "components/optimization_guide/core/model_execution/on_device_model_metadata.h"
#include "components/optimization_guide/core/model_execution/safety_model_info.h"
#include "components/optimization_guide/core/model_execution/session_impl.h"
#include "components/optimization_guide/core/model_util.h"
#include "components/optimization_guide/core/optimization_guide_constants.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_model_executor.h"
#include "components/optimization_guide/core/optimization_guide_switches.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/proto/model_execution.pb.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/on_device_model/public/cpp/model_assets.h"
#include "services/on_device_model/public/mojom/on_device_model.mojom.h"
#include "services/on_device_model/public/mojom/on_device_model_service.mojom.h"
namespace optimization_guide {
namespace {
proto::OnDeviceModelVersions GetModelVersions(
const OnDeviceModelMetadata& model_metadata,
const SafetyModelInfo* safety_model_info,
std::optional<int64_t> adaptation_version) { … }
const auto& GetTokenLimits() { … }
}
OnDeviceModelServiceController::OnDeviceModelServiceController(
std::unique_ptr<OnDeviceModelAccessController> access_controller,
base::WeakPtr<optimization_guide::OnDeviceModelComponentStateManager>
on_device_component_state_manager)
: … { … }
OnDeviceModelServiceController::~OnDeviceModelServiceController() = default;
void OnDeviceModelServiceController::Init() { … }
OnDeviceModelEligibilityReason OnDeviceModelServiceController::CanCreateSession(
ModelBasedCapabilityKey feature) { … }
std::unique_ptr<OptimizationGuideModelExecutor::Session>
OnDeviceModelServiceController::CreateSession(
ModelBasedCapabilityKey feature,
ExecuteRemoteFn execute_remote_fn,
base::WeakPtr<OptimizationGuideLogger> optimization_guide_logger,
base::WeakPtr<ModelQualityLogsUploaderService>
model_quality_uploader_service,
const std::optional<SessionConfigParams>& config_params) { … }
void OnDeviceModelServiceController::GetEstimatedPerformanceClass(
GetEstimatedPerformanceClassCallback callback) { … }
mojo::Remote<on_device_model::mojom::OnDeviceModel>&
OnDeviceModelServiceController::GetOrCreateModelRemote(
ModelBasedCapabilityKey feature,
const on_device_model::ModelAssetPaths& model_paths,
base::optional_ref<const on_device_model::AdaptationAssetPaths>
adaptation_assets) { … }
void OnDeviceModelServiceController::MaybeCreateBaseModelRemote(
const on_device_model::ModelAssetPaths& model_paths) { … }
void OnDeviceModelServiceController::OnModelAssetsLoaded(
mojo::PendingReceiver<on_device_model::mojom::OnDeviceModel> model,
on_device_model::ModelAssets assets) { … }
void OnDeviceModelServiceController::SetLanguageDetectionModel(
base::optional_ref<const ModelInfo> model_info) { … }
void OnDeviceModelServiceController::MaybeUpdateSafetyModel(
base::optional_ref<const ModelInfo> model_info) { … }
on_device_model::ModelAssetPaths
OnDeviceModelServiceController::PopulateModelPaths() { … }
void OnDeviceModelServiceController::UpdateModel(
std::unique_ptr<OnDeviceModelMetadata> model_metadata) { … }
void OnDeviceModelServiceController::StartValidation() { … }
void OnDeviceModelServiceController::FinishValidation(
OnDeviceModelValidationResult result) { … }
void OnDeviceModelServiceController::MaybeUpdateModelAdaptation(
ModelBasedCapabilityKey feature,
std::unique_ptr<OnDeviceModelAdaptationMetadata> adaptation_metadata) { … }
void OnDeviceModelServiceController::OnLoadModelResult(
on_device_model::mojom::LoadModelResult result) { … }
void OnDeviceModelServiceController::OnBaseModelDisconnected() { … }
void OnDeviceModelServiceController::OnBaseModelRemoteIdle() { … }
void OnDeviceModelServiceController::OnModelAdaptationRemoteDisconnected(
ModelBasedCapabilityKey feature,
ModelRemoteDisconnectReason reason) { … }
OnDeviceModelServiceController::OnDeviceModelClient::OnDeviceModelClient(
ModelBasedCapabilityKey feature,
base::WeakPtr<OnDeviceModelServiceController> controller,
const on_device_model::ModelAssetPaths& model_paths,
base::optional_ref<const on_device_model::AdaptationAssetPaths>
adaptation_assets)
: … { … }
OnDeviceModelServiceController::OnDeviceModelClient::~OnDeviceModelClient() =
default;
bool OnDeviceModelServiceController::OnDeviceModelClient::ShouldUse() { … }
mojo::Remote<on_device_model::mojom::OnDeviceModel>&
OnDeviceModelServiceController::OnDeviceModelClient::GetModelRemote() { … }
void OnDeviceModelServiceController::OnDeviceModelClient::
OnResponseCompleted() { … }
void OnDeviceModelServiceController::OnDeviceModelClient::OnSessionTimedOut() { … }
scoped_refptr<const OnDeviceModelFeatureAdapter>
OnDeviceModelServiceController::GetFeatureAdapter(
ModelBasedCapabilityKey feature) { … }
void OnDeviceModelServiceController::AddOnDeviceModelAvailabilityChangeObserver(
ModelBasedCapabilityKey feature,
OnDeviceModelAvailabilityObserver* observer) { … }
void OnDeviceModelServiceController::
RemoveOnDeviceModelAvailabilityChangeObserver(
ModelBasedCapabilityKey feature,
OnDeviceModelAvailabilityObserver* observer) { … }
void OnDeviceModelServiceController::NotifyModelAvailabilityChange(
ModelBasedCapabilityKey feature) { … }
}