Index: services/preferences/persistent_pref_store_impl.cc |
diff --git a/services/preferences/persistent_pref_store_impl.cc b/services/preferences/persistent_pref_store_impl.cc |
index 65e2444a3790a7ed9e796575e8491e0fa3f659c6..275f8a0b2260d8cbe3038993343acbf783a27fb7 100644 |
--- a/services/preferences/persistent_pref_store_impl.cc |
+++ b/services/preferences/persistent_pref_store_impl.cc |
@@ -8,6 +8,7 @@ |
#include "base/auto_reset.h" |
#include "base/memory/ptr_util.h" |
+#include "base/stl_util.h" |
#include "base/values.h" |
#include "components/prefs/persistent_pref_store.h" |
#include "mojo/public/cpp/bindings/binding.h" |
@@ -18,10 +19,12 @@ class PersistentPrefStoreImpl::Connection : public mojom::PersistentPrefStore { |
public: |
Connection(PersistentPrefStoreImpl* pref_store, |
mojom::PersistentPrefStoreRequest request, |
- mojom::PrefStoreObserverPtr observer) |
+ mojom::PrefStoreObserverPtr observer, |
+ mojom::PrefRegistryPtr pref_registry) |
: pref_store_(pref_store), |
binding_(this, std::move(request)), |
- observer_(std::move(observer)) { |
+ observer_(std::move(observer)), |
+ pref_registry_(std::move(pref_registry)) { |
auto error_callback = |
base::Bind(&PersistentPrefStoreImpl::Connection::OnConnectionError, |
base::Unretained(this)); |
@@ -35,6 +38,9 @@ class PersistentPrefStoreImpl::Connection : public mojom::PersistentPrefStore { |
if (write_in_progress_) |
return; |
+ if (!IsKeyObserved(key)) |
+ return; |
+ |
observer_->OnPrefChanged(key, value ? value->CreateDeepCopy() : nullptr); |
} |
@@ -55,11 +61,16 @@ class PersistentPrefStoreImpl::Connection : public mojom::PersistentPrefStore { |
void OnConnectionError() { pref_store_->OnConnectionError(this); } |
+ bool IsKeyObserved(const std::string& key) { |
+ return base::ContainsKey(pref_registry_->registrations, key); |
+ } |
+ |
// Owns |this|. |
PersistentPrefStoreImpl* pref_store_; |
mojo::Binding<mojom::PersistentPrefStore> binding_; |
mojom::PrefStoreObserverPtr observer_; |
+ mojom::PrefRegistryPtr pref_registry_; |
// If true then a write is in progress and any update notifications should be |
// ignored, as those updates would originate from ourselves. |
@@ -70,11 +81,13 @@ class PersistentPrefStoreImpl::Connection : public mojom::PersistentPrefStore { |
PersistentPrefStoreImpl::PersistentPrefStoreImpl( |
scoped_refptr<PersistentPrefStore> backing_pref_store, |
- mojom::TrackedPreferenceValidationDelegatePtr validation_delegate) |
+ mojom::TrackedPreferenceValidationDelegatePtr validation_delegate, |
+ base::Closure on_initialized) |
: backing_pref_store_(backing_pref_store), |
validation_delegate_(std::move(validation_delegate)) { |
backing_pref_store_->AddObserver(this); |
if (!backing_pref_store_->IsInitializationComplete()) { |
+ on_initialized_ = std::move(on_initialized); |
initializing_ = true; |
backing_pref_store_->ReadPrefsAsync(nullptr); |
} |
@@ -85,12 +98,28 @@ PersistentPrefStoreImpl::~PersistentPrefStoreImpl() { |
} |
// mojom::PersistentPrefStoreConnector override: |
-void PersistentPrefStoreImpl::Connect(const ConnectCallback& callback) { |
- if (initializing_) { |
- pending_connect_callbacks_.push_back(callback); |
- return; |
+mojom::PersistentPrefStoreConnectionPtr PersistentPrefStoreImpl::Connect( |
+ mojom::PrefRegistryPtr pref_registry) { |
+ DCHECK(!initializing_); |
+ if (!backing_pref_store_->IsInitializationComplete()) { |
+ return mojom::PersistentPrefStoreConnection::New( |
+ nullptr, nullptr, backing_pref_store_->GetReadError(), |
+ backing_pref_store_->ReadOnly()); |
} |
- CallConnectCallback(callback); |
+ mojom::PersistentPrefStorePtr pref_store_ptr; |
+ mojom::PrefStoreObserverPtr observer; |
+ mojom::PrefStoreObserverRequest observer_request = |
+ mojo::MakeRequest(&observer); |
+ auto connection = base::MakeUnique<Connection>( |
+ this, mojo::MakeRequest(&pref_store_ptr), std::move(observer), |
+ std::move(pref_registry)); |
+ auto* connection_ptr = connection.get(); |
+ connections_.insert(std::make_pair(connection_ptr, std::move(connection))); |
+ return mojom::PersistentPrefStoreConnection::New( |
+ mojom::PrefStoreConnection::New(std::move(observer_request), |
+ backing_pref_store_->GetValues(), true), |
+ std::move(pref_store_ptr), backing_pref_store_->GetReadError(), |
+ backing_pref_store_->ReadOnly()); |
} |
void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) { |
@@ -109,32 +138,7 @@ void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) { |
void PersistentPrefStoreImpl::OnInitializationCompleted(bool succeeded) { |
DCHECK(initializing_); |
initializing_ = false; |
- for (const auto& callback : pending_connect_callbacks_) { |
- CallConnectCallback(callback); |
- } |
- pending_connect_callbacks_.clear(); |
-} |
- |
-void PersistentPrefStoreImpl::CallConnectCallback( |
- const mojom::PersistentPrefStoreConnector::ConnectCallback& callback) { |
- DCHECK(!initializing_); |
- if (!backing_pref_store_->IsInitializationComplete()) { |
- callback.Run(backing_pref_store_->GetReadError(), |
- backing_pref_store_->ReadOnly(), nullptr, nullptr, nullptr); |
- return; |
- } |
- mojom::PersistentPrefStorePtr pref_store_ptr; |
- mojom::PrefStoreObserverPtr observer; |
- mojom::PrefStoreObserverRequest observer_request = |
- mojo::MakeRequest(&observer); |
- auto connection = base::MakeUnique<Connection>( |
- this, mojo::MakeRequest(&pref_store_ptr), std::move(observer)); |
- auto* connection_ptr = connection.get(); |
- connections_.insert(std::make_pair(connection_ptr, std::move(connection))); |
- callback.Run(backing_pref_store_->GetReadError(), |
- backing_pref_store_->ReadOnly(), |
- backing_pref_store_->GetValues(), std::move(pref_store_ptr), |
- std::move(observer_request)); |
+ base::ResetAndReturn(&on_initialized_).Run(); |
} |
void PersistentPrefStoreImpl::SetValue(const std::string& key, |