#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include <optional>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/path_service.h"
#include "base/system/sys_info.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/optimization_guide_on_device_model_installer.h"
#include "chrome/browser/download/background_download_service_factory.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/optimization_guide/chrome_hints_manager.h"
#include "chrome/browser/optimization_guide/chrome_model_quality_logs_uploader_service.h"
#include "chrome/browser/optimization_guide/chrome_prediction_model_store.h"
#include "chrome/browser/optimization_guide/model_execution/chrome_on_device_model_service_controller.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/chrome_paths.h"
#include "components/component_updater/component_updater_paths.h"
#include "components/component_updater/pref_names.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/metrics/metrics_log.h"
#include "components/metrics/version_utils.h"
#include "components/metrics_services_manager/metrics_services_manager.h"
#include "components/optimization_guide/core/command_line_top_host_provider.h"
#include "components/optimization_guide/core/hints_processing_util.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_features_controller.h"
#include "components/optimization_guide/core/model_execution/model_execution_manager.h"
#include "components/optimization_guide/core/model_execution/on_device_model_component.h"
#include "components/optimization_guide/core/model_execution/on_device_model_service_controller.h"
#include "components/optimization_guide/core/model_quality/model_quality_log_entry.h"
#include "components/optimization_guide/core/model_quality/model_quality_logs_uploader_service.h"
#include "components/optimization_guide/core/model_quality/model_quality_util.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_features.h"
#include "components/optimization_guide/core/optimization_guide_logger.h"
#include "components/optimization_guide/core/optimization_guide_navigation_data.h"
#include "components/optimization_guide/core/optimization_guide_prefs.h"
#include "components/optimization_guide/core/optimization_guide_store.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_manager.h"
#include "components/optimization_guide/core/tab_url_provider.h"
#include "components/optimization_guide/core/top_host_provider.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/optimization_guide/proto/models.pb.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "components/variations/service/variations_service.h"
#include "components/variations/synthetic_trials.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/storage_partition.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/commerce/price_tracking/android/price_tracking_notification_bridge.h"
#include "chrome/browser/optimization_guide/android/optimization_guide_bridge.h"
#include "chrome/browser/optimization_guide/android/optimization_guide_tab_url_provider_android.h"
#else
#include "chrome/browser/optimization_guide/optimization_guide_tab_url_provider.h"
#endif
namespace {
ModelExecutionFeaturesController;
OnDeviceModelComponentStateManager;
OnDeviceModelPerformanceClass;
void DeleteOldStorePaths(const base::FilePath& profile_path) { … }
Profile* GetProfileForOTROptimizationGuide(Profile* profile) { … }
OnDeviceModelPerformanceClass ConvertToOnDeviceModelPerformanceClass(
std::optional<on_device_model::mojom::PerformanceClass> performance_class) { … }
std::string OnDeviceModelPerformanceClassToString(
OnDeviceModelPerformanceClass performance_class) { … }
scoped_refptr<optimization_guide::OnDeviceModelServiceController>
GetOnDeviceModelServiceController(
base::WeakPtr<optimization_guide::OnDeviceModelComponentStateManager>
on_device_component_manager) { … }
class OnDeviceModelComponentStateManagerDelegate
: public OnDeviceModelComponentStateManager::Delegate { … };
}
std::unique_ptr<optimization_guide::PushNotificationManager>
OptimizationGuideKeyedService::MaybeCreatePushNotificationManager(
Profile* profile) { … }
void OptimizationGuideKeyedService::DeterminePerformanceClass(
base::WeakPtr<optimization_guide::OnDeviceModelComponentStateManager>
on_device_component_state_manager) { … }
OptimizationGuideKeyedService::OptimizationGuideKeyedService(
content::BrowserContext* browser_context)
: … { … }
OptimizationGuideKeyedService::~OptimizationGuideKeyedService() { … }
#if BUILDFLAG(IS_ANDROID)
base::android::ScopedJavaLocalRef<jobject>
OptimizationGuideKeyedService::GetJavaObject() {
if (!android_bridge_) {
android_bridge_ =
std::make_unique<optimization_guide::android::OptimizationGuideBridge>(
this);
}
return android_bridge_->GetJavaObject();
}
#endif
download::BackgroundDownloadService*
OptimizationGuideKeyedService::BackgroundDownloadServiceProvider() { … }
bool OptimizationGuideKeyedService::ComponentUpdatesEnabledProvider() const { … }
void OptimizationGuideKeyedService::Initialize() { … }
optimization_guide::ChromeHintsManager*
OptimizationGuideKeyedService::GetHintsManager() { … }
void OptimizationGuideKeyedService::OnNavigationStartOrRedirect(
OptimizationGuideNavigationData* navigation_data) { … }
void OptimizationGuideKeyedService::OnNavigationFinish(
const std::vector<GURL>& navigation_redirect_chain) { … }
void OptimizationGuideKeyedService::AddObserverForOptimizationTargetModel(
optimization_guide::proto::OptimizationTarget optimization_target,
const std::optional<optimization_guide::proto::Any>& model_metadata,
optimization_guide::OptimizationTargetModelObserver* observer) { … }
void OptimizationGuideKeyedService::RemoveObserverForOptimizationTargetModel(
optimization_guide::proto::OptimizationTarget optimization_target,
optimization_guide::OptimizationTargetModelObserver* observer) { … }
void OptimizationGuideKeyedService::RegisterOptimizationTypes(
const std::vector<optimization_guide::proto::OptimizationType>&
optimization_types) { … }
optimization_guide::OptimizationGuideDecision
OptimizationGuideKeyedService::CanApplyOptimization(
const GURL& url,
optimization_guide::proto::OptimizationType optimization_type,
optimization_guide::OptimizationMetadata* optimization_metadata) { … }
void OptimizationGuideKeyedService::CanApplyOptimization(
const GURL& url,
optimization_guide::proto::OptimizationType optimization_type,
optimization_guide::OptimizationGuideDecisionCallback callback) { … }
void OptimizationGuideKeyedService::CanApplyOptimizationOnDemand(
const std::vector<GURL>& urls,
const base::flat_set<optimization_guide::proto::OptimizationType>&
optimization_types,
optimization_guide::proto::RequestContext request_context,
optimization_guide::OnDemandOptimizationGuideDecisionRepeatingCallback
callback,
std::optional<optimization_guide::proto::RequestContextMetadata>
request_context_metadata) { … }
bool OptimizationGuideKeyedService::CanCreateOnDeviceSession(
optimization_guide::ModelBasedCapabilityKey feature,
optimization_guide::OnDeviceModelEligibilityReason*
on_device_model_eligibility_reason) { … }
std::unique_ptr<optimization_guide::OptimizationGuideModelExecutor::Session>
OptimizationGuideKeyedService::StartSession(
optimization_guide::ModelBasedCapabilityKey feature,
const std::optional<optimization_guide::SessionConfigParams>&
config_params) { … }
void OptimizationGuideKeyedService::ExecuteModel(
optimization_guide::ModelBasedCapabilityKey feature,
const google::protobuf::MessageLite& request_metadata,
optimization_guide::OptimizationGuideModelExecutionResultCallback
callback) { … }
void OptimizationGuideKeyedService::AddOnDeviceModelAvailabilityChangeObserver(
optimization_guide::ModelBasedCapabilityKey feature,
optimization_guide::OnDeviceModelAvailabilityObserver* observer) { … }
void OptimizationGuideKeyedService::
RemoveOnDeviceModelAvailabilityChangeObserver(
optimization_guide::ModelBasedCapabilityKey feature,
optimization_guide::OnDeviceModelAvailabilityObserver* observer) { … }
void OptimizationGuideKeyedService::OnProfileInitializationComplete(
Profile* profile) { … }
void OptimizationGuideKeyedService::AddHintForTesting(
const GURL& url,
optimization_guide::proto::OptimizationType optimization_type,
const std::optional<optimization_guide::OptimizationMetadata>& metadata) { … }
void OptimizationGuideKeyedService::ClearData() { … }
void OptimizationGuideKeyedService::Shutdown() { … }
void OptimizationGuideKeyedService::OverrideTargetModelForTesting(
optimization_guide::proto::OptimizationTarget optimization_target,
std::unique_ptr<optimization_guide::ModelInfo> model_info) { … }
void OptimizationGuideKeyedService::
SetModelQualityLogsUploaderServiceForTesting(
std::unique_ptr<optimization_guide::ModelQualityLogsUploaderService>
uploader) { … }
bool OptimizationGuideKeyedService::ShouldFeatureBeCurrentlyEnabledForUser(
optimization_guide::UserVisibleFeatureKey feature) const { … }
bool OptimizationGuideKeyedService::ShouldFeatureBeCurrentlyAllowedForFeedback(
optimization_guide::proto::LogAiDataRequest::FeatureCase feature) const { … }
bool OptimizationGuideKeyedService::IsSettingVisible(
optimization_guide::UserVisibleFeatureKey feature) const { … }
bool OptimizationGuideKeyedService::ShouldShowExperimentalAIPromo() const { … }
void OptimizationGuideKeyedService::AddModelExecutionSettingsEnabledObserver(
optimization_guide::SettingsEnabledObserver* observer) { … }
void OptimizationGuideKeyedService::RemoveModelExecutionSettingsEnabledObserver(
optimization_guide::SettingsEnabledObserver* observer) { … }
void OptimizationGuideKeyedService::
RecordModelExecutionFeatureSyntheticFieldTrial(
optimization_guide::UserVisibleFeatureKey feature,
const std::string_view feature_name) { … }