url_keyed_data_collection_consent_helper.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 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_UNIFIED_CONSENT_URL_KEYED_DATA_COLLECTION_CONSENT_HELPER_H_
  5. #define COMPONENTS_UNIFIED_CONSENT_URL_KEYED_DATA_COLLECTION_CONSENT_HELPER_H_
  6. #include <memory>
  7. #include "base/observer_list.h"
  8. class PrefService;
  9. namespace syncer {
  10. class SyncService;
  11. }
  12. namespace unified_consent {
  13. // Helper class that allows clients to check whether the user has consented
  14. // for URL-keyed data collection.
  15. class UrlKeyedDataCollectionConsentHelper {
  16. public:
  17. class Observer {
  18. public:
  19. // Called when the state of the URL-keyed data collection changes.
  20. virtual void OnUrlKeyedDataCollectionConsentStateChanged(
  21. UrlKeyedDataCollectionConsentHelper* consent_helper) = 0;
  22. };
  23. // Creates a new |UrlKeyedDataCollectionConsentHelper| instance that checks
  24. // whether *anonymized* data collection is enabled. This should be used when
  25. // the client needs to check whether the user has granted consent for
  26. // *anonymized* URL-keyed data collection. It is enabled if the preference
  27. // |prefs::kUrlKeyedAnonymizedDataCollectionEnabled| from |pref_service| is
  28. // set to true.
  29. //
  30. // Note: |pref_service| must outlive the returned instance.
  31. static std::unique_ptr<UrlKeyedDataCollectionConsentHelper>
  32. NewAnonymizedDataCollectionConsentHelper(PrefService* pref_service);
  33. // Creates a new |UrlKeyedDataCollectionConsentHelper| instance that checks
  34. // whether *personalized* data collection is enabled. This should be used when
  35. // the client needs to check whether the user has granted consent for
  36. // URL-keyed data collection keyed by their Google account.
  37. //
  38. // Implementation-wise URL-keyed data collection is enabled if history sync
  39. // has an active upload state.
  40. static std::unique_ptr<UrlKeyedDataCollectionConsentHelper>
  41. NewPersonalizedDataCollectionConsentHelper(syncer::SyncService* sync_service);
  42. UrlKeyedDataCollectionConsentHelper(
  43. const UrlKeyedDataCollectionConsentHelper&) = delete;
  44. UrlKeyedDataCollectionConsentHelper& operator=(
  45. const UrlKeyedDataCollectionConsentHelper&) = delete;
  46. virtual ~UrlKeyedDataCollectionConsentHelper();
  47. // Returns true if the user has consented for URL keyed anonymized data
  48. // collection.
  49. virtual bool IsEnabled() = 0;
  50. // Methods to register or remove observers.
  51. void AddObserver(Observer* observer);
  52. void RemoveObserver(Observer* observer);
  53. protected:
  54. UrlKeyedDataCollectionConsentHelper();
  55. // Fires |OnUrlKeyedDataCollectionConsentStateChanged| on all the observers.
  56. void FireOnStateChanged();
  57. private:
  58. base::ObserverList<Observer, true>::Unchecked observer_list_;
  59. };
  60. } // namespace unified_consent
  61. #endif // COMPONENTS_UNIFIED_CONSENT_URL_KEYED_DATA_COLLECTION_CONSENT_HELPER_H_