consent_auditor_impl.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "components/consent_auditor/consent_auditor_impl.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/values.h"
  8. #include "components/consent_auditor/pref_names.h"
  9. #include "components/prefs/pref_registry_simple.h"
  10. #include "components/prefs/pref_service.h"
  11. #include "components/prefs/scoped_user_pref_update.h"
  12. #include "components/sync/model/model_type_sync_bridge.h"
  13. #include "components/sync/protocol/user_consent_specifics.pb.h"
  14. #include "components/sync/protocol/user_consent_types.pb.h"
  15. using ArcPlayTermsOfServiceConsent =
  16. sync_pb::UserConsentTypes::ArcPlayTermsOfServiceConsent;
  17. using sync_pb::UserConsentTypes;
  18. using sync_pb::UserConsentSpecifics;
  19. namespace consent_auditor {
  20. namespace {
  21. constexpr char kLocalConsentDescriptionKey[] = "description";
  22. constexpr char kLocalConsentConfirmationKey[] = "confirmation";
  23. constexpr char kLocalConsentVersionKey[] = "version";
  24. constexpr char kLocalConsentLocaleKey[] = "locale";
  25. std::unique_ptr<sync_pb::UserConsentSpecifics> CreateUserConsentSpecifics(
  26. const CoreAccountId& account_id,
  27. const std::string& locale,
  28. base::Clock* clock) {
  29. std::unique_ptr<sync_pb::UserConsentSpecifics> specifics =
  30. std::make_unique<sync_pb::UserConsentSpecifics>();
  31. specifics->set_account_id(account_id.ToString());
  32. specifics->set_client_consent_time_usec(
  33. clock->Now().since_origin().InMicroseconds());
  34. specifics->set_locale(locale);
  35. return specifics;
  36. }
  37. } // namespace
  38. ConsentAuditorImpl::ConsentAuditorImpl(
  39. PrefService* pref_service,
  40. std::unique_ptr<ConsentSyncBridge> consent_sync_bridge,
  41. const std::string& app_version,
  42. const std::string& app_locale,
  43. base::Clock* clock)
  44. : pref_service_(pref_service),
  45. consent_sync_bridge_(std::move(consent_sync_bridge)),
  46. app_version_(app_version),
  47. app_locale_(app_locale),
  48. clock_(clock) {
  49. DCHECK(consent_sync_bridge_);
  50. DCHECK(pref_service_);
  51. }
  52. ConsentAuditorImpl::~ConsentAuditorImpl() {}
  53. void ConsentAuditorImpl::Shutdown() {}
  54. // static
  55. void ConsentAuditorImpl::RegisterProfilePrefs(PrefRegistrySimple* registry) {
  56. registry->RegisterDictionaryPref(prefs::kLocalConsentsDictionary);
  57. }
  58. void ConsentAuditorImpl::RecordArcPlayConsent(
  59. const CoreAccountId& account_id,
  60. const ArcPlayTermsOfServiceConsent& consent) {
  61. std::unique_ptr<sync_pb::UserConsentSpecifics> specifics =
  62. CreateUserConsentSpecifics(account_id, app_locale_, clock_);
  63. sync_pb::UserConsentTypes::ArcPlayTermsOfServiceConsent* arc_play_consent =
  64. specifics->mutable_arc_play_terms_of_service_consent();
  65. arc_play_consent->CopyFrom(consent);
  66. consent_sync_bridge_->RecordConsent(std::move(specifics));
  67. }
  68. void ConsentAuditorImpl::RecordArcGoogleLocationServiceConsent(
  69. const CoreAccountId& account_id,
  70. const UserConsentTypes::ArcGoogleLocationServiceConsent& consent) {
  71. std::unique_ptr<sync_pb::UserConsentSpecifics> specifics =
  72. CreateUserConsentSpecifics(account_id, app_locale_, clock_);
  73. sync_pb::UserConsentTypes::ArcGoogleLocationServiceConsent*
  74. arc_google_location_service_consent =
  75. specifics->mutable_arc_location_service_consent();
  76. arc_google_location_service_consent->CopyFrom(consent);
  77. consent_sync_bridge_->RecordConsent(std::move(specifics));
  78. }
  79. void ConsentAuditorImpl::RecordArcBackupAndRestoreConsent(
  80. const CoreAccountId& account_id,
  81. const UserConsentTypes::ArcBackupAndRestoreConsent& consent) {
  82. std::unique_ptr<sync_pb::UserConsentSpecifics> specifics =
  83. CreateUserConsentSpecifics(account_id, app_locale_, clock_);
  84. sync_pb::UserConsentTypes::ArcBackupAndRestoreConsent*
  85. arc_backup_and_restore_consent =
  86. specifics->mutable_arc_backup_and_restore_consent();
  87. arc_backup_and_restore_consent->CopyFrom(consent);
  88. consent_sync_bridge_->RecordConsent(std::move(specifics));
  89. }
  90. void ConsentAuditorImpl::RecordSyncConsent(
  91. const CoreAccountId& account_id,
  92. const UserConsentTypes::SyncConsent& consent) {
  93. std::unique_ptr<sync_pb::UserConsentSpecifics> specifics =
  94. CreateUserConsentSpecifics(account_id, app_locale_, clock_);
  95. sync_pb::UserConsentTypes::SyncConsent* sync_consent =
  96. specifics->mutable_sync_consent();
  97. sync_consent->CopyFrom(consent);
  98. consent_sync_bridge_->RecordConsent(std::move(specifics));
  99. }
  100. void ConsentAuditorImpl::RecordAssistantActivityControlConsent(
  101. const CoreAccountId& account_id,
  102. const sync_pb::UserConsentTypes::AssistantActivityControlConsent& consent) {
  103. std::unique_ptr<sync_pb::UserConsentSpecifics> specifics =
  104. CreateUserConsentSpecifics(account_id, app_locale_, clock_);
  105. sync_pb::UserConsentTypes::AssistantActivityControlConsent*
  106. assistant_consent =
  107. specifics->mutable_assistant_activity_control_consent();
  108. assistant_consent->CopyFrom(consent);
  109. consent_sync_bridge_->RecordConsent(std::move(specifics));
  110. }
  111. void ConsentAuditorImpl::RecordAccountPasswordsConsent(
  112. const CoreAccountId& account_id,
  113. const sync_pb::UserConsentTypes::AccountPasswordsConsent& consent) {
  114. std::unique_ptr<sync_pb::UserConsentSpecifics> specifics =
  115. CreateUserConsentSpecifics(account_id, app_locale_, clock_);
  116. specifics->mutable_account_passwords_consent()->CopyFrom(consent);
  117. consent_sync_bridge_->RecordConsent(std::move(specifics));
  118. }
  119. void ConsentAuditorImpl::RecordAutofillAssistantConsent(
  120. const CoreAccountId& account_id,
  121. const sync_pb::UserConsentTypes::AutofillAssistantConsent& consent) {
  122. std::unique_ptr<sync_pb::UserConsentSpecifics> specifics =
  123. CreateUserConsentSpecifics(account_id, app_locale_, clock_);
  124. *specifics->mutable_autofill_assistant_consent() = consent;
  125. consent_sync_bridge_->RecordConsent(std::move(specifics));
  126. }
  127. void ConsentAuditorImpl::RecordLocalConsent(
  128. const std::string& feature,
  129. const std::string& description_text,
  130. const std::string& confirmation_text) {
  131. DictionaryPrefUpdate consents_update(pref_service_,
  132. prefs::kLocalConsentsDictionary);
  133. base::Value* consents = consents_update.Get();
  134. DCHECK(consents);
  135. base::Value record(base::Value::Type::DICTIONARY);
  136. record.SetStringKey(kLocalConsentDescriptionKey, description_text);
  137. record.SetStringKey(kLocalConsentConfirmationKey, confirmation_text);
  138. record.SetStringKey(kLocalConsentVersionKey, app_version_);
  139. record.SetStringKey(kLocalConsentLocaleKey, app_locale_);
  140. consents->SetKey(feature, std::move(record));
  141. }
  142. base::WeakPtr<syncer::ModelTypeControllerDelegate>
  143. ConsentAuditorImpl::GetControllerDelegate() {
  144. if (consent_sync_bridge_) {
  145. return consent_sync_bridge_->GetControllerDelegate();
  146. }
  147. return base::WeakPtr<syncer::ModelTypeControllerDelegate>();
  148. }
  149. } // namespace consent_auditor