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

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

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

Powered by Google App Engine
This is Rietveld 408576698