tracked_persistent_pref_store_factory.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2017 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 "services/preferences/tracked/tracked_persistent_pref_store_factory.h"
  5. #include <memory>
  6. #include <set>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/bind.h"
  11. #include "build/build_config.h"
  12. #include "components/prefs/json_pref_store.h"
  13. #include "components/prefs/pref_filter.h"
  14. #include "components/prefs/segregated_pref_store.h"
  15. #include "mojo/public/cpp/bindings/remote.h"
  16. #include "services/preferences/public/mojom/tracked_preference_validation_delegate.mojom.h"
  17. #include "services/preferences/tracked/pref_hash_filter.h"
  18. #include "services/preferences/tracked/pref_hash_store_impl.h"
  19. #include "services/preferences/tracked/temp_scoped_dir_cleaner.h"
  20. #include "services/preferences/tracked/tracked_preferences_migration.h"
  21. #if BUILDFLAG(IS_WIN)
  22. #include "base/files/scoped_temp_dir.h"
  23. #include "base/strings/string_util.h"
  24. #include "services/preferences/tracked/registry_hash_store_contents_win.h"
  25. #endif
  26. namespace {
  27. void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store,
  28. const std::string& key) {
  29. if (pref_store) {
  30. pref_store->RemoveValueSilently(
  31. key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  32. }
  33. }
  34. std::unique_ptr<PrefHashStore> CreatePrefHashStore(
  35. const prefs::mojom::TrackedPersistentPrefStoreConfiguration& config,
  36. bool use_super_mac) {
  37. return std::make_unique<PrefHashStoreImpl>(
  38. config.seed, config.legacy_device_id, use_super_mac);
  39. }
  40. std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>>
  41. GetExternalVerificationPrefHashStorePair(
  42. const prefs::mojom::TrackedPersistentPrefStoreConfiguration& config,
  43. scoped_refptr<TempScopedDirCleaner> temp_dir_cleaner) {
  44. #if BUILDFLAG(IS_WIN)
  45. return std::make_pair(
  46. std::make_unique<PrefHashStoreImpl>(config.registry_seed,
  47. config.legacy_device_id,
  48. false /* use_super_mac */),
  49. std::make_unique<RegistryHashStoreContentsWin>(
  50. base::AsWString(config.registry_path),
  51. config.unprotected_pref_filename.DirName().BaseName().value(),
  52. std::move(temp_dir_cleaner)));
  53. #else
  54. return std::make_pair(nullptr, nullptr);
  55. #endif
  56. }
  57. } // namespace
  58. PersistentPrefStore* CreateTrackedPersistentPrefStore(
  59. prefs::mojom::TrackedPersistentPrefStoreConfigurationPtr config,
  60. scoped_refptr<base::SequencedTaskRunner> io_task_runner) {
  61. std::vector<prefs::mojom::TrackedPreferenceMetadataPtr>
  62. unprotected_configuration;
  63. std::vector<prefs::mojom::TrackedPreferenceMetadataPtr>
  64. protected_configuration;
  65. std::set<std::string> protected_pref_names;
  66. std::set<std::string> unprotected_pref_names;
  67. for (auto& metadata : config->tracking_configuration) {
  68. if (metadata->enforcement_level > prefs::mojom::TrackedPreferenceMetadata::
  69. EnforcementLevel::NO_ENFORCEMENT) {
  70. protected_pref_names.insert(metadata->name);
  71. protected_configuration.push_back(std::move(metadata));
  72. } else {
  73. unprotected_pref_names.insert(metadata->name);
  74. unprotected_configuration.push_back(std::move(metadata));
  75. }
  76. }
  77. config->tracking_configuration.clear();
  78. scoped_refptr<TempScopedDirCleaner> temp_scoped_dir_cleaner;
  79. #if BUILDFLAG(IS_WIN)
  80. // For tests that create a profile in a ScopedTempDir, share a ref_counted
  81. // object between the unprotected and protected hash filter's
  82. // RegistryHashStoreContentsWin which will clear the registry keys when
  83. // destroyed. (https://crbug.com/721245)
  84. if (base::StartsWith(
  85. config->unprotected_pref_filename.DirName().BaseName().value(),
  86. base::ScopedTempDir::GetTempDirPrefix(),
  87. base::CompareCase::INSENSITIVE_ASCII)) {
  88. temp_scoped_dir_cleaner =
  89. base::MakeRefCounted<TempScopedDirRegistryCleaner>();
  90. }
  91. #endif
  92. mojo::Remote<prefs::mojom::TrackedPreferenceValidationDelegate>
  93. validation_delegate;
  94. validation_delegate.Bind(std::move(config->validation_delegate));
  95. auto validation_delegate_ref = base::MakeRefCounted<base::RefCountedData<
  96. mojo::Remote<prefs::mojom::TrackedPreferenceValidationDelegate>>>(
  97. std::move(validation_delegate));
  98. std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter(
  99. new PrefHashFilter(CreatePrefHashStore(*config, false),
  100. GetExternalVerificationPrefHashStorePair(
  101. *config, temp_scoped_dir_cleaner),
  102. unprotected_configuration, mojo::NullRemote(),
  103. validation_delegate_ref, config->reporting_ids_count));
  104. std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter(
  105. CreatePrefHashStore(*config, true),
  106. GetExternalVerificationPrefHashStorePair(*config,
  107. temp_scoped_dir_cleaner),
  108. protected_configuration, std::move(config->reset_on_load_observer),
  109. validation_delegate_ref, config->reporting_ids_count));
  110. PrefHashFilter* raw_unprotected_pref_hash_filter =
  111. unprotected_pref_hash_filter.get();
  112. PrefHashFilter* raw_protected_pref_hash_filter =
  113. protected_pref_hash_filter.get();
  114. scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore(
  115. config->unprotected_pref_filename,
  116. std::move(unprotected_pref_hash_filter), io_task_runner.get()));
  117. scoped_refptr<JsonPrefStore> protected_pref_store(new JsonPrefStore(
  118. config->protected_pref_filename, std::move(protected_pref_hash_filter),
  119. io_task_runner.get()));
  120. SetupTrackedPreferencesMigration(
  121. unprotected_pref_names, protected_pref_names,
  122. base::BindRepeating(&RemoveValueSilently,
  123. unprotected_pref_store->AsWeakPtr()),
  124. base::BindRepeating(&RemoveValueSilently,
  125. protected_pref_store->AsWeakPtr()),
  126. base::BindRepeating(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply,
  127. unprotected_pref_store->AsWeakPtr()),
  128. base::BindRepeating(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply,
  129. protected_pref_store->AsWeakPtr()),
  130. CreatePrefHashStore(*config, false), CreatePrefHashStore(*config, true),
  131. raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter);
  132. return new SegregatedPrefStore(std::move(unprotected_pref_store),
  133. std::move(protected_pref_store),
  134. std::move(protected_pref_names));
  135. }
  136. void InitializeMasterPrefsTracking(
  137. prefs::mojom::TrackedPersistentPrefStoreConfigurationPtr configuration,
  138. base::DictionaryValue* master_prefs) {
  139. PrefHashFilter(
  140. CreatePrefHashStore(*configuration, false),
  141. GetExternalVerificationPrefHashStorePair(*configuration, nullptr),
  142. configuration->tracking_configuration, mojo::NullRemote(), nullptr,
  143. configuration->reporting_ids_count)
  144. .Initialize(master_prefs);
  145. }