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

Side by Side Diff: services/preferences/persistent_pref_store_impl.cc

Issue 2777483002: Pref service: Add initial support for pref registration. (Closed)
Patch Set: rebase Created 3 years, 9 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 unified diff | Download patch
OLDNEW
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_impl.h" 5 #include "services/preferences/persistent_pref_store_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/stl_util.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "components/prefs/persistent_pref_store.h" 13 #include "components/prefs/persistent_pref_store.h"
13 #include "mojo/public/cpp/bindings/binding.h" 14 #include "mojo/public/cpp/bindings/binding.h"
14 15
15 namespace prefs { 16 namespace prefs {
16 17
17 class PersistentPrefStoreImpl::Connection : public mojom::PersistentPrefStore { 18 class PersistentPrefStoreImpl::Connection : public mojom::PersistentPrefStore {
18 public: 19 public:
19 Connection(PersistentPrefStoreImpl* pref_store, 20 Connection(PersistentPrefStoreImpl* pref_store,
20 mojom::PersistentPrefStoreRequest request, 21 mojom::PersistentPrefStoreRequest request,
21 mojom::PrefStoreObserverPtr observer) 22 mojom::PrefStoreObserverPtr observer,
23 ObservedPrefs observed_keys)
22 : pref_store_(pref_store), 24 : pref_store_(pref_store),
23 binding_(this, std::move(request)), 25 binding_(this, std::move(request)),
24 observer_(std::move(observer)) { 26 observer_(std::move(observer)),
27 observed_keys_(std::move(observed_keys)) {
25 auto error_callback = 28 auto error_callback =
26 base::Bind(&PersistentPrefStoreImpl::Connection::OnConnectionError, 29 base::Bind(&PersistentPrefStoreImpl::Connection::OnConnectionError,
27 base::Unretained(this)); 30 base::Unretained(this));
28 binding_.set_connection_error_handler(error_callback); 31 binding_.set_connection_error_handler(error_callback);
29 observer_.set_connection_error_handler(error_callback); 32 observer_.set_connection_error_handler(error_callback);
30 } 33 }
31 34
32 ~Connection() override = default; 35 ~Connection() override = default;
33 36
34 void OnPrefValueChanged(const std::string& key, const base::Value* value) { 37 void OnPrefValueChanged(const std::string& key, const base::Value* value) {
35 if (write_in_progress_) 38 if (write_in_progress_ || !base::ContainsKey(observed_keys_, key))
36 return; 39 return;
37 40
38 observer_->OnPrefChanged(key, value ? value->CreateDeepCopy() : nullptr); 41 observer_->OnPrefChanged(key, value ? value->CreateDeepCopy() : nullptr);
39 } 42 }
40 43
41 private: 44 private:
42 // mojom::PersistentPrefStore: 45 // mojom::PersistentPrefStore:
43 void SetValue(const std::string& key, 46 void SetValue(const std::string& key,
44 std::unique_ptr<base::Value> value, 47 std::unique_ptr<base::Value> value,
45 uint32_t flags) override { 48 uint32_t flags) override {
46 base::AutoReset<bool> scoped_call_in_progress(&write_in_progress_, true); 49 base::AutoReset<bool> scoped_call_in_progress(&write_in_progress_, true);
47 pref_store_->SetValue(key, std::move(value), flags); 50 pref_store_->SetValue(key, std::move(value), flags);
48 } 51 }
49 52
50 void CommitPendingWrite() override { pref_store_->CommitPendingWrite(); } 53 void CommitPendingWrite() override { pref_store_->CommitPendingWrite(); }
51 void SchedulePendingLossyWrites() override { 54 void SchedulePendingLossyWrites() override {
52 pref_store_->SchedulePendingLossyWrites(); 55 pref_store_->SchedulePendingLossyWrites();
53 } 56 }
54 void ClearMutableValues() override { pref_store_->ClearMutableValues(); } 57 void ClearMutableValues() override { pref_store_->ClearMutableValues(); }
55 58
56 void OnConnectionError() { pref_store_->OnConnectionError(this); } 59 void OnConnectionError() { pref_store_->OnConnectionError(this); }
57 60
58 // Owns |this|. 61 // Owns |this|.
59 PersistentPrefStoreImpl* pref_store_; 62 PersistentPrefStoreImpl* const pref_store_;
60 63
61 mojo::Binding<mojom::PersistentPrefStore> binding_; 64 mojo::Binding<mojom::PersistentPrefStore> binding_;
62 mojom::PrefStoreObserverPtr observer_; 65 mojom::PrefStoreObserverPtr observer_;
66 const ObservedPrefs observed_keys_;
63 67
64 // If true then a write is in progress and any update notifications should be 68 // If true then a write is in progress and any update notifications should be
65 // ignored, as those updates would originate from ourselves. 69 // ignored, as those updates would originate from ourselves.
66 bool write_in_progress_ = false; 70 bool write_in_progress_ = false;
67 71
68 DISALLOW_COPY_AND_ASSIGN(Connection); 72 DISALLOW_COPY_AND_ASSIGN(Connection);
69 }; 73 };
70 74
71 PersistentPrefStoreImpl::PersistentPrefStoreImpl( 75 PersistentPrefStoreImpl::PersistentPrefStoreImpl(
72 scoped_refptr<PersistentPrefStore> backing_pref_store, 76 scoped_refptr<PersistentPrefStore> backing_pref_store,
73 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate, 77 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate,
74 base::OnceClosure on_initialized) 78 base::OnceClosure on_initialized)
75 : backing_pref_store_(backing_pref_store), 79 : backing_pref_store_(backing_pref_store),
76 validation_delegate_(std::move(validation_delegate)) { 80 validation_delegate_(std::move(validation_delegate)) {
77 backing_pref_store_->AddObserver(this); 81 backing_pref_store_->AddObserver(this);
78 if (!backing_pref_store_->IsInitializationComplete()) { 82 if (!backing_pref_store_->IsInitializationComplete()) {
79 on_initialized_ = std::move(on_initialized); 83 on_initialized_ = std::move(on_initialized);
80 initializing_ = true; 84 initializing_ = true;
81 backing_pref_store_->ReadPrefsAsync(nullptr); 85 backing_pref_store_->ReadPrefsAsync(nullptr);
82 } 86 }
83 } 87 }
84 88
85 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() { 89 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() {
86 backing_pref_store_->RemoveObserver(this); 90 backing_pref_store_->RemoveObserver(this);
87 } 91 }
88 92
89 mojom::PersistentPrefStoreConnectionPtr 93 mojom::PersistentPrefStoreConnectionPtr
90 PersistentPrefStoreImpl::CreateConnection() { 94 PersistentPrefStoreImpl::CreateConnection(ObservedPrefs observed_prefs) {
91 DCHECK(!initializing_); 95 DCHECK(!initializing_);
92 if (!backing_pref_store_->IsInitializationComplete()) { 96 if (!backing_pref_store_->IsInitializationComplete()) {
93 // |backing_pref_store_| initialization failed. 97 // |backing_pref_store_| initialization failed.
94 return mojom::PersistentPrefStoreConnection::New( 98 return mojom::PersistentPrefStoreConnection::New(
95 nullptr, nullptr, backing_pref_store_->GetReadError(), 99 nullptr, nullptr, backing_pref_store_->GetReadError(),
96 backing_pref_store_->ReadOnly()); 100 backing_pref_store_->ReadOnly());
97 } 101 }
98 mojom::PersistentPrefStorePtr pref_store_ptr; 102 mojom::PersistentPrefStorePtr pref_store_ptr;
99 mojom::PrefStoreObserverPtr observer; 103 mojom::PrefStoreObserverPtr observer;
100 mojom::PrefStoreObserverRequest observer_request = 104 mojom::PrefStoreObserverRequest observer_request =
101 mojo::MakeRequest(&observer); 105 mojo::MakeRequest(&observer);
102 auto connection = base::MakeUnique<Connection>( 106 auto connection = base::MakeUnique<Connection>(
103 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer)); 107 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer),
108 std::move(observed_prefs));
104 auto* connection_ptr = connection.get(); 109 auto* connection_ptr = connection.get();
105 connections_.insert(std::make_pair(connection_ptr, std::move(connection))); 110 connections_.insert(std::make_pair(connection_ptr, std::move(connection)));
106 return mojom::PersistentPrefStoreConnection::New( 111 return mojom::PersistentPrefStoreConnection::New(
107 mojom::PrefStoreConnection::New(std::move(observer_request), 112 mojom::PrefStoreConnection::New(std::move(observer_request),
108 backing_pref_store_->GetValues(), true), 113 backing_pref_store_->GetValues(), true),
109 std::move(pref_store_ptr), backing_pref_store_->GetReadError(), 114 std::move(pref_store_ptr), backing_pref_store_->GetReadError(),
110 backing_pref_store_->ReadOnly()); 115 backing_pref_store_->ReadOnly());
111 } 116 }
112 117
113 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) { 118 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 153
149 void PersistentPrefStoreImpl::ClearMutableValues() { 154 void PersistentPrefStoreImpl::ClearMutableValues() {
150 backing_pref_store_->ClearMutableValues(); 155 backing_pref_store_->ClearMutableValues();
151 } 156 }
152 157
153 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) { 158 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) {
154 connections_.erase(connection); 159 connections_.erase(connection);
155 } 160 }
156 161
157 } // namespace prefs 162 } // namespace prefs
OLDNEW
« no previous file with comments | « services/preferences/persistent_pref_store_impl.h ('k') | services/preferences/persistent_pref_store_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698