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

Side by Side Diff: components/user_prefs/tracked/segregated_pref_store.cc

Issue 2782803002: Move tracked prefs into services/preferences/tracked. (Closed)
Patch Set: rebase Created 3 years, 8 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
(Empty)
1 // Copyright 2014 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 "components/user_prefs/tracked/segregated_pref_store.h"
6
7 #include <utility>
8
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "base/values.h"
12
13 SegregatedPrefStore::AggregatingObserver::AggregatingObserver(
14 SegregatedPrefStore* outer)
15 : outer_(outer),
16 failed_sub_initializations_(0),
17 successful_sub_initializations_(0) {
18 }
19
20 void SegregatedPrefStore::AggregatingObserver::OnPrefValueChanged(
21 const std::string& key) {
22 // There is no need to tell clients about changes if they have not yet been
23 // told about initialization.
24 if (failed_sub_initializations_ + successful_sub_initializations_ < 2)
25 return;
26
27 for (auto& observer : outer_->observers_)
28 observer.OnPrefValueChanged(key);
29 }
30
31 void SegregatedPrefStore::AggregatingObserver::OnInitializationCompleted(
32 bool succeeded) {
33 if (succeeded)
34 ++successful_sub_initializations_;
35 else
36 ++failed_sub_initializations_;
37
38 DCHECK_LE(failed_sub_initializations_ + successful_sub_initializations_, 2);
39
40 if (failed_sub_initializations_ + successful_sub_initializations_ == 2) {
41 if (successful_sub_initializations_ == 2 && outer_->read_error_delegate_) {
42 PersistentPrefStore::PrefReadError read_error = outer_->GetReadError();
43 if (read_error != PersistentPrefStore::PREF_READ_ERROR_NONE)
44 outer_->read_error_delegate_->OnError(read_error);
45 }
46
47 for (auto& observer : outer_->observers_)
48 observer.OnInitializationCompleted(successful_sub_initializations_ == 2);
49 }
50 }
51
52 SegregatedPrefStore::SegregatedPrefStore(
53 const scoped_refptr<PersistentPrefStore>& default_pref_store,
54 const scoped_refptr<PersistentPrefStore>& selected_pref_store,
55 const std::set<std::string>& selected_pref_names)
56 : default_pref_store_(default_pref_store),
57 selected_pref_store_(selected_pref_store),
58 selected_preference_names_(selected_pref_names),
59 aggregating_observer_(this) {
60 default_pref_store_->AddObserver(&aggregating_observer_);
61 selected_pref_store_->AddObserver(&aggregating_observer_);
62 }
63
64 void SegregatedPrefStore::AddObserver(Observer* observer) {
65 observers_.AddObserver(observer);
66 }
67
68 void SegregatedPrefStore::RemoveObserver(Observer* observer) {
69 observers_.RemoveObserver(observer);
70 }
71
72 bool SegregatedPrefStore::HasObservers() const {
73 return observers_.might_have_observers();
74 }
75
76 bool SegregatedPrefStore::IsInitializationComplete() const {
77 return default_pref_store_->IsInitializationComplete() &&
78 selected_pref_store_->IsInitializationComplete();
79 }
80
81 bool SegregatedPrefStore::GetValue(const std::string& key,
82 const base::Value** result) const {
83 return StoreForKey(key)->GetValue(key, result);
84 }
85
86 std::unique_ptr<base::DictionaryValue> SegregatedPrefStore::GetValues() const {
87 auto values = default_pref_store_->GetValues();
88 auto selected_pref_store_values = selected_pref_store_->GetValues();
89 for (const auto& key : selected_preference_names_) {
90 const base::Value* value = nullptr;
91 if (selected_pref_store_values->Get(key, &value)) {
92 values->Set(key, value->CreateDeepCopy());
93 } else {
94 values->Remove(key, nullptr);
95 }
96 }
97 return values;
98 }
99
100 void SegregatedPrefStore::SetValue(const std::string& key,
101 std::unique_ptr<base::Value> value,
102 uint32_t flags) {
103 StoreForKey(key)->SetValue(key, std::move(value), flags);
104 }
105
106 void SegregatedPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
107 StoreForKey(key)->RemoveValue(key, flags);
108 }
109
110 bool SegregatedPrefStore::GetMutableValue(const std::string& key,
111 base::Value** result) {
112 return StoreForKey(key)->GetMutableValue(key, result);
113 }
114
115 void SegregatedPrefStore::ReportValueChanged(const std::string& key,
116 uint32_t flags) {
117 StoreForKey(key)->ReportValueChanged(key, flags);
118 }
119
120 void SegregatedPrefStore::SetValueSilently(const std::string& key,
121 std::unique_ptr<base::Value> value,
122 uint32_t flags) {
123 StoreForKey(key)->SetValueSilently(key, std::move(value), flags);
124 }
125
126 bool SegregatedPrefStore::ReadOnly() const {
127 return selected_pref_store_->ReadOnly() || default_pref_store_->ReadOnly();
128 }
129
130 PersistentPrefStore::PrefReadError SegregatedPrefStore::GetReadError() const {
131 PersistentPrefStore::PrefReadError read_error =
132 default_pref_store_->GetReadError();
133 if (read_error == PersistentPrefStore::PREF_READ_ERROR_NONE) {
134 read_error = selected_pref_store_->GetReadError();
135 // Ignore NO_FILE from selected_pref_store_.
136 if (read_error == PersistentPrefStore::PREF_READ_ERROR_NO_FILE)
137 read_error = PersistentPrefStore::PREF_READ_ERROR_NONE;
138 }
139 return read_error;
140 }
141
142 PersistentPrefStore::PrefReadError SegregatedPrefStore::ReadPrefs() {
143 // Note: Both of these stores own PrefFilters which makes ReadPrefs
144 // asynchronous. This is okay in this case as only the first call will be
145 // truly asynchronous, the second call will then unblock the migration in
146 // TrackedPreferencesMigrator and complete synchronously.
147 default_pref_store_->ReadPrefs();
148 PersistentPrefStore::PrefReadError selected_store_read_error =
149 selected_pref_store_->ReadPrefs();
150 DCHECK_NE(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE,
151 selected_store_read_error);
152
153 return GetReadError();
154 }
155
156 void SegregatedPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
157 read_error_delegate_.reset(error_delegate);
158 default_pref_store_->ReadPrefsAsync(NULL);
159 selected_pref_store_->ReadPrefsAsync(NULL);
160 }
161
162 void SegregatedPrefStore::CommitPendingWrite() {
163 default_pref_store_->CommitPendingWrite();
164 selected_pref_store_->CommitPendingWrite();
165 }
166
167 void SegregatedPrefStore::SchedulePendingLossyWrites() {
168 default_pref_store_->SchedulePendingLossyWrites();
169 selected_pref_store_->SchedulePendingLossyWrites();
170 }
171
172 void SegregatedPrefStore::ClearMutableValues() {
173 NOTIMPLEMENTED();
174 }
175
176 SegregatedPrefStore::~SegregatedPrefStore() {
177 default_pref_store_->RemoveObserver(&aggregating_observer_);
178 selected_pref_store_->RemoveObserver(&aggregating_observer_);
179 }
180
181 PersistentPrefStore* SegregatedPrefStore::StoreForKey(const std::string& key) {
182 return (base::ContainsKey(selected_preference_names_, key)
183 ? selected_pref_store_
184 : default_pref_store_)
185 .get();
186 }
187
188 const PersistentPrefStore* SegregatedPrefStore::StoreForKey(
189 const std::string& key) const {
190 return (base::ContainsKey(selected_preference_names_, key)
191 ? selected_pref_store_
192 : default_pref_store_)
193 .get();
194 }
OLDNEW
« no previous file with comments | « components/user_prefs/tracked/segregated_pref_store.h ('k') | components/user_prefs/tracked/segregated_pref_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698