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" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/prefs/default_pref_store.h" | 14 #include "base/prefs/default_pref_store.h" |
15 #include "base/prefs/pref_notifier_impl.h" | 15 #include "base/prefs/pref_notifier_impl.h" |
16 #include "base/prefs/pref_registry.h" | 16 #include "base/prefs/pref_registry.h" |
17 #include "base/prefs/pref_value_store.h" | 17 #include "base/prefs/pref_value_store.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "base/strings/string_number_conversions.h" | |
20 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
21 #include "base/value_conversions.h" | 20 #include "base/value_conversions.h" |
22 #include "build/build_config.h" | 21 #include "build/build_config.h" |
23 | 22 |
24 namespace { | 23 namespace { |
25 | 24 |
26 class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate { | 25 class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate { |
27 public: | 26 public: |
28 ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb) | 27 ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb) |
29 : callback_(cb) {} | 28 : callback_(cb) {} |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 const base::Value* value = GetPreferenceValue(path); | 138 const base::Value* value = GetPreferenceValue(path); |
140 if (!value) { | 139 if (!value) { |
141 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 140 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
142 return result; | 141 return result; |
143 } | 142 } |
144 bool rv = value->GetAsString(&result); | 143 bool rv = value->GetAsString(&result); |
145 DCHECK(rv); | 144 DCHECK(rv); |
146 return result; | 145 return result; |
147 } | 146 } |
148 | 147 |
| 148 int64 PrefService::GetInt64(const char* path) const { |
| 149 DCHECK(CalledOnValidThread()); |
| 150 |
| 151 int64 result = 0; |
| 152 |
| 153 const base::Value* value = GetPreferenceValue(path); |
| 154 if (!value) { |
| 155 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 156 return result; |
| 157 } |
| 158 bool rv = value->GetAsInt64(&result); |
| 159 DCHECK(rv); |
| 160 return result; |
| 161 } |
| 162 |
| 163 uint64 PrefService::GetUint64(const char* path) const { |
| 164 DCHECK(CalledOnValidThread()); |
| 165 |
| 166 uint64 result = 0; |
| 167 |
| 168 const base::Value* value = GetPreferenceValue(path); |
| 169 if (!value) { |
| 170 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 171 return result; |
| 172 } |
| 173 bool rv = value->GetAsUint64(&result); |
| 174 DCHECK(rv); |
| 175 return result; |
| 176 } |
| 177 |
149 base::FilePath PrefService::GetFilePath(const char* path) const { | 178 base::FilePath PrefService::GetFilePath(const char* path) const { |
150 DCHECK(CalledOnValidThread()); | 179 DCHECK(CalledOnValidThread()); |
151 | 180 |
152 base::FilePath result; | 181 base::FilePath result; |
153 | 182 |
154 const base::Value* value = GetPreferenceValue(path); | 183 const base::Value* value = GetPreferenceValue(path); |
155 if (!value) { | 184 if (!value) { |
156 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 185 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
157 return base::FilePath(result); | 186 return base::FilePath(result); |
158 } | 187 } |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 } | 375 } |
347 | 376 |
348 void PrefService::SetDouble(const char* path, double value) { | 377 void PrefService::SetDouble(const char* path, double value) { |
349 SetUserPrefValue(path, new base::FundamentalValue(value)); | 378 SetUserPrefValue(path, new base::FundamentalValue(value)); |
350 } | 379 } |
351 | 380 |
352 void PrefService::SetString(const char* path, const std::string& value) { | 381 void PrefService::SetString(const char* path, const std::string& value) { |
353 SetUserPrefValue(path, new base::StringValue(value)); | 382 SetUserPrefValue(path, new base::StringValue(value)); |
354 } | 383 } |
355 | 384 |
| 385 void PrefService::SetInt64(const char* path, int64 value) { |
| 386 SetUserPrefValue(path, new base::StringValue(value)); |
| 387 } |
| 388 |
| 389 void PrefService::SetUint64(const char* path, uint64 value) { |
| 390 SetUserPrefValue(path, new base::StringValue(value)); |
| 391 } |
| 392 |
356 void PrefService::SetFilePath(const char* path, const base::FilePath& value) { | 393 void PrefService::SetFilePath(const char* path, const base::FilePath& value) { |
357 SetUserPrefValue(path, base::CreateFilePathValue(value)); | 394 SetUserPrefValue(path, base::CreateFilePathValue(value)); |
358 } | 395 } |
359 | 396 |
360 void PrefService::SetInt64(const char* path, int64 value) { | |
361 SetUserPrefValue(path, new base::StringValue(base::Int64ToString(value))); | |
362 } | |
363 | |
364 int64 PrefService::GetInt64(const char* path) const { | |
365 DCHECK(CalledOnValidThread()); | |
366 | |
367 const base::Value* value = GetPreferenceValue(path); | |
368 if (!value) { | |
369 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
370 return 0; | |
371 } | |
372 std::string result("0"); | |
373 bool rv = value->GetAsString(&result); | |
374 DCHECK(rv); | |
375 | |
376 int64 val; | |
377 base::StringToInt64(result, &val); | |
378 return val; | |
379 } | |
380 | |
381 void PrefService::SetUint64(const char* path, uint64 value) { | |
382 SetUserPrefValue(path, new base::StringValue(base::Uint64ToString(value))); | |
383 } | |
384 | |
385 uint64 PrefService::GetUint64(const char* path) const { | |
386 DCHECK(CalledOnValidThread()); | |
387 | |
388 const base::Value* value = GetPreferenceValue(path); | |
389 if (!value) { | |
390 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
391 return 0; | |
392 } | |
393 std::string result("0"); | |
394 bool rv = value->GetAsString(&result); | |
395 DCHECK(rv); | |
396 | |
397 uint64 val; | |
398 base::StringToUint64(result, &val); | |
399 return val; | |
400 } | |
401 | |
402 base::Value* PrefService::GetMutableUserPref(const char* path, | 397 base::Value* PrefService::GetMutableUserPref(const char* path, |
403 base::Value::Type type) { | 398 base::Value::Type type) { |
404 CHECK(type == base::Value::TYPE_DICTIONARY || type == base::Value::TYPE_LIST); | 399 CHECK(type == base::Value::TYPE_DICTIONARY || type == base::Value::TYPE_LIST); |
405 DCHECK(CalledOnValidThread()); | 400 DCHECK(CalledOnValidThread()); |
406 | 401 |
407 const Preference* pref = FindPreference(path); | 402 const Preference* pref = FindPreference(path); |
408 if (!pref) { | 403 if (!pref) { |
409 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 404 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
410 return NULL; | 405 return NULL; |
411 } | 406 } |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 DCHECK(found_value->IsType(default_type)); | 541 DCHECK(found_value->IsType(default_type)); |
547 return found_value; | 542 return found_value; |
548 } else { | 543 } else { |
549 // Every registered preference has at least a default value. | 544 // Every registered preference has at least a default value. |
550 NOTREACHED() << "no valid value found for registered pref " << path; | 545 NOTREACHED() << "no valid value found for registered pref " << path; |
551 } | 546 } |
552 } | 547 } |
553 | 548 |
554 return NULL; | 549 return NULL; |
555 } | 550 } |
OLD | NEW |