browsing_topics_service_impl.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2022 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_BROWSING_TOPICS_BROWSING_TOPICS_SERVICE_IMPL_H_
  5. #define COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_SERVICE_IMPL_H_
  6. #include "base/memory/weak_ptr.h"
  7. #include "base/scoped_observation.h"
  8. #include "base/timer/timer.h"
  9. #include "components/browsing_topics/browsing_topics_calculator.h"
  10. #include "components/browsing_topics/browsing_topics_service.h"
  11. #include "components/browsing_topics/browsing_topics_state.h"
  12. #include "components/history/core/browser/history_service.h"
  13. #include "components/history/core/browser/history_service_observer.h"
  14. #include "components/privacy_sandbox/privacy_sandbox_settings.h"
  15. namespace content {
  16. class BrowsingTopicsSiteDataManager;
  17. } // namespace content
  18. namespace optimization_guide {
  19. class PageContentAnnotationsService;
  20. } // namespace optimization_guide
  21. namespace browsing_topics {
  22. // A profile keyed service for scheduling browsing topics calculation,
  23. // calculating the topics to give to a requesting context or to other internal
  24. // components (e.g. UX), and handling relevant data deletion. Browsing topics
  25. // calculation will happen periodically every time period of
  26. // `kBrowsingTopicsTimePeriodPerEpoch`. See the `BrowsingTopicsCalculator` class
  27. // for the calculation details.
  28. class BrowsingTopicsServiceImpl
  29. : public BrowsingTopicsService,
  30. public privacy_sandbox::PrivacySandboxSettings::Observer,
  31. public history::HistoryServiceObserver {
  32. public:
  33. BrowsingTopicsServiceImpl(const BrowsingTopicsServiceImpl&) = delete;
  34. BrowsingTopicsServiceImpl& operator=(const BrowsingTopicsServiceImpl&) =
  35. delete;
  36. BrowsingTopicsServiceImpl(BrowsingTopicsServiceImpl&&) = delete;
  37. BrowsingTopicsServiceImpl& operator=(BrowsingTopicsServiceImpl&&) = delete;
  38. ~BrowsingTopicsServiceImpl() override;
  39. std::vector<blink::mojom::EpochTopicPtr> GetBrowsingTopicsForJsApi(
  40. const url::Origin& context_origin,
  41. content::RenderFrameHost* main_frame) override;
  42. void GetBrowsingTopicsStateForWebUi(
  43. bool calculate_now,
  44. mojom::PageHandler::GetBrowsingTopicsStateCallback callback) override;
  45. std::vector<privacy_sandbox::CanonicalTopic> GetTopicsForSiteForDisplay(
  46. const url::Origin& top_origin) const override;
  47. std::vector<privacy_sandbox::CanonicalTopic> GetTopTopicsForDisplay()
  48. const override;
  49. void ClearTopic(
  50. const privacy_sandbox::CanonicalTopic& canonical_topic) override;
  51. void ClearTopicsDataForOrigin(const url::Origin& origin) override;
  52. void ClearAllTopicsData() override;
  53. protected:
  54. // The following methods are marked protected so that they may be overridden
  55. // by tests.
  56. virtual std::unique_ptr<BrowsingTopicsCalculator> CreateCalculator(
  57. privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
  58. history::HistoryService* history_service,
  59. content::BrowsingTopicsSiteDataManager* site_data_manager,
  60. optimization_guide::PageContentAnnotationsService* annotations_service,
  61. const base::circular_deque<EpochTopics>& epochs,
  62. BrowsingTopicsCalculator::CalculateCompletedCallback callback);
  63. // Allow tests to access `browsing_topics_state_`.
  64. virtual const BrowsingTopicsState& browsing_topics_state();
  65. // privacy_sandbox::PrivacySandboxSettings::Observer:
  66. //
  67. // When the floc-accessible-since time is updated (due to e.g. cookies
  68. // deletion), we'll invalidate the underlying browsing topics.
  69. void OnTopicsDataAccessibleSinceUpdated() override;
  70. // history::HistoryServiceObserver:
  71. //
  72. // On history deletion, the top topics of history epochs will be invalidated
  73. // if the deletion time range overlaps with the time range of the underlying
  74. // data used to derive the topics.
  75. void OnURLsDeleted(history::HistoryService* history_service,
  76. const history::DeletionInfo& deletion_info) override;
  77. // Called when the outstanding calculation completes. It's going to reset
  78. // `topics_calculator_`, add the new `epoch_topics` to `browsing_topics_`, and
  79. // schedule the next calculation.
  80. virtual void OnCalculateBrowsingTopicsCompleted(EpochTopics epoch_topics);
  81. private:
  82. friend class BrowsingTopicsServiceFactory;
  83. friend class BrowsingTopicsBrowserTest;
  84. friend class TesterBrowsingTopicsService;
  85. BrowsingTopicsServiceImpl(
  86. const base::FilePath& profile_path,
  87. privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
  88. history::HistoryService* history_service,
  89. content::BrowsingTopicsSiteDataManager* site_data_manager,
  90. optimization_guide::PageContentAnnotationsService* annotations_service);
  91. void ScheduleBrowsingTopicsCalculation(base::TimeDelta delay);
  92. // Initialize `topics_calculator_` to start calculating this epoch's top
  93. // topics and context observed topics.
  94. void CalculateBrowsingTopics();
  95. // Set `browsing_topics_state_loaded_` to true. Start scheduling the topics
  96. // calculation.
  97. void OnBrowsingTopicsStateLoaded();
  98. // KeyedService:
  99. void Shutdown() override;
  100. mojom::WebUIGetBrowsingTopicsStateResultPtr
  101. GetBrowsingTopicsStateForWebUiHelper();
  102. // These pointers are safe to hold and use throughout the lifetime of
  103. // `this`:
  104. // - For `privacy_sandbox_settings_`, `history_service_` and
  105. // `annotations_service_`: the dependency declared in
  106. // `BrowsingTopicsServiceFactory`'s constructor guarantees that
  107. // `BrowsingTopicsService` will be destroyed first before those depend-on
  108. // services.
  109. // - For `site_data_manager_`: it lives in the StoragePartition which lives
  110. // in the BrowserContext, and thus outlives all BrowserContext's KeyedService.
  111. raw_ptr<privacy_sandbox::PrivacySandboxSettings> privacy_sandbox_settings_;
  112. raw_ptr<history::HistoryService> history_service_;
  113. raw_ptr<content::BrowsingTopicsSiteDataManager> site_data_manager_;
  114. raw_ptr<optimization_guide::PageContentAnnotationsService>
  115. annotations_service_;
  116. BrowsingTopicsState browsing_topics_state_;
  117. // Whether the `browsing_topics_state_` has finished loading. Before the
  118. // loading finishes, accessor methods will use a default handling (i.e. return
  119. // an empty value; skip usage tracking; ignore data deletions). This is fine
  120. // in practice, as the loading should be reasonably fast, and normally the API
  121. // usage or data deletion won't happen at the browser start.
  122. bool browsing_topics_state_loaded_ = false;
  123. // This is non-null if a calculation is in progress. A calculation can be
  124. // triggered periodically, or due to the "Calculate Now" request from the
  125. // WebUI.
  126. std::unique_ptr<BrowsingTopicsCalculator> topics_calculator_;
  127. // This is populated when a request for the topics state arrives during an
  128. // ongoing topics calculation, or for a request that requires "Calculate Now"
  129. // in the first place. Callbacks will be invoked to return the latest topics
  130. // state as soon as the ongoing calculation finishes, and
  131. // `get_state_for_webui_callbacks_` will be cleared afterwards.
  132. std::vector<mojom::PageHandler::GetBrowsingTopicsStateCallback>
  133. get_state_for_webui_callbacks_;
  134. base::OneShotTimer schedule_calculate_timer_;
  135. base::ScopedObservation<privacy_sandbox::PrivacySandboxSettings,
  136. privacy_sandbox::PrivacySandboxSettings::Observer>
  137. privacy_sandbox_settings_observation_{this};
  138. base::ScopedObservation<history::HistoryService,
  139. history::HistoryServiceObserver>
  140. history_service_observation_{this};
  141. base::WeakPtrFactory<BrowsingTopicsServiceImpl> weak_ptr_factory_{this};
  142. };
  143. } // namespace browsing_topics
  144. #endif // COMPONENTS_BROWSING_TOPICS_BROWSING_TOPICS_SERVICE_IMPL_H_