OLD | NEW |
---|---|
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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/json/json_file_value_serializer.h" | 9 #include "base/json/json_file_value_serializer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/prefs/json_pref_store.h" | 12 #include "base/prefs/json_pref_store.h" |
13 #include "base/prefs/persistent_pref_store.h" | 13 #include "base/prefs/persistent_pref_store.h" |
14 #include "base/prefs/pref_registry_simple.h" | 14 #include "base/prefs/pref_registry_simple.h" |
15 #include "chrome/browser/prefs/pref_hash_store_impl.h" | 15 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
16 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" | 16 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" |
17 #include "chrome/browser/prefs/tracked/segregated_pref_store.h" | 17 #include "chrome/browser/prefs/tracked/segregated_pref_store.h" |
18 #include "chrome/browser/prefs/tracked/tracked_preference_validation_observer.h" | |
18 #include "chrome/browser/prefs/tracked/tracked_preferences_migration.h" | 19 #include "chrome/browser/prefs/tracked/tracked_preferences_migration.h" |
19 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
20 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
21 #include "components/user_prefs/pref_registry_syncable.h" | 22 #include "components/user_prefs/pref_registry_syncable.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // An adaptor that allows a PrefHashStoreImpl to access a preference store | 26 // An adaptor that allows a PrefHashStoreImpl to access a preference store |
26 // directly as a dictionary. Uses an equivalent layout to | 27 // directly as a dictionary. Uses an equivalent layout to |
27 // PrefStoreHashStoreContents. | 28 // PrefStoreHashStoreContents. |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {} | 176 void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {} |
176 | 177 |
177 void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) { | 178 void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) { |
178 // If we successfully loaded the preferences _and_ the PrefHashStoreImpl | 179 // If we successfully loaded the preferences _and_ the PrefHashStoreImpl |
179 // hasn't been initialized by someone else in the meantime, initialize it now. | 180 // hasn't been initialized by someone else in the meantime, initialize it now. |
180 const PrefHashStoreImpl::StoreVersion pre_update_version = | 181 const PrefHashStoreImpl::StoreVersion pre_update_version = |
181 pref_hash_store_impl_->GetCurrentVersion(); | 182 pref_hash_store_impl_->GetCurrentVersion(); |
182 if (succeeded && pre_update_version < PrefHashStoreImpl::VERSION_LATEST) { | 183 if (succeeded && pre_update_version < PrefHashStoreImpl::VERSION_LATEST) { |
183 PrefHashFilter(pref_hash_store_impl_.PassAs<PrefHashStore>(), | 184 PrefHashFilter(pref_hash_store_impl_.PassAs<PrefHashStore>(), |
184 tracking_configuration_, | 185 tracking_configuration_, |
186 scoped_ptr<TrackedPreferenceValidationObserver>(), | |
185 reporting_ids_count_).Initialize(*pref_store_); | 187 reporting_ids_count_).Initialize(*pref_store_); |
186 UMA_HISTOGRAM_ENUMERATION( | 188 UMA_HISTOGRAM_ENUMERATION( |
187 "Settings.TrackedPreferencesAlternateStoreVersionUpdatedFrom", | 189 "Settings.TrackedPreferencesAlternateStoreVersionUpdatedFrom", |
188 pre_update_version, | 190 pre_update_version, |
189 PrefHashStoreImpl::VERSION_LATEST + 1); | 191 PrefHashStoreImpl::VERSION_LATEST + 1); |
190 } | 192 } |
191 pref_store_->RemoveObserver(this); | 193 pref_store_->RemoveObserver(this); |
192 delete this; | 194 delete this; |
193 } | 195 } |
194 | 196 |
197 // A TrackedPreferenceValidationObserver that delegates to another shared | |
198 // observer instance. | |
199 class DelegatingValidationObserver | |
Mattias Nissler (ping if slow)
2014/05/15 14:32:25
Ah, so this is where the ownership awkwardness hap
grt (UTC plus 2)
2014/05/15 18:21:35
Yes. This introduces other issues that Erik has al
| |
200 : public TrackedPreferenceValidationObserver { | |
201 public: | |
202 // A reference-counted holder of a TrackedPreferenceValidationObserver. | |
203 class RefCountedObserver : public base::RefCounted<RefCountedObserver> { | |
204 public: | |
205 explicit RefCountedObserver( | |
206 scoped_ptr<TrackedPreferenceValidationObserver> observer); | |
207 TrackedPreferenceValidationObserver* get(); | |
208 | |
209 private: | |
210 friend class base::RefCounted<RefCountedObserver>; | |
211 ~RefCountedObserver(); | |
212 | |
213 scoped_ptr<TrackedPreferenceValidationObserver> observer_; | |
214 }; | |
215 | |
216 explicit DelegatingValidationObserver( | |
217 const scoped_refptr<RefCountedObserver>& observer); | |
218 virtual ~DelegatingValidationObserver(); | |
219 | |
220 // TrackedPreferenceValidationObserver methods. | |
221 virtual void OnAtomicPreferenceValidation( | |
222 const std::string& pref_path, | |
223 const base::Value* value, | |
224 PrefHashStoreTransaction::ValueState value_state, | |
225 TrackedPreferenceHelper::ResetAction reset_action) OVERRIDE; | |
226 virtual void OnSplitPreferenceValidation( | |
227 const std::string& pref_path, | |
228 const base::DictionaryValue* dict_value, | |
229 const std::vector<std::string>& invalid_keys, | |
230 PrefHashStoreTransaction::ValueState value_state, | |
231 TrackedPreferenceHelper::ResetAction reset_action) OVERRIDE; | |
232 | |
233 private: | |
234 scoped_refptr<RefCountedObserver> observer_; | |
235 DISALLOW_COPY_AND_ASSIGN(DelegatingValidationObserver); | |
236 }; | |
237 | |
238 DelegatingValidationObserver::RefCountedObserver::RefCountedObserver( | |
239 scoped_ptr<TrackedPreferenceValidationObserver> observer) | |
240 : observer_(observer.Pass()) { | |
241 } | |
242 | |
243 DelegatingValidationObserver::RefCountedObserver::~RefCountedObserver() { | |
244 } | |
245 | |
246 TrackedPreferenceValidationObserver* | |
247 DelegatingValidationObserver::RefCountedObserver::get() { | |
248 return observer_.get(); | |
249 } | |
250 | |
251 DelegatingValidationObserver::DelegatingValidationObserver( | |
252 const scoped_refptr<RefCountedObserver>& observer) | |
253 : observer_(observer) { | |
254 } | |
255 | |
256 DelegatingValidationObserver::~DelegatingValidationObserver() { | |
257 } | |
258 | |
259 void DelegatingValidationObserver::OnAtomicPreferenceValidation( | |
260 const std::string& pref_path, | |
261 const base::Value* value, | |
262 PrefHashStoreTransaction::ValueState value_state, | |
263 TrackedPreferenceHelper::ResetAction reset_action) { | |
264 observer_->get()->OnAtomicPreferenceValidation( | |
265 pref_path, value, value_state, reset_action); | |
266 } | |
267 | |
268 void DelegatingValidationObserver::OnSplitPreferenceValidation( | |
269 const std::string& pref_path, | |
270 const base::DictionaryValue* dict_value, | |
271 const std::vector<std::string>& invalid_keys, | |
272 PrefHashStoreTransaction::ValueState value_state, | |
273 TrackedPreferenceHelper::ResetAction reset_action) { | |
274 observer_->get()->OnSplitPreferenceValidation( | |
275 pref_path, dict_value, invalid_keys, value_state, reset_action); | |
276 } | |
277 | |
195 } // namespace | 278 } // namespace |
196 | 279 |
197 // TODO(erikwright): Enable this on Chrome OS and Android once MACs are moved | 280 // TODO(erikwright): Enable this on Chrome OS and Android once MACs are moved |
198 // out of Local State. This will resolve a race condition on Android and a | 281 // out of Local State. This will resolve a race condition on Android and a |
199 // privacy issue on ChromeOS. http://crbug.com/349158 | 282 // privacy issue on ChromeOS. http://crbug.com/349158 |
200 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = | 283 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = |
201 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 284 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
202 false; | 285 false; |
203 #else | 286 #else |
204 true; | 287 true; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { | 335 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { |
253 PrefHashFilter::ClearResetTime(pref_service); | 336 PrefHashFilter::ClearResetTime(pref_service); |
254 } | 337 } |
255 | 338 |
256 void ProfilePrefStoreManager::ResetPrefHashStore() { | 339 void ProfilePrefStoreManager::ResetPrefHashStore() { |
257 if (kPlatformSupportsPreferenceTracking) | 340 if (kPlatformSupportsPreferenceTracking) |
258 GetPrefHashStoreImpl()->Reset(); | 341 GetPrefHashStoreImpl()->Reset(); |
259 } | 342 } |
260 | 343 |
261 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 344 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
262 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 345 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
346 scoped_ptr<TrackedPreferenceValidationObserver> verification_observer) { | |
263 scoped_ptr<PrefFilter> pref_filter; | 347 scoped_ptr<PrefFilter> pref_filter; |
264 if (!kPlatformSupportsPreferenceTracking) { | 348 if (!kPlatformSupportsPreferenceTracking) { |
265 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 349 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
266 io_task_runner, | 350 io_task_runner, |
267 scoped_ptr<PrefFilter>()); | 351 scoped_ptr<PrefFilter>()); |
268 } | 352 } |
269 | 353 |
270 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | 354 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
271 unprotected_configuration; | 355 unprotected_configuration; |
272 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | 356 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
273 protected_configuration; | 357 protected_configuration; |
274 std::set<std::string> protected_pref_names; | 358 std::set<std::string> protected_pref_names; |
275 std::set<std::string> unprotected_pref_names; | 359 std::set<std::string> unprotected_pref_names; |
276 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::const_iterator | 360 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::const_iterator |
277 it = tracking_configuration_.begin(); | 361 it = tracking_configuration_.begin(); |
278 it != tracking_configuration_.end(); | 362 it != tracking_configuration_.end(); |
279 ++it) { | 363 ++it) { |
280 if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { | 364 if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { |
281 protected_configuration.push_back(*it); | 365 protected_configuration.push_back(*it); |
282 protected_pref_names.insert(it->name); | 366 protected_pref_names.insert(it->name); |
283 } else { | 367 } else { |
284 unprotected_configuration.push_back(*it); | 368 unprotected_configuration.push_back(*it); |
285 unprotected_pref_names.insert(it->name); | 369 unprotected_pref_names.insert(it->name); |
286 } | 370 } |
287 } | 371 } |
288 | 372 |
289 scoped_ptr<PrefHashFilter> unprotected_pref_hash_filter( | 373 scoped_refptr<DelegatingValidationObserver::RefCountedObserver> observer; |
290 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 374 if (verification_observer) { |
291 unprotected_configuration, | 375 observer = new DelegatingValidationObserver::RefCountedObserver( |
292 reporting_ids_count_)); | 376 verification_observer.Pass()); |
293 scoped_ptr<PrefHashFilter> protected_pref_hash_filter( | 377 } |
294 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 378 |
295 protected_configuration, | 379 scoped_ptr<PrefHashFilter> unprotected_pref_hash_filter(new PrefHashFilter( |
296 reporting_ids_count_)); | 380 GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
381 unprotected_configuration, | |
382 observer ? | |
383 scoped_ptr<TrackedPreferenceValidationObserver>( | |
384 new DelegatingValidationObserver(observer)) : | |
385 scoped_ptr<TrackedPreferenceValidationObserver>(), | |
386 reporting_ids_count_)); | |
387 scoped_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( | |
388 GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | |
389 protected_configuration, | |
390 observer ? | |
391 scoped_ptr<TrackedPreferenceValidationObserver>( | |
392 new DelegatingValidationObserver(observer)) : | |
393 scoped_ptr<TrackedPreferenceValidationObserver>(), | |
394 reporting_ids_count_)); | |
297 | 395 |
298 PrefHashFilter* raw_unprotected_pref_hash_filter = | 396 PrefHashFilter* raw_unprotected_pref_hash_filter = |
299 unprotected_pref_hash_filter.get(); | 397 unprotected_pref_hash_filter.get(); |
300 PrefHashFilter* raw_protected_pref_hash_filter = | 398 PrefHashFilter* raw_protected_pref_hash_filter = |
301 protected_pref_hash_filter.get(); | 399 protected_pref_hash_filter.get(); |
302 | 400 |
303 scoped_refptr<JsonPrefStore> unprotected_pref_store( | 401 scoped_refptr<JsonPrefStore> unprotected_pref_store( |
304 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 402 new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
305 io_task_runner, | 403 io_task_runner, |
306 unprotected_pref_hash_filter.PassAs<PrefFilter>())); | 404 unprotected_pref_hash_filter.PassAs<PrefFilter>())); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 // complete before Chrome can start (as master preferences seed the Local | 468 // complete before Chrome can start (as master preferences seed the Local |
371 // State and Preferences files). This won't trip ThreadIORestrictions as they | 469 // State and Preferences files). This won't trip ThreadIORestrictions as they |
372 // won't have kicked in yet on the main thread. | 470 // won't have kicked in yet on the main thread. |
373 bool success = serializer.Serialize(master_prefs); | 471 bool success = serializer.Serialize(master_prefs); |
374 | 472 |
375 if (success && kPlatformSupportsPreferenceTracking) { | 473 if (success && kPlatformSupportsPreferenceTracking) { |
376 scoped_refptr<const PrefStore> pref_store( | 474 scoped_refptr<const PrefStore> pref_store( |
377 new DictionaryPrefStore(&master_prefs)); | 475 new DictionaryPrefStore(&master_prefs)); |
378 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 476 PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
379 tracking_configuration_, | 477 tracking_configuration_, |
478 scoped_ptr<TrackedPreferenceValidationObserver>(), | |
380 reporting_ids_count_).Initialize(*pref_store); | 479 reporting_ids_count_).Initialize(*pref_store); |
381 } | 480 } |
382 | 481 |
383 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 482 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
384 return success; | 483 return success; |
385 } | 484 } |
386 | 485 |
387 PersistentPrefStore* | 486 PersistentPrefStore* |
388 ProfilePrefStoreManager::CreateDeprecatedCombinedProfilePrefStore( | 487 ProfilePrefStoreManager::CreateDeprecatedCombinedProfilePrefStore( |
389 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { | 488 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
390 scoped_ptr<PrefFilter> pref_filter; | 489 scoped_ptr<PrefFilter> pref_filter; |
391 if (kPlatformSupportsPreferenceTracking) { | 490 if (kPlatformSupportsPreferenceTracking) { |
392 pref_filter.reset( | 491 pref_filter.reset( |
393 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), | 492 new PrefHashFilter(GetPrefHashStoreImpl().PassAs<PrefHashStore>(), |
394 tracking_configuration_, | 493 tracking_configuration_, |
494 scoped_ptr<TrackedPreferenceValidationObserver>(), | |
395 reporting_ids_count_)); | 495 reporting_ids_count_)); |
396 } | 496 } |
397 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), | 497 return new JsonPrefStore(GetPrefFilePathFromProfilePath(profile_path_), |
398 io_task_runner, | 498 io_task_runner, |
399 pref_filter.Pass()); | 499 pref_filter.Pass()); |
400 } | 500 } |
401 | 501 |
402 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { | 502 scoped_ptr<PrefHashStoreImpl> ProfilePrefStoreManager::GetPrefHashStoreImpl() { |
403 DCHECK(kPlatformSupportsPreferenceTracking); | 503 DCHECK(kPlatformSupportsPreferenceTracking); |
404 | 504 |
405 return make_scoped_ptr(new PrefHashStoreImpl( | 505 return make_scoped_ptr(new PrefHashStoreImpl( |
406 seed_, | 506 seed_, |
407 device_id_, | 507 device_id_, |
408 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( | 508 scoped_ptr<HashStoreContents>(new PrefServiceHashStoreContents( |
409 profile_path_.AsUTF8Unsafe(), local_state_)))); | 509 profile_path_.AsUTF8Unsafe(), local_state_)))); |
410 } | 510 } |
OLD | NEW |