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