| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/prefs/pref_service.h" | 5 #include "chrome/browser/prefs/pref_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 base::StringToDouble(resource_string, &val); | 54 base::StringToDouble(resource_string, &val); |
| 55 return Value::CreateRealValue(val); | 55 return Value::CreateRealValue(val); |
| 56 } | 56 } |
| 57 | 57 |
| 58 case Value::TYPE_STRING: { | 58 case Value::TYPE_STRING: { |
| 59 return Value::CreateStringValue(resource_string); | 59 return Value::CreateStringValue(resource_string); |
| 60 } | 60 } |
| 61 | 61 |
| 62 default: { | 62 default: { |
| 63 NOTREACHED() << | 63 NOTREACHED() << |
| 64 "list and dictionary types can not have default locale values"; | 64 "list and dictionary types cannot have default locale values"; |
| 65 } | 65 } |
| 66 } | 66 } |
| 67 NOTREACHED(); | 67 NOTREACHED(); |
| 68 return Value::CreateNullValue(); | 68 return Value::CreateNullValue(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 // Forwards a notification after a PostMessage so that we can wait for the | 71 // Forwards a notification after a PostMessage so that we can wait for the |
| 72 // MessageLoop to run. | 72 // MessageLoop to run. |
| 73 void NotifyReadError(PrefService* pref, int message_id) { | 73 void NotifyReadError(PrefService* pref, int message_id) { |
| 74 Source<PrefService> source(pref); | 74 Source<PrefService> source(pref); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 return (LoadPersistentPrefs() == PrefStore::PREF_READ_ERROR_NONE); | 142 return (LoadPersistentPrefs() == PrefStore::PREF_READ_ERROR_NONE); |
| 143 } | 143 } |
| 144 | 144 |
| 145 PrefStore::PrefReadError PrefService::LoadPersistentPrefs() { | 145 PrefStore::PrefReadError PrefService::LoadPersistentPrefs() { |
| 146 DCHECK(CalledOnValidThread()); | 146 DCHECK(CalledOnValidThread()); |
| 147 | 147 |
| 148 PrefStore::PrefReadError pref_error = pref_value_store_->ReadPrefs(); | 148 PrefStore::PrefReadError pref_error = pref_value_store_->ReadPrefs(); |
| 149 | 149 |
| 150 for (PreferenceSet::iterator it = prefs_.begin(); | 150 for (PreferenceSet::iterator it = prefs_.begin(); |
| 151 it != prefs_.end(); ++it) { | 151 it != prefs_.end(); ++it) { |
| 152 (*it)->pref_value_store_ = pref_value_store_.get(); | 152 (*it)->pref_service_ = this; |
| 153 } | 153 } |
| 154 | 154 |
| 155 return pref_error; | 155 return pref_error; |
| 156 } | 156 } |
| 157 | 157 |
| 158 bool PrefService::SavePersistentPrefs() { | 158 bool PrefService::SavePersistentPrefs() { |
| 159 DCHECK(CalledOnValidThread()); | 159 DCHECK(CalledOnValidThread()); |
| 160 | 160 |
| 161 return pref_value_store_->WritePrefs(); | 161 return pref_value_store_->WritePrefs(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void PrefService::ScheduleSavePersistentPrefs() { | 164 void PrefService::ScheduleSavePersistentPrefs() { |
| 165 DCHECK(CalledOnValidThread()); | 165 DCHECK(CalledOnValidThread()); |
| 166 | 166 |
| 167 pref_value_store_->ScheduleWritePrefs(); | 167 pref_value_store_->ScheduleWritePrefs(); |
| 168 } | 168 } |
| 169 | 169 |
| 170 void PrefService::RegisterBooleanPref(const char* path, | 170 void PrefService::RegisterBooleanPref(const char* path, |
| 171 bool default_value) { | 171 bool default_value) { |
| 172 Preference* pref = new Preference(pref_value_store_.get(), path, | 172 RegisterPreference(path, Value::CreateBooleanValue(default_value)); |
| 173 Value::CreateBooleanValue(default_value)); | |
| 174 RegisterPreference(pref); | |
| 175 } | 173 } |
| 176 | 174 |
| 177 void PrefService::RegisterIntegerPref(const char* path, int default_value) { | 175 void PrefService::RegisterIntegerPref(const char* path, int default_value) { |
| 178 Preference* pref = new Preference(pref_value_store_.get(), path, | 176 RegisterPreference(path, Value::CreateIntegerValue(default_value)); |
| 179 Value::CreateIntegerValue(default_value)); | |
| 180 RegisterPreference(pref); | |
| 181 } | 177 } |
| 182 | 178 |
| 183 void PrefService::RegisterRealPref(const char* path, double default_value) { | 179 void PrefService::RegisterRealPref(const char* path, double default_value) { |
| 184 Preference* pref = new Preference(pref_value_store_.get(), path, | 180 RegisterPreference(path, Value::CreateRealValue(default_value)); |
| 185 Value::CreateRealValue(default_value)); | |
| 186 RegisterPreference(pref); | |
| 187 } | 181 } |
| 188 | 182 |
| 189 void PrefService::RegisterStringPref(const char* path, | 183 void PrefService::RegisterStringPref(const char* path, |
| 190 const std::string& default_value) { | 184 const std::string& default_value) { |
| 191 Preference* pref = new Preference(pref_value_store_.get(), path, | 185 RegisterPreference(path, Value::CreateStringValue(default_value)); |
| 192 Value::CreateStringValue(default_value)); | |
| 193 RegisterPreference(pref); | |
| 194 } | 186 } |
| 195 | 187 |
| 196 void PrefService::RegisterFilePathPref(const char* path, | 188 void PrefService::RegisterFilePathPref(const char* path, |
| 197 const FilePath& default_value) { | 189 const FilePath& default_value) { |
| 198 Preference* pref = new Preference(pref_value_store_.get(), path, | 190 RegisterPreference(path, Value::CreateStringValue(default_value.value())); |
| 199 Value::CreateStringValue(default_value.value())); | |
| 200 RegisterPreference(pref); | |
| 201 } | 191 } |
| 202 | 192 |
| 203 void PrefService::RegisterListPref(const char* path) { | 193 void PrefService::RegisterListPref(const char* path) { |
| 204 Preference* pref = new Preference(pref_value_store_.get(), path, | 194 RegisterPreference(path, new ListValue()); |
| 205 new ListValue); | |
| 206 RegisterPreference(pref); | |
| 207 } | 195 } |
| 208 | 196 |
| 209 void PrefService::RegisterDictionaryPref(const char* path) { | 197 void PrefService::RegisterDictionaryPref(const char* path) { |
| 210 Preference* pref = new Preference(pref_value_store_.get(), path, | 198 RegisterPreference(path, new DictionaryValue()); |
| 211 new DictionaryValue()); | |
| 212 RegisterPreference(pref); | |
| 213 } | 199 } |
| 214 | 200 |
| 215 void PrefService::RegisterLocalizedBooleanPref(const char* path, | 201 void PrefService::RegisterLocalizedBooleanPref(const char* path, |
| 216 int locale_default_message_id) { | 202 int locale_default_message_id) { |
| 217 Preference* pref = new Preference(pref_value_store_.get(), path, | 203 RegisterPreference( |
| 204 path, |
| 218 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id)); | 205 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id)); |
| 219 RegisterPreference(pref); | |
| 220 } | 206 } |
| 221 | 207 |
| 222 void PrefService::RegisterLocalizedIntegerPref(const char* path, | 208 void PrefService::RegisterLocalizedIntegerPref(const char* path, |
| 223 int locale_default_message_id) { | 209 int locale_default_message_id) { |
| 224 Preference* pref = new Preference(pref_value_store_.get(), path, | 210 RegisterPreference( |
| 211 path, |
| 225 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id)); | 212 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id)); |
| 226 RegisterPreference(pref); | |
| 227 } | 213 } |
| 228 | 214 |
| 229 void PrefService::RegisterLocalizedRealPref(const char* path, | 215 void PrefService::RegisterLocalizedRealPref(const char* path, |
| 230 int locale_default_message_id) { | 216 int locale_default_message_id) { |
| 231 Preference* pref = new Preference(pref_value_store_.get(), path, | 217 RegisterPreference( |
| 218 path, |
| 232 CreateLocaleDefaultValue(Value::TYPE_REAL, locale_default_message_id)); | 219 CreateLocaleDefaultValue(Value::TYPE_REAL, locale_default_message_id)); |
| 233 RegisterPreference(pref); | |
| 234 } | 220 } |
| 235 | 221 |
| 236 void PrefService::RegisterLocalizedStringPref(const char* path, | 222 void PrefService::RegisterLocalizedStringPref(const char* path, |
| 237 int locale_default_message_id) { | 223 int locale_default_message_id) { |
| 238 Preference* pref = new Preference(pref_value_store_.get(), path, | 224 RegisterPreference( |
| 225 path, |
| 239 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id)); | 226 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id)); |
| 240 RegisterPreference(pref); | |
| 241 } | 227 } |
| 242 | 228 |
| 243 bool PrefService::GetBoolean(const char* path) const { | 229 bool PrefService::GetBoolean(const char* path) const { |
| 244 DCHECK(CalledOnValidThread()); | 230 DCHECK(CalledOnValidThread()); |
| 245 | 231 |
| 246 bool result = false; | 232 bool result = false; |
| 247 | 233 |
| 248 const Preference* pref = FindPreference(path); | 234 const Preference* pref = FindPreference(path); |
| 249 if (!pref) { | 235 if (!pref) { |
| 250 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 236 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 return FilePath(result); | 305 return FilePath(result); |
| 320 } | 306 } |
| 321 | 307 |
| 322 bool PrefService::HasPrefPath(const char* path) const { | 308 bool PrefService::HasPrefPath(const char* path) const { |
| 323 return pref_value_store_->HasPrefPath(path); | 309 return pref_value_store_->HasPrefPath(path); |
| 324 } | 310 } |
| 325 | 311 |
| 326 const PrefService::Preference* PrefService::FindPreference( | 312 const PrefService::Preference* PrefService::FindPreference( |
| 327 const char* pref_name) const { | 313 const char* pref_name) const { |
| 328 DCHECK(CalledOnValidThread()); | 314 DCHECK(CalledOnValidThread()); |
| 329 Preference p(NULL, pref_name, NULL); | 315 // We only look up prefs by name, so the type is irrelevant, except that it |
| 316 // must be one that Preference() allows (i.e., neither TYPE_NULL nor |
| 317 // TYPE_BINARY). |
| 318 Preference p(this, pref_name, Value::TYPE_INTEGER); |
| 330 PreferenceSet::const_iterator it = prefs_.find(&p); | 319 PreferenceSet::const_iterator it = prefs_.find(&p); |
| 331 return it == prefs_.end() ? NULL : *it; | 320 return it == prefs_.end() ? NULL : *it; |
| 332 } | 321 } |
| 333 | 322 |
| 334 bool PrefService::IsManagedPreference(const char* pref_name) const { | 323 bool PrefService::IsManagedPreference(const char* pref_name) const { |
| 335 const Preference* pref = FindPreference(pref_name); | 324 const Preference* pref = FindPreference(pref_name); |
| 336 if (pref && pref->IsManaged()) { | 325 if (pref && pref->IsManaged()) { |
| 337 return true; | 326 return true; |
| 338 } | 327 } |
| 339 return false; | 328 return false; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 370 void PrefService::AddPrefObserver(const char* path, | 359 void PrefService::AddPrefObserver(const char* path, |
| 371 NotificationObserver* obs) { | 360 NotificationObserver* obs) { |
| 372 pref_notifier_->AddPrefObserver(path, obs); | 361 pref_notifier_->AddPrefObserver(path, obs); |
| 373 } | 362 } |
| 374 | 363 |
| 375 void PrefService::RemovePrefObserver(const char* path, | 364 void PrefService::RemovePrefObserver(const char* path, |
| 376 NotificationObserver* obs) { | 365 NotificationObserver* obs) { |
| 377 pref_notifier_->RemovePrefObserver(path, obs); | 366 pref_notifier_->RemovePrefObserver(path, obs); |
| 378 } | 367 } |
| 379 | 368 |
| 380 void PrefService::RegisterPreference(Preference* pref) { | 369 void PrefService::RegisterPreference(const char* path, Value* default_value) { |
| 381 DCHECK(CalledOnValidThread()); | 370 DCHECK(CalledOnValidThread()); |
| 382 | 371 |
| 383 if (FindPreference(pref->name().c_str())) { | 372 // The main code path takes ownership, but most don't. We'll be safe. |
| 384 NOTREACHED() << "Tried to register duplicate pref " << pref->name(); | 373 scoped_ptr<Value> scoped_value(default_value); |
| 385 delete pref; | 374 |
| 375 if (FindPreference(path)) { |
| 376 NOTREACHED() << "Tried to register duplicate pref " << path; |
| 386 return; | 377 return; |
| 387 } | 378 } |
| 388 prefs_.insert(pref); | 379 |
| 380 // We set the default value of dictionaries and lists to be null so it's |
| 381 // easier for callers to check for empty dict/list prefs. The PrefValueStore |
| 382 // accepts ownership of the value (null or default_value). |
| 383 Value::ValueType orig_type = default_value->GetType(); |
| 384 if (Value::TYPE_LIST == orig_type || Value::TYPE_DICTIONARY == orig_type) { |
| 385 pref_value_store_->SetDefaultPrefValue(path, Value::CreateNullValue()); |
| 386 } else { |
| 387 // Hand off ownership. |
| 388 pref_value_store_->SetDefaultPrefValue(path, scoped_value.release()); |
| 389 } |
| 390 |
| 391 prefs_.insert(new Preference(this, path, orig_type)); |
| 389 } | 392 } |
| 390 | 393 |
| 391 void PrefService::ClearPref(const char* path) { | 394 void PrefService::ClearPref(const char* path) { |
| 392 DCHECK(CalledOnValidThread()); | 395 DCHECK(CalledOnValidThread()); |
| 393 | 396 |
| 394 const Preference* pref = FindPreference(path); | 397 const Preference* pref = FindPreference(path); |
| 395 if (!pref) { | 398 if (!pref) { |
| 396 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 399 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
| 397 return; | 400 return; |
| 398 } | 401 } |
| 399 Value* value; | 402 if (pref_value_store_->RemoveUserPrefValue(path)) |
| 400 bool has_old_value = pref_value_store_->GetValue(path, &value); | 403 pref_notifier_->OnUserPreferenceSet(path); |
| 401 pref_value_store_->RemoveUserPrefValue(path); | |
| 402 | |
| 403 // TODO(pamg): Removing the user value when there's a higher-priority setting | |
| 404 // doesn't change the value and shouldn't fire observers. | |
| 405 if (has_old_value) | |
| 406 pref_notifier_->FireObservers(path); | |
| 407 } | 404 } |
| 408 | 405 |
| 409 void PrefService::Set(const char* path, const Value& value) { | 406 void PrefService::Set(const char* path, const Value& value) { |
| 410 DCHECK(CalledOnValidThread()); | 407 DCHECK(CalledOnValidThread()); |
| 411 | 408 |
| 412 const Preference* pref = FindPreference(path); | 409 const Preference* pref = FindPreference(path); |
| 413 if (!pref) { | 410 if (!pref) { |
| 414 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 411 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 415 return; | 412 return; |
| 416 } | 413 } |
| 417 | 414 |
| 418 // Allow dictionary and list types to be set to null. | 415 // Allow dictionary and list types to be set to null, which removes their |
| 416 // user values. |
| 417 bool value_changed = false; |
| 419 if (value.GetType() == Value::TYPE_NULL && | 418 if (value.GetType() == Value::TYPE_NULL && |
| 420 (pref->type() == Value::TYPE_DICTIONARY || | 419 (pref->type() == Value::TYPE_DICTIONARY || |
| 421 pref->type() == Value::TYPE_LIST)) { | 420 pref->type() == Value::TYPE_LIST)) { |
| 422 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 421 value_changed = pref_value_store_->RemoveUserPrefValue(path); |
| 423 if (!old_value->Equals(&value)) { | 422 } else if (pref->type() != value.GetType()) { |
| 424 pref_value_store_->RemoveUserPrefValue(path); | 423 NOTREACHED() << "Trying to set pref " << path << " of type " << pref->type() |
| 425 pref_notifier_->FireObservers(path); | 424 << " to value of type " << value.GetType(); |
| 426 } | 425 } else { |
| 427 return; | 426 value_changed = pref_value_store_->SetUserPrefValue(path, value.DeepCopy()); |
| 428 } | 427 } |
| 429 | 428 |
| 430 if (pref->type() != value.GetType()) { | 429 if (value_changed) |
| 431 NOTREACHED() << "Wrong type for Set: " << path; | 430 pref_notifier_->OnUserPreferenceSet(path); |
| 432 } | |
| 433 | |
| 434 scoped_ptr<Value> old_value(GetPrefCopy(path)); | |
| 435 pref_value_store_->SetUserPrefValue(path, value.DeepCopy()); | |
| 436 | |
| 437 pref_notifier_->OnUserPreferenceSet(path, old_value.get()); | |
| 438 } | 431 } |
| 439 | 432 |
| 440 void PrefService::SetBoolean(const char* path, bool value) { | 433 void PrefService::SetBoolean(const char* path, bool value) { |
| 441 DCHECK(CalledOnValidThread()); | 434 SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
| 442 | |
| 443 const Preference* pref = FindPreference(path); | |
| 444 if (!pref) { | |
| 445 NOTREACHED() << "Trying to write an unregistered pref: " << path; | |
| 446 return; | |
| 447 } | |
| 448 if (pref->IsManaged()) { | |
| 449 NOTREACHED() << "Preference is managed: " << path; | |
| 450 return; | |
| 451 } | |
| 452 if (pref->type() != Value::TYPE_BOOLEAN) { | |
| 453 NOTREACHED() << "Wrong type for SetBoolean: " << path; | |
| 454 return; | |
| 455 } | |
| 456 | |
| 457 scoped_ptr<Value> old_value(GetPrefCopy(path)); | |
| 458 Value* new_value = Value::CreateBooleanValue(value); | |
| 459 pref_value_store_->SetUserPrefValue(path, new_value); | |
| 460 | |
| 461 pref_notifier_->OnUserPreferenceSet(path, old_value.get()); | |
| 462 } | 435 } |
| 463 | 436 |
| 464 void PrefService::SetInteger(const char* path, int value) { | 437 void PrefService::SetInteger(const char* path, int value) { |
| 465 DCHECK(CalledOnValidThread()); | 438 SetUserPrefValue(path, Value::CreateIntegerValue(value)); |
| 466 | |
| 467 const Preference* pref = FindPreference(path); | |
| 468 if (!pref) { | |
| 469 NOTREACHED() << "Trying to write an unregistered pref: " << path; | |
| 470 return; | |
| 471 } | |
| 472 if (pref->IsManaged()) { | |
| 473 NOTREACHED() << "Preference is managed: " << path; | |
| 474 return; | |
| 475 } | |
| 476 if (pref->type() != Value::TYPE_INTEGER) { | |
| 477 NOTREACHED() << "Wrong type for SetInteger: " << path; | |
| 478 return; | |
| 479 } | |
| 480 | |
| 481 scoped_ptr<Value> old_value(GetPrefCopy(path)); | |
| 482 Value* new_value = Value::CreateIntegerValue(value); | |
| 483 pref_value_store_->SetUserPrefValue(path, new_value); | |
| 484 | |
| 485 pref_notifier_->OnUserPreferenceSet(path, old_value.get()); | |
| 486 } | 439 } |
| 487 | 440 |
| 488 void PrefService::SetReal(const char* path, double value) { | 441 void PrefService::SetReal(const char* path, double value) { |
| 489 DCHECK(CalledOnValidThread()); | 442 SetUserPrefValue(path, Value::CreateRealValue(value)); |
| 490 | |
| 491 const Preference* pref = FindPreference(path); | |
| 492 if (!pref) { | |
| 493 NOTREACHED() << "Trying to write an unregistered pref: " << path; | |
| 494 return; | |
| 495 } | |
| 496 if (pref->IsManaged()) { | |
| 497 NOTREACHED() << "Preference is managed: " << path; | |
| 498 return; | |
| 499 } | |
| 500 if (pref->type() != Value::TYPE_REAL) { | |
| 501 NOTREACHED() << "Wrong type for SetReal: " << path; | |
| 502 return; | |
| 503 } | |
| 504 | |
| 505 scoped_ptr<Value> old_value(GetPrefCopy(path)); | |
| 506 Value* new_value = Value::CreateRealValue(value); | |
| 507 pref_value_store_->SetUserPrefValue(path, new_value); | |
| 508 | |
| 509 pref_notifier_->OnUserPreferenceSet(path, old_value.get()); | |
| 510 } | 443 } |
| 511 | 444 |
| 512 void PrefService::SetString(const char* path, const std::string& value) { | 445 void PrefService::SetString(const char* path, const std::string& value) { |
| 513 DCHECK(CalledOnValidThread()); | 446 SetUserPrefValue(path, Value::CreateStringValue(value)); |
| 514 | |
| 515 const Preference* pref = FindPreference(path); | |
| 516 if (!pref) { | |
| 517 NOTREACHED() << "Trying to write an unregistered pref: " << path; | |
| 518 return; | |
| 519 } | |
| 520 if (pref->IsManaged()) { | |
| 521 NOTREACHED() << "Preference is managed: " << path; | |
| 522 return; | |
| 523 } | |
| 524 if (pref->type() != Value::TYPE_STRING) { | |
| 525 NOTREACHED() << "Wrong type for SetString: " << path; | |
| 526 return; | |
| 527 } | |
| 528 | |
| 529 scoped_ptr<Value> old_value(GetPrefCopy(path)); | |
| 530 Value* new_value = Value::CreateStringValue(value); | |
| 531 pref_value_store_->SetUserPrefValue(path, new_value); | |
| 532 | |
| 533 pref_notifier_->OnUserPreferenceSet(path, old_value.get()); | |
| 534 } | 447 } |
| 535 | 448 |
| 536 void PrefService::SetFilePath(const char* path, const FilePath& value) { | 449 void PrefService::SetFilePath(const char* path, const FilePath& value) { |
| 537 DCHECK(CalledOnValidThread()); | |
| 538 | |
| 539 const Preference* pref = FindPreference(path); | |
| 540 if (!pref) { | |
| 541 NOTREACHED() << "Trying to write an unregistered pref: " << path; | |
| 542 return; | |
| 543 } | |
| 544 if (pref->IsManaged()) { | |
| 545 NOTREACHED() << "Preference is managed: " << path; | |
| 546 return; | |
| 547 } | |
| 548 if (pref->type() != Value::TYPE_STRING) { | |
| 549 NOTREACHED() << "Wrong type for SetFilePath: " << path; | |
| 550 return; | |
| 551 } | |
| 552 | |
| 553 scoped_ptr<Value> old_value(GetPrefCopy(path)); | |
| 554 #if defined(OS_POSIX) | 450 #if defined(OS_POSIX) |
| 555 // Value::SetString only knows about UTF8 strings, so convert the path from | 451 // Value::SetString only knows about UTF8 strings, so convert the path from |
| 556 // the system native value to UTF8. | 452 // the system native value to UTF8. |
| 557 std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(value.value())); | 453 std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(value.value())); |
| 558 Value* new_value = Value::CreateStringValue(path_utf8); | 454 Value* new_value = Value::CreateStringValue(path_utf8); |
| 559 pref_value_store_->SetUserPrefValue(path, new_value); | |
| 560 #else | 455 #else |
| 561 Value* new_value = Value::CreateStringValue(value.value()); | 456 Value* new_value = Value::CreateStringValue(value.value()); |
| 562 pref_value_store_->SetUserPrefValue(path, new_value); | |
| 563 #endif | 457 #endif |
| 564 | 458 |
| 565 pref_notifier_->OnUserPreferenceSet(path, old_value.get()); | 459 SetUserPrefValue(path, new_value); |
| 566 } | 460 } |
| 567 | 461 |
| 568 void PrefService::SetInt64(const char* path, int64 value) { | 462 void PrefService::SetInt64(const char* path, int64 value) { |
| 569 DCHECK(CalledOnValidThread()); | 463 SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value))); |
| 570 | |
| 571 const Preference* pref = FindPreference(path); | |
| 572 if (!pref) { | |
| 573 NOTREACHED() << "Trying to write an unregistered pref: " << path; | |
| 574 return; | |
| 575 } | |
| 576 if (pref->IsManaged()) { | |
| 577 NOTREACHED() << "Preference is managed: " << path; | |
| 578 return; | |
| 579 } | |
| 580 if (pref->type() != Value::TYPE_STRING) { | |
| 581 NOTREACHED() << "Wrong type for SetInt64: " << path; | |
| 582 return; | |
| 583 } | |
| 584 | |
| 585 scoped_ptr<Value> old_value(GetPrefCopy(path)); | |
| 586 Value* new_value = Value::CreateStringValue(base::Int64ToString(value)); | |
| 587 pref_value_store_->SetUserPrefValue(path, new_value); | |
| 588 | |
| 589 pref_notifier_->OnUserPreferenceSet(path, old_value.get()); | |
| 590 } | 464 } |
| 591 | 465 |
| 592 int64 PrefService::GetInt64(const char* path) const { | 466 int64 PrefService::GetInt64(const char* path) const { |
| 593 DCHECK(CalledOnValidThread()); | 467 DCHECK(CalledOnValidThread()); |
| 594 | 468 |
| 595 const Preference* pref = FindPreference(path); | 469 const Preference* pref = FindPreference(path); |
| 596 if (!pref) { | 470 if (!pref) { |
| 597 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 471 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 598 return 0; | 472 return 0; |
| 599 } | 473 } |
| 600 std::string result("0"); | 474 std::string result("0"); |
| 601 bool rv = pref->GetValue()->GetAsString(&result); | 475 bool rv = pref->GetValue()->GetAsString(&result); |
| 602 DCHECK(rv); | 476 DCHECK(rv); |
| 603 | 477 |
| 604 int64 val; | 478 int64 val; |
| 605 base::StringToInt64(result, &val); | 479 base::StringToInt64(result, &val); |
| 606 return val; | 480 return val; |
| 607 } | 481 } |
| 608 | 482 |
| 609 void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { | 483 void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { |
| 610 Preference* pref = new Preference(pref_value_store_.get(), path, | 484 RegisterPreference( |
| 611 Value::CreateStringValue(base::Int64ToString(default_value))); | 485 path, Value::CreateStringValue(base::Int64ToString(default_value))); |
| 612 RegisterPreference(pref); | |
| 613 } | 486 } |
| 614 | 487 |
| 615 DictionaryValue* PrefService::GetMutableDictionary(const char* path) { | 488 DictionaryValue* PrefService::GetMutableDictionary(const char* path) { |
| 616 DCHECK(CalledOnValidThread()); | 489 DCHECK(CalledOnValidThread()); |
| 617 | 490 |
| 618 const Preference* pref = FindPreference(path); | 491 const Preference* pref = FindPreference(path); |
| 619 if (!pref) { | 492 if (!pref) { |
| 620 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 493 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 621 return NULL; | 494 return NULL; |
| 622 } | 495 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 645 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 518 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 646 return NULL; | 519 return NULL; |
| 647 } | 520 } |
| 648 if (pref->type() != Value::TYPE_LIST) { | 521 if (pref->type() != Value::TYPE_LIST) { |
| 649 NOTREACHED() << "Wrong type for GetMutableList: " << path; | 522 NOTREACHED() << "Wrong type for GetMutableList: " << path; |
| 650 return NULL; | 523 return NULL; |
| 651 } | 524 } |
| 652 | 525 |
| 653 ListValue* list = NULL; | 526 ListValue* list = NULL; |
| 654 Value* tmp_value = NULL; | 527 Value* tmp_value = NULL; |
| 655 if (!pref_value_store_->GetValue(path, &tmp_value)) { | 528 if (!pref_value_store_->GetValue(path, &tmp_value) || |
| 529 !tmp_value->IsType(Value::TYPE_LIST)) { |
| 656 list = new ListValue; | 530 list = new ListValue; |
| 657 pref_value_store_->SetUserPrefValue(path, list); | 531 pref_value_store_->SetUserPrefValue(path, list); |
| 658 } else { | 532 } else { |
| 659 list = static_cast<ListValue*>(tmp_value); | 533 list = static_cast<ListValue*>(tmp_value); |
| 660 } | 534 } |
| 661 return list; | 535 return list; |
| 662 } | 536 } |
| 663 | 537 |
| 664 Value* PrefService::GetPrefCopy(const char* path) { | 538 Value* PrefService::GetPrefCopy(const char* path) { |
| 665 DCHECK(CalledOnValidThread()); | 539 DCHECK(CalledOnValidThread()); |
| 666 | 540 |
| 667 const Preference* pref = FindPreference(path); | 541 const Preference* pref = FindPreference(path); |
| 668 DCHECK(pref); | 542 DCHECK(pref); |
| 669 return pref->GetValue()->DeepCopy(); | 543 return pref->GetValue()->DeepCopy(); |
| 670 } | 544 } |
| 671 | 545 |
| 546 void PrefService::SetUserPrefValue(const char* path, Value* new_value) { |
| 547 DCHECK(CalledOnValidThread()); |
| 548 |
| 549 const Preference* pref = FindPreference(path); |
| 550 if (!pref) { |
| 551 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 552 return; |
| 553 } |
| 554 if (pref->IsManaged()) { |
| 555 NOTREACHED() << "Preference is managed: " << path; |
| 556 return; |
| 557 } |
| 558 if (pref->type() != new_value->GetType()) { |
| 559 NOTREACHED() << "Trying to set pref " << path << " of type " << pref->type() |
| 560 << " to value of type " << new_value->GetType(); |
| 561 return; |
| 562 } |
| 563 |
| 564 if (pref_value_store_->SetUserPrefValue(path, new_value)) |
| 565 pref_notifier_->OnUserPreferenceSet(path); |
| 566 } |
| 567 |
| 672 /////////////////////////////////////////////////////////////////////////////// | 568 /////////////////////////////////////////////////////////////////////////////// |
| 673 // PrefService::Preference | 569 // PrefService::Preference |
| 674 | 570 |
| 675 PrefService::Preference::Preference(PrefValueStore* pref_value_store, | 571 PrefService::Preference::Preference(const PrefService* service, |
| 676 const char* name, | 572 const char* name, |
| 677 Value* default_value) | 573 Value::ValueType type) |
| 678 : type_(Value::TYPE_NULL), | 574 : type_(type), |
| 679 name_(name), | 575 name_(name), |
| 680 default_value_(default_value), | 576 pref_service_(service) { |
| 681 pref_value_store_(pref_value_store) { | |
| 682 DCHECK(name); | 577 DCHECK(name); |
| 683 | 578 DCHECK(service); |
| 684 if (default_value) { | 579 DCHECK(type != Value::TYPE_NULL && type != Value::TYPE_BINARY) << |
| 685 type_ = default_value->GetType(); | 580 "invalid preference type: " << type; |
| 686 DCHECK(type_ != Value::TYPE_NULL && type_ != Value::TYPE_BINARY) << | |
| 687 "invalid preference type: " << type_; | |
| 688 } | |
| 689 | |
| 690 // We set the default value of lists and dictionaries to be null so it's | |
| 691 // easier for callers to check for empty list/dict prefs. | |
| 692 if (Value::TYPE_LIST == type_ || Value::TYPE_DICTIONARY == type_) | |
| 693 default_value_.reset(Value::CreateNullValue()); | |
| 694 } | 581 } |
| 695 | 582 |
| 696 const Value* PrefService::Preference::GetValue() const { | 583 const Value* PrefService::Preference::GetValue() const { |
| 697 DCHECK(NULL != pref_value_store_) << | 584 DCHECK(pref_service_->FindPreference(name_.c_str())) << |
| 698 "Must register pref before getting its value"; | 585 "Must register pref before getting its value"; |
| 699 | 586 |
| 700 Value* temp_value = NULL; | 587 Value* found_value = NULL; |
| 701 if (pref_value_store_->GetValue(name_, &temp_value) && | 588 if (pref_service_->pref_value_store_->GetValue(name_, &found_value)) { |
| 702 temp_value->GetType() == type_) { | 589 Value::ValueType found_type = found_value->GetType(); |
| 703 return temp_value; | 590 // Dictionaries and lists have default values of TYPE_NULL. |
| 591 if (found_type == type_ || |
| 592 (found_type == Value::TYPE_NULL |
| 593 && (type_ == Value::TYPE_LIST || type_ == Value::TYPE_DICTIONARY))) { |
| 594 return found_value; |
| 595 } |
| 704 } | 596 } |
| 705 | 597 |
| 706 // Pref not found, just return the app default. | 598 // Every registered preference has at least a default value. |
| 707 return default_value_.get(); | 599 NOTREACHED() << "no default value for registered pref " << name_; |
| 600 return NULL; |
| 601 } |
| 602 |
| 603 bool PrefService::Preference::IsManaged() const { |
| 604 return pref_service_->pref_value_store_-> |
| 605 PrefValueInManagedStore(name_.c_str()); |
| 606 } |
| 607 |
| 608 bool PrefService::Preference::HasExtensionSetting() const { |
| 609 return pref_service_->pref_value_store_-> |
| 610 PrefValueInExtensionStore(name_.c_str()); |
| 611 } |
| 612 |
| 613 bool PrefService::Preference::HasUserSetting() const { |
| 614 return pref_service_->pref_value_store_-> |
| 615 PrefValueInUserStore(name_.c_str()); |
| 616 } |
| 617 |
| 618 bool PrefService::Preference::IsExtensionControlled() const { |
| 619 return pref_service_->pref_value_store_-> |
| 620 PrefValueFromExtensionStore(name_.c_str()); |
| 621 } |
| 622 |
| 623 bool PrefService::Preference::IsUserControlled() const { |
| 624 return pref_service_->pref_value_store_-> |
| 625 PrefValueFromUserStore(name_.c_str()); |
| 708 } | 626 } |
| 709 | 627 |
| 710 bool PrefService::Preference::IsDefaultValue() const { | 628 bool PrefService::Preference::IsDefaultValue() const { |
| 711 DCHECK(default_value_.get()); | 629 return pref_service_->pref_value_store_-> |
| 712 return default_value_->Equals(GetValue()); | 630 PrefValueFromDefaultStore(name_.c_str()); |
| 713 } | |
| 714 | |
| 715 bool PrefService::Preference::IsManaged() const { | |
| 716 return pref_value_store_->PrefValueInManagedStore(name_.c_str()); | |
| 717 } | |
| 718 | |
| 719 bool PrefService::Preference::HasExtensionSetting() const { | |
| 720 return pref_value_store_->PrefValueInExtensionStore(name_.c_str()); | |
| 721 } | |
| 722 | |
| 723 bool PrefService::Preference::HasUserSetting() const { | |
| 724 return pref_value_store_->PrefValueInUserStore(name_.c_str()); | |
| 725 } | |
| 726 | |
| 727 bool PrefService::Preference::IsExtensionControlled() const { | |
| 728 return pref_value_store_->PrefValueFromExtensionStore(name_.c_str()); | |
| 729 } | |
| 730 | |
| 731 bool PrefService::Preference::IsUserControlled() const { | |
| 732 return pref_value_store_->PrefValueFromUserStore(name_.c_str()); | |
| 733 } | 631 } |
| 734 | 632 |
| 735 bool PrefService::Preference::IsUserModifiable() const { | 633 bool PrefService::Preference::IsUserModifiable() const { |
| 736 return pref_value_store_->PrefValueUserModifiable(name_.c_str()); | 634 return pref_service_->pref_value_store_-> |
| 635 PrefValueUserModifiable(name_.c_str()); |
| 737 } | 636 } |
| OLD | NEW |