OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/preferences/user_prefs.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/auto_reset.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/values.h" | |
12 #include "components/prefs/persistent_pref_store.h" | |
13 #include "mojo/public/cpp/bindings/binding.h" | |
14 | |
15 namespace prefs { | |
16 | |
17 class UserPrefs::Connection : public mojom::PersistentPrefStore { | |
18 public: | |
19 Connection(UserPrefs* user_prefs, | |
20 mojom::PersistentPrefStoreRequest request, | |
21 mojom::PrefStoreObserverPtr observer) | |
22 : user_prefs_(user_prefs), | |
23 binding_(this, std::move(request)), | |
24 observer_(std::move(observer)) { | |
25 auto error_callback = base::Bind(&UserPrefs::Connection::OnConnectionError, | |
26 base::Unretained(this)); | |
27 binding_.set_connection_error_handler(error_callback); | |
28 observer_.set_connection_error_handler(error_callback); | |
29 } | |
30 | |
31 ~Connection() override = default; | |
32 | |
33 void OnPrefValueChanged(const std::string& key, const base::Value* value) { | |
34 if (call_in_progress_) | |
35 return; | |
36 | |
37 observer_->OnPrefChanged(key, value ? value->CreateDeepCopy() : nullptr); | |
38 } | |
39 | |
40 private: | |
41 // mojom::PersistentPrefStore: | |
42 void SetValue(const std::string& key, | |
43 std::unique_ptr<base::Value> value, | |
44 uint32_t flags) override { | |
45 base::AutoReset<bool> scoped_call_in_progress(&call_in_progress_, true); | |
46 user_prefs_->SetValue(key, std::move(value), flags); | |
47 } | |
48 | |
49 void CommitPendingWrite() override { user_prefs_->CommitPendingWrite(); } | |
50 void SchedulePendingLossyWrites() override { | |
51 user_prefs_->SchedulePendingLossyWrites(); | |
52 } | |
53 void ClearMutableValues() override { user_prefs_->ClearMutableValues(); } | |
54 | |
55 void OnConnectionError() { user_prefs_->OnConnectionError(this); } | |
56 | |
57 // Owns |this|. | |
58 UserPrefs* user_prefs_; | |
59 | |
60 mojo::Binding<mojom::PersistentPrefStore> binding_; | |
61 mojom::PrefStoreObserverPtr observer_; | |
62 | |
63 bool call_in_progress_ = false; | |
tibell
2017/03/10 02:18:27
Is this used to prohibit some kind of feedback loo
Sam McNally
2017/03/10 02:43:25
Done.
| |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(Connection); | |
66 }; | |
67 | |
68 UserPrefs::UserPrefs( | |
69 scoped_refptr<PersistentPrefStore> backing_pref_store, | |
70 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate) | |
71 : backing_pref_store_(backing_pref_store), | |
72 validation_delegate_(std::move(validation_delegate)) { | |
73 backing_pref_store_->AddObserver(this); | |
74 if (!backing_pref_store_->IsInitializationComplete()) { | |
75 initializing_ = true; | |
76 backing_pref_store_->ReadPrefsAsync(nullptr); | |
77 } | |
78 } | |
79 | |
80 UserPrefs::~UserPrefs() { | |
81 backing_pref_store_->RemoveObserver(this); | |
82 } | |
83 | |
84 // mojom::PersistentPrefStoreConnector override: | |
85 void UserPrefs::Connect(const ConnectCallback& callback) { | |
86 if (initializing_) { | |
87 pending_connect_callbacks_.push_back(callback); | |
88 return; | |
89 } | |
90 CallConnectCallback(callback); | |
91 } | |
92 | |
93 void UserPrefs::OnPrefValueChanged(const std::string& key) { | |
94 // All mutations are triggered by a client. Updates are only sent to clients | |
95 // other than the instigator. | |
96 if (connections_.size() == 1) | |
97 return; | |
98 | |
99 const base::Value* value = nullptr; | |
100 backing_pref_store_->GetValue(key, &value); | |
101 for (auto& entry : connections_) | |
102 entry.first->OnPrefValueChanged(key, value); | |
103 } | |
104 | |
105 void UserPrefs::OnInitializationCompleted(bool succeeded) { | |
106 DCHECK(initializing_); | |
107 initializing_ = false; | |
108 for (const auto& callback : pending_connect_callbacks_) { | |
109 CallConnectCallback(callback); | |
110 } | |
111 pending_connect_callbacks_.clear(); | |
112 } | |
113 | |
114 void UserPrefs::CallConnectCallback( | |
115 const mojom::PersistentPrefStoreConnector::ConnectCallback& callback) { | |
116 DCHECK(!initializing_); | |
117 if (!backing_pref_store_->IsInitializationComplete()) { | |
118 callback.Run(backing_pref_store_->GetReadError(), | |
119 backing_pref_store_->ReadOnly(), nullptr, nullptr, nullptr); | |
120 return; | |
121 } | |
122 mojom::PersistentPrefStorePtr pref_store_ptr; | |
123 mojom::PrefStoreObserverPtr observer; | |
124 mojom::PrefStoreObserverRequest observer_request = | |
125 mojo::MakeRequest(&observer); | |
126 auto connection = base::MakeUnique<Connection>( | |
127 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer)); | |
128 auto* connection_ptr = connection.get(); | |
129 connections_.insert(std::make_pair(connection_ptr, std::move(connection))); | |
130 callback.Run(backing_pref_store_->GetReadError(), | |
131 backing_pref_store_->ReadOnly(), | |
132 backing_pref_store_->GetValues(), std::move(pref_store_ptr), | |
133 std::move(observer_request)); | |
134 } | |
135 | |
136 void UserPrefs::SetValue(const std::string& key, | |
137 std::unique_ptr<base::Value> value, | |
138 uint32_t flags) { | |
139 if (value) | |
140 backing_pref_store_->SetValue(key, std::move(value), flags); | |
141 else | |
142 backing_pref_store_->RemoveValue(key, flags); | |
143 } | |
144 | |
145 void UserPrefs::CommitPendingWrite() { | |
146 backing_pref_store_->CommitPendingWrite(); | |
147 } | |
148 | |
149 void UserPrefs::SchedulePendingLossyWrites() { | |
150 backing_pref_store_->SchedulePendingLossyWrites(); | |
151 } | |
152 | |
153 void UserPrefs::ClearMutableValues() { | |
154 backing_pref_store_->ClearMutableValues(); | |
155 } | |
156 | |
157 void UserPrefs::OnConnectionError(Connection* connection) { | |
158 connections_.erase(connection); | |
159 } | |
160 | |
161 } // namespace prefs | |
OLD | NEW |