Chromium Code Reviews| 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/public/cpp/user_prefs_impl.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "base/values.h" | |
| 9 #include "components/prefs/json_pref_store.h" | |
| 10 #include "components/prefs/pref_filter.h" | |
| 11 #include "components/user_prefs/tracked/pref_hash_store_impl.h" | |
| 12 #include "components/user_prefs/tracked/segregated_pref_store.h" | |
| 13 #include "components/user_prefs/tracked/tracked_preference_validation_delegate.h " | |
| 14 #include "components/user_prefs/tracked/tracked_preferences_migration.h" | |
| 15 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 16 | |
| 17 #if defined(OS_WIN) | |
| 18 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" | |
| 19 #endif | |
| 20 | |
| 21 namespace prefs { | |
| 22 namespace { | |
| 23 | |
| 24 class ForwardingTrackedPreferenceValidationDelegate | |
|
tibell
2017/03/08 03:39:54
A short comment why this class exists.
| |
| 25 : public TrackedPreferenceValidationDelegate { | |
| 26 public: | |
| 27 ForwardingTrackedPreferenceValidationDelegate( | |
| 28 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 29 base::WeakPtr<TrackedPreferenceValidationDelegate> target_delegate) | |
| 30 : task_runner_(std::move(task_runner)), | |
| 31 target_delegate_(target_delegate) {} | |
| 32 | |
| 33 void OnAtomicPreferenceValidation( | |
| 34 const std::string& pref_path, | |
| 35 const base::Value* value, | |
| 36 PrefHashStoreTransaction::ValueState value_state, | |
| 37 PrefHashStoreTransaction::ValueState external_validation_value_state, | |
| 38 bool is_personal) override { | |
| 39 task_runner_->PostTask( | |
| 40 FROM_HERE, | |
| 41 base::Bind(&ForwardingTrackedPreferenceValidationDelegate:: | |
| 42 OnAtomicPreferenceValidationOnTaskRunner, | |
| 43 target_delegate_, pref_path, | |
| 44 base::Passed(value ? value->CreateDeepCopy() : nullptr), | |
| 45 value_state, external_validation_value_state, is_personal)); | |
| 46 } | |
| 47 | |
| 48 // Notifies observes of the result (|value_state|) of checking the split | |
| 49 // |dict_value| (which may be NULL) at |pref_path|. |is_personal| indicates | |
| 50 // whether or not the value may contain personal information. | |
| 51 void OnSplitPreferenceValidation( | |
| 52 const std::string& pref_path, | |
| 53 const base::DictionaryValue* dict_value, | |
| 54 const std::vector<std::string>& invalid_keys, | |
| 55 const std::vector<std::string>& external_validation_invalid_keys, | |
| 56 PrefHashStoreTransaction::ValueState value_state, | |
| 57 PrefHashStoreTransaction::ValueState external_validation_value_state, | |
| 58 bool is_personal) override { | |
| 59 task_runner_->PostTask( | |
| 60 FROM_HERE, | |
| 61 base::Bind( | |
| 62 &ForwardingTrackedPreferenceValidationDelegate:: | |
| 63 OnSplitPreferenceValidationOnTaskRunner, | |
| 64 target_delegate_, pref_path, | |
| 65 base::Passed(dict_value ? dict_value->CreateDeepCopy() : nullptr), | |
| 66 invalid_keys, external_validation_invalid_keys, value_state, | |
| 67 external_validation_value_state, is_personal)); | |
| 68 } | |
| 69 | |
| 70 private: | |
| 71 static void OnAtomicPreferenceValidationOnTaskRunner( | |
| 72 base::WeakPtr<TrackedPreferenceValidationDelegate> target_delegate, | |
| 73 const std::string& pref_path, | |
| 74 std::unique_ptr<base::Value> value, | |
| 75 PrefHashStoreTransaction::ValueState value_state, | |
| 76 PrefHashStoreTransaction::ValueState external_validation_value_state, | |
| 77 bool is_personal) { | |
| 78 if (!target_delegate) | |
| 79 return; | |
| 80 | |
| 81 target_delegate->OnAtomicPreferenceValidation( | |
| 82 pref_path, value.get(), value_state, external_validation_value_state, | |
| 83 is_personal); | |
| 84 } | |
| 85 | |
| 86 static void OnSplitPreferenceValidationOnTaskRunner( | |
| 87 base::WeakPtr<TrackedPreferenceValidationDelegate> target_delegate, | |
| 88 const std::string& pref_path, | |
| 89 std::unique_ptr<base::DictionaryValue> dict_value, | |
| 90 const std::vector<std::string>& invalid_keys, | |
| 91 const std::vector<std::string>& external_validation_invalid_keys, | |
| 92 PrefHashStoreTransaction::ValueState value_state, | |
| 93 PrefHashStoreTransaction::ValueState external_validation_value_state, | |
| 94 bool is_personal) { | |
| 95 if (!target_delegate) | |
| 96 return; | |
| 97 | |
| 98 target_delegate->OnSplitPreferenceValidation( | |
| 99 pref_path, dict_value.get(), invalid_keys, | |
| 100 external_validation_invalid_keys, value_state, | |
| 101 external_validation_value_state, is_personal); | |
| 102 } | |
| 103 | |
| 104 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 105 | |
| 106 // The delegate to forward to. May only be used on |task_runner_|. | |
| 107 const base::WeakPtr<TrackedPreferenceValidationDelegate> target_delegate_; | |
| 108 }; | |
| 109 | |
| 110 class PersistentPrefStoreImpl : public mojom::PersistentPrefStore { | |
| 111 public: | |
| 112 explicit PersistentPrefStoreImpl( | |
| 113 scoped_refptr<::PersistentPrefStore> pref_store); | |
| 114 | |
| 115 ~PersistentPrefStoreImpl() override; | |
| 116 | |
| 117 // mojom::PersistentPrefStore: | |
| 118 void SetValue(const std::string& key, | |
| 119 std::unique_ptr<base::Value> value, | |
| 120 uint32_t flags) override; | |
| 121 void RemoveValue(const std::string& key, uint32_t flags) override; | |
| 122 | |
| 123 void CommitPendingWrite() override; | |
| 124 void SchedulePendingLossyWrites() override; | |
| 125 void ClearMutableValues() override; | |
| 126 | |
| 127 private: | |
| 128 scoped_refptr<::PersistentPrefStore> backing_pref_store_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(PersistentPrefStoreImpl); | |
| 131 }; | |
| 132 | |
| 133 PersistentPrefStoreImpl::PersistentPrefStoreImpl( | |
| 134 scoped_refptr<::PersistentPrefStore> pref_store) | |
| 135 : backing_pref_store_(std::move(pref_store)) {} | |
| 136 | |
| 137 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() = default; | |
| 138 | |
| 139 // mojomJsonPrefStore: | |
| 140 void PersistentPrefStoreImpl::SetValue(const std::string& key, | |
| 141 std::unique_ptr<base::Value> value, | |
| 142 uint32_t flags) { | |
| 143 backing_pref_store_->SetValue(key, std::move(value), flags); | |
| 144 } | |
| 145 | |
| 146 void PersistentPrefStoreImpl::RemoveValue(const std::string& key, | |
| 147 uint32_t flags) { | |
| 148 backing_pref_store_->RemoveValue(key, flags); | |
| 149 } | |
| 150 | |
| 151 void PersistentPrefStoreImpl::CommitPendingWrite() { | |
| 152 backing_pref_store_->CommitPendingWrite(); | |
| 153 } | |
| 154 | |
| 155 void PersistentPrefStoreImpl::SchedulePendingLossyWrites() { | |
| 156 backing_pref_store_->SchedulePendingLossyWrites(); | |
| 157 } | |
| 158 | |
| 159 void PersistentPrefStoreImpl::ClearMutableValues() { | |
| 160 backing_pref_store_->ClearMutableValues(); | |
| 161 } | |
| 162 | |
| 163 void CallConnectCallback( | |
| 164 PersistentPrefStore* pref_store, | |
| 165 const mojom::PersistentPrefStoreConnector::ConnectCallback& callback) { | |
| 166 mojom::PersistentPrefStorePtr pref_store_ptr; | |
| 167 mojo::MakeStrongBinding( | |
| 168 base::MakeUnique<PersistentPrefStoreImpl>(std::move(pref_store)), | |
| 169 mojo::MakeRequest(&pref_store_ptr)); | |
| 170 callback.Run(pref_store->GetReadError(), pref_store->ReadOnly(), | |
| 171 pref_store->IsInitializationComplete() ? pref_store->GetValues() | |
| 172 : nullptr, | |
| 173 std::move(pref_store_ptr)); | |
| 174 } | |
| 175 | |
| 176 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store, | |
| 177 const std::string& key) { | |
| 178 if (pref_store) { | |
| 179 pref_store->RemoveValueSilently( | |
| 180 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 std::unique_ptr<PrefHashStore> CreatePrefHashStore( | |
| 185 const std::string& seed, | |
| 186 const std::string& legacy_device_id, | |
| 187 bool use_super_mac) { | |
| 188 return std::unique_ptr<PrefHashStore>( | |
| 189 new PrefHashStoreImpl(seed, legacy_device_id, use_super_mac)); | |
| 190 } | |
| 191 | |
| 192 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>> | |
| 193 GetExternalVerificationPrefHashStorePair(const std::string& seed, | |
| 194 const std::string& legacy_device_id, | |
| 195 const base::string16& registry_path, | |
| 196 const base::FilePath& prefs_path) { | |
| 197 #if defined(OS_WIN) | |
| 198 return std::make_pair( | |
| 199 base::MakeUnique<PrefHashStoreImpl>(seed, legacy_device_id, | |
| 200 false /* use_super_mac */), | |
| 201 base::MakeUnique<RegistryHashStoreContentsWin>( | |
| 202 registry_path, prefs_path.DirName().BaseName().LossyDisplayName())); | |
| 203 #else | |
| 204 return std::make_pair(nullptr, nullptr); | |
| 205 #endif | |
| 206 } | |
| 207 | |
| 208 class PersistentPrefStoreConnectorImpl | |
| 209 : public mojom::PersistentPrefStoreConnector, | |
| 210 public PrefStore::Observer { | |
| 211 public: | |
| 212 PersistentPrefStoreConnectorImpl( | |
| 213 scoped_refptr<PersistentPrefStore> backing_pref_store, | |
| 214 std::unique_ptr<TrackedPreferenceValidationDelegate> validation_delegate) | |
| 215 : backing_pref_store_(backing_pref_store), | |
| 216 validation_delegate_(std::move(validation_delegate)) {} | |
| 217 | |
| 218 ~PersistentPrefStoreConnectorImpl() override = default; | |
| 219 | |
| 220 // mojom::PersistentPrefStoreConnector override: | |
| 221 void Connect(const ConnectCallback& callback) override { | |
| 222 if (backing_pref_store_->IsInitializationComplete()) { | |
| 223 CallConnectCallback(backing_pref_store_.get(), callback); | |
| 224 return; | |
| 225 } | |
| 226 connect_callbacks_.push_back(callback); | |
| 227 if (loading_) | |
| 228 return; | |
| 229 | |
| 230 backing_pref_store_->AddObserver(this); | |
| 231 loading_ = true; | |
| 232 backing_pref_store_->ReadPrefsAsync(nullptr); | |
| 233 } | |
| 234 | |
| 235 static void CreateUserPrefs( | |
| 236 const base::FilePath& pref_filename, | |
| 237 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | |
| 238 mojom::PersistentPrefStoreConnectorRequest request) { | |
| 239 mojo::MakeStrongBinding( | |
| 240 base::MakeUnique<PersistentPrefStoreConnectorImpl>( | |
| 241 new JsonPrefStore(pref_filename, io_task_runner, nullptr), nullptr), | |
| 242 std::move(request)); | |
| 243 } | |
| 244 | |
| 245 static void CreateSegregatedUserPrefs( | |
| 246 const base::FilePath& unprotected_pref_filename, | |
| 247 const base::FilePath& protected_pref_filename, | |
| 248 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>& | |
| 249 tracking_configuration, | |
| 250 size_t reporting_ids_count, | |
| 251 const std::string& seed, | |
| 252 const std::string& legacy_device_id, | |
| 253 const base::string16& registry_path, | |
| 254 std::unique_ptr<TrackedPreferenceValidationDelegate> validation_delegate, | |
| 255 const base::Closure& on_reset_on_load, | |
| 256 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | |
| 257 mojom::PersistentPrefStoreConnectorRequest request) { | |
| 258 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
| 259 unprotected_configuration; | |
| 260 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
| 261 protected_configuration; | |
| 262 std::set<std::string> protected_pref_names; | |
| 263 std::set<std::string> unprotected_pref_names; | |
| 264 for (const auto& metadata : tracking_configuration) { | |
| 265 if (metadata.enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { | |
| 266 protected_configuration.push_back(metadata); | |
| 267 protected_pref_names.insert(metadata.name); | |
| 268 } else { | |
| 269 unprotected_configuration.push_back(metadata); | |
| 270 unprotected_pref_names.insert(metadata.name); | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter( | |
| 275 new PrefHashFilter(CreatePrefHashStore(seed, legacy_device_id, false), | |
| 276 GetExternalVerificationPrefHashStorePair( | |
| 277 seed, legacy_device_id, registry_path, | |
| 278 unprotected_pref_filename), | |
| 279 unprotected_configuration, base::Closure(), | |
| 280 validation_delegate.get(), reporting_ids_count, | |
| 281 false)); | |
| 282 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter( | |
| 283 new PrefHashFilter(CreatePrefHashStore(seed, legacy_device_id, true), | |
| 284 GetExternalVerificationPrefHashStorePair( | |
| 285 seed, legacy_device_id, registry_path, | |
| 286 unprotected_pref_filename), | |
| 287 protected_configuration, on_reset_on_load, | |
| 288 validation_delegate.get(), reporting_ids_count, | |
| 289 true)); | |
| 290 | |
| 291 PrefHashFilter* raw_unprotected_pref_hash_filter = | |
| 292 unprotected_pref_hash_filter.get(); | |
| 293 PrefHashFilter* raw_protected_pref_hash_filter = | |
| 294 protected_pref_hash_filter.get(); | |
| 295 | |
| 296 scoped_refptr<JsonPrefStore> unprotected_pref_store( | |
| 297 new JsonPrefStore(unprotected_pref_filename, io_task_runner.get(), | |
| 298 std::move(unprotected_pref_hash_filter))); | |
| 299 scoped_refptr<JsonPrefStore> protected_pref_store( | |
| 300 new JsonPrefStore(protected_pref_filename, io_task_runner.get(), | |
| 301 std::move(protected_pref_hash_filter))); | |
| 302 | |
| 303 SetupTrackedPreferencesMigration( | |
| 304 unprotected_pref_names, protected_pref_names, | |
| 305 base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()), | |
| 306 base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()), | |
| 307 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, | |
| 308 unprotected_pref_store->AsWeakPtr()), | |
| 309 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, | |
| 310 protected_pref_store->AsWeakPtr()), | |
| 311 CreatePrefHashStore(seed, legacy_device_id, false), | |
| 312 CreatePrefHashStore(seed, legacy_device_id, true), | |
| 313 raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter); | |
| 314 | |
| 315 mojo::MakeStrongBinding( | |
| 316 base::MakeUnique<PersistentPrefStoreConnectorImpl>( | |
| 317 new SegregatedPrefStore(unprotected_pref_store, | |
| 318 protected_pref_store, protected_pref_names), | |
| 319 std::move(validation_delegate)), | |
| 320 std::move(request)); | |
| 321 } | |
| 322 | |
| 323 private: | |
| 324 void OnPrefValueChanged(const std::string& key) override {} | |
| 325 | |
| 326 void OnInitializationCompleted(bool succeeded) override { | |
| 327 loading_ = false; | |
| 328 backing_pref_store_->RemoveObserver(this); | |
| 329 for (const auto& callback : connect_callbacks_) { | |
| 330 CallConnectCallback(backing_pref_store_.get(), callback); | |
| 331 } | |
| 332 connect_callbacks_.clear(); | |
| 333 } | |
| 334 | |
| 335 scoped_refptr<PersistentPrefStore> backing_pref_store_; | |
| 336 std::unique_ptr<TrackedPreferenceValidationDelegate> validation_delegate_; | |
| 337 | |
| 338 bool loading_ = false; | |
| 339 std::vector<ConnectCallback> connect_callbacks_; | |
| 340 | |
| 341 DISALLOW_COPY_AND_ASSIGN(PersistentPrefStoreConnectorImpl); | |
| 342 }; | |
| 343 | |
| 344 } // namespace | |
| 345 | |
| 346 void CreateUserPrefs( | |
| 347 const base::FilePath& pref_filename, | |
| 348 const scoped_refptr<base::SingleThreadTaskRunner>& connection_task_runner, | |
| 349 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | |
| 350 mojom::PersistentPrefStoreConnectorRequest request) { | |
| 351 connection_task_runner->PostTask( | |
| 352 FROM_HERE, | |
| 353 base::Bind(&PersistentPrefStoreConnectorImpl::CreateUserPrefs, | |
| 354 pref_filename, io_task_runner, base::Passed(&request))); | |
| 355 } | |
| 356 | |
| 357 void CreateSegregatedUserPrefs( | |
| 358 const base::FilePath& unprotected_pref_filename, | |
| 359 const base::FilePath& protected_pref_filename, | |
| 360 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>& | |
| 361 tracking_configuration, | |
| 362 size_t reporting_ids_count, | |
| 363 const std::string& seed, | |
| 364 const std::string& legacy_device_id, | |
| 365 const base::string16& registry_path, | |
| 366 base::WeakPtr<TrackedPreferenceValidationDelegate> validation_delegate, | |
| 367 const base::Closure& on_reset_on_load, | |
| 368 const scoped_refptr<base::SingleThreadTaskRunner>& connection_task_runner, | |
| 369 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | |
| 370 mojom::PersistentPrefStoreConnectorRequest request) { | |
| 371 connection_task_runner->PostTask( | |
| 372 FROM_HERE, | |
| 373 base::Bind( | |
| 374 &PersistentPrefStoreConnectorImpl::CreateSegregatedUserPrefs, | |
| 375 unprotected_pref_filename, protected_pref_filename, | |
| 376 tracking_configuration, reporting_ids_count, seed, legacy_device_id, | |
| 377 registry_path, | |
| 378 base::Passed(validation_delegate | |
| 379 ? base::MakeUnique< | |
| 380 ForwardingTrackedPreferenceValidationDelegate>( | |
| 381 base::ThreadTaskRunnerHandle::Get(), | |
| 382 validation_delegate) | |
| 383 : nullptr), | |
| 384 on_reset_on_load, io_task_runner, base::Passed(&request))); | |
| 385 } | |
| 386 | |
| 387 } // namespace prefs | |
| OLD | NEW |