Index: services/preferences/user_prefs_factory.cc |
diff --git a/services/preferences/user_prefs_factory.cc b/services/preferences/user_prefs_factory.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ae8c5953d814e194c0657bb42def97a1f271256d |
--- /dev/null |
+++ b/services/preferences/user_prefs_factory.cc |
@@ -0,0 +1,170 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "services/preferences/user_prefs_factory.h" |
+ |
+#include <memory> |
+#include <set> |
+#include <utility> |
+ |
+#include "base/macros.h" |
+#include "base/values.h" |
+#include "components/prefs/json_pref_store.h" |
+#include "components/user_prefs/tracked/pref_hash_filter.h" |
+#include "components/user_prefs/tracked/pref_hash_store_impl.h" |
+#include "components/user_prefs/tracked/segregated_pref_store.h" |
+#include "components/user_prefs/tracked/tracked_preferences_migration.h" |
+#include "mojo/public/cpp/bindings/strong_binding.h" |
+#include "services/preferences/public/interfaces/tracked_preference_validation_delegate.mojom.h" |
+#include "services/preferences/user_prefs.h" |
+ |
+#if defined(OS_WIN) |
+#include "components/user_prefs/tracked/registry_hash_store_contents_win.h" |
+#endif |
+ |
+namespace prefs { |
+namespace { |
+ |
+void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store, |
+ const std::string& key) { |
+ if (pref_store) { |
+ pref_store->RemoveValueSilently( |
+ key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
+ } |
+} |
+ |
+std::unique_ptr<PrefHashStore> CreatePrefHashStore( |
+ const std::string& seed, |
+ const std::string& legacy_device_id, |
+ bool use_super_mac) { |
+ return base::MakeUnique<PrefHashStoreImpl>(seed, legacy_device_id, |
+ use_super_mac); |
+} |
+ |
+std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>> |
+GetExternalVerificationPrefHashStorePair(const std::string& seed, |
+ const std::string& legacy_device_id, |
+ const base::string16& registry_path, |
+ const base::FilePath& prefs_path) { |
+#if defined(OS_WIN) |
+ return std::make_pair( |
+ base::MakeUnique<PrefHashStoreImpl>(seed, legacy_device_id, |
+ false /* use_super_mac */), |
+ base::MakeUnique<RegistryHashStoreContentsWin>( |
+ registry_path, prefs_path.DirName().BaseName().LossyDisplayName())); |
+#else |
+ return std::make_pair(nullptr, nullptr); |
+#endif |
+} |
+ |
+void ForwardToResetOnLoadObserver(mojom::ResetOnLoadObserverPtr observer) { |
+ if (observer) |
+ observer->OnResetOnLoad(); |
+} |
+ |
+std::unique_ptr<UserPrefs> CreateSimpleUserPrefs( |
+ mojom::SimpleUserPrefsConfigurationPtr config, |
+ base::SequencedWorkerPool* worker_pool) { |
+ return base::MakeUnique<UserPrefs>( |
+ new JsonPrefStore(config->pref_filename, |
+ JsonPrefStore::GetTaskRunnerForFile( |
+ config->pref_filename.DirName(), worker_pool), |
+ nullptr), |
+ nullptr); |
+} |
+ |
+std::unique_ptr<UserPrefs> CreateSegregatedUserPrefs( |
+ mojom::SegregatedUserPrefsConfigurationPtr config, |
+ base::SequencedWorkerPool* worker_pool) { |
+ std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
+ unprotected_configuration; |
+ std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
+ protected_configuration; |
+ std::set<std::string> protected_pref_names; |
+ std::set<std::string> unprotected_pref_names; |
+ for (const auto& metadata : config->tracking_configuration) { |
+ PrefHashFilter::TrackedPreferenceMetadata metadata_view = { |
+ metadata->reporting_id, metadata->name.c_str(), |
+ metadata->enforcement_level, metadata->strategy, |
+ metadata->value_type, |
+ }; |
+ if (metadata->enforcement_level > |
+ mojom::TrackedPreferenceMetadata::EnforcementLevel::NO_ENFORCEMENT) { |
+ protected_configuration.push_back(metadata_view); |
+ protected_pref_names.insert(metadata->name); |
+ } else { |
+ unprotected_configuration.push_back(metadata_view); |
+ unprotected_pref_names.insert(metadata->name); |
+ } |
+ } |
+ |
+ std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter( |
+ new PrefHashFilter( |
+ CreatePrefHashStore(config->seed, config->legacy_device_id, false), |
+ GetExternalVerificationPrefHashStorePair( |
+ config->seed, config->legacy_device_id, config->registry_path, |
+ config->unprotected_pref_filename), |
+ unprotected_configuration, base::Closure(), |
+ config->validation_delegate.get(), config->reporting_ids_count, |
+ false)); |
+ std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( |
+ CreatePrefHashStore(config->seed, config->legacy_device_id, true), |
+ GetExternalVerificationPrefHashStorePair( |
+ config->seed, config->legacy_device_id, config->registry_path, |
+ config->unprotected_pref_filename), |
+ protected_configuration, |
+ base::Bind(&ForwardToResetOnLoadObserver, |
+ base::Passed(&config->reset_on_load_observer)), |
+ config->validation_delegate.get(), config->reporting_ids_count, true)); |
+ |
+ PrefHashFilter* raw_unprotected_pref_hash_filter = |
+ unprotected_pref_hash_filter.get(); |
+ PrefHashFilter* raw_protected_pref_hash_filter = |
+ protected_pref_hash_filter.get(); |
+ |
+ auto io_task_runner = JsonPrefStore::GetTaskRunnerForFile( |
+ config->unprotected_pref_filename.DirName(), worker_pool); |
+ scoped_refptr<JsonPrefStore> unprotected_pref_store( |
+ new JsonPrefStore(config->unprotected_pref_filename, io_task_runner.get(), |
+ std::move(unprotected_pref_hash_filter))); |
+ scoped_refptr<JsonPrefStore> protected_pref_store( |
+ new JsonPrefStore(config->protected_pref_filename, io_task_runner.get(), |
+ std::move(protected_pref_hash_filter))); |
+ |
+ SetupTrackedPreferencesMigration( |
+ unprotected_pref_names, protected_pref_names, |
+ base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()), |
+ base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()), |
+ base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, |
+ unprotected_pref_store->AsWeakPtr()), |
+ base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, |
+ protected_pref_store->AsWeakPtr()), |
+ CreatePrefHashStore(config->seed, config->legacy_device_id, false), |
+ CreatePrefHashStore(config->seed, config->legacy_device_id, true), |
+ raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter); |
+ |
+ return base::MakeUnique<UserPrefs>( |
+ new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, |
+ protected_pref_names), |
+ std::move(config->validation_delegate)); |
+} |
+ |
+} // namespace |
+ |
+std::unique_ptr<UserPrefs> CreateUserPrefs( |
+ mojom::UserPrefsConfigurationPtr configuration, |
+ base::SequencedWorkerPool* worker_pool) { |
+ if (configuration->is_simple_configuration()) { |
+ return CreateSimpleUserPrefs( |
+ std::move(configuration->get_simple_configuration()), worker_pool); |
+ } |
+ if (configuration->is_segregated_configuration()) { |
+ return CreateSegregatedUserPrefs( |
+ std::move(configuration->get_segregated_configuration()), worker_pool); |
+ } |
+ NOTREACHED(); |
+ return nullptr; |
+} |
+ |
+} // namespace prefs |