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

Side by Side Diff: services/preferences/public/cpp/persistent_pref_store_client.cc

Issue 2791893002: Pref service: Batch pref updates. (Closed)
Patch Set: rebase Created 3 years, 8 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/public/cpp/persistent_pref_store_client.h" 5 #include "services/preferences/public/cpp/persistent_pref_store_client.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "components/prefs/pref_registry.h" 10 #include "components/prefs/pref_registry.h"
11 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" 11 #include "mojo/public/cpp/bindings/sync_call_restrictions.h"
12 #include "services/preferences/public/cpp/pref_registry_serializer.h" 12 #include "services/preferences/public/cpp/pref_registry_serializer.h"
13 13
14 namespace prefs { 14 namespace prefs {
15 15
16 PersistentPrefStoreClient::PersistentPrefStoreClient( 16 PersistentPrefStoreClient::PersistentPrefStoreClient(
17 mojom::PrefStoreConnectorPtr connector, 17 mojom::PrefStoreConnectorPtr connector,
18 scoped_refptr<PrefRegistry> pref_registry, 18 scoped_refptr<PrefRegistry> pref_registry,
19 std::vector<PrefValueStore::PrefStoreType> already_connected_types) 19 std::vector<PrefValueStore::PrefStoreType> already_connected_types)
20 : connector_(std::move(connector)), 20 : connector_(std::move(connector)),
21 pref_registry_(std::move(pref_registry)), 21 pref_registry_(std::move(pref_registry)),
22 already_connected_types_(std::move(already_connected_types)) { 22 already_connected_types_(std::move(already_connected_types)),
23 weak_factory_(this) {
23 DCHECK(connector_); 24 DCHECK(connector_);
24 } 25 }
25 26
26 PersistentPrefStoreClient::PersistentPrefStoreClient( 27 PersistentPrefStoreClient::PersistentPrefStoreClient(
27 mojom::PersistentPrefStoreConnectionPtr connection) { 28 mojom::PersistentPrefStoreConnectionPtr connection)
29 : weak_factory_(this) {
28 OnConnect(std::move(connection), 30 OnConnect(std::move(connection),
29 std::unordered_map<PrefValueStore::PrefStoreType, 31 std::unordered_map<PrefValueStore::PrefStoreType,
30 prefs::mojom::PrefStoreConnectionPtr>()); 32 prefs::mojom::PrefStoreConnectionPtr>());
31 } 33 }
32 34
33 void PersistentPrefStoreClient::SetValue(const std::string& key, 35 void PersistentPrefStoreClient::SetValue(const std::string& key,
34 std::unique_ptr<base::Value> value, 36 std::unique_ptr<base::Value> value,
35 uint32_t flags) { 37 uint32_t flags) {
36 base::Value* old_value = nullptr; 38 base::Value* old_value = nullptr;
37 GetMutableValues().Get(key, &old_value); 39 GetMutableValues().Get(key, &old_value);
(...skipping 12 matching lines...) Expand all
50 bool PersistentPrefStoreClient::GetMutableValue(const std::string& key, 52 bool PersistentPrefStoreClient::GetMutableValue(const std::string& key,
51 base::Value** result) { 53 base::Value** result) {
52 return GetMutableValues().Get(key, result); 54 return GetMutableValues().Get(key, result);
53 } 55 }
54 56
55 void PersistentPrefStoreClient::ReportValueChanged(const std::string& key, 57 void PersistentPrefStoreClient::ReportValueChanged(const std::string& key,
56 uint32_t flags) { 58 uint32_t flags) {
57 DCHECK(pref_store_); 59 DCHECK(pref_store_);
58 const base::Value* local_value = nullptr; 60 const base::Value* local_value = nullptr;
59 GetMutableValues().Get(key, &local_value); 61 GetMutableValues().Get(key, &local_value);
60 pref_store_->SetValue( 62
61 key, local_value ? local_value->CreateDeepCopy() : nullptr, flags); 63 QueueWrite(key, flags);
62 ReportPrefValueChanged(key); 64 ReportPrefValueChanged(key);
63 } 65 }
64 66
65 void PersistentPrefStoreClient::SetValueSilently( 67 void PersistentPrefStoreClient::SetValueSilently(
66 const std::string& key, 68 const std::string& key,
67 std::unique_ptr<base::Value> value, 69 std::unique_ptr<base::Value> value,
68 uint32_t flags) { 70 uint32_t flags) {
69 DCHECK(pref_store_); 71 DCHECK(pref_store_);
70 pref_store_->SetValue(key, value->CreateDeepCopy(), flags); 72 QueueWrite(key, flags);
71 GetMutableValues().Set(key, std::move(value)); 73 GetMutableValues().Set(key, std::move(value));
72 } 74 }
73 75
74 bool PersistentPrefStoreClient::ReadOnly() const { 76 bool PersistentPrefStoreClient::ReadOnly() const {
75 return read_only_; 77 return read_only_;
76 } 78 }
77 79
78 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::GetReadError() 80 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::GetReadError()
79 const { 81 const {
80 return read_error_; 82 return read_error_;
(...skipping 20 matching lines...) Expand all
101 error_delegate_.reset(error_delegate); 103 error_delegate_.reset(error_delegate);
102 connector_->Connect(SerializePrefRegistry(*pref_registry_), 104 connector_->Connect(SerializePrefRegistry(*pref_registry_),
103 already_connected_types_, 105 already_connected_types_,
104 base::Bind(&PersistentPrefStoreClient::OnConnect, 106 base::Bind(&PersistentPrefStoreClient::OnConnect,
105 base::Unretained(this))); 107 base::Unretained(this)));
106 pref_registry_ = nullptr; 108 pref_registry_ = nullptr;
107 } 109 }
108 110
109 void PersistentPrefStoreClient::CommitPendingWrite() { 111 void PersistentPrefStoreClient::CommitPendingWrite() {
110 DCHECK(pref_store_); 112 DCHECK(pref_store_);
113 if (!pending_writes_.empty())
114 FlushPendingWrites();
111 pref_store_->CommitPendingWrite(); 115 pref_store_->CommitPendingWrite();
112 } 116 }
113 117
114 void PersistentPrefStoreClient::SchedulePendingLossyWrites() { 118 void PersistentPrefStoreClient::SchedulePendingLossyWrites() {
115 DCHECK(pref_store_); 119 DCHECK(pref_store_);
116 return pref_store_->SchedulePendingLossyWrites(); 120 return pref_store_->SchedulePendingLossyWrites();
117 } 121 }
118 122
119 void PersistentPrefStoreClient::ClearMutableValues() { 123 void PersistentPrefStoreClient::ClearMutableValues() {
120 DCHECK(pref_store_); 124 DCHECK(pref_store_);
121 return pref_store_->ClearMutableValues(); 125 return pref_store_->ClearMutableValues();
122 } 126 }
123 127
124 PersistentPrefStoreClient::~PersistentPrefStoreClient() { 128 PersistentPrefStoreClient::~PersistentPrefStoreClient() {
125 if (!pref_store_) 129 if (!pref_store_)
126 return; 130 return;
127 131
128 pref_store_->CommitPendingWrite(); 132 CommitPendingWrite();
129 } 133 }
130 134
131 void PersistentPrefStoreClient::OnConnect( 135 void PersistentPrefStoreClient::OnConnect(
132 mojom::PersistentPrefStoreConnectionPtr connection, 136 mojom::PersistentPrefStoreConnectionPtr connection,
133 std::unordered_map<PrefValueStore::PrefStoreType, 137 std::unordered_map<PrefValueStore::PrefStoreType,
134 prefs::mojom::PrefStoreConnectionPtr> 138 prefs::mojom::PrefStoreConnectionPtr>
135 other_pref_stores) { 139 other_pref_stores) {
136 connector_.reset(); 140 connector_.reset();
137 read_error_ = connection->read_error; 141 read_error_ = connection->read_error;
138 read_only_ = connection->read_only; 142 read_only_ = connection->read_only;
139 pref_store_ = std::move(connection->pref_store); 143 pref_store_ = std::move(connection->pref_store);
140 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) 144 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE)
141 error_delegate_->OnError(read_error_); 145 error_delegate_->OnError(read_error_);
142 error_delegate_.reset(); 146 error_delegate_.reset();
143 147
144 if (connection->pref_store_connection) { 148 if (connection->pref_store_connection) {
145 Init(std::move(connection->pref_store_connection->initial_prefs), true, 149 Init(std::move(connection->pref_store_connection->initial_prefs), true,
146 std::move(connection->pref_store_connection->observer)); 150 std::move(connection->pref_store_connection->observer));
147 } else { 151 } else {
148 Init(nullptr, false, nullptr); 152 Init(nullptr, false, nullptr);
149 } 153 }
150 } 154 }
151 155
156 void PersistentPrefStoreClient::QueueWrite(const std::string& key,
157 uint32_t flags) {
158 if (pending_writes_.empty()) {
159 // Use a weak pointer since a pending write should not prolong the life of
160 // |this|. Instead, the destruction of |this| will flush any pending writes.
161 base::ThreadTaskRunnerHandle::Get()->PostTask(
162 FROM_HERE, base::Bind(&PersistentPrefStoreClient::FlushPendingWrites,
163 weak_factory_.GetWeakPtr()));
164 }
165 pending_writes_.insert(std::make_pair(key, flags));
166 }
167
168 void PersistentPrefStoreClient::FlushPendingWrites() {
169 std::vector<mojom::PrefUpdatePtr> updates;
170 for (const auto& pref : pending_writes_) {
171 const base::Value* value = nullptr;
172 if (GetValue(pref.first, &value)) {
173 updates.push_back(mojom::PrefUpdate::New(
174 pref.first, value->CreateDeepCopy(), pref.second));
175 } else {
176 updates.push_back(
177 mojom::PrefUpdate::New(pref.first, nullptr, pref.second));
178 }
179 }
180 pref_store_->SetValues(std::move(updates));
181 pending_writes_.clear();
182 }
183
152 } // namespace prefs 184 } // namespace prefs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698