accuracy_service.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_ACCURACY_TIPS_ACCURACY_SERVICE_H_
  5. #define COMPONENTS_ACCURACY_TIPS_ACCURACY_SERVICE_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "base/scoped_observation.h"
  13. #include "base/time/clock.h"
  14. #include "base/time/default_clock.h"
  15. #include "base/time/time.h"
  16. #include "components/accuracy_tips/accuracy_tip_interaction.h"
  17. #include "components/accuracy_tips/accuracy_tip_status.h"
  18. #include "components/history/core/browser/history_service.h"
  19. #include "components/history/core/browser/history_service_observer.h"
  20. #include "components/keyed_service/core/keyed_service.h"
  21. #include "services/metrics/public/cpp/ukm_source_id.h"
  22. #include "url/gurl.h"
  23. namespace base {
  24. class TimeTicks;
  25. }
  26. namespace content {
  27. class WebContents;
  28. }
  29. namespace safe_browsing {
  30. class SafeBrowsingDatabaseManager;
  31. }
  32. namespace user_prefs {
  33. class PrefRegistrySyncable;
  34. }
  35. class PrefService;
  36. namespace accuracy_tips {
  37. class AccuracyTipSafeBrowsingClient;
  38. // Checks if URL is news-related for AccuracyTips.
  39. // Handles rate-limiting and feature checks.
  40. class AccuracyService : public KeyedService, history::HistoryServiceObserver {
  41. public:
  42. static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
  43. // The site_engagement_service is currently not available on iOS
  44. // (crbug.com/775390), so it is supplied through a delegate.
  45. class Delegate {
  46. public:
  47. // Returns true if the engagement of this site is too high to show an
  48. // accuracy tip.
  49. virtual bool IsEngagementHigh(const GURL& url) = 0;
  50. // Shows AccuracyTip UI using the specified information if it is not already
  51. // showing. |close_callback| will be called when the dialog is closed.
  52. // The argument indicates the action that the user took to close the dialog.
  53. virtual void ShowAccuracyTip(
  54. content::WebContents* web_contents,
  55. AccuracyTipStatus type,
  56. bool show_opt_out,
  57. base::OnceCallback<void(AccuracyTipInteraction)> close_callback) = 0;
  58. // Launches accuracy tips survey with the product specific data.
  59. virtual void ShowSurvey(
  60. const std::map<std::string, bool>& product_specific_bits_data,
  61. const std::map<std::string, std::string>&
  62. product_specific_string_data) = 0;
  63. // Returns whether the security level of |web_contents| is secure.
  64. virtual bool IsSecureConnection(content::WebContents* web_contents) = 0;
  65. virtual ~Delegate() = default;
  66. };
  67. class Observer {
  68. public:
  69. // Called when an accuracy tip was shown.
  70. virtual void OnAccuracyTipShown() = 0;
  71. // Called when an accuracy tip was closed.
  72. virtual void OnAccuracyTipClosed() = 0;
  73. virtual ~Observer() = default;
  74. };
  75. AccuracyService(
  76. std::unique_ptr<Delegate> delegate,
  77. PrefService* pref_service,
  78. scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> sb_database,
  79. history::HistoryService* history_service,
  80. scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
  81. scoped_refptr<base::SequencedTaskRunner> io_task_runner);
  82. ~AccuracyService() override;
  83. AccuracyService(const AccuracyService&) = delete;
  84. AccuracyService& operator=(const AccuracyService&) = delete;
  85. // Callback for accuracy check result.
  86. using AccuracyCheckCallback = base::OnceCallback<void(AccuracyTipStatus)>;
  87. // Returns the accuracy status for |url|. Virtual for testing purposes.
  88. virtual void CheckAccuracyStatus(const GURL& url,
  89. AccuracyCheckCallback callback);
  90. // Shows an accuracy tip UI for web_contents after checking rate limits.
  91. // Virtual for testing purposes.
  92. virtual void MaybeShowAccuracyTip(content::WebContents* web_contents);
  93. // Shows a HaTS survey after checking features states and pre-conditions
  94. // configured by feature params in `CanShowSurvey`.
  95. void MaybeShowSurvey();
  96. // TODO(olesiamarukhno): Figure out how to remove content dependencies if we
  97. // want to use this service on iOS.
  98. // Returns is the security level of |web_contents| is secure.
  99. virtual bool IsSecureConnection(content::WebContents* web_contents);
  100. bool IsShowingAccuracyTip(content::WebContents* web_contents);
  101. // KeyedService:
  102. void Shutdown() override;
  103. // history::HistoryServiceObserver:
  104. void OnURLsDeleted(history::HistoryService* history_service,
  105. const history::DeletionInfo& deletion_info) override;
  106. // Adds/Removes an Observer.
  107. void AddObserver(Observer* observer);
  108. void RemoveObserver(Observer* observer);
  109. void SetClockForTesting(base::Clock* clock) { clock_ = clock; }
  110. private:
  111. void OnAccuracyStatusReceived(const GURL& url,
  112. AccuracyCheckCallback callback,
  113. AccuracyTipStatus status);
  114. void OnAccuracyTipClosed(base::TimeTicks time_opened,
  115. ukm::SourceId ukm_source_id,
  116. AccuracyTipInteraction interaction);
  117. // Returns if a HaTS survey for accuracy tips can be shown based on feature
  118. // state and feature params.
  119. bool CanShowSurvey();
  120. std::unique_ptr<Delegate> delegate_;
  121. raw_ptr<PrefService> pref_service_ = nullptr;
  122. scoped_refptr<AccuracyTipSafeBrowsingClient> sb_client_;
  123. scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
  124. GURL url_for_last_shown_tip_;
  125. // Feature params:
  126. const GURL sample_url_;
  127. const base::TimeDelta time_between_prompts_;
  128. const bool disable_ui_ = false;
  129. raw_ptr<base::Clock> clock_ = base::DefaultClock::GetInstance();
  130. raw_ptr<content::WebContents> web_contents_showing_accuracy_tip_ = nullptr;
  131. base::ObserverList<Observer>::Unchecked observers_;
  132. base::ScopedObservation<history::HistoryService,
  133. history::HistoryServiceObserver>
  134. history_service_observation_{this};
  135. base::WeakPtrFactory<AccuracyService> weak_factory_{this};
  136. };
  137. } // namespace accuracy_tips
  138. #endif // COMPONENTS_ACCURACY_TIPS_ACCURACY_SERVICE_H_