segmentation_platform_service_impl.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_SEGMENTATION_PLATFORM_SERVICE_IMPL_H_
  5. #define COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_SEGMENTATION_PLATFORM_SERVICE_IMPL_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/containers/circular_deque.h"
  9. #include "base/containers/flat_map.h"
  10. #include "base/containers/flat_set.h"
  11. #include "base/gtest_prod_util.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/scoped_refptr.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "components/segmentation_platform/internal/database/storage_service.h"
  16. #include "components/segmentation_platform/internal/execution/model_execution_manager.h"
  17. #include "components/segmentation_platform/internal/platform_options.h"
  18. #include "components/segmentation_platform/internal/scheduler/execution_service.h"
  19. #include "components/segmentation_platform/internal/service_proxy_impl.h"
  20. #include "components/segmentation_platform/internal/signals/signal_handler.h"
  21. #include "components/segmentation_platform/public/proto/segmentation_platform.pb.h"
  22. #include "components/segmentation_platform/public/segmentation_platform_service.h"
  23. namespace base {
  24. class Clock;
  25. class FilePath;
  26. class SequencedTaskRunner;
  27. } // namespace base
  28. namespace history {
  29. class HistoryService;
  30. }
  31. namespace leveldb_proto {
  32. class ProtoDatabaseProvider;
  33. } // namespace leveldb_proto
  34. class PrefService;
  35. namespace segmentation_platform {
  36. namespace processing {
  37. class InputDelegateHolder;
  38. }
  39. struct Config;
  40. class FieldTrialRegister;
  41. class ModelProviderFactory;
  42. class SegmentSelectorImpl;
  43. class SegmentScoreProvider;
  44. class UkmDataManager;
  45. // The internal implementation of the SegmentationPlatformService.
  46. class SegmentationPlatformServiceImpl : public SegmentationPlatformService {
  47. public:
  48. struct InitParams {
  49. InitParams();
  50. ~InitParams();
  51. bool IsValid();
  52. // Profile data:
  53. raw_ptr<leveldb_proto::ProtoDatabaseProvider> db_provider = nullptr;
  54. raw_ptr<history::HistoryService> history_service = nullptr;
  55. base::FilePath storage_dir;
  56. raw_ptr<PrefService> profile_prefs = nullptr;
  57. // Platform configuration:
  58. std::unique_ptr<ModelProviderFactory> model_provider;
  59. raw_ptr<UkmDataManager> ukm_data_manager = nullptr;
  60. std::vector<std::unique_ptr<Config>> configs;
  61. std::unique_ptr<FieldTrialRegister> field_trial_register;
  62. std::unique_ptr<processing::InputDelegateHolder> input_delegate_holder;
  63. scoped_refptr<base::SequencedTaskRunner> task_runner;
  64. raw_ptr<base::Clock> clock = nullptr;
  65. // Test only:
  66. std::unique_ptr<StorageService> storage_service;
  67. };
  68. explicit SegmentationPlatformServiceImpl(
  69. std::unique_ptr<InitParams> init_params);
  70. SegmentationPlatformServiceImpl();
  71. ~SegmentationPlatformServiceImpl() override;
  72. // Disallow copy/assign.
  73. SegmentationPlatformServiceImpl(const SegmentationPlatformServiceImpl&) =
  74. delete;
  75. SegmentationPlatformServiceImpl& operator=(
  76. const SegmentationPlatformServiceImpl&) = delete;
  77. // SegmentationPlatformService overrides.
  78. void GetSelectedSegment(const std::string& segmentation_key,
  79. SegmentSelectionCallback callback) override;
  80. SegmentSelectionResult GetCachedSegmentResult(
  81. const std::string& segmentation_key) override;
  82. void GetSelectedSegmentOnDemand(const std::string& segmentation_key,
  83. scoped_refptr<InputContext> input_context,
  84. SegmentSelectionCallback callback) override;
  85. void EnableMetrics(bool signal_collection_allowed) override;
  86. ServiceProxy* GetServiceProxy() override;
  87. bool IsPlatformInitialized() override;
  88. private:
  89. friend class SegmentationPlatformServiceImplTest;
  90. friend class TestServicesForPlatform;
  91. void OnDatabaseInitialized(bool success);
  92. // Must only be invoked with a valid SegmentInfo.
  93. void OnSegmentationModelUpdated(proto::SegmentInfo segment_info);
  94. // Callback sent to child classes to notify when model results need to be
  95. // refreshed. For example, when history is cleared.
  96. void OnModelRefreshNeeded();
  97. // Called when service status changes.
  98. void OnServiceStatusChanged();
  99. // Task that runs every day or at startup to keep the platform data updated.
  100. void RunDailyTasks(bool is_startup);
  101. std::unique_ptr<ModelProviderFactory> model_provider_factory_;
  102. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  103. raw_ptr<base::Clock> clock_;
  104. const PlatformOptions platform_options_;
  105. // Temporarily stored till initialization and moved to `execution_service_`.
  106. std::unique_ptr<processing::InputDelegateHolder> input_delegate_holder_;
  107. // Config.
  108. std::vector<std::unique_ptr<Config>> configs_;
  109. base::flat_set<proto::SegmentId> all_segment_ids_;
  110. std::unique_ptr<FieldTrialRegister> field_trial_register_;
  111. std::unique_ptr<StorageService> storage_service_;
  112. bool storage_initialized_ = false;
  113. // Signal processing.
  114. SignalHandler signal_handler_;
  115. // Segment selection.
  116. // TODO(shaktisahu): Determine safe destruction ordering between
  117. // SegmentSelectorImpl and ModelExecutionSchedulerImpl.
  118. base::flat_map<std::string, std::unique_ptr<SegmentSelectorImpl>>
  119. segment_selectors_;
  120. // Segment results.
  121. std::unique_ptr<SegmentScoreProvider> segment_score_provider_;
  122. ExecutionService execution_service_;
  123. std::unique_ptr<ServiceProxyImpl> proxy_;
  124. // PrefService from profile.
  125. raw_ptr<PrefService> profile_prefs_;
  126. // For metrics only:
  127. const base::Time creation_time_;
  128. base::Time init_time_;
  129. // For caching any method calls that were received before initialization.
  130. base::circular_deque<base::OnceClosure> pending_actions_;
  131. base::WeakPtrFactory<SegmentationPlatformServiceImpl> weak_ptr_factory_{this};
  132. };
  133. } // namespace segmentation_platform
  134. #endif // COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_SEGMENTATION_PLATFORM_SERVICE_IMPL_H_