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

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

Issue 2771723002: Pref service: Merge PrefStoreConnector interfaces. (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"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 63
64 // If true then a write is in progress and any update notifications should be 64 // If true then a write is in progress and any update notifications should be
65 // ignored, as those updates would originate from ourselves. 65 // ignored, as those updates would originate from ourselves.
66 bool write_in_progress_ = false; 66 bool write_in_progress_ = false;
67 67
68 DISALLOW_COPY_AND_ASSIGN(Connection); 68 DISALLOW_COPY_AND_ASSIGN(Connection);
69 }; 69 };
70 70
71 PersistentPrefStoreImpl::PersistentPrefStoreImpl( 71 PersistentPrefStoreImpl::PersistentPrefStoreImpl(
72 scoped_refptr<PersistentPrefStore> backing_pref_store, 72 scoped_refptr<PersistentPrefStore> backing_pref_store,
73 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate) 73 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate,
74 base::OnceClosure on_initialized)
74 : backing_pref_store_(backing_pref_store), 75 : backing_pref_store_(backing_pref_store),
75 validation_delegate_(std::move(validation_delegate)) { 76 validation_delegate_(std::move(validation_delegate)) {
76 backing_pref_store_->AddObserver(this); 77 backing_pref_store_->AddObserver(this);
77 if (!backing_pref_store_->IsInitializationComplete()) { 78 if (!backing_pref_store_->IsInitializationComplete()) {
79 on_initialized_ = std::move(on_initialized);
78 initializing_ = true; 80 initializing_ = true;
79 backing_pref_store_->ReadPrefsAsync(nullptr); 81 backing_pref_store_->ReadPrefsAsync(nullptr);
80 } 82 }
81 } 83 }
82 84
83 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() { 85 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() {
84 backing_pref_store_->RemoveObserver(this); 86 backing_pref_store_->RemoveObserver(this);
85 } 87 }
86 88
87 // mojom::PersistentPrefStoreConnector override: 89 mojom::PersistentPrefStoreConnectionPtr
88 void PersistentPrefStoreImpl::Connect(const ConnectCallback& callback) { 90 PersistentPrefStoreImpl::CreateConnection() {
89 if (initializing_) { 91 DCHECK(!initializing_);
90 pending_connect_callbacks_.push_back(callback); 92 if (!backing_pref_store_->IsInitializationComplete()) {
91 return; 93 // |backing_pref_store_| initialization failed.
94 return mojom::PersistentPrefStoreConnection::New(
95 nullptr, nullptr, backing_pref_store_->GetReadError(),
96 backing_pref_store_->ReadOnly());
92 } 97 }
93 CallConnectCallback(callback); 98 mojom::PersistentPrefStorePtr pref_store_ptr;
99 mojom::PrefStoreObserverPtr observer;
100 mojom::PrefStoreObserverRequest observer_request =
101 mojo::MakeRequest(&observer);
102 auto connection = base::MakeUnique<Connection>(
103 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer));
104 auto* connection_ptr = connection.get();
105 connections_.insert(std::make_pair(connection_ptr, std::move(connection)));
106 return mojom::PersistentPrefStoreConnection::New(
107 mojom::PrefStoreConnection::New(std::move(observer_request),
108 backing_pref_store_->GetValues(), true),
109 std::move(pref_store_ptr), backing_pref_store_->GetReadError(),
110 backing_pref_store_->ReadOnly());
94 } 111 }
95 112
96 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) { 113 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) {
97 // All mutations are triggered by a client. Updates are only sent to clients 114 // 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 115 // other than the instigator so if there is only one client, it will ignore
99 // the update. 116 // the update.
100 if (connections_.size() == 1) 117 if (connections_.size() == 1)
101 return; 118 return;
102 119
103 const base::Value* value = nullptr; 120 const base::Value* value = nullptr;
104 backing_pref_store_->GetValue(key, &value); 121 backing_pref_store_->GetValue(key, &value);
105 for (auto& entry : connections_) 122 for (auto& entry : connections_)
106 entry.first->OnPrefValueChanged(key, value); 123 entry.first->OnPrefValueChanged(key, value);
107 } 124 }
108 125
109 void PersistentPrefStoreImpl::OnInitializationCompleted(bool succeeded) { 126 void PersistentPrefStoreImpl::OnInitializationCompleted(bool succeeded) {
110 DCHECK(initializing_); 127 DCHECK(initializing_);
111 initializing_ = false; 128 initializing_ = false;
112 for (const auto& callback : pending_connect_callbacks_) { 129 std::move(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 } 130 }
139 131
140 void PersistentPrefStoreImpl::SetValue(const std::string& key, 132 void PersistentPrefStoreImpl::SetValue(const std::string& key,
141 std::unique_ptr<base::Value> value, 133 std::unique_ptr<base::Value> value,
142 uint32_t flags) { 134 uint32_t flags) {
143 if (value) 135 if (value)
144 backing_pref_store_->SetValue(key, std::move(value), flags); 136 backing_pref_store_->SetValue(key, std::move(value), flags);
145 else 137 else
146 backing_pref_store_->RemoveValue(key, flags); 138 backing_pref_store_->RemoveValue(key, flags);
147 } 139 }
148 140
149 void PersistentPrefStoreImpl::CommitPendingWrite() { 141 void PersistentPrefStoreImpl::CommitPendingWrite() {
150 backing_pref_store_->CommitPendingWrite(); 142 backing_pref_store_->CommitPendingWrite();
151 } 143 }
152 144
153 void PersistentPrefStoreImpl::SchedulePendingLossyWrites() { 145 void PersistentPrefStoreImpl::SchedulePendingLossyWrites() {
154 backing_pref_store_->SchedulePendingLossyWrites(); 146 backing_pref_store_->SchedulePendingLossyWrites();
155 } 147 }
156 148
157 void PersistentPrefStoreImpl::ClearMutableValues() { 149 void PersistentPrefStoreImpl::ClearMutableValues() {
158 backing_pref_store_->ClearMutableValues(); 150 backing_pref_store_->ClearMutableValues();
159 } 151 }
160 152
161 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) { 153 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) {
162 connections_.erase(connection); 154 connections_.erase(connection);
163 } 155 }
164 156
165 } // namespace prefs 157 } // 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