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

Side by Side Diff: base/prefs/public/pref_member.cc

Issue 12789006: base/prefs: Remove the public/ directory and move the files to //base/prefs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 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 | Annotate | Revision Log
« no previous file with comments | « base/prefs/public/pref_member.h ('k') | base/prefs/public/pref_member_unittest.cc » ('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 (c) 2012 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 "base/prefs/public/pref_member.h"
6
7 #include "base/bind_helpers.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/value_conversions.h"
12
13 using base::MessageLoopProxy;
14
15 namespace subtle {
16
17 PrefMemberBase::PrefMemberBase()
18 : prefs_(NULL),
19 setting_value_(false) {
20 }
21
22 PrefMemberBase::~PrefMemberBase() {
23 Destroy();
24 }
25
26 void PrefMemberBase::Init(const char* pref_name,
27 PrefService* prefs,
28 const NamedChangeCallback& observer) {
29 observer_ = observer;
30 Init(pref_name, prefs);
31 }
32
33 void PrefMemberBase::Init(const char* pref_name,
34 PrefService* prefs) {
35 DCHECK(pref_name);
36 DCHECK(prefs);
37 DCHECK(pref_name_.empty()); // Check that Init is only called once.
38 prefs_ = prefs;
39 pref_name_ = pref_name;
40 // Check that the preference is registered.
41 DCHECK(prefs_->FindPreference(pref_name_.c_str()))
42 << pref_name << " not registered.";
43
44 // Add ourselves as a pref observer so we can keep our local value in sync.
45 prefs_->AddPrefObserver(pref_name, this);
46 }
47
48 void PrefMemberBase::Destroy() {
49 if (prefs_ && !pref_name_.empty()) {
50 prefs_->RemovePrefObserver(pref_name_.c_str(), this);
51 prefs_ = NULL;
52 }
53 }
54
55 void PrefMemberBase::MoveToThread(
56 const scoped_refptr<MessageLoopProxy>& message_loop) {
57 VerifyValuePrefName();
58 // Load the value from preferences if it hasn't been loaded so far.
59 if (!internal())
60 UpdateValueFromPref(base::Closure());
61 internal()->MoveToThread(message_loop);
62 }
63
64 void PrefMemberBase::OnPreferenceChanged(PrefService* service,
65 const std::string& pref_name) {
66 VerifyValuePrefName();
67 UpdateValueFromPref((!setting_value_ && !observer_.is_null()) ?
68 base::Bind(observer_, pref_name) : base::Closure());
69 }
70
71 void PrefMemberBase::UpdateValueFromPref(const base::Closure& callback) const {
72 VerifyValuePrefName();
73 const PrefService::Preference* pref =
74 prefs_->FindPreference(pref_name_.c_str());
75 DCHECK(pref);
76 if (!internal())
77 CreateInternal();
78 internal()->UpdateValue(pref->GetValue()->DeepCopy(),
79 pref->IsManaged(),
80 pref->IsUserModifiable(),
81 callback);
82 }
83
84 void PrefMemberBase::VerifyPref() const {
85 VerifyValuePrefName();
86 if (!internal())
87 UpdateValueFromPref(base::Closure());
88 }
89
90 void PrefMemberBase::InvokeUnnamedCallback(const base::Closure& callback,
91 const std::string& pref_name) {
92 callback.Run();
93 }
94
95 PrefMemberBase::Internal::Internal()
96 : thread_loop_(MessageLoopProxy::current()),
97 is_managed_(false) {
98 }
99 PrefMemberBase::Internal::~Internal() { }
100
101 bool PrefMemberBase::Internal::IsOnCorrectThread() const {
102 // In unit tests, there may not be a message loop.
103 return thread_loop_ == NULL || thread_loop_->BelongsToCurrentThread();
104 }
105
106 void PrefMemberBase::Internal::UpdateValue(
107 base::Value* v,
108 bool is_managed,
109 bool is_user_modifiable,
110 const base::Closure& callback) const {
111 scoped_ptr<base::Value> value(v);
112 base::ScopedClosureRunner closure_runner(callback);
113 if (IsOnCorrectThread()) {
114 bool rv = UpdateValueInternal(*value);
115 DCHECK(rv);
116 is_managed_ = is_managed;
117 is_user_modifiable_ = is_user_modifiable;
118 } else {
119 bool may_run = thread_loop_->PostTask(
120 FROM_HERE,
121 base::Bind(&PrefMemberBase::Internal::UpdateValue, this,
122 value.release(), is_managed, is_user_modifiable,
123 closure_runner.Release()));
124 DCHECK(may_run);
125 }
126 }
127
128 void PrefMemberBase::Internal::MoveToThread(
129 const scoped_refptr<MessageLoopProxy>& message_loop) {
130 CheckOnCorrectThread();
131 thread_loop_ = message_loop;
132 }
133
134 bool PrefMemberVectorStringUpdate(const base::Value& value,
135 std::vector<std::string>* string_vector) {
136 if (!value.IsType(base::Value::TYPE_LIST))
137 return false;
138 const ListValue* list = static_cast<const ListValue*>(&value);
139
140 std::vector<std::string> local_vector;
141 for (ListValue::const_iterator it = list->begin(); it != list->end(); ++it) {
142 std::string string_value;
143 if (!(*it)->GetAsString(&string_value))
144 return false;
145
146 local_vector.push_back(string_value);
147 }
148
149 string_vector->swap(local_vector);
150 return true;
151 }
152
153 } // namespace subtle
154
155 template <>
156 void PrefMember<bool>::UpdatePref(const bool& value) {
157 prefs()->SetBoolean(pref_name().c_str(), value);
158 }
159
160 template <>
161 bool PrefMember<bool>::Internal::UpdateValueInternal(
162 const base::Value& value) const {
163 return value.GetAsBoolean(&value_);
164 }
165
166 template <>
167 void PrefMember<int>::UpdatePref(const int& value) {
168 prefs()->SetInteger(pref_name().c_str(), value);
169 }
170
171 template <>
172 bool PrefMember<int>::Internal::UpdateValueInternal(
173 const base::Value& value) const {
174 return value.GetAsInteger(&value_);
175 }
176
177 template <>
178 void PrefMember<double>::UpdatePref(const double& value) {
179 prefs()->SetDouble(pref_name().c_str(), value);
180 }
181
182 template <>
183 bool PrefMember<double>::Internal::UpdateValueInternal(const base::Value& value)
184 const {
185 return value.GetAsDouble(&value_);
186 }
187
188 template <>
189 void PrefMember<std::string>::UpdatePref(const std::string& value) {
190 prefs()->SetString(pref_name().c_str(), value);
191 }
192
193 template <>
194 bool PrefMember<std::string>::Internal::UpdateValueInternal(
195 const base::Value& value)
196 const {
197 return value.GetAsString(&value_);
198 }
199
200 template <>
201 void PrefMember<base::FilePath>::UpdatePref(const base::FilePath& value) {
202 prefs()->SetFilePath(pref_name().c_str(), value);
203 }
204
205 template <>
206 bool PrefMember<base::FilePath>::Internal::UpdateValueInternal(
207 const base::Value& value)
208 const {
209 return base::GetValueAsFilePath(value, &value_);
210 }
211
212 template <>
213 void PrefMember<std::vector<std::string> >::UpdatePref(
214 const std::vector<std::string>& value) {
215 ListValue list_value;
216 list_value.AppendStrings(value);
217 prefs()->Set(pref_name().c_str(), list_value);
218 }
219
220 template <>
221 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal(
222 const base::Value& value) const {
223 return subtle::PrefMemberVectorStringUpdate(value, &value_);
224 }
OLDNEW
« no previous file with comments | « base/prefs/public/pref_member.h ('k') | base/prefs/public/pref_member_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698