Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "services/preferences/persistent_pref_store_factory.h" | 5 #include "services/preferences/persistent_pref_store_factory.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <set> | |
| 9 #include <string> | |
| 8 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | |
| 9 | 12 |
| 10 #include "components/prefs/json_pref_store.h" | 13 #include "components/prefs/json_pref_store.h" |
| 11 #include "components/prefs/pref_filter.h" | 14 #include "components/prefs/pref_filter.h" |
| 15 #include "components/user_prefs/tracked/pref_hash_filter.h" | |
| 16 #include "components/user_prefs/tracked/pref_hash_store_impl.h" | |
| 17 #include "components/user_prefs/tracked/segregated_pref_store.h" | |
| 18 #include "components/user_prefs/tracked/tracked_preferences_migration.h" | |
| 19 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 12 #include "services/preferences/persistent_pref_store_impl.h" | 20 #include "services/preferences/persistent_pref_store_impl.h" |
| 21 #include "services/preferences/public/interfaces/tracked_preference_validation_d elegate.mojom.h" | |
| 22 | |
| 23 #if defined(OS_WIN) | |
| 24 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" | |
| 25 #endif | |
| 13 | 26 |
| 14 namespace prefs { | 27 namespace prefs { |
| 15 namespace { | 28 namespace { |
| 16 | 29 |
| 30 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store, | |
| 31 const std::string& key) { | |
| 32 if (pref_store) { | |
| 33 pref_store->RemoveValueSilently( | |
| 34 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 std::unique_ptr<PrefHashStore> CreatePrefHashStore( | |
| 39 const std::string& seed, | |
| 40 const std::string& legacy_device_id, | |
| 41 bool use_super_mac) { | |
| 42 return base::MakeUnique<PrefHashStoreImpl>(seed, legacy_device_id, | |
| 43 use_super_mac); | |
| 44 } | |
| 45 | |
| 46 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>> | |
| 47 GetExternalVerificationPrefHashStorePair(const std::string& seed, | |
| 48 const std::string& legacy_device_id, | |
| 49 const base::string16& registry_path, | |
| 50 const base::FilePath& prefs_path) { | |
| 51 #if defined(OS_WIN) | |
| 52 return std::make_pair( | |
| 53 base::MakeUnique<PrefHashStoreImpl>(seed, legacy_device_id, | |
| 54 false /* use_super_mac */), | |
| 55 base::MakeUnique<RegistryHashStoreContentsWin>( | |
| 56 registry_path, prefs_path.DirName().BaseName().LossyDisplayName())); | |
| 57 #else | |
| 58 return std::make_pair(nullptr, nullptr); | |
| 59 #endif | |
| 60 } | |
| 61 | |
| 62 void ForwardToResetOnLoadObserver(mojom::ResetOnLoadObserverPtr observer) { | |
| 63 if (observer) | |
| 64 observer->OnResetOnLoad(); | |
| 65 } | |
| 66 | |
| 17 std::unique_ptr<PersistentPrefStoreImpl> CreateSimplePersistentPrefStore( | 67 std::unique_ptr<PersistentPrefStoreImpl> CreateSimplePersistentPrefStore( |
| 18 mojom::SimplePersistentPrefStoreConfigurationPtr config, | 68 mojom::SimplePersistentPrefStoreConfigurationPtr config, |
| 19 base::SequencedWorkerPool* worker_pool) { | 69 base::SequencedWorkerPool* worker_pool) { |
| 20 return base::MakeUnique<PersistentPrefStoreImpl>( | 70 return base::MakeUnique<PersistentPrefStoreImpl>( |
| 21 new JsonPrefStore(config->pref_filename, | 71 new JsonPrefStore(config->pref_filename, |
| 22 JsonPrefStore::GetTaskRunnerForFile( | 72 JsonPrefStore::GetTaskRunnerForFile( |
| 23 config->pref_filename.DirName(), worker_pool), | 73 config->pref_filename.DirName(), worker_pool), |
| 24 nullptr), | 74 nullptr), |
| 25 nullptr); | 75 nullptr); |
| 26 } | 76 } |
| 27 | 77 |
| 78 std::unique_ptr<PersistentPrefStoreImpl> CreateTrackedPersistentPrefStore( | |
|
tibell
2017/03/10 04:29:01
Does this method have to be kept in sync with some
Sam McNally
2017/03/10 05:04:44
Extracted out the common parts.
| |
| 79 mojom::TrackedPersistentPrefStoreConfigurationPtr config, | |
| 80 base::SequencedWorkerPool* worker_pool) { | |
| 81 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
| 82 unprotected_configuration; | |
| 83 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
| 84 protected_configuration; | |
| 85 std::set<std::string> protected_pref_names; | |
| 86 std::set<std::string> unprotected_pref_names; | |
| 87 for (const auto& metadata : config->tracking_configuration) { | |
| 88 PrefHashFilter::TrackedPreferenceMetadata metadata_view = { | |
| 89 metadata->reporting_id, metadata->name.c_str(), | |
| 90 metadata->enforcement_level, metadata->strategy, | |
| 91 metadata->value_type, | |
| 92 }; | |
| 93 if (metadata->enforcement_level > | |
| 94 mojom::TrackedPreferenceMetadata::EnforcementLevel::NO_ENFORCEMENT) { | |
| 95 protected_configuration.push_back(metadata_view); | |
| 96 protected_pref_names.insert(metadata->name); | |
| 97 } else { | |
| 98 unprotected_configuration.push_back(metadata_view); | |
| 99 unprotected_pref_names.insert(metadata->name); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter( | |
| 104 new PrefHashFilter( | |
| 105 CreatePrefHashStore(config->seed, config->legacy_device_id, false), | |
| 106 GetExternalVerificationPrefHashStorePair( | |
| 107 config->seed, config->legacy_device_id, config->registry_path, | |
| 108 config->unprotected_pref_filename), | |
| 109 unprotected_configuration, base::Closure(), | |
| 110 config->validation_delegate.get(), config->reporting_ids_count, | |
| 111 false)); | |
| 112 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( | |
| 113 CreatePrefHashStore(config->seed, config->legacy_device_id, true), | |
| 114 GetExternalVerificationPrefHashStorePair( | |
| 115 config->seed, config->legacy_device_id, config->registry_path, | |
| 116 config->unprotected_pref_filename), | |
| 117 protected_configuration, | |
| 118 base::Bind(&ForwardToResetOnLoadObserver, | |
| 119 base::Passed(&config->reset_on_load_observer)), | |
| 120 config->validation_delegate.get(), config->reporting_ids_count, true)); | |
| 121 | |
| 122 PrefHashFilter* raw_unprotected_pref_hash_filter = | |
| 123 unprotected_pref_hash_filter.get(); | |
| 124 PrefHashFilter* raw_protected_pref_hash_filter = | |
| 125 protected_pref_hash_filter.get(); | |
| 126 | |
| 127 auto io_task_runner = JsonPrefStore::GetTaskRunnerForFile( | |
| 128 config->unprotected_pref_filename.DirName(), worker_pool); | |
| 129 scoped_refptr<JsonPrefStore> unprotected_pref_store( | |
| 130 new JsonPrefStore(config->unprotected_pref_filename, io_task_runner.get(), | |
| 131 std::move(unprotected_pref_hash_filter))); | |
| 132 scoped_refptr<JsonPrefStore> protected_pref_store( | |
| 133 new JsonPrefStore(config->protected_pref_filename, io_task_runner.get(), | |
| 134 std::move(protected_pref_hash_filter))); | |
| 135 | |
| 136 SetupTrackedPreferencesMigration( | |
| 137 unprotected_pref_names, protected_pref_names, | |
| 138 base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()), | |
| 139 base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()), | |
| 140 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, | |
| 141 unprotected_pref_store->AsWeakPtr()), | |
| 142 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, | |
| 143 protected_pref_store->AsWeakPtr()), | |
| 144 CreatePrefHashStore(config->seed, config->legacy_device_id, false), | |
| 145 CreatePrefHashStore(config->seed, config->legacy_device_id, true), | |
| 146 raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter); | |
| 147 | |
| 148 return base::MakeUnique<PersistentPrefStoreImpl>( | |
| 149 new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, | |
| 150 protected_pref_names), | |
| 151 std::move(config->validation_delegate)); | |
| 152 } | |
| 153 | |
| 28 } // namespace | 154 } // namespace |
| 29 | 155 |
| 30 std::unique_ptr<PersistentPrefStoreImpl> CreatePersistentPrefStore( | 156 std::unique_ptr<PersistentPrefStoreImpl> CreatePersistentPrefStore( |
| 31 mojom::PersistentPrefStoreConfigurationPtr configuration, | 157 mojom::PersistentPrefStoreConfigurationPtr configuration, |
| 32 base::SequencedWorkerPool* worker_pool) { | 158 base::SequencedWorkerPool* worker_pool) { |
| 33 if (configuration->is_simple_configuration()) { | 159 if (configuration->is_simple_configuration()) { |
| 34 return CreateSimplePersistentPrefStore( | 160 return CreateSimplePersistentPrefStore( |
| 35 std::move(configuration->get_simple_configuration()), worker_pool); | 161 std::move(configuration->get_simple_configuration()), worker_pool); |
| 36 } | 162 } |
| 163 if (configuration->is_tracked_configuration()) { | |
| 164 return CreateTrackedPersistentPrefStore( | |
| 165 std::move(configuration->get_tracked_configuration()), worker_pool); | |
| 166 } | |
| 37 NOTREACHED(); | 167 NOTREACHED(); |
| 38 return nullptr; | 168 return nullptr; |
| 39 } | 169 } |
| 40 | 170 |
| 41 } // namespace prefs | 171 } // namespace prefs |
| OLD | NEW |