Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(329)

Side by Side Diff: chrome/browser/prefs/profile_pref_store_manager.cc

Issue 2745563005: Pref service: add support for tracked prefs. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/prefs/profile_pref_store_manager.h" 5 #include "chrome/browser/prefs/profile_pref_store_manager.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/json/json_file_value_serializer.h" 12 #include "base/json/json_file_value_serializer.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/metrics/histogram_macros.h" 15 #include "base/metrics/histogram_macros.h"
16 #include "base/sequenced_task_runner.h" 16 #include "base/sequenced_task_runner.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "chrome/common/chrome_constants.h" 18 #include "chrome/common/chrome_constants.h"
19 #include "components/pref_registry/pref_registry_syncable.h" 19 #include "components/pref_registry/pref_registry_syncable.h"
20 #include "components/prefs/json_pref_store.h" 20 #include "components/prefs/json_pref_store.h"
21 #include "components/prefs/persistent_pref_store.h" 21 #include "components/prefs/persistent_pref_store.h"
22 #include "components/prefs/pref_registry_simple.h" 22 #include "components/prefs/pref_registry_simple.h"
23 #include "components/user_prefs/tracked/pref_hash_store_impl.h" 23 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
24 #include "components/user_prefs/tracked/segregated_pref_store.h" 24 #include "components/user_prefs/tracked/segregated_pref_store.h"
25 #include "components/user_prefs/tracked/tracked_preferences_migration.h" 25 #include "components/user_prefs/tracked/tracked_preferences_migration.h"
26 #include "services/preferences/public/cpp/tracked_persistent_pref_store_factory. h"
26 27
27 #if defined(OS_WIN) 28 #if defined(OS_WIN)
28 #include "chrome/install_static/install_util.h" 29 #include "chrome/install_static/install_util.h"
29 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" 30 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h"
30 #endif 31 #endif
31 32
32 namespace { 33 namespace {
33 34
34 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store,
35 const std::string& key) {
36 if (pref_store) {
37 pref_store->RemoveValueSilently(
38 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
39 }
40 }
41
42 #if defined(OS_WIN) 35 #if defined(OS_WIN)
43 // Forces a different registry key to be used for storing preference validation 36 // Forces a different registry key to be used for storing preference validation
44 // MACs. See |SetPreferenceValidationRegistryPathForTesting|. 37 // MACs. See |SetPreferenceValidationRegistryPathForTesting|.
45 const base::string16* g_preference_validation_registry_path_for_testing = 38 const base::string16* g_preference_validation_registry_path_for_testing =
46 nullptr; 39 nullptr;
47 #endif // OS_WIN 40 #endif // OS_WIN
48 41
42 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr>
43 WrapTrackedPreferenceMetadata(
44 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>& input) {
45 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> output;
46 for (const auto& metadata : input) {
47 output.emplace_back(base::in_place, metadata.reporting_id, metadata.name,
48 metadata.enforcement_level, metadata.strategy,
49 metadata.value_type);
50 }
51 return output;
52 }
53
49 } // namespace 54 } // namespace
50 55
51 // Preference tracking and protection is not required on platforms where other 56 // Preference tracking and protection is not required on platforms where other
52 // apps do not have access to chrome's persistent storage. 57 // apps do not have access to chrome's persistent storage.
53 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = 58 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking =
54 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) 59 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
55 false; 60 false;
56 #else 61 #else
57 true; 62 true;
58 #endif 63 #endif
59 64
60 ProfilePrefStoreManager::ProfilePrefStoreManager( 65 ProfilePrefStoreManager::ProfilePrefStoreManager(
61 const base::FilePath& profile_path, 66 const base::FilePath& profile_path,
62 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>& 67 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>&
63 tracking_configuration, 68 tracking_configuration,
64 size_t reporting_ids_count, 69 size_t reporting_ids_count,
65 const std::string& seed, 70 const std::string& seed,
66 const std::string& legacy_device_id, 71 const std::string& legacy_device_id)
67 PrefService* local_state)
68 : profile_path_(profile_path), 72 : profile_path_(profile_path),
69 tracking_configuration_(tracking_configuration), 73 tracking_configuration_(tracking_configuration),
70 reporting_ids_count_(reporting_ids_count), 74 reporting_ids_count_(reporting_ids_count),
71 seed_(seed), 75 seed_(seed),
72 legacy_device_id_(legacy_device_id), 76 legacy_device_id_(legacy_device_id) {}
73 local_state_(local_state) {}
74 77
75 ProfilePrefStoreManager::~ProfilePrefStoreManager() {} 78 ProfilePrefStoreManager::~ProfilePrefStoreManager() {}
76 79
77 // static 80 // static
78 void ProfilePrefStoreManager::RegisterProfilePrefs( 81 void ProfilePrefStoreManager::RegisterProfilePrefs(
79 user_prefs::PrefRegistrySyncable* registry) { 82 user_prefs::PrefRegistrySyncable* registry) {
80 PrefHashFilter::RegisterProfilePrefs(registry); 83 PrefHashFilter::RegisterProfilePrefs(registry);
81 } 84 }
82 85
83 // static 86 // static
84 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { 87 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) {
85 return PrefHashFilter::GetResetTime(pref_service); 88 return PrefHashFilter::GetResetTime(pref_service);
86 } 89 }
87 90
88 // static 91 // static
89 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { 92 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) {
90 PrefHashFilter::ClearResetTime(pref_service); 93 PrefHashFilter::ClearResetTime(pref_service);
91 } 94 }
92 95
93 #if defined(OS_WIN) 96 #if defined(OS_WIN)
94 // static 97 // static
95 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( 98 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting(
96 const base::string16* path) { 99 const base::string16* path) {
97 DCHECK(!path->empty()); 100 DCHECK(!path->empty());
98 g_preference_validation_registry_path_for_testing = path; 101 g_preference_validation_registry_path_for_testing = path;
99 } 102 }
100 #endif // OS_WIN 103 #endif // OS_WIN
101 104
102 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( 105 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore(
103 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, 106 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
104 const base::Closure& on_reset_on_load, 107 const base::Closure& on_reset_on_load,
105 prefs::mojom::TrackedPreferenceValidationDelegate* validation_delegate) { 108 prefs::mojom::TrackedPreferenceValidationDelegate* validation_delegate) {
106 std::unique_ptr<PrefFilter> pref_filter;
107 if (!kPlatformSupportsPreferenceTracking) { 109 if (!kPlatformSupportsPreferenceTracking) {
108 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), 110 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename),
109 io_task_runner.get(), 111 std::move(io_task_runner), nullptr);
110 std::unique_ptr<PrefFilter>());
111 } 112 }
112 113 return prefs::CreateTrackedPersistentPrefStore(
113 std::vector<PrefHashFilter::TrackedPreferenceMetadata> 114 std::move(io_task_runner), CreateTrackedPrefsConfiguration(),
114 unprotected_configuration; 115 validation_delegate, on_reset_on_load);
115 std::vector<PrefHashFilter::TrackedPreferenceMetadata>
116 protected_configuration;
117 std::set<std::string> protected_pref_names;
118 std::set<std::string> unprotected_pref_names;
119 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::const_iterator
120 it = tracking_configuration_.begin();
121 it != tracking_configuration_.end();
122 ++it) {
123 if (it->enforcement_level >
124 PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT) {
125 protected_configuration.push_back(*it);
126 protected_pref_names.insert(it->name);
127 } else {
128 unprotected_configuration.push_back(*it);
129 unprotected_pref_names.insert(it->name);
130 }
131 }
132
133 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter(
134 new PrefHashFilter(GetPrefHashStore(false),
135 GetExternalVerificationPrefHashStorePair(),
136 unprotected_configuration, base::Closure(),
137 validation_delegate, reporting_ids_count_, false));
138 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter(
139 GetPrefHashStore(true), GetExternalVerificationPrefHashStorePair(),
140 protected_configuration, on_reset_on_load, validation_delegate,
141 reporting_ids_count_, true));
142
143 PrefHashFilter* raw_unprotected_pref_hash_filter =
144 unprotected_pref_hash_filter.get();
145 PrefHashFilter* raw_protected_pref_hash_filter =
146 protected_pref_hash_filter.get();
147
148 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore(
149 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(),
150 std::move(unprotected_pref_hash_filter)));
151 scoped_refptr<JsonPrefStore> protected_pref_store(new JsonPrefStore(
152 profile_path_.Append(chrome::kSecurePreferencesFilename),
153 io_task_runner.get(), std::move(protected_pref_hash_filter)));
154
155 SetupTrackedPreferencesMigration(
156 unprotected_pref_names, protected_pref_names,
157 base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()),
158 base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()),
159 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply,
160 unprotected_pref_store->AsWeakPtr()),
161 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply,
162 protected_pref_store->AsWeakPtr()),
163 GetPrefHashStore(false), GetPrefHashStore(true),
164 raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter);
165
166 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store,
167 protected_pref_names);
168 } 116 }
169 117
170 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( 118 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs(
171 std::unique_ptr<base::DictionaryValue> master_prefs) { 119 std::unique_ptr<base::DictionaryValue> master_prefs) {
172 // Create the profile directory if it doesn't exist yet (very possible on 120 // Create the profile directory if it doesn't exist yet (very possible on
173 // first run). 121 // first run).
174 if (!base::CreateDirectory(profile_path_)) 122 if (!base::CreateDirectory(profile_path_))
175 return false; 123 return false;
176 124
177 if (kPlatformSupportsPreferenceTracking) { 125 if (kPlatformSupportsPreferenceTracking) {
178 PrefHashFilter(GetPrefHashStore(false), 126 PrefHashFilter(
gab 2017/03/21 21:19:22 After this CL this is the only instantiation of Pr
179 GetExternalVerificationPrefHashStorePair(), 127 base::MakeUnique<PrefHashStoreImpl>(seed_, legacy_device_id_, false),
180 tracking_configuration_, base::Closure(), NULL, 128 GetExternalVerificationPrefHashStorePair(), tracking_configuration_,
181 reporting_ids_count_, false) 129 base::Closure(), NULL, reporting_ids_count_, false)
182 .Initialize(master_prefs.get()); 130 .Initialize(master_prefs.get());
183 } 131 }
184 132
185 // This will write out to a single combined file which will be immediately 133 // This will write out to a single combined file which will be immediately
186 // migrated to two files on load. 134 // migrated to two files on load.
187 JSONFileValueSerializer serializer( 135 JSONFileValueSerializer serializer(
188 profile_path_.Append(chrome::kPreferencesFilename)); 136 profile_path_.Append(chrome::kPreferencesFilename));
189 137
190 // Call Serialize (which does IO) on the main thread, which would _normally_ 138 // Call Serialize (which does IO) on the main thread, which would _normally_
191 // be verboten. In this case however, we require this IO to synchronously 139 // be verboten. In this case however, we require this IO to synchronously
192 // complete before Chrome can start (as master preferences seed the Local 140 // complete before Chrome can start (as master preferences seed the Local
193 // State and Preferences files). This won't trip ThreadIORestrictions as they 141 // State and Preferences files). This won't trip ThreadIORestrictions as they
194 // won't have kicked in yet on the main thread. 142 // won't have kicked in yet on the main thread.
195 bool success = serializer.Serialize(*master_prefs); 143 bool success = serializer.Serialize(*master_prefs);
196 144
197 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); 145 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success);
198 return success; 146 return success;
199 } 147 }
200 148
201 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore(
202 bool use_super_mac) {
203 DCHECK(kPlatformSupportsPreferenceTracking);
204
205 return std::unique_ptr<PrefHashStore>(
206 new PrefHashStoreImpl(seed_, legacy_device_id_, use_super_mac));
207 }
208
209 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>> 149 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>>
210 ProfilePrefStoreManager::GetExternalVerificationPrefHashStorePair() { 150 ProfilePrefStoreManager::GetExternalVerificationPrefHashStorePair() {
211 DCHECK(kPlatformSupportsPreferenceTracking); 151 DCHECK(kPlatformSupportsPreferenceTracking);
212 #if defined(OS_WIN) 152 #if defined(OS_WIN)
213 return std::make_pair( 153 return std::make_pair(
214 base::MakeUnique<PrefHashStoreImpl>( 154 base::MakeUnique<PrefHashStoreImpl>(
215 "ChromeRegistryHashStoreValidationSeed", legacy_device_id_, 155 "ChromeRegistryHashStoreValidationSeed", legacy_device_id_,
216 false /* use_super_mac */), 156 false /* use_super_mac */),
217 g_preference_validation_registry_path_for_testing 157 g_preference_validation_registry_path_for_testing
218 ? base::MakeUnique<RegistryHashStoreContentsWin>( 158 ? base::MakeUnique<RegistryHashStoreContentsWin>(
219 *g_preference_validation_registry_path_for_testing, 159 *g_preference_validation_registry_path_for_testing,
220 profile_path_.BaseName().LossyDisplayName()) 160 profile_path_.BaseName().LossyDisplayName())
221 : base::MakeUnique<RegistryHashStoreContentsWin>( 161 : base::MakeUnique<RegistryHashStoreContentsWin>(
222 install_static::GetRegistryPath(), 162 install_static::GetRegistryPath(),
223 profile_path_.BaseName().LossyDisplayName())); 163 profile_path_.BaseName().LossyDisplayName()));
224 #else 164 #else
225 return std::make_pair(nullptr, nullptr); 165 return std::make_pair(nullptr, nullptr);
226 #endif 166 #endif
227 } 167 }
168
169 prefs::mojom::TrackedPersistentPrefStoreConfigurationPtr
170 ProfilePrefStoreManager::CreateTrackedPrefsConfiguration() {
171 DCHECK(kPlatformSupportsPreferenceTracking);
172 prefs::mojom::TrackedPreferenceValidationDelegatePtr validation_delegate_ptr;
gab 2017/03/28 16:42:08 Unused?
Sam McNally 2017/03/30 09:06:05 Done.
173 return prefs::mojom::TrackedPersistentPrefStoreConfiguration::New(
gab 2017/03/28 16:42:08 Where are things like ::New() documented? I dug ar
Sam McNally 2017/03/30 09:06:05 It does appear that mojo structs and unions are no
174 profile_path_.Append(chrome::kPreferencesFilename),
175 profile_path_.Append(chrome::kSecurePreferencesFilename),
176 WrapTrackedPreferenceMetadata(tracking_configuration_),
177 reporting_ids_count_, seed_, legacy_device_id_,
178 #if defined(OS_WIN)
179 g_preference_validation_registry_path_for_testing
180 ? *g_preference_validation_registry_path_for_testing
181 : install_static::GetRegistryPath(),
182 #else
183 base::string16(),
184 #endif
185 nullptr, nullptr);
186 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698