Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Unified Diff: services/preferences/public/cpp/user_prefs_impl.cc

Issue 2601873002: Add a mojo bridge for PersistentPrefStore. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: services/preferences/public/cpp/user_prefs_impl.cc
diff --git a/services/preferences/public/cpp/user_prefs_impl.cc b/services/preferences/public/cpp/user_prefs_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8286f214691cc1300789eeca73b911e059bc01c9
--- /dev/null
+++ b/services/preferences/public/cpp/user_prefs_impl.cc
@@ -0,0 +1,387 @@
+// 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/public/cpp/user_prefs_impl.h"
+
+#include "base/macros.h"
+#include "base/values.h"
+#include "components/prefs/json_pref_store.h"
+#include "components/prefs/pref_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_preference_validation_delegate.h"
+#include "components/user_prefs/tracked/tracked_preferences_migration.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
+
+#if defined(OS_WIN)
+#include "components/user_prefs/tracked/registry_hash_store_contents_win.h"
+#endif
+
+namespace prefs {
+namespace {
+
+class ForwardingTrackedPreferenceValidationDelegate
tibell 2017/03/08 03:39:54 A short comment why this class exists.
+ : public TrackedPreferenceValidationDelegate {
+ public:
+ ForwardingTrackedPreferenceValidationDelegate(
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner,
+ base::WeakPtr<TrackedPreferenceValidationDelegate> target_delegate)
+ : task_runner_(std::move(task_runner)),
+ target_delegate_(target_delegate) {}
+
+ void OnAtomicPreferenceValidation(
+ const std::string& pref_path,
+ const base::Value* value,
+ PrefHashStoreTransaction::ValueState value_state,
+ PrefHashStoreTransaction::ValueState external_validation_value_state,
+ bool is_personal) override {
+ task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&ForwardingTrackedPreferenceValidationDelegate::
+ OnAtomicPreferenceValidationOnTaskRunner,
+ target_delegate_, pref_path,
+ base::Passed(value ? value->CreateDeepCopy() : nullptr),
+ value_state, external_validation_value_state, is_personal));
+ }
+
+ // Notifies observes of the result (|value_state|) of checking the split
+ // |dict_value| (which may be NULL) at |pref_path|. |is_personal| indicates
+ // whether or not the value may contain personal information.
+ void OnSplitPreferenceValidation(
+ const std::string& pref_path,
+ const base::DictionaryValue* dict_value,
+ const std::vector<std::string>& invalid_keys,
+ const std::vector<std::string>& external_validation_invalid_keys,
+ PrefHashStoreTransaction::ValueState value_state,
+ PrefHashStoreTransaction::ValueState external_validation_value_state,
+ bool is_personal) override {
+ task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &ForwardingTrackedPreferenceValidationDelegate::
+ OnSplitPreferenceValidationOnTaskRunner,
+ target_delegate_, pref_path,
+ base::Passed(dict_value ? dict_value->CreateDeepCopy() : nullptr),
+ invalid_keys, external_validation_invalid_keys, value_state,
+ external_validation_value_state, is_personal));
+ }
+
+ private:
+ static void OnAtomicPreferenceValidationOnTaskRunner(
+ base::WeakPtr<TrackedPreferenceValidationDelegate> target_delegate,
+ const std::string& pref_path,
+ std::unique_ptr<base::Value> value,
+ PrefHashStoreTransaction::ValueState value_state,
+ PrefHashStoreTransaction::ValueState external_validation_value_state,
+ bool is_personal) {
+ if (!target_delegate)
+ return;
+
+ target_delegate->OnAtomicPreferenceValidation(
+ pref_path, value.get(), value_state, external_validation_value_state,
+ is_personal);
+ }
+
+ static void OnSplitPreferenceValidationOnTaskRunner(
+ base::WeakPtr<TrackedPreferenceValidationDelegate> target_delegate,
+ const std::string& pref_path,
+ std::unique_ptr<base::DictionaryValue> dict_value,
+ const std::vector<std::string>& invalid_keys,
+ const std::vector<std::string>& external_validation_invalid_keys,
+ PrefHashStoreTransaction::ValueState value_state,
+ PrefHashStoreTransaction::ValueState external_validation_value_state,
+ bool is_personal) {
+ if (!target_delegate)
+ return;
+
+ target_delegate->OnSplitPreferenceValidation(
+ pref_path, dict_value.get(), invalid_keys,
+ external_validation_invalid_keys, value_state,
+ external_validation_value_state, is_personal);
+ }
+
+ const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
+
+ // The delegate to forward to. May only be used on |task_runner_|.
+ const base::WeakPtr<TrackedPreferenceValidationDelegate> target_delegate_;
+};
+
+class PersistentPrefStoreImpl : public mojom::PersistentPrefStore {
+ public:
+ explicit PersistentPrefStoreImpl(
+ scoped_refptr<::PersistentPrefStore> pref_store);
+
+ ~PersistentPrefStoreImpl() override;
+
+ // mojom::PersistentPrefStore:
+ void SetValue(const std::string& key,
+ std::unique_ptr<base::Value> value,
+ uint32_t flags) override;
+ void RemoveValue(const std::string& key, uint32_t flags) override;
+
+ void CommitPendingWrite() override;
+ void SchedulePendingLossyWrites() override;
+ void ClearMutableValues() override;
+
+ private:
+ scoped_refptr<::PersistentPrefStore> backing_pref_store_;
+
+ DISALLOW_COPY_AND_ASSIGN(PersistentPrefStoreImpl);
+};
+
+PersistentPrefStoreImpl::PersistentPrefStoreImpl(
+ scoped_refptr<::PersistentPrefStore> pref_store)
+ : backing_pref_store_(std::move(pref_store)) {}
+
+PersistentPrefStoreImpl::~PersistentPrefStoreImpl() = default;
+
+// mojomJsonPrefStore:
+void PersistentPrefStoreImpl::SetValue(const std::string& key,
+ std::unique_ptr<base::Value> value,
+ uint32_t flags) {
+ backing_pref_store_->SetValue(key, std::move(value), flags);
+}
+
+void PersistentPrefStoreImpl::RemoveValue(const std::string& key,
+ uint32_t flags) {
+ backing_pref_store_->RemoveValue(key, flags);
+}
+
+void PersistentPrefStoreImpl::CommitPendingWrite() {
+ backing_pref_store_->CommitPendingWrite();
+}
+
+void PersistentPrefStoreImpl::SchedulePendingLossyWrites() {
+ backing_pref_store_->SchedulePendingLossyWrites();
+}
+
+void PersistentPrefStoreImpl::ClearMutableValues() {
+ backing_pref_store_->ClearMutableValues();
+}
+
+void CallConnectCallback(
+ PersistentPrefStore* pref_store,
+ const mojom::PersistentPrefStoreConnector::ConnectCallback& callback) {
+ mojom::PersistentPrefStorePtr pref_store_ptr;
+ mojo::MakeStrongBinding(
+ base::MakeUnique<PersistentPrefStoreImpl>(std::move(pref_store)),
+ mojo::MakeRequest(&pref_store_ptr));
+ callback.Run(pref_store->GetReadError(), pref_store->ReadOnly(),
+ pref_store->IsInitializationComplete() ? pref_store->GetValues()
+ : nullptr,
+ std::move(pref_store_ptr));
+}
+
+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 std::unique_ptr<PrefHashStore>(
+ new 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
+}
+
+class PersistentPrefStoreConnectorImpl
+ : public mojom::PersistentPrefStoreConnector,
+ public PrefStore::Observer {
+ public:
+ PersistentPrefStoreConnectorImpl(
+ scoped_refptr<PersistentPrefStore> backing_pref_store,
+ std::unique_ptr<TrackedPreferenceValidationDelegate> validation_delegate)
+ : backing_pref_store_(backing_pref_store),
+ validation_delegate_(std::move(validation_delegate)) {}
+
+ ~PersistentPrefStoreConnectorImpl() override = default;
+
+ // mojom::PersistentPrefStoreConnector override:
+ void Connect(const ConnectCallback& callback) override {
+ if (backing_pref_store_->IsInitializationComplete()) {
+ CallConnectCallback(backing_pref_store_.get(), callback);
+ return;
+ }
+ connect_callbacks_.push_back(callback);
+ if (loading_)
+ return;
+
+ backing_pref_store_->AddObserver(this);
+ loading_ = true;
+ backing_pref_store_->ReadPrefsAsync(nullptr);
+ }
+
+ static void CreateUserPrefs(
+ const base::FilePath& pref_filename,
+ const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
+ mojom::PersistentPrefStoreConnectorRequest request) {
+ mojo::MakeStrongBinding(
+ base::MakeUnique<PersistentPrefStoreConnectorImpl>(
+ new JsonPrefStore(pref_filename, io_task_runner, nullptr), nullptr),
+ std::move(request));
+ }
+
+ static void CreateSegregatedUserPrefs(
+ const base::FilePath& unprotected_pref_filename,
+ const base::FilePath& protected_pref_filename,
+ const std::vector<PrefHashFilter::TrackedPreferenceMetadata>&
+ tracking_configuration,
+ size_t reporting_ids_count,
+ const std::string& seed,
+ const std::string& legacy_device_id,
+ const base::string16& registry_path,
+ std::unique_ptr<TrackedPreferenceValidationDelegate> validation_delegate,
+ const base::Closure& on_reset_on_load,
+ const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
+ mojom::PersistentPrefStoreConnectorRequest request) {
+ 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 : tracking_configuration) {
+ if (metadata.enforcement_level > PrefHashFilter::NO_ENFORCEMENT) {
+ protected_configuration.push_back(metadata);
+ protected_pref_names.insert(metadata.name);
+ } else {
+ unprotected_configuration.push_back(metadata);
+ unprotected_pref_names.insert(metadata.name);
+ }
+ }
+
+ std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter(
+ new PrefHashFilter(CreatePrefHashStore(seed, legacy_device_id, false),
+ GetExternalVerificationPrefHashStorePair(
+ seed, legacy_device_id, registry_path,
+ unprotected_pref_filename),
+ unprotected_configuration, base::Closure(),
+ validation_delegate.get(), reporting_ids_count,
+ false));
+ std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(
+ new PrefHashFilter(CreatePrefHashStore(seed, legacy_device_id, true),
+ GetExternalVerificationPrefHashStorePair(
+ seed, legacy_device_id, registry_path,
+ unprotected_pref_filename),
+ protected_configuration, on_reset_on_load,
+ validation_delegate.get(), 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();
+
+ scoped_refptr<JsonPrefStore> unprotected_pref_store(
+ new JsonPrefStore(unprotected_pref_filename, io_task_runner.get(),
+ std::move(unprotected_pref_hash_filter)));
+ scoped_refptr<JsonPrefStore> protected_pref_store(
+ new JsonPrefStore(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(seed, legacy_device_id, false),
+ CreatePrefHashStore(seed, legacy_device_id, true),
+ raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter);
+
+ mojo::MakeStrongBinding(
+ base::MakeUnique<PersistentPrefStoreConnectorImpl>(
+ new SegregatedPrefStore(unprotected_pref_store,
+ protected_pref_store, protected_pref_names),
+ std::move(validation_delegate)),
+ std::move(request));
+ }
+
+ private:
+ void OnPrefValueChanged(const std::string& key) override {}
+
+ void OnInitializationCompleted(bool succeeded) override {
+ loading_ = false;
+ backing_pref_store_->RemoveObserver(this);
+ for (const auto& callback : connect_callbacks_) {
+ CallConnectCallback(backing_pref_store_.get(), callback);
+ }
+ connect_callbacks_.clear();
+ }
+
+ scoped_refptr<PersistentPrefStore> backing_pref_store_;
+ std::unique_ptr<TrackedPreferenceValidationDelegate> validation_delegate_;
+
+ bool loading_ = false;
+ std::vector<ConnectCallback> connect_callbacks_;
+
+ DISALLOW_COPY_AND_ASSIGN(PersistentPrefStoreConnectorImpl);
+};
+
+} // namespace
+
+void CreateUserPrefs(
+ const base::FilePath& pref_filename,
+ const scoped_refptr<base::SingleThreadTaskRunner>& connection_task_runner,
+ const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
+ mojom::PersistentPrefStoreConnectorRequest request) {
+ connection_task_runner->PostTask(
+ FROM_HERE,
+ base::Bind(&PersistentPrefStoreConnectorImpl::CreateUserPrefs,
+ pref_filename, io_task_runner, base::Passed(&request)));
+}
+
+void CreateSegregatedUserPrefs(
+ const base::FilePath& unprotected_pref_filename,
+ const base::FilePath& protected_pref_filename,
+ const std::vector<PrefHashFilter::TrackedPreferenceMetadata>&
+ tracking_configuration,
+ size_t reporting_ids_count,
+ const std::string& seed,
+ const std::string& legacy_device_id,
+ const base::string16& registry_path,
+ base::WeakPtr<TrackedPreferenceValidationDelegate> validation_delegate,
+ const base::Closure& on_reset_on_load,
+ const scoped_refptr<base::SingleThreadTaskRunner>& connection_task_runner,
+ const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
+ mojom::PersistentPrefStoreConnectorRequest request) {
+ connection_task_runner->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &PersistentPrefStoreConnectorImpl::CreateSegregatedUserPrefs,
+ unprotected_pref_filename, protected_pref_filename,
+ tracking_configuration, reporting_ids_count, seed, legacy_device_id,
+ registry_path,
+ base::Passed(validation_delegate
+ ? base::MakeUnique<
+ ForwardingTrackedPreferenceValidationDelegate>(
+ base::ThreadTaskRunnerHandle::Get(),
+ validation_delegate)
+ : nullptr),
+ on_reset_on_load, io_task_runner, base::Passed(&request)));
+}
+
+} // namespace prefs
« no previous file with comments | « services/preferences/public/cpp/user_prefs_impl.h ('k') | services/preferences/public/interfaces/preferences.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698