| 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 "components/prefs/pref_service.h" | 5 #include "components/prefs/pref_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // TODO(battre): This is a check for crbug.com/435208 to make sure that | 73 // TODO(battre): This is a check for crbug.com/435208 to make sure that |
| 74 // access violations are caused by a use-after-free bug and not by an | 74 // access violations are caused by a use-after-free bug and not by an |
| 75 // initialization bug. | 75 // initialization bug. |
| 76 CHECK(pref_registry_); | 76 CHECK(pref_registry_); |
| 77 CHECK(pref_value_store_); | 77 CHECK(pref_value_store_); |
| 78 | 78 |
| 79 InitFromStorage(async); | 79 InitFromStorage(async); |
| 80 } | 80 } |
| 81 | 81 |
| 82 PrefService::~PrefService() { | 82 PrefService::~PrefService() { |
| 83 DCHECK(CalledOnValidThread()); | 83 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 84 | 84 |
| 85 // Reset pointers so accesses after destruction reliably crash. | 85 // Reset pointers so accesses after destruction reliably crash. |
| 86 pref_value_store_.reset(); | 86 pref_value_store_.reset(); |
| 87 pref_registry_ = NULL; | 87 pref_registry_ = NULL; |
| 88 user_pref_store_ = NULL; | 88 user_pref_store_ = NULL; |
| 89 pref_notifier_.reset(); | 89 pref_notifier_.reset(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void PrefService::InitFromStorage(bool async) { | 92 void PrefService::InitFromStorage(bool async) { |
| 93 if (user_pref_store_->IsInitializationComplete()) { | 93 if (user_pref_store_->IsInitializationComplete()) { |
| 94 read_error_callback_.Run(user_pref_store_->GetReadError()); | 94 read_error_callback_.Run(user_pref_store_->GetReadError()); |
| 95 } else if (!async) { | 95 } else if (!async) { |
| 96 read_error_callback_.Run(user_pref_store_->ReadPrefs()); | 96 read_error_callback_.Run(user_pref_store_->ReadPrefs()); |
| 97 } else { | 97 } else { |
| 98 // Guarantee that initialization happens after this function returned. | 98 // Guarantee that initialization happens after this function returned. |
| 99 base::ThreadTaskRunnerHandle::Get()->PostTask( | 99 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 100 FROM_HERE, | 100 FROM_HERE, |
| 101 base::Bind(&PersistentPrefStore::ReadPrefsAsync, user_pref_store_, | 101 base::Bind(&PersistentPrefStore::ReadPrefsAsync, user_pref_store_, |
| 102 new ReadErrorHandler(read_error_callback_))); | 102 new ReadErrorHandler(read_error_callback_))); |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 | 105 |
| 106 void PrefService::CommitPendingWrite() { | 106 void PrefService::CommitPendingWrite() { |
| 107 DCHECK(CalledOnValidThread()); | 107 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 108 user_pref_store_->CommitPendingWrite(); | 108 user_pref_store_->CommitPendingWrite(); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void PrefService::SchedulePendingLossyWrites() { | 111 void PrefService::SchedulePendingLossyWrites() { |
| 112 DCHECK(CalledOnValidThread()); | 112 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 113 user_pref_store_->SchedulePendingLossyWrites(); | 113 user_pref_store_->SchedulePendingLossyWrites(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool PrefService::GetBoolean(const std::string& path) const { | 116 bool PrefService::GetBoolean(const std::string& path) const { |
| 117 DCHECK(CalledOnValidThread()); | 117 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 118 | 118 |
| 119 bool result = false; | 119 bool result = false; |
| 120 | 120 |
| 121 const base::Value* value = GetPreferenceValue(path); | 121 const base::Value* value = GetPreferenceValue(path); |
| 122 if (!value) { | 122 if (!value) { |
| 123 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 123 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 124 return result; | 124 return result; |
| 125 } | 125 } |
| 126 bool rv = value->GetAsBoolean(&result); | 126 bool rv = value->GetAsBoolean(&result); |
| 127 DCHECK(rv); | 127 DCHECK(rv); |
| 128 return result; | 128 return result; |
| 129 } | 129 } |
| 130 | 130 |
| 131 int PrefService::GetInteger(const std::string& path) const { | 131 int PrefService::GetInteger(const std::string& path) const { |
| 132 DCHECK(CalledOnValidThread()); | 132 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 133 | 133 |
| 134 int result = 0; | 134 int result = 0; |
| 135 | 135 |
| 136 const base::Value* value = GetPreferenceValue(path); | 136 const base::Value* value = GetPreferenceValue(path); |
| 137 if (!value) { | 137 if (!value) { |
| 138 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 138 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 139 return result; | 139 return result; |
| 140 } | 140 } |
| 141 bool rv = value->GetAsInteger(&result); | 141 bool rv = value->GetAsInteger(&result); |
| 142 DCHECK(rv); | 142 DCHECK(rv); |
| 143 return result; | 143 return result; |
| 144 } | 144 } |
| 145 | 145 |
| 146 double PrefService::GetDouble(const std::string& path) const { | 146 double PrefService::GetDouble(const std::string& path) const { |
| 147 DCHECK(CalledOnValidThread()); | 147 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 148 | 148 |
| 149 double result = 0.0; | 149 double result = 0.0; |
| 150 | 150 |
| 151 const base::Value* value = GetPreferenceValue(path); | 151 const base::Value* value = GetPreferenceValue(path); |
| 152 if (!value) { | 152 if (!value) { |
| 153 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 153 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 154 return result; | 154 return result; |
| 155 } | 155 } |
| 156 bool rv = value->GetAsDouble(&result); | 156 bool rv = value->GetAsDouble(&result); |
| 157 DCHECK(rv); | 157 DCHECK(rv); |
| 158 return result; | 158 return result; |
| 159 } | 159 } |
| 160 | 160 |
| 161 std::string PrefService::GetString(const std::string& path) const { | 161 std::string PrefService::GetString(const std::string& path) const { |
| 162 DCHECK(CalledOnValidThread()); | 162 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 163 | 163 |
| 164 std::string result; | 164 std::string result; |
| 165 | 165 |
| 166 const base::Value* value = GetPreferenceValue(path); | 166 const base::Value* value = GetPreferenceValue(path); |
| 167 if (!value) { | 167 if (!value) { |
| 168 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 168 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 169 return result; | 169 return result; |
| 170 } | 170 } |
| 171 bool rv = value->GetAsString(&result); | 171 bool rv = value->GetAsString(&result); |
| 172 DCHECK(rv); | 172 DCHECK(rv); |
| 173 return result; | 173 return result; |
| 174 } | 174 } |
| 175 | 175 |
| 176 base::FilePath PrefService::GetFilePath(const std::string& path) const { | 176 base::FilePath PrefService::GetFilePath(const std::string& path) const { |
| 177 DCHECK(CalledOnValidThread()); | 177 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 178 | 178 |
| 179 base::FilePath result; | 179 base::FilePath result; |
| 180 | 180 |
| 181 const base::Value* value = GetPreferenceValue(path); | 181 const base::Value* value = GetPreferenceValue(path); |
| 182 if (!value) { | 182 if (!value) { |
| 183 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 183 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 184 return base::FilePath(result); | 184 return base::FilePath(result); |
| 185 } | 185 } |
| 186 bool rv = base::GetValueAsFilePath(*value, &result); | 186 bool rv = base::GetValueAsFilePath(*value, &result); |
| 187 DCHECK(rv); | 187 DCHECK(rv); |
| 188 return result; | 188 return result; |
| 189 } | 189 } |
| 190 | 190 |
| 191 bool PrefService::HasPrefPath(const std::string& path) const { | 191 bool PrefService::HasPrefPath(const std::string& path) const { |
| 192 const Preference* pref = FindPreference(path); | 192 const Preference* pref = FindPreference(path); |
| 193 return pref && !pref->IsDefaultValue(); | 193 return pref && !pref->IsDefaultValue(); |
| 194 } | 194 } |
| 195 | 195 |
| 196 void PrefService::IteratePreferenceValues( | 196 void PrefService::IteratePreferenceValues( |
| 197 base::RepeatingCallback<void(const std::string& key, | 197 base::RepeatingCallback<void(const std::string& key, |
| 198 const base::Value& value)> callback) const { | 198 const base::Value& value)> callback) const { |
| 199 DCHECK(CalledOnValidThread()); | 199 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 200 for (const auto& it : *pref_registry_) | 200 for (const auto& it : *pref_registry_) |
| 201 callback.Run(it.first, *GetPreferenceValue(it.first)); | 201 callback.Run(it.first, *GetPreferenceValue(it.first)); |
| 202 } | 202 } |
| 203 | 203 |
| 204 std::unique_ptr<base::DictionaryValue> PrefService::GetPreferenceValues( | 204 std::unique_ptr<base::DictionaryValue> PrefService::GetPreferenceValues( |
| 205 IncludeDefaults include_defaults) const { | 205 IncludeDefaults include_defaults) const { |
| 206 DCHECK(CalledOnValidThread()); | 206 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 207 std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 207 std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
| 208 for (const auto& it : *pref_registry_) { | 208 for (const auto& it : *pref_registry_) { |
| 209 if (include_defaults == INCLUDE_DEFAULTS) { | 209 if (include_defaults == INCLUDE_DEFAULTS) { |
| 210 out->Set(it.first, GetPreferenceValue(it.first)->CreateDeepCopy()); | 210 out->Set(it.first, GetPreferenceValue(it.first)->CreateDeepCopy()); |
| 211 } else { | 211 } else { |
| 212 const Preference* pref = FindPreference(it.first); | 212 const Preference* pref = FindPreference(it.first); |
| 213 if (pref->IsDefaultValue()) | 213 if (pref->IsDefaultValue()) |
| 214 continue; | 214 continue; |
| 215 out->Set(it.first, pref->GetValue()->CreateDeepCopy()); | 215 out->Set(it.first, pref->GetValue()->CreateDeepCopy()); |
| 216 } | 216 } |
| 217 } | 217 } |
| 218 return out; | 218 return out; |
| 219 } | 219 } |
| 220 | 220 |
| 221 const PrefService::Preference* PrefService::FindPreference( | 221 const PrefService::Preference* PrefService::FindPreference( |
| 222 const std::string& pref_name) const { | 222 const std::string& pref_name) const { |
| 223 DCHECK(CalledOnValidThread()); | 223 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 224 PreferenceMap::iterator it = prefs_map_.find(pref_name); | 224 PreferenceMap::iterator it = prefs_map_.find(pref_name); |
| 225 if (it != prefs_map_.end()) | 225 if (it != prefs_map_.end()) |
| 226 return &(it->second); | 226 return &(it->second); |
| 227 const base::Value* default_value = NULL; | 227 const base::Value* default_value = NULL; |
| 228 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) | 228 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) |
| 229 return NULL; | 229 return NULL; |
| 230 it = prefs_map_.insert( | 230 it = prefs_map_.insert( |
| 231 std::make_pair(pref_name, Preference( | 231 std::make_pair(pref_name, Preference( |
| 232 this, pref_name, default_value->GetType()))).first; | 232 this, pref_name, default_value->GetType()))).first; |
| 233 return &(it->second); | 233 return &(it->second); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 264 } | 264 } |
| 265 | 265 |
| 266 bool PrefService::IsUserModifiablePreference( | 266 bool PrefService::IsUserModifiablePreference( |
| 267 const std::string& pref_name) const { | 267 const std::string& pref_name) const { |
| 268 const Preference* pref = FindPreference(pref_name); | 268 const Preference* pref = FindPreference(pref_name); |
| 269 return pref && pref->IsUserModifiable(); | 269 return pref && pref->IsUserModifiable(); |
| 270 } | 270 } |
| 271 | 271 |
| 272 const base::DictionaryValue* PrefService::GetDictionary( | 272 const base::DictionaryValue* PrefService::GetDictionary( |
| 273 const std::string& path) const { | 273 const std::string& path) const { |
| 274 DCHECK(CalledOnValidThread()); | 274 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 275 | 275 |
| 276 const base::Value* value = GetPreferenceValue(path); | 276 const base::Value* value = GetPreferenceValue(path); |
| 277 if (!value) { | 277 if (!value) { |
| 278 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 278 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 279 return NULL; | 279 return NULL; |
| 280 } | 280 } |
| 281 if (value->GetType() != base::Value::Type::DICTIONARY) { | 281 if (value->GetType() != base::Value::Type::DICTIONARY) { |
| 282 NOTREACHED(); | 282 NOTREACHED(); |
| 283 return NULL; | 283 return NULL; |
| 284 } | 284 } |
| 285 return static_cast<const base::DictionaryValue*>(value); | 285 return static_cast<const base::DictionaryValue*>(value); |
| 286 } | 286 } |
| 287 | 287 |
| 288 const base::Value* PrefService::GetUserPrefValue( | 288 const base::Value* PrefService::GetUserPrefValue( |
| 289 const std::string& path) const { | 289 const std::string& path) const { |
| 290 DCHECK(CalledOnValidThread()); | 290 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 291 | 291 |
| 292 const Preference* pref = FindPreference(path); | 292 const Preference* pref = FindPreference(path); |
| 293 if (!pref) { | 293 if (!pref) { |
| 294 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 294 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 295 return NULL; | 295 return NULL; |
| 296 } | 296 } |
| 297 | 297 |
| 298 // Look for an existing preference in the user store. If it doesn't | 298 // Look for an existing preference in the user store. If it doesn't |
| 299 // exist, return NULL. | 299 // exist, return NULL. |
| 300 base::Value* value = NULL; | 300 base::Value* value = NULL; |
| 301 if (!user_pref_store_->GetMutableValue(path, &value)) | 301 if (!user_pref_store_->GetMutableValue(path, &value)) |
| 302 return NULL; | 302 return NULL; |
| 303 | 303 |
| 304 if (!value->IsType(pref->GetType())) { | 304 if (!value->IsType(pref->GetType())) { |
| 305 NOTREACHED() << "Pref value type doesn't match registered type."; | 305 NOTREACHED() << "Pref value type doesn't match registered type."; |
| 306 return NULL; | 306 return NULL; |
| 307 } | 307 } |
| 308 | 308 |
| 309 return value; | 309 return value; |
| 310 } | 310 } |
| 311 | 311 |
| 312 void PrefService::SetDefaultPrefValue(const std::string& path, | 312 void PrefService::SetDefaultPrefValue(const std::string& path, |
| 313 base::Value* value) { | 313 base::Value* value) { |
| 314 DCHECK(CalledOnValidThread()); | 314 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 315 pref_registry_->SetDefaultPrefValue(path, value); | 315 pref_registry_->SetDefaultPrefValue(path, value); |
| 316 } | 316 } |
| 317 | 317 |
| 318 const base::Value* PrefService::GetDefaultPrefValue( | 318 const base::Value* PrefService::GetDefaultPrefValue( |
| 319 const std::string& path) const { | 319 const std::string& path) const { |
| 320 DCHECK(CalledOnValidThread()); | 320 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 321 // Lookup the preference in the default store. | 321 // Lookup the preference in the default store. |
| 322 const base::Value* value = NULL; | 322 const base::Value* value = NULL; |
| 323 if (!pref_registry_->defaults()->GetValue(path, &value)) { | 323 if (!pref_registry_->defaults()->GetValue(path, &value)) { |
| 324 NOTREACHED() << "Default value missing for pref: " << path; | 324 NOTREACHED() << "Default value missing for pref: " << path; |
| 325 return NULL; | 325 return NULL; |
| 326 } | 326 } |
| 327 return value; | 327 return value; |
| 328 } | 328 } |
| 329 | 329 |
| 330 const base::ListValue* PrefService::GetList(const std::string& path) const { | 330 const base::ListValue* PrefService::GetList(const std::string& path) const { |
| 331 DCHECK(CalledOnValidThread()); | 331 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 332 | 332 |
| 333 const base::Value* value = GetPreferenceValue(path); | 333 const base::Value* value = GetPreferenceValue(path); |
| 334 if (!value) { | 334 if (!value) { |
| 335 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 335 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 336 return NULL; | 336 return NULL; |
| 337 } | 337 } |
| 338 if (value->GetType() != base::Value::Type::LIST) { | 338 if (value->GetType() != base::Value::Type::LIST) { |
| 339 NOTREACHED(); | 339 NOTREACHED(); |
| 340 return NULL; | 340 return NULL; |
| 341 } | 341 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 353 | 353 |
| 354 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { | 354 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { |
| 355 pref_notifier_->AddInitObserver(obs); | 355 pref_notifier_->AddInitObserver(obs); |
| 356 } | 356 } |
| 357 | 357 |
| 358 PrefRegistry* PrefService::DeprecatedGetPrefRegistry() { | 358 PrefRegistry* PrefService::DeprecatedGetPrefRegistry() { |
| 359 return pref_registry_.get(); | 359 return pref_registry_.get(); |
| 360 } | 360 } |
| 361 | 361 |
| 362 void PrefService::ClearPref(const std::string& path) { | 362 void PrefService::ClearPref(const std::string& path) { |
| 363 DCHECK(CalledOnValidThread()); | 363 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 364 | 364 |
| 365 const Preference* pref = FindPreference(path); | 365 const Preference* pref = FindPreference(path); |
| 366 if (!pref) { | 366 if (!pref) { |
| 367 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 367 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
| 368 return; | 368 return; |
| 369 } | 369 } |
| 370 user_pref_store_->RemoveValue(path, GetWriteFlags(pref)); | 370 user_pref_store_->RemoveValue(path, GetWriteFlags(pref)); |
| 371 } | 371 } |
| 372 | 372 |
| 373 void PrefService::ClearMutableValues() { | 373 void PrefService::ClearMutableValues() { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 398 const base::FilePath& value) { | 398 const base::FilePath& value) { |
| 399 SetUserPrefValue(path, base::CreateFilePathValue(value)); | 399 SetUserPrefValue(path, base::CreateFilePathValue(value)); |
| 400 } | 400 } |
| 401 | 401 |
| 402 void PrefService::SetInt64(const std::string& path, int64_t value) { | 402 void PrefService::SetInt64(const std::string& path, int64_t value) { |
| 403 SetUserPrefValue(path, | 403 SetUserPrefValue(path, |
| 404 base::MakeUnique<base::Value>(base::Int64ToString(value))); | 404 base::MakeUnique<base::Value>(base::Int64ToString(value))); |
| 405 } | 405 } |
| 406 | 406 |
| 407 int64_t PrefService::GetInt64(const std::string& path) const { | 407 int64_t PrefService::GetInt64(const std::string& path) const { |
| 408 DCHECK(CalledOnValidThread()); | 408 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 409 | 409 |
| 410 const base::Value* value = GetPreferenceValue(path); | 410 const base::Value* value = GetPreferenceValue(path); |
| 411 if (!value) { | 411 if (!value) { |
| 412 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 412 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 413 return 0; | 413 return 0; |
| 414 } | 414 } |
| 415 std::string result("0"); | 415 std::string result("0"); |
| 416 bool rv = value->GetAsString(&result); | 416 bool rv = value->GetAsString(&result); |
| 417 DCHECK(rv); | 417 DCHECK(rv); |
| 418 | 418 |
| 419 int64_t val; | 419 int64_t val; |
| 420 base::StringToInt64(result, &val); | 420 base::StringToInt64(result, &val); |
| 421 return val; | 421 return val; |
| 422 } | 422 } |
| 423 | 423 |
| 424 void PrefService::SetUint64(const std::string& path, uint64_t value) { | 424 void PrefService::SetUint64(const std::string& path, uint64_t value) { |
| 425 SetUserPrefValue(path, | 425 SetUserPrefValue(path, |
| 426 base::MakeUnique<base::Value>(base::Uint64ToString(value))); | 426 base::MakeUnique<base::Value>(base::Uint64ToString(value))); |
| 427 } | 427 } |
| 428 | 428 |
| 429 uint64_t PrefService::GetUint64(const std::string& path) const { | 429 uint64_t PrefService::GetUint64(const std::string& path) const { |
| 430 DCHECK(CalledOnValidThread()); | 430 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 431 | 431 |
| 432 const base::Value* value = GetPreferenceValue(path); | 432 const base::Value* value = GetPreferenceValue(path); |
| 433 if (!value) { | 433 if (!value) { |
| 434 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 434 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 435 return 0; | 435 return 0; |
| 436 } | 436 } |
| 437 std::string result("0"); | 437 std::string result("0"); |
| 438 bool rv = value->GetAsString(&result); | 438 bool rv = value->GetAsString(&result); |
| 439 DCHECK(rv); | 439 DCHECK(rv); |
| 440 | 440 |
| 441 uint64_t val; | 441 uint64_t val; |
| 442 base::StringToUint64(result, &val); | 442 base::StringToUint64(result, &val); |
| 443 return val; | 443 return val; |
| 444 } | 444 } |
| 445 | 445 |
| 446 base::Value* PrefService::GetMutableUserPref(const std::string& path, | 446 base::Value* PrefService::GetMutableUserPref(const std::string& path, |
| 447 base::Value::Type type) { | 447 base::Value::Type type) { |
| 448 CHECK(type == base::Value::Type::DICTIONARY || | 448 CHECK(type == base::Value::Type::DICTIONARY || |
| 449 type == base::Value::Type::LIST); | 449 type == base::Value::Type::LIST); |
| 450 DCHECK(CalledOnValidThread()); | 450 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 451 | 451 |
| 452 const Preference* pref = FindPreference(path); | 452 const Preference* pref = FindPreference(path); |
| 453 if (!pref) { | 453 if (!pref) { |
| 454 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 454 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 455 return NULL; | 455 return NULL; |
| 456 } | 456 } |
| 457 if (pref->GetType() != type) { | 457 if (pref->GetType() != type) { |
| 458 NOTREACHED() << "Wrong type for GetMutableValue: " << path; | 458 NOTREACHED() << "Wrong type for GetMutableValue: " << path; |
| 459 return NULL; | 459 return NULL; |
| 460 } | 460 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 471 } else { | 471 } else { |
| 472 NOTREACHED(); | 472 NOTREACHED(); |
| 473 } | 473 } |
| 474 user_pref_store_->SetValueSilently(path, base::WrapUnique(value), | 474 user_pref_store_->SetValueSilently(path, base::WrapUnique(value), |
| 475 GetWriteFlags(pref)); | 475 GetWriteFlags(pref)); |
| 476 } | 476 } |
| 477 return value; | 477 return value; |
| 478 } | 478 } |
| 479 | 479 |
| 480 void PrefService::ReportUserPrefChanged(const std::string& key) { | 480 void PrefService::ReportUserPrefChanged(const std::string& key) { |
| 481 DCHECK(CalledOnValidThread()); | 481 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 482 user_pref_store_->ReportValueChanged(key, GetWriteFlags(FindPreference(key))); | 482 user_pref_store_->ReportValueChanged(key, GetWriteFlags(FindPreference(key))); |
| 483 } | 483 } |
| 484 | 484 |
| 485 void PrefService::ReportUserPrefChanged( | 485 void PrefService::ReportUserPrefChanged( |
| 486 const std::string& key, | 486 const std::string& key, |
| 487 std::set<std::vector<std::string>> path_components) { | 487 std::set<std::vector<std::string>> path_components) { |
| 488 DCHECK(CalledOnValidThread()); | 488 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 489 user_pref_store_->ReportSubValuesChanged(key, std::move(path_components), | 489 user_pref_store_->ReportSubValuesChanged(key, std::move(path_components), |
| 490 GetWriteFlags(FindPreference(key))); | 490 GetWriteFlags(FindPreference(key))); |
| 491 } | 491 } |
| 492 | 492 |
| 493 void PrefService::SetUserPrefValue(const std::string& path, | 493 void PrefService::SetUserPrefValue(const std::string& path, |
| 494 std::unique_ptr<base::Value> new_value) { | 494 std::unique_ptr<base::Value> new_value) { |
| 495 DCHECK(CalledOnValidThread()); | 495 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 496 | 496 |
| 497 const Preference* pref = FindPreference(path); | 497 const Preference* pref = FindPreference(path); |
| 498 if (!pref) { | 498 if (!pref) { |
| 499 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 499 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 500 return; | 500 return; |
| 501 } | 501 } |
| 502 if (pref->GetType() != new_value->GetType()) { | 502 if (pref->GetType() != new_value->GetType()) { |
| 503 NOTREACHED() << "Trying to set pref " << path | 503 NOTREACHED() << "Trying to set pref " << path |
| 504 << " of type " << pref->GetType() | 504 << " of type " << pref->GetType() |
| 505 << " to value of type " << new_value->GetType(); | 505 << " to value of type " << new_value->GetType(); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 bool PrefService::Preference::IsUserModifiable() const { | 589 bool PrefService::Preference::IsUserModifiable() const { |
| 590 return pref_value_store()->PrefValueUserModifiable(name_); | 590 return pref_value_store()->PrefValueUserModifiable(name_); |
| 591 } | 591 } |
| 592 | 592 |
| 593 bool PrefService::Preference::IsExtensionModifiable() const { | 593 bool PrefService::Preference::IsExtensionModifiable() const { |
| 594 return pref_value_store()->PrefValueExtensionModifiable(name_); | 594 return pref_value_store()->PrefValueExtensionModifiable(name_); |
| 595 } | 595 } |
| 596 | 596 |
| 597 const base::Value* PrefService::GetPreferenceValue( | 597 const base::Value* PrefService::GetPreferenceValue( |
| 598 const std::string& path) const { | 598 const std::string& path) const { |
| 599 DCHECK(CalledOnValidThread()); | 599 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 600 | 600 |
| 601 // TODO(battre): This is a check for crbug.com/435208. After analyzing some | 601 // TODO(battre): This is a check for crbug.com/435208. After analyzing some |
| 602 // crash dumps it looks like the PrefService is accessed even though it has | 602 // crash dumps it looks like the PrefService is accessed even though it has |
| 603 // been cleared already. | 603 // been cleared already. |
| 604 CHECK(pref_registry_); | 604 CHECK(pref_registry_); |
| 605 CHECK(pref_registry_->defaults()); | 605 CHECK(pref_registry_->defaults()); |
| 606 CHECK(pref_value_store_); | 606 CHECK(pref_value_store_); |
| 607 | 607 |
| 608 const base::Value* default_value = NULL; | 608 const base::Value* default_value = NULL; |
| 609 if (pref_registry_->defaults()->GetValue(path, &default_value)) { | 609 if (pref_registry_->defaults()->GetValue(path, &default_value)) { |
| 610 const base::Value* found_value = NULL; | 610 const base::Value* found_value = NULL; |
| 611 base::Value::Type default_type = default_value->GetType(); | 611 base::Value::Type default_type = default_value->GetType(); |
| 612 if (pref_value_store_->GetValue(path, default_type, &found_value)) { | 612 if (pref_value_store_->GetValue(path, default_type, &found_value)) { |
| 613 DCHECK(found_value->IsType(default_type)); | 613 DCHECK(found_value->IsType(default_type)); |
| 614 return found_value; | 614 return found_value; |
| 615 } else { | 615 } else { |
| 616 // Every registered preference has at least a default value. | 616 // Every registered preference has at least a default value. |
| 617 NOTREACHED() << "no valid value found for registered pref " << path; | 617 NOTREACHED() << "no valid value found for registered pref " << path; |
| 618 } | 618 } |
| 619 } | 619 } |
| 620 | 620 |
| 621 return NULL; | 621 return NULL; |
| 622 } | 622 } |
| OLD | NEW |