OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/prefs/pref_service.h" | 5 #include "base/prefs/pref_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 user_pref_store_.get(), | 79 user_pref_store_.get(), |
80 new ReadErrorHandler(read_error_callback_))); | 80 new ReadErrorHandler(read_error_callback_))); |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 void PrefService::CommitPendingWrite() { | 84 void PrefService::CommitPendingWrite() { |
85 DCHECK(CalledOnValidThread()); | 85 DCHECK(CalledOnValidThread()); |
86 user_pref_store_->CommitPendingWrite(); | 86 user_pref_store_->CommitPendingWrite(); |
87 } | 87 } |
88 | 88 |
89 bool PrefService::GetBoolean(const char* path) const { | 89 bool PrefService::GetBoolean(const std::string& path) const { |
90 DCHECK(CalledOnValidThread()); | 90 DCHECK(CalledOnValidThread()); |
91 | 91 |
92 bool result = false; | 92 bool result = false; |
93 | 93 |
94 const base::Value* value = GetPreferenceValue(path); | 94 const base::Value* value = GetPreferenceValue(path); |
95 if (!value) { | 95 if (!value) { |
96 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 96 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
97 return result; | 97 return result; |
98 } | 98 } |
99 bool rv = value->GetAsBoolean(&result); | 99 bool rv = value->GetAsBoolean(&result); |
100 DCHECK(rv); | 100 DCHECK(rv); |
101 return result; | 101 return result; |
102 } | 102 } |
103 | 103 |
104 int PrefService::GetInteger(const char* path) const { | 104 int PrefService::GetInteger(const std::string& path) const { |
105 DCHECK(CalledOnValidThread()); | 105 DCHECK(CalledOnValidThread()); |
106 | 106 |
107 int result = 0; | 107 int result = 0; |
108 | 108 |
109 const base::Value* value = GetPreferenceValue(path); | 109 const base::Value* value = GetPreferenceValue(path); |
110 if (!value) { | 110 if (!value) { |
111 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 111 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
112 return result; | 112 return result; |
113 } | 113 } |
114 bool rv = value->GetAsInteger(&result); | 114 bool rv = value->GetAsInteger(&result); |
115 DCHECK(rv); | 115 DCHECK(rv); |
116 return result; | 116 return result; |
117 } | 117 } |
118 | 118 |
119 double PrefService::GetDouble(const char* path) const { | 119 double PrefService::GetDouble(const std::string& path) const { |
120 DCHECK(CalledOnValidThread()); | 120 DCHECK(CalledOnValidThread()); |
121 | 121 |
122 double result = 0.0; | 122 double result = 0.0; |
123 | 123 |
124 const base::Value* value = GetPreferenceValue(path); | 124 const base::Value* value = GetPreferenceValue(path); |
125 if (!value) { | 125 if (!value) { |
126 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 126 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
127 return result; | 127 return result; |
128 } | 128 } |
129 bool rv = value->GetAsDouble(&result); | 129 bool rv = value->GetAsDouble(&result); |
130 DCHECK(rv); | 130 DCHECK(rv); |
131 return result; | 131 return result; |
132 } | 132 } |
133 | 133 |
134 std::string PrefService::GetString(const char* path) const { | 134 std::string PrefService::GetString(const std::string& path) const { |
135 DCHECK(CalledOnValidThread()); | 135 DCHECK(CalledOnValidThread()); |
136 | 136 |
137 std::string result; | 137 std::string result; |
138 | 138 |
139 const base::Value* value = GetPreferenceValue(path); | 139 const base::Value* value = GetPreferenceValue(path); |
140 if (!value) { | 140 if (!value) { |
141 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 141 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
142 return result; | 142 return result; |
143 } | 143 } |
144 bool rv = value->GetAsString(&result); | 144 bool rv = value->GetAsString(&result); |
145 DCHECK(rv); | 145 DCHECK(rv); |
146 return result; | 146 return result; |
147 } | 147 } |
148 | 148 |
149 base::FilePath PrefService::GetFilePath(const char* path) const { | 149 base::FilePath PrefService::GetFilePath(const std::string& path) const { |
150 DCHECK(CalledOnValidThread()); | 150 DCHECK(CalledOnValidThread()); |
151 | 151 |
152 base::FilePath result; | 152 base::FilePath result; |
153 | 153 |
154 const base::Value* value = GetPreferenceValue(path); | 154 const base::Value* value = GetPreferenceValue(path); |
155 if (!value) { | 155 if (!value) { |
156 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 156 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
157 return base::FilePath(result); | 157 return base::FilePath(result); |
158 } | 158 } |
159 bool rv = base::GetValueAsFilePath(*value, &result); | 159 bool rv = base::GetValueAsFilePath(*value, &result); |
160 DCHECK(rv); | 160 DCHECK(rv); |
161 return result; | 161 return result; |
162 } | 162 } |
163 | 163 |
164 bool PrefService::HasPrefPath(const char* path) const { | 164 bool PrefService::HasPrefPath(const std::string& path) const { |
165 const Preference* pref = FindPreference(path); | 165 const Preference* pref = FindPreference(path); |
166 return pref && !pref->IsDefaultValue(); | 166 return pref && !pref->IsDefaultValue(); |
167 } | 167 } |
168 | 168 |
169 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValues() const { | 169 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValues() const { |
170 DCHECK(CalledOnValidThread()); | 170 DCHECK(CalledOnValidThread()); |
171 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 171 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
172 for (const auto& it : *pref_registry_) { | 172 for (const auto& it : *pref_registry_) { |
173 const base::Value* value = GetPreferenceValue(it.first); | 173 const base::Value* value = GetPreferenceValue(it.first); |
174 out->Set(it.first, value->DeepCopy()); | 174 out->Set(it.first, value->DeepCopy()); |
175 } | 175 } |
176 return out.Pass(); | 176 return out.Pass(); |
177 } | 177 } |
178 | 178 |
179 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValuesOmitDefaults() | 179 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValuesOmitDefaults() |
180 const { | 180 const { |
181 DCHECK(CalledOnValidThread()); | 181 DCHECK(CalledOnValidThread()); |
182 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 182 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
183 for (const auto& it : *pref_registry_) { | 183 for (const auto& it : *pref_registry_) { |
184 const Preference* pref = FindPreference(it.first.c_str()); | 184 const Preference* pref = FindPreference(it.first); |
185 if (pref->IsDefaultValue()) | 185 if (pref->IsDefaultValue()) |
186 continue; | 186 continue; |
187 out->Set(it.first, pref->GetValue()->DeepCopy()); | 187 out->Set(it.first, pref->GetValue()->DeepCopy()); |
188 } | 188 } |
189 return out.Pass(); | 189 return out.Pass(); |
190 } | 190 } |
191 | 191 |
192 scoped_ptr<base::DictionaryValue> | 192 scoped_ptr<base::DictionaryValue> |
193 PrefService::GetPreferenceValuesWithoutPathExpansion() const { | 193 PrefService::GetPreferenceValuesWithoutPathExpansion() const { |
194 DCHECK(CalledOnValidThread()); | 194 DCHECK(CalledOnValidThread()); |
195 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 195 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
196 for (const auto& it : *pref_registry_) { | 196 for (const auto& it : *pref_registry_) { |
197 const base::Value* value = GetPreferenceValue(it.first); | 197 const base::Value* value = GetPreferenceValue(it.first); |
198 DCHECK(value); | 198 DCHECK(value); |
199 out->SetWithoutPathExpansion(it.first, value->DeepCopy()); | 199 out->SetWithoutPathExpansion(it.first, value->DeepCopy()); |
200 } | 200 } |
201 return out.Pass(); | 201 return out.Pass(); |
202 } | 202 } |
203 | 203 |
204 const PrefService::Preference* PrefService::FindPreference( | 204 const PrefService::Preference* PrefService::FindPreference( |
205 const char* pref_name) const { | 205 const std::string& pref_name) const { |
206 DCHECK(CalledOnValidThread()); | 206 DCHECK(CalledOnValidThread()); |
207 PreferenceMap::iterator it = prefs_map_.find(pref_name); | 207 PreferenceMap::iterator it = prefs_map_.find(pref_name); |
208 if (it != prefs_map_.end()) | 208 if (it != prefs_map_.end()) |
209 return &(it->second); | 209 return &(it->second); |
210 const base::Value* default_value = NULL; | 210 const base::Value* default_value = NULL; |
211 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) | 211 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) |
212 return NULL; | 212 return NULL; |
213 it = prefs_map_.insert( | 213 it = prefs_map_.insert( |
214 std::make_pair(pref_name, Preference( | 214 std::make_pair(pref_name, Preference( |
215 this, pref_name, default_value->GetType()))).first; | 215 this, pref_name, default_value->GetType()))).first; |
(...skipping 12 matching lines...) Expand all Loading... |
228 switch (user_pref_store_->GetReadError()) { | 228 switch (user_pref_store_->GetReadError()) { |
229 case PersistentPrefStore::PREF_READ_ERROR_NONE: | 229 case PersistentPrefStore::PREF_READ_ERROR_NONE: |
230 return INITIALIZATION_STATUS_SUCCESS; | 230 return INITIALIZATION_STATUS_SUCCESS; |
231 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE: | 231 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE: |
232 return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE; | 232 return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE; |
233 default: | 233 default: |
234 return INITIALIZATION_STATUS_ERROR; | 234 return INITIALIZATION_STATUS_ERROR; |
235 } | 235 } |
236 } | 236 } |
237 | 237 |
238 bool PrefService::IsManagedPreference(const char* pref_name) const { | 238 bool PrefService::IsManagedPreference(const std::string& pref_name) const { |
239 const Preference* pref = FindPreference(pref_name); | 239 const Preference* pref = FindPreference(pref_name); |
240 return pref && pref->IsManaged(); | 240 return pref && pref->IsManaged(); |
241 } | 241 } |
242 | 242 |
243 bool PrefService::IsUserModifiablePreference(const char* pref_name) const { | 243 bool PrefService::IsUserModifiablePreference( |
| 244 const std::string& pref_name) const { |
244 const Preference* pref = FindPreference(pref_name); | 245 const Preference* pref = FindPreference(pref_name); |
245 return pref && pref->IsUserModifiable(); | 246 return pref && pref->IsUserModifiable(); |
246 } | 247 } |
247 | 248 |
248 const base::DictionaryValue* PrefService::GetDictionary( | 249 const base::DictionaryValue* PrefService::GetDictionary( |
249 const char* path) const { | 250 const std::string& path) const { |
250 DCHECK(CalledOnValidThread()); | 251 DCHECK(CalledOnValidThread()); |
251 | 252 |
252 const base::Value* value = GetPreferenceValue(path); | 253 const base::Value* value = GetPreferenceValue(path); |
253 if (!value) { | 254 if (!value) { |
254 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 255 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
255 return NULL; | 256 return NULL; |
256 } | 257 } |
257 if (value->GetType() != base::Value::TYPE_DICTIONARY) { | 258 if (value->GetType() != base::Value::TYPE_DICTIONARY) { |
258 NOTREACHED(); | 259 NOTREACHED(); |
259 return NULL; | 260 return NULL; |
260 } | 261 } |
261 return static_cast<const base::DictionaryValue*>(value); | 262 return static_cast<const base::DictionaryValue*>(value); |
262 } | 263 } |
263 | 264 |
264 const base::Value* PrefService::GetUserPrefValue(const char* path) const { | 265 const base::Value* PrefService::GetUserPrefValue( |
| 266 const std::string& path) const { |
265 DCHECK(CalledOnValidThread()); | 267 DCHECK(CalledOnValidThread()); |
266 | 268 |
267 const Preference* pref = FindPreference(path); | 269 const Preference* pref = FindPreference(path); |
268 if (!pref) { | 270 if (!pref) { |
269 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 271 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
270 return NULL; | 272 return NULL; |
271 } | 273 } |
272 | 274 |
273 // Look for an existing preference in the user store. If it doesn't | 275 // Look for an existing preference in the user store. If it doesn't |
274 // exist, return NULL. | 276 // exist, return NULL. |
275 base::Value* value = NULL; | 277 base::Value* value = NULL; |
276 if (!user_pref_store_->GetMutableValue(path, &value)) | 278 if (!user_pref_store_->GetMutableValue(path, &value)) |
277 return NULL; | 279 return NULL; |
278 | 280 |
279 if (!value->IsType(pref->GetType())) { | 281 if (!value->IsType(pref->GetType())) { |
280 NOTREACHED() << "Pref value type doesn't match registered type."; | 282 NOTREACHED() << "Pref value type doesn't match registered type."; |
281 return NULL; | 283 return NULL; |
282 } | 284 } |
283 | 285 |
284 return value; | 286 return value; |
285 } | 287 } |
286 | 288 |
287 void PrefService::SetDefaultPrefValue(const char* path, | 289 void PrefService::SetDefaultPrefValue(const std::string& path, |
288 base::Value* value) { | 290 base::Value* value) { |
289 DCHECK(CalledOnValidThread()); | 291 DCHECK(CalledOnValidThread()); |
290 pref_registry_->SetDefaultPrefValue(path, value); | 292 pref_registry_->SetDefaultPrefValue(path, value); |
291 } | 293 } |
292 | 294 |
293 const base::Value* PrefService::GetDefaultPrefValue(const char* path) const { | 295 const base::Value* PrefService::GetDefaultPrefValue( |
| 296 const std::string& path) const { |
294 DCHECK(CalledOnValidThread()); | 297 DCHECK(CalledOnValidThread()); |
295 // Lookup the preference in the default store. | 298 // Lookup the preference in the default store. |
296 const base::Value* value = NULL; | 299 const base::Value* value = NULL; |
297 if (!pref_registry_->defaults()->GetValue(path, &value)) { | 300 if (!pref_registry_->defaults()->GetValue(path, &value)) { |
298 NOTREACHED() << "Default value missing for pref: " << path; | 301 NOTREACHED() << "Default value missing for pref: " << path; |
299 return NULL; | 302 return NULL; |
300 } | 303 } |
301 return value; | 304 return value; |
302 } | 305 } |
303 | 306 |
304 const base::ListValue* PrefService::GetList(const char* path) const { | 307 const base::ListValue* PrefService::GetList(const std::string& path) const { |
305 DCHECK(CalledOnValidThread()); | 308 DCHECK(CalledOnValidThread()); |
306 | 309 |
307 const base::Value* value = GetPreferenceValue(path); | 310 const base::Value* value = GetPreferenceValue(path); |
308 if (!value) { | 311 if (!value) { |
309 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 312 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
310 return NULL; | 313 return NULL; |
311 } | 314 } |
312 if (value->GetType() != base::Value::TYPE_LIST) { | 315 if (value->GetType() != base::Value::TYPE_LIST) { |
313 NOTREACHED(); | 316 NOTREACHED(); |
314 return NULL; | 317 return NULL; |
315 } | 318 } |
316 return static_cast<const base::ListValue*>(value); | 319 return static_cast<const base::ListValue*>(value); |
317 } | 320 } |
318 | 321 |
319 void PrefService::AddPrefObserver(const char* path, PrefObserver* obs) { | 322 void PrefService::AddPrefObserver(const std::string& path, PrefObserver* obs) { |
320 pref_notifier_->AddPrefObserver(path, obs); | 323 pref_notifier_->AddPrefObserver(path, obs); |
321 } | 324 } |
322 | 325 |
323 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { | 326 void PrefService::RemovePrefObserver(const std::string& path, |
| 327 PrefObserver* obs) { |
324 pref_notifier_->RemovePrefObserver(path, obs); | 328 pref_notifier_->RemovePrefObserver(path, obs); |
325 } | 329 } |
326 | 330 |
327 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { | 331 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { |
328 pref_notifier_->AddInitObserver(obs); | 332 pref_notifier_->AddInitObserver(obs); |
329 } | 333 } |
330 | 334 |
331 PrefRegistry* PrefService::DeprecatedGetPrefRegistry() { | 335 PrefRegistry* PrefService::DeprecatedGetPrefRegistry() { |
332 return pref_registry_.get(); | 336 return pref_registry_.get(); |
333 } | 337 } |
334 | 338 |
335 void PrefService::ClearPref(const char* path) { | 339 void PrefService::ClearPref(const std::string& path) { |
336 DCHECK(CalledOnValidThread()); | 340 DCHECK(CalledOnValidThread()); |
337 | 341 |
338 const Preference* pref = FindPreference(path); | 342 const Preference* pref = FindPreference(path); |
339 if (!pref) { | 343 if (!pref) { |
340 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 344 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
341 return; | 345 return; |
342 } | 346 } |
343 user_pref_store_->RemoveValue(path); | 347 user_pref_store_->RemoveValue(path); |
344 } | 348 } |
345 | 349 |
346 void PrefService::Set(const char* path, const base::Value& value) { | 350 void PrefService::Set(const std::string& path, const base::Value& value) { |
347 SetUserPrefValue(path, value.DeepCopy()); | 351 SetUserPrefValue(path, value.DeepCopy()); |
348 } | 352 } |
349 | 353 |
350 void PrefService::SetBoolean(const char* path, bool value) { | 354 void PrefService::SetBoolean(const std::string& path, bool value) { |
351 SetUserPrefValue(path, new base::FundamentalValue(value)); | 355 SetUserPrefValue(path, new base::FundamentalValue(value)); |
352 } | 356 } |
353 | 357 |
354 void PrefService::SetInteger(const char* path, int value) { | 358 void PrefService::SetInteger(const std::string& path, int value) { |
355 SetUserPrefValue(path, new base::FundamentalValue(value)); | 359 SetUserPrefValue(path, new base::FundamentalValue(value)); |
356 } | 360 } |
357 | 361 |
358 void PrefService::SetDouble(const char* path, double value) { | 362 void PrefService::SetDouble(const std::string& path, double value) { |
359 SetUserPrefValue(path, new base::FundamentalValue(value)); | 363 SetUserPrefValue(path, new base::FundamentalValue(value)); |
360 } | 364 } |
361 | 365 |
362 void PrefService::SetString(const char* path, const std::string& value) { | 366 void PrefService::SetString(const std::string& path, const std::string& value) { |
363 SetUserPrefValue(path, new base::StringValue(value)); | 367 SetUserPrefValue(path, new base::StringValue(value)); |
364 } | 368 } |
365 | 369 |
366 void PrefService::SetFilePath(const char* path, const base::FilePath& value) { | 370 void PrefService::SetFilePath(const std::string& path, |
| 371 const base::FilePath& value) { |
367 SetUserPrefValue(path, base::CreateFilePathValue(value)); | 372 SetUserPrefValue(path, base::CreateFilePathValue(value)); |
368 } | 373 } |
369 | 374 |
370 void PrefService::SetInt64(const char* path, int64 value) { | 375 void PrefService::SetInt64(const std::string& path, int64 value) { |
371 SetUserPrefValue(path, new base::StringValue(base::Int64ToString(value))); | 376 SetUserPrefValue(path, new base::StringValue(base::Int64ToString(value))); |
372 } | 377 } |
373 | 378 |
374 int64 PrefService::GetInt64(const char* path) const { | 379 int64 PrefService::GetInt64(const std::string& path) const { |
375 DCHECK(CalledOnValidThread()); | 380 DCHECK(CalledOnValidThread()); |
376 | 381 |
377 const base::Value* value = GetPreferenceValue(path); | 382 const base::Value* value = GetPreferenceValue(path); |
378 if (!value) { | 383 if (!value) { |
379 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 384 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
380 return 0; | 385 return 0; |
381 } | 386 } |
382 std::string result("0"); | 387 std::string result("0"); |
383 bool rv = value->GetAsString(&result); | 388 bool rv = value->GetAsString(&result); |
384 DCHECK(rv); | 389 DCHECK(rv); |
385 | 390 |
386 int64 val; | 391 int64 val; |
387 base::StringToInt64(result, &val); | 392 base::StringToInt64(result, &val); |
388 return val; | 393 return val; |
389 } | 394 } |
390 | 395 |
391 void PrefService::SetUint64(const char* path, uint64 value) { | 396 void PrefService::SetUint64(const std::string& path, uint64 value) { |
392 SetUserPrefValue(path, new base::StringValue(base::Uint64ToString(value))); | 397 SetUserPrefValue(path, new base::StringValue(base::Uint64ToString(value))); |
393 } | 398 } |
394 | 399 |
395 uint64 PrefService::GetUint64(const char* path) const { | 400 uint64 PrefService::GetUint64(const std::string& path) const { |
396 DCHECK(CalledOnValidThread()); | 401 DCHECK(CalledOnValidThread()); |
397 | 402 |
398 const base::Value* value = GetPreferenceValue(path); | 403 const base::Value* value = GetPreferenceValue(path); |
399 if (!value) { | 404 if (!value) { |
400 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 405 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
401 return 0; | 406 return 0; |
402 } | 407 } |
403 std::string result("0"); | 408 std::string result("0"); |
404 bool rv = value->GetAsString(&result); | 409 bool rv = value->GetAsString(&result); |
405 DCHECK(rv); | 410 DCHECK(rv); |
406 | 411 |
407 uint64 val; | 412 uint64 val; |
408 base::StringToUint64(result, &val); | 413 base::StringToUint64(result, &val); |
409 return val; | 414 return val; |
410 } | 415 } |
411 | 416 |
412 base::Value* PrefService::GetMutableUserPref(const char* path, | 417 base::Value* PrefService::GetMutableUserPref(const std::string& path, |
413 base::Value::Type type) { | 418 base::Value::Type type) { |
414 CHECK(type == base::Value::TYPE_DICTIONARY || type == base::Value::TYPE_LIST); | 419 CHECK(type == base::Value::TYPE_DICTIONARY || type == base::Value::TYPE_LIST); |
415 DCHECK(CalledOnValidThread()); | 420 DCHECK(CalledOnValidThread()); |
416 | 421 |
417 const Preference* pref = FindPreference(path); | 422 const Preference* pref = FindPreference(path); |
418 if (!pref) { | 423 if (!pref) { |
419 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 424 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
420 return NULL; | 425 return NULL; |
421 } | 426 } |
422 if (pref->GetType() != type) { | 427 if (pref->GetType() != type) { |
(...skipping 16 matching lines...) Expand all Loading... |
439 user_pref_store_->SetValueSilently(path, value); | 444 user_pref_store_->SetValueSilently(path, value); |
440 } | 445 } |
441 return value; | 446 return value; |
442 } | 447 } |
443 | 448 |
444 void PrefService::ReportUserPrefChanged(const std::string& key) { | 449 void PrefService::ReportUserPrefChanged(const std::string& key) { |
445 DCHECK(CalledOnValidThread()); | 450 DCHECK(CalledOnValidThread()); |
446 user_pref_store_->ReportValueChanged(key); | 451 user_pref_store_->ReportValueChanged(key); |
447 } | 452 } |
448 | 453 |
449 void PrefService::SetUserPrefValue(const char* path, base::Value* new_value) { | 454 void PrefService::SetUserPrefValue(const std::string& path, |
| 455 base::Value* new_value) { |
450 scoped_ptr<base::Value> owned_value(new_value); | 456 scoped_ptr<base::Value> owned_value(new_value); |
451 DCHECK(CalledOnValidThread()); | 457 DCHECK(CalledOnValidThread()); |
452 | 458 |
453 const Preference* pref = FindPreference(path); | 459 const Preference* pref = FindPreference(path); |
454 if (!pref) { | 460 if (!pref) { |
455 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 461 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
456 return; | 462 return; |
457 } | 463 } |
458 if (pref->GetType() != new_value->GetType()) { | 464 if (pref->GetType() != new_value->GetType()) { |
459 NOTREACHED() << "Trying to set pref " << path | 465 NOTREACHED() << "Trying to set pref " << path |
460 << " of type " << pref->GetType() | 466 << " of type " << pref->GetType() |
461 << " to value of type " << new_value->GetType(); | 467 << " to value of type " << new_value->GetType(); |
462 return; | 468 return; |
463 } | 469 } |
464 | 470 |
465 user_pref_store_->SetValue(path, owned_value.release()); | 471 user_pref_store_->SetValue(path, owned_value.release()); |
466 } | 472 } |
467 | 473 |
468 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { | 474 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { |
469 pref_value_store_->UpdateCommandLinePrefStore(command_line_store); | 475 pref_value_store_->UpdateCommandLinePrefStore(command_line_store); |
470 } | 476 } |
471 | 477 |
472 /////////////////////////////////////////////////////////////////////////////// | 478 /////////////////////////////////////////////////////////////////////////////// |
473 // PrefService::Preference | 479 // PrefService::Preference |
474 | 480 |
475 PrefService::Preference::Preference(const PrefService* service, | 481 PrefService::Preference::Preference(const PrefService* service, |
476 const char* name, | 482 const std::string& name, |
477 base::Value::Type type) | 483 base::Value::Type type) |
478 : name_(name), | 484 : name_(name), type_(type), pref_service_(service) { |
479 type_(type), | |
480 pref_service_(service) { | |
481 DCHECK(name); | |
482 DCHECK(service); | 485 DCHECK(service); |
483 } | 486 } |
484 | 487 |
485 const std::string PrefService::Preference::name() const { | 488 const std::string PrefService::Preference::name() const { |
486 return name_; | 489 return name_; |
487 } | 490 } |
488 | 491 |
489 base::Value::Type PrefService::Preference::GetType() const { | 492 base::Value::Type PrefService::Preference::GetType() const { |
490 return type_; | 493 return type_; |
491 } | 494 } |
492 | 495 |
493 const base::Value* PrefService::Preference::GetValue() const { | 496 const base::Value* PrefService::Preference::GetValue() const { |
494 const base::Value* result= pref_service_->GetPreferenceValue(name_); | 497 const base::Value* result= pref_service_->GetPreferenceValue(name_); |
495 DCHECK(result) << "Must register pref before getting its value"; | 498 DCHECK(result) << "Must register pref before getting its value"; |
496 return result; | 499 return result; |
497 } | 500 } |
498 | 501 |
499 const base::Value* PrefService::Preference::GetRecommendedValue() const { | 502 const base::Value* PrefService::Preference::GetRecommendedValue() const { |
500 DCHECK(pref_service_->FindPreference(name_.c_str())) << | 503 DCHECK(pref_service_->FindPreference(name_)) |
501 "Must register pref before getting its value"; | 504 << "Must register pref before getting its value"; |
502 | 505 |
503 const base::Value* found_value = NULL; | 506 const base::Value* found_value = NULL; |
504 if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) { | 507 if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) { |
505 DCHECK(found_value->IsType(type_)); | 508 DCHECK(found_value->IsType(type_)); |
506 return found_value; | 509 return found_value; |
507 } | 510 } |
508 | 511 |
509 // The pref has no recommended value. | 512 // The pref has no recommended value. |
510 return NULL; | 513 return NULL; |
511 } | 514 } |
512 | 515 |
513 bool PrefService::Preference::IsManaged() const { | 516 bool PrefService::Preference::IsManaged() const { |
514 return pref_value_store()->PrefValueInManagedStore(name_.c_str()); | 517 return pref_value_store()->PrefValueInManagedStore(name_); |
515 } | 518 } |
516 | 519 |
517 bool PrefService::Preference::IsRecommended() const { | 520 bool PrefService::Preference::IsRecommended() const { |
518 return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str()); | 521 return pref_value_store()->PrefValueFromRecommendedStore(name_); |
519 } | 522 } |
520 | 523 |
521 bool PrefService::Preference::HasExtensionSetting() const { | 524 bool PrefService::Preference::HasExtensionSetting() const { |
522 return pref_value_store()->PrefValueInExtensionStore(name_.c_str()); | 525 return pref_value_store()->PrefValueInExtensionStore(name_); |
523 } | 526 } |
524 | 527 |
525 bool PrefService::Preference::HasUserSetting() const { | 528 bool PrefService::Preference::HasUserSetting() const { |
526 return pref_value_store()->PrefValueInUserStore(name_.c_str()); | 529 return pref_value_store()->PrefValueInUserStore(name_); |
527 } | 530 } |
528 | 531 |
529 bool PrefService::Preference::IsExtensionControlled() const { | 532 bool PrefService::Preference::IsExtensionControlled() const { |
530 return pref_value_store()->PrefValueFromExtensionStore(name_.c_str()); | 533 return pref_value_store()->PrefValueFromExtensionStore(name_); |
531 } | 534 } |
532 | 535 |
533 bool PrefService::Preference::IsUserControlled() const { | 536 bool PrefService::Preference::IsUserControlled() const { |
534 return pref_value_store()->PrefValueFromUserStore(name_.c_str()); | 537 return pref_value_store()->PrefValueFromUserStore(name_); |
535 } | 538 } |
536 | 539 |
537 bool PrefService::Preference::IsDefaultValue() const { | 540 bool PrefService::Preference::IsDefaultValue() const { |
538 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); | 541 return pref_value_store()->PrefValueFromDefaultStore(name_); |
539 } | 542 } |
540 | 543 |
541 bool PrefService::Preference::IsUserModifiable() const { | 544 bool PrefService::Preference::IsUserModifiable() const { |
542 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); | 545 return pref_value_store()->PrefValueUserModifiable(name_); |
543 } | 546 } |
544 | 547 |
545 bool PrefService::Preference::IsExtensionModifiable() const { | 548 bool PrefService::Preference::IsExtensionModifiable() const { |
546 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); | 549 return pref_value_store()->PrefValueExtensionModifiable(name_); |
547 } | 550 } |
548 | 551 |
549 const base::Value* PrefService::GetPreferenceValue( | 552 const base::Value* PrefService::GetPreferenceValue( |
550 const std::string& path) const { | 553 const std::string& path) const { |
551 DCHECK(CalledOnValidThread()); | 554 DCHECK(CalledOnValidThread()); |
552 const base::Value* default_value = NULL; | 555 const base::Value* default_value = NULL; |
553 if (pref_registry_->defaults()->GetValue(path, &default_value)) { | 556 if (pref_registry_->defaults()->GetValue(path, &default_value)) { |
554 const base::Value* found_value = NULL; | 557 const base::Value* found_value = NULL; |
555 base::Value::Type default_type = default_value->GetType(); | 558 base::Value::Type default_type = default_value->GetType(); |
556 if (pref_value_store_->GetValue(path, default_type, &found_value)) { | 559 if (pref_value_store_->GetValue(path, default_type, &found_value)) { |
557 DCHECK(found_value->IsType(default_type)); | 560 DCHECK(found_value->IsType(default_type)); |
558 return found_value; | 561 return found_value; |
559 } else { | 562 } else { |
560 // Every registered preference has at least a default value. | 563 // Every registered preference has at least a default value. |
561 NOTREACHED() << "no valid value found for registered pref " << path; | 564 NOTREACHED() << "no valid value found for registered pref " << path; |
562 } | 565 } |
563 } | 566 } |
564 | 567 |
565 return NULL; | 568 return NULL; |
566 } | 569 } |
OLD | NEW |