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

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

Issue 2767743003: Pref service: Merge connectors and send a PrefRegistry in Connect(). (Closed)
Patch Set: 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 mojom::PrefRegistryPtr pref_registry)
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 pref_registry_(std::move(pref_registry)) {
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_)
36 return; 39 return;
37 40
41 if (!IsKeyObserved(key))
42 return;
43
38 observer_->OnPrefChanged(key, value ? value->CreateDeepCopy() : nullptr); 44 observer_->OnPrefChanged(key, value ? value->CreateDeepCopy() : nullptr);
39 } 45 }
40 46
41 private: 47 private:
42 // mojom::PersistentPrefStore: 48 // mojom::PersistentPrefStore:
43 void SetValue(const std::string& key, 49 void SetValue(const std::string& key,
44 std::unique_ptr<base::Value> value, 50 std::unique_ptr<base::Value> value,
45 uint32_t flags) override { 51 uint32_t flags) override {
46 base::AutoReset<bool> scoped_call_in_progress(&write_in_progress_, true); 52 base::AutoReset<bool> scoped_call_in_progress(&write_in_progress_, true);
47 pref_store_->SetValue(key, std::move(value), flags); 53 pref_store_->SetValue(key, std::move(value), flags);
48 } 54 }
49 55
50 void CommitPendingWrite() override { pref_store_->CommitPendingWrite(); } 56 void CommitPendingWrite() override { pref_store_->CommitPendingWrite(); }
51 void SchedulePendingLossyWrites() override { 57 void SchedulePendingLossyWrites() override {
52 pref_store_->SchedulePendingLossyWrites(); 58 pref_store_->SchedulePendingLossyWrites();
53 } 59 }
54 void ClearMutableValues() override { pref_store_->ClearMutableValues(); } 60 void ClearMutableValues() override { pref_store_->ClearMutableValues(); }
55 61
56 void OnConnectionError() { pref_store_->OnConnectionError(this); } 62 void OnConnectionError() { pref_store_->OnConnectionError(this); }
57 63
64 bool IsKeyObserved(const std::string& key) {
65 return base::ContainsKey(pref_registry_->registrations, key);
66 }
67
58 // Owns |this|. 68 // Owns |this|.
59 PersistentPrefStoreImpl* pref_store_; 69 PersistentPrefStoreImpl* pref_store_;
60 70
61 mojo::Binding<mojom::PersistentPrefStore> binding_; 71 mojo::Binding<mojom::PersistentPrefStore> binding_;
62 mojom::PrefStoreObserverPtr observer_; 72 mojom::PrefStoreObserverPtr observer_;
73 mojom::PrefRegistryPtr pref_registry_;
63 74
64 // If true then a write is in progress and any update notifications should be 75 // If true then a write is in progress and any update notifications should be
65 // ignored, as those updates would originate from ourselves. 76 // ignored, as those updates would originate from ourselves.
66 bool write_in_progress_ = false; 77 bool write_in_progress_ = false;
67 78
68 DISALLOW_COPY_AND_ASSIGN(Connection); 79 DISALLOW_COPY_AND_ASSIGN(Connection);
69 }; 80 };
70 81
71 PersistentPrefStoreImpl::PersistentPrefStoreImpl( 82 PersistentPrefStoreImpl::PersistentPrefStoreImpl(
72 scoped_refptr<PersistentPrefStore> backing_pref_store, 83 scoped_refptr<PersistentPrefStore> backing_pref_store,
73 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate) 84 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate,
85 base::Closure on_initialized)
74 : backing_pref_store_(backing_pref_store), 86 : backing_pref_store_(backing_pref_store),
75 validation_delegate_(std::move(validation_delegate)) { 87 validation_delegate_(std::move(validation_delegate)) {
76 backing_pref_store_->AddObserver(this); 88 backing_pref_store_->AddObserver(this);
77 if (!backing_pref_store_->IsInitializationComplete()) { 89 if (!backing_pref_store_->IsInitializationComplete()) {
90 on_initialized_ = std::move(on_initialized);
78 initializing_ = true; 91 initializing_ = true;
79 backing_pref_store_->ReadPrefsAsync(nullptr); 92 backing_pref_store_->ReadPrefsAsync(nullptr);
80 } 93 }
81 } 94 }
82 95
83 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() { 96 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() {
84 backing_pref_store_->RemoveObserver(this); 97 backing_pref_store_->RemoveObserver(this);
85 } 98 }
86 99
87 // mojom::PersistentPrefStoreConnector override: 100 // mojom::PersistentPrefStoreConnector override:
88 void PersistentPrefStoreImpl::Connect(const ConnectCallback& callback) { 101 mojom::PersistentPrefStoreConnectionPtr PersistentPrefStoreImpl::Connect(
89 if (initializing_) { 102 mojom::PrefRegistryPtr pref_registry) {
90 pending_connect_callbacks_.push_back(callback); 103 DCHECK(!initializing_);
91 return; 104 if (!backing_pref_store_->IsInitializationComplete()) {
105 return mojom::PersistentPrefStoreConnection::New(
106 nullptr, nullptr, backing_pref_store_->GetReadError(),
107 backing_pref_store_->ReadOnly());
92 } 108 }
93 CallConnectCallback(callback); 109 mojom::PersistentPrefStorePtr pref_store_ptr;
110 mojom::PrefStoreObserverPtr observer;
111 mojom::PrefStoreObserverRequest observer_request =
112 mojo::MakeRequest(&observer);
113 auto connection = base::MakeUnique<Connection>(
114 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer),
115 std::move(pref_registry));
116 auto* connection_ptr = connection.get();
117 connections_.insert(std::make_pair(connection_ptr, std::move(connection)));
118 return mojom::PersistentPrefStoreConnection::New(
119 mojom::PrefStoreConnection::New(std::move(observer_request),
120 backing_pref_store_->GetValues(), true),
121 std::move(pref_store_ptr), backing_pref_store_->GetReadError(),
122 backing_pref_store_->ReadOnly());
94 } 123 }
95 124
96 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) { 125 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) {
97 // All mutations are triggered by a client. Updates are only sent to clients 126 // All mutations are triggered by a client. Updates are only sent to clients
98 // other than the instigator so if there is only one client, it will ignore 127 // other than the instigator so if there is only one client, it will ignore
99 // the update. 128 // the update.
100 if (connections_.size() == 1) 129 if (connections_.size() == 1)
101 return; 130 return;
102 131
103 const base::Value* value = nullptr; 132 const base::Value* value = nullptr;
104 backing_pref_store_->GetValue(key, &value); 133 backing_pref_store_->GetValue(key, &value);
105 for (auto& entry : connections_) 134 for (auto& entry : connections_)
106 entry.first->OnPrefValueChanged(key, value); 135 entry.first->OnPrefValueChanged(key, value);
107 } 136 }
108 137
109 void PersistentPrefStoreImpl::OnInitializationCompleted(bool succeeded) { 138 void PersistentPrefStoreImpl::OnInitializationCompleted(bool succeeded) {
110 DCHECK(initializing_); 139 DCHECK(initializing_);
111 initializing_ = false; 140 initializing_ = false;
112 for (const auto& callback : pending_connect_callbacks_) { 141 base::ResetAndReturn(&on_initialized_).Run();
113 CallConnectCallback(callback);
114 }
115 pending_connect_callbacks_.clear();
116 }
117
118 void PersistentPrefStoreImpl::CallConnectCallback(
119 const mojom::PersistentPrefStoreConnector::ConnectCallback& callback) {
120 DCHECK(!initializing_);
121 if (!backing_pref_store_->IsInitializationComplete()) {
122 callback.Run(backing_pref_store_->GetReadError(),
123 backing_pref_store_->ReadOnly(), nullptr, nullptr, nullptr);
124 return;
125 }
126 mojom::PersistentPrefStorePtr pref_store_ptr;
127 mojom::PrefStoreObserverPtr observer;
128 mojom::PrefStoreObserverRequest observer_request =
129 mojo::MakeRequest(&observer);
130 auto connection = base::MakeUnique<Connection>(
131 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer));
132 auto* connection_ptr = connection.get();
133 connections_.insert(std::make_pair(connection_ptr, std::move(connection)));
134 callback.Run(backing_pref_store_->GetReadError(),
135 backing_pref_store_->ReadOnly(),
136 backing_pref_store_->GetValues(), std::move(pref_store_ptr),
137 std::move(observer_request));
138 } 142 }
139 143
140 void PersistentPrefStoreImpl::SetValue(const std::string& key, 144 void PersistentPrefStoreImpl::SetValue(const std::string& key,
141 std::unique_ptr<base::Value> value, 145 std::unique_ptr<base::Value> value,
142 uint32_t flags) { 146 uint32_t flags) {
143 if (value) 147 if (value)
144 backing_pref_store_->SetValue(key, std::move(value), flags); 148 backing_pref_store_->SetValue(key, std::move(value), flags);
145 else 149 else
146 backing_pref_store_->RemoveValue(key, flags); 150 backing_pref_store_->RemoveValue(key, flags);
147 } 151 }
148 152
149 void PersistentPrefStoreImpl::CommitPendingWrite() { 153 void PersistentPrefStoreImpl::CommitPendingWrite() {
150 backing_pref_store_->CommitPendingWrite(); 154 backing_pref_store_->CommitPendingWrite();
151 } 155 }
152 156
153 void PersistentPrefStoreImpl::SchedulePendingLossyWrites() { 157 void PersistentPrefStoreImpl::SchedulePendingLossyWrites() {
154 backing_pref_store_->SchedulePendingLossyWrites(); 158 backing_pref_store_->SchedulePendingLossyWrites();
155 } 159 }
156 160
157 void PersistentPrefStoreImpl::ClearMutableValues() { 161 void PersistentPrefStoreImpl::ClearMutableValues() {
158 backing_pref_store_->ClearMutableValues(); 162 backing_pref_store_->ClearMutableValues();
159 } 163 }
160 164
161 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) { 165 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) {
162 connections_.erase(connection); 166 connections_.erase(connection);
163 } 167 }
164 168
165 } // namespace prefs 169 } // 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