#include "components/optimization_guide/core/model_execution/model_execution_manager.h"
#include "base/command_line.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/types/expected.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_fetcher.h"
#include "components/optimization_guide/core/model_execution/model_execution_util.h"
#include "components/optimization_guide/core/model_execution/on_device_model_adaptation_loader.h"
#include "components/optimization_guide/core/model_execution/on_device_model_metadata.h"
#include "components/optimization_guide/core/model_execution/on_device_model_service_controller.h"
#include "components/optimization_guide/core/model_execution/optimization_guide_model_execution_error.h"
#include "components/optimization_guide/core/model_quality/model_quality_log_entry.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_logger.h"
#include "components/optimization_guide/core/optimization_guide_model_executor.h"
#include "components/optimization_guide/core/optimization_guide_model_provider.h"
#include "components/optimization_guide/core/optimization_guide_prefs.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/core/optimization_metadata.h"
#include "components/optimization_guide/proto/common_types.pb.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace optimization_guide {
namespace {
const std::string& ProtoName(ModelBasedCapabilityKey feature) { … }
class ScopedModelExecutionResponseLogger { … };
GURL GetModelExecutionServiceURL() { … }
void RecordSessionUsedRemoteExecutionHistogram(ModelBasedCapabilityKey feature,
bool is_remote) { … }
void RecordModelExecutionResultHistogram(ModelBasedCapabilityKey feature,
bool result) { … }
void NoOpExecuteRemoteFn(
ModelBasedCapabilityKey feature,
const google::protobuf::MessageLite& request,
std::unique_ptr<proto::LogAiDataRequest> log_ai_data_request,
OptimizationGuideModelExecutionResultCallback callback) { … }
std::map<ModelBasedCapabilityKey, OnDeviceModelAdaptationLoader>
GetRequiredModelAdaptationLoaders(
OptimizationGuideModelProvider* model_provider,
base::WeakPtr<OnDeviceModelComponentStateManager>
on_device_component_state_manager,
PrefService* local_state,
base::WeakPtr<OnDeviceModelServiceController>
on_device_model_service_controller) { … }
}
ModelExecutionError;
ModelExecutionManager::ModelExecutionManager(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
PrefService* local_state,
signin::IdentityManager* identity_manager,
scoped_refptr<OnDeviceModelServiceController>
on_device_model_service_controller,
OptimizationGuideModelProvider* model_provider,
base::WeakPtr<OnDeviceModelComponentStateManager>
on_device_component_state_manager,
OptimizationGuideLogger* optimization_guide_logger,
base::WeakPtr<ModelQualityLogsUploaderService>
model_quality_uploader_service)
: … { … }
ModelExecutionManager::~ModelExecutionManager() { … }
void ModelExecutionManager::Shutdown() { … }
void ModelExecutionManager::ExecuteModel(
ModelBasedCapabilityKey feature,
const google::protobuf::MessageLite& request_metadata,
std::unique_ptr<proto::LogAiDataRequest> log_ai_data_request,
OptimizationGuideModelExecutionResultCallback callback) { … }
bool ModelExecutionManager::CanCreateOnDeviceSession(
ModelBasedCapabilityKey feature,
OnDeviceModelEligibilityReason* on_device_model_eligibility_reason) { … }
std::unique_ptr<OptimizationGuideModelExecutor::Session>
ModelExecutionManager::StartSession(
ModelBasedCapabilityKey feature,
const std::optional<SessionConfigParams>& config_params) { … }
void ModelExecutionManager::OnModelExecuteResponse(
ModelBasedCapabilityKey feature,
std::unique_ptr<proto::LogAiDataRequest> log_ai_data_request,
OptimizationGuideModelExecutionResultCallback callback,
base::expected<const proto::ExecuteResponse,
OptimizationGuideModelExecutionError> execute_response) { … }
void ModelExecutionManager::OnModelUpdated(
proto::OptimizationTarget optimization_target,
base::optional_ref<const ModelInfo> model_info) { … }
}