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

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

Issue 2777483002: Pref service: Add initial support for pref registration. (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 ObservedPrefs observed_keys)
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 observed_keys_(std::move(observed_keys)) {
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 (!base::ContainsKey(observed_keys_, key))
tibell 2017/03/26 22:54:47 Optional: merge with previous if?
Sam McNally 2017/03/26 23:02:45 Done.
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
58 // Owns |this|. 64 // Owns |this|.
59 PersistentPrefStoreImpl* pref_store_; 65 PersistentPrefStoreImpl* const pref_store_;
60 66
61 mojo::Binding<mojom::PersistentPrefStore> binding_; 67 mojo::Binding<mojom::PersistentPrefStore> binding_;
62 mojom::PrefStoreObserverPtr observer_; 68 mojom::PrefStoreObserverPtr observer_;
69 const ObservedPrefs observed_keys_;
63 70
64 // If true then a write is in progress and any update notifications should be 71 // If true then a write is in progress and any update notifications should be
65 // ignored, as those updates would originate from ourselves. 72 // ignored, as those updates would originate from ourselves.
66 bool write_in_progress_ = false; 73 bool write_in_progress_ = false;
67 74
68 DISALLOW_COPY_AND_ASSIGN(Connection); 75 DISALLOW_COPY_AND_ASSIGN(Connection);
69 }; 76 };
70 77
71 PersistentPrefStoreImpl::PersistentPrefStoreImpl( 78 PersistentPrefStoreImpl::PersistentPrefStoreImpl(
72 scoped_refptr<PersistentPrefStore> backing_pref_store, 79 scoped_refptr<PersistentPrefStore> backing_pref_store,
73 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate, 80 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate,
74 base::OnceClosure on_initialized) 81 base::OnceClosure on_initialized)
75 : backing_pref_store_(backing_pref_store), 82 : backing_pref_store_(backing_pref_store),
76 validation_delegate_(std::move(validation_delegate)) { 83 validation_delegate_(std::move(validation_delegate)) {
77 backing_pref_store_->AddObserver(this); 84 backing_pref_store_->AddObserver(this);
78 if (!backing_pref_store_->IsInitializationComplete()) { 85 if (!backing_pref_store_->IsInitializationComplete()) {
79 on_initialized_ = std::move(on_initialized); 86 on_initialized_ = std::move(on_initialized);
80 initializing_ = true; 87 initializing_ = true;
81 backing_pref_store_->ReadPrefsAsync(nullptr); 88 backing_pref_store_->ReadPrefsAsync(nullptr);
82 } 89 }
83 } 90 }
84 91
85 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() { 92 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() {
86 backing_pref_store_->RemoveObserver(this); 93 backing_pref_store_->RemoveObserver(this);
87 } 94 }
88 95
89 mojom::PersistentPrefStoreConnectionPtr 96 mojom::PersistentPrefStoreConnectionPtr
90 PersistentPrefStoreImpl::CreateConnection() { 97 PersistentPrefStoreImpl::CreateConnection(ObservedPrefs observed_prefs) {
91 DCHECK(!initializing_); 98 DCHECK(!initializing_);
92 if (!backing_pref_store_->IsInitializationComplete()) { 99 if (!backing_pref_store_->IsInitializationComplete()) {
93 // |backing_pref_store_| initialization failed. 100 // |backing_pref_store_| initialization failed.
94 return mojom::PersistentPrefStoreConnection::New( 101 return mojom::PersistentPrefStoreConnection::New(
95 nullptr, nullptr, backing_pref_store_->GetReadError(), 102 nullptr, nullptr, backing_pref_store_->GetReadError(),
96 backing_pref_store_->ReadOnly()); 103 backing_pref_store_->ReadOnly());
97 } 104 }
98 mojom::PersistentPrefStorePtr pref_store_ptr; 105 mojom::PersistentPrefStorePtr pref_store_ptr;
99 mojom::PrefStoreObserverPtr observer; 106 mojom::PrefStoreObserverPtr observer;
100 mojom::PrefStoreObserverRequest observer_request = 107 mojom::PrefStoreObserverRequest observer_request =
101 mojo::MakeRequest(&observer); 108 mojo::MakeRequest(&observer);
102 auto connection = base::MakeUnique<Connection>( 109 auto connection = base::MakeUnique<Connection>(
103 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer)); 110 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer),
111 std::move(observed_prefs));
104 auto* connection_ptr = connection.get(); 112 auto* connection_ptr = connection.get();
105 connections_.insert(std::make_pair(connection_ptr, std::move(connection))); 113 connections_.insert(std::make_pair(connection_ptr, std::move(connection)));
106 return mojom::PersistentPrefStoreConnection::New( 114 return mojom::PersistentPrefStoreConnection::New(
107 mojom::PrefStoreConnection::New(std::move(observer_request), 115 mojom::PrefStoreConnection::New(std::move(observer_request),
108 backing_pref_store_->GetValues(), true), 116 backing_pref_store_->GetValues(), true),
109 std::move(pref_store_ptr), backing_pref_store_->GetReadError(), 117 std::move(pref_store_ptr), backing_pref_store_->GetReadError(),
110 backing_pref_store_->ReadOnly()); 118 backing_pref_store_->ReadOnly());
111 } 119 }
112 120
113 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) { 121 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 156
149 void PersistentPrefStoreImpl::ClearMutableValues() { 157 void PersistentPrefStoreImpl::ClearMutableValues() {
150 backing_pref_store_->ClearMutableValues(); 158 backing_pref_store_->ClearMutableValues();
151 } 159 }
152 160
153 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) { 161 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) {
154 connections_.erase(connection); 162 connections_.erase(connection);
155 } 163 }
156 164
157 } // namespace prefs 165 } // namespace prefs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698