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

Side by Side Diff: services/preferences/public/cpp/persistent_pref_store_client.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/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/threading/thread_restrictions.h" 9 #include "base/threading/thread_restrictions.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "components/prefs/pref_registry.h"
11 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" 12 #include "mojo/public/cpp/bindings/sync_call_restrictions.h"
12 13
13 namespace prefs { 14 namespace prefs {
15 namespace {
16
17 mojom::PrefRegistryPtr WrapPrefRegistry(PrefRegistry& pref_registry) {
18 auto registry = mojom::PrefRegistry::New();
19 for (auto& pref : pref_registry) {
20 auto registration = mojom::PrefRegistration::New();
21 registration->set_owning_registration(mojom::OwningPrefRegistration::New(
22 pref.second->CreateDeepCopy(),
23 pref_registry.GetRegistrationFlags(pref.first)));
24 registry->registrations[pref.first] = std::move(registration);
25 }
26 return registry;
27 }
28
29 } // namespace
14 30
15 PersistentPrefStoreClient::PersistentPrefStoreClient( 31 PersistentPrefStoreClient::PersistentPrefStoreClient(
16 mojom::PersistentPrefStoreConnectorPtr connector) 32 mojom::PrefStoreConnectorPtr connector,
17 : connector_(std::move(connector)) {} 33 scoped_refptr<PrefRegistry> pref_registry)
34 : connector_(std::move(connector)),
35 pref_registry_(std::move(pref_registry)) {
36 DCHECK(connector_);
37 }
38
39 PersistentPrefStoreClient::PersistentPrefStoreClient(
40 mojom::PersistentPrefStoreConnectionPtr connection) {
41 OnCreateComplete(std::move(connection),
42 std::unordered_map<PrefValueStore::PrefStoreType,
43 prefs::mojom::PrefStoreConnectionPtr>());
44 }
18 45
19 void PersistentPrefStoreClient::SetValue(const std::string& key, 46 void PersistentPrefStoreClient::SetValue(const std::string& key,
20 std::unique_ptr<base::Value> value, 47 std::unique_ptr<base::Value> value,
21 uint32_t flags) { 48 uint32_t flags) {
22 base::Value* old_value = nullptr; 49 base::Value* old_value = nullptr;
23 GetMutableValues().Get(key, &old_value); 50 GetMutableValues().Get(key, &old_value);
24 if (!old_value || !value->Equals(old_value)) { 51 if (!old_value || !value->Equals(old_value)) {
25 GetMutableValues().Set(key, std::move(value)); 52 GetMutableValues().Set(key, std::move(value));
26 ReportValueChanged(key, flags); 53 ReportValueChanged(key, flags);
27 } 54 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 bool PersistentPrefStoreClient::ReadOnly() const { 87 bool PersistentPrefStoreClient::ReadOnly() const {
61 return read_only_; 88 return read_only_;
62 } 89 }
63 90
64 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::GetReadError() 91 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::GetReadError()
65 const { 92 const {
66 return read_error_; 93 return read_error_;
67 } 94 }
68 95
69 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::ReadPrefs() { 96 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::ReadPrefs() {
70 PrefReadError read_error = PrefReadError::PREF_READ_ERROR_NONE; 97 mojom::PersistentPrefStoreConnectionPtr connection;
71 bool read_only = false; 98 std::unordered_map<PrefValueStore::PrefStoreType,
72 std::unique_ptr<base::DictionaryValue> local_prefs; 99 prefs::mojom::PrefStoreConnectionPtr>
73 mojom::PersistentPrefStorePtr pref_store; 100 other_pref_stores;
74 mojom::PrefStoreObserverRequest observer_request;
75 base::ThreadRestrictions::AssertWaitAllowed(); 101 base::ThreadRestrictions::AssertWaitAllowed();
76 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_calls; 102 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_calls;
77 if (!connector_->Connect(&read_error, &read_only, &local_prefs, &pref_store, 103 if (!connector_->Connect(WrapPrefRegistry(*pref_registry_), &connection,
78 &observer_request)) { 104 &other_pref_stores)) {
79 NOTREACHED(); 105 NOTREACHED();
80 } 106 }
81 107
82 OnCreateComplete(read_error, read_only, std::move(local_prefs), 108 OnCreateComplete(std::move(connection), std::move(other_pref_stores));
83 std::move(pref_store), std::move(observer_request));
84 return read_error_; 109 return read_error_;
85 } 110 }
86 111
87 void PersistentPrefStoreClient::ReadPrefsAsync( 112 void PersistentPrefStoreClient::ReadPrefsAsync(
88 ReadErrorDelegate* error_delegate) { 113 ReadErrorDelegate* error_delegate) {
89 error_delegate_.reset(error_delegate); 114 error_delegate_.reset(error_delegate);
90 connector_->Connect(base::Bind(&PersistentPrefStoreClient::OnCreateComplete, 115 connector_->Connect(WrapPrefRegistry(*pref_registry_),
116 base::Bind(&PersistentPrefStoreClient::OnCreateComplete,
91 base::Unretained(this))); 117 base::Unretained(this)));
92 } 118 }
93 119
94 void PersistentPrefStoreClient::CommitPendingWrite() { 120 void PersistentPrefStoreClient::CommitPendingWrite() {
95 DCHECK(pref_store_); 121 DCHECK(pref_store_);
96 pref_store_->CommitPendingWrite(); 122 pref_store_->CommitPendingWrite();
97 } 123 }
98 124
99 void PersistentPrefStoreClient::SchedulePendingLossyWrites() { 125 void PersistentPrefStoreClient::SchedulePendingLossyWrites() {
100 DCHECK(pref_store_); 126 DCHECK(pref_store_);
101 return pref_store_->SchedulePendingLossyWrites(); 127 return pref_store_->SchedulePendingLossyWrites();
102 } 128 }
103 129
104 void PersistentPrefStoreClient::ClearMutableValues() { 130 void PersistentPrefStoreClient::ClearMutableValues() {
105 DCHECK(pref_store_); 131 DCHECK(pref_store_);
106 return pref_store_->ClearMutableValues(); 132 return pref_store_->ClearMutableValues();
107 } 133 }
108 134
109 PersistentPrefStoreClient::~PersistentPrefStoreClient() { 135 PersistentPrefStoreClient::~PersistentPrefStoreClient() {
110 if (!pref_store_) 136 if (!pref_store_)
111 return; 137 return;
112 138
113 pref_store_->CommitPendingWrite(); 139 pref_store_->CommitPendingWrite();
114 } 140 }
115 141
116 void PersistentPrefStoreClient::OnCreateComplete( 142 void PersistentPrefStoreClient::OnCreateComplete(
117 PrefReadError read_error, 143 mojom::PersistentPrefStoreConnectionPtr connection,
118 bool read_only, 144 std::unordered_map<PrefValueStore::PrefStoreType,
119 std::unique_ptr<base::DictionaryValue> cached_prefs, 145 prefs::mojom::PrefStoreConnectionPtr>
120 mojom::PersistentPrefStorePtr pref_store, 146 other_pref_stores) {
121 mojom::PrefStoreObserverRequest observer_request) { 147 DCHECK(other_pref_stores.empty());
122 connector_.reset(); 148 connector_.reset();
123 read_error_ = read_error; 149 read_error_ = connection->read_error;
124 read_only_ = read_only; 150 read_only_ = connection->read_only;
125 pref_store_ = std::move(pref_store); 151 pref_store_ = std::move(connection->pref_store);
126 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) 152 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE)
127 error_delegate_->OnError(read_error_); 153 error_delegate_->OnError(read_error_);
128 error_delegate_.reset(); 154 error_delegate_.reset();
129 155
130 Init(std::move(cached_prefs), true, std::move(observer_request)); 156 if (connection->pref_store_connection) {
157 Init(std::move(connection->pref_store_connection->initial_prefs), true,
158 std::move(connection->pref_store_connection->observer));
159 } else {
160 Init(nullptr, false, nullptr);
161 }
131 } 162 }
132 163
133 } // namespace prefs 164 } // namespace prefs
OLDNEW
« no previous file with comments | « services/preferences/public/cpp/persistent_pref_store_client.h ('k') | services/preferences/public/cpp/preferences.typemap » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698