OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/common/pref_service.h" | 5 #include "chrome/common/pref_service.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 bool PrefService::IsPrefRegistered(const wchar_t* path) { | 282 bool PrefService::IsPrefRegistered(const wchar_t* path) { |
283 DCHECK(CalledOnValidThread()); | 283 DCHECK(CalledOnValidThread()); |
284 // TODO(tc): We can remove this method and just use FindPreference. | 284 // TODO(tc): We can remove this method and just use FindPreference. |
285 return FindPreference(path) ? true : false; | 285 return FindPreference(path) ? true : false; |
286 } | 286 } |
287 | 287 |
288 bool PrefService::GetBoolean(const wchar_t* path) const { | 288 bool PrefService::GetBoolean(const wchar_t* path) const { |
289 DCHECK(CalledOnValidThread()); | 289 DCHECK(CalledOnValidThread()); |
290 | 290 |
291 bool result = false; | 291 bool result = false; |
292 if (transient_->GetBoolean(path, &result)) | 292 if (transient_->GetBoolean(WideToUTF16Hack(path), &result)) |
293 return result; | 293 return result; |
294 | 294 |
295 const Preference* pref = FindPreference(path); | 295 const Preference* pref = FindPreference(path); |
296 if (!pref) { | 296 if (!pref) { |
297 DCHECK(false) << "Trying to read an unregistered pref: " << path; | 297 DCHECK(false) << "Trying to read an unregistered pref: " << path; |
298 return result; | 298 return result; |
299 } | 299 } |
300 bool rv = pref->GetValue()->GetAsBoolean(&result); | 300 bool rv = pref->GetValue()->GetAsBoolean(&result); |
301 DCHECK(rv); | 301 DCHECK(rv); |
302 return result; | 302 return result; |
303 } | 303 } |
304 | 304 |
305 int PrefService::GetInteger(const wchar_t* path) const { | 305 int PrefService::GetInteger(const wchar_t* path) const { |
306 DCHECK(CalledOnValidThread()); | 306 DCHECK(CalledOnValidThread()); |
307 | 307 |
308 int result = 0; | 308 int result = 0; |
309 if (transient_->GetInteger(path, &result)) | 309 if (transient_->GetInteger(WideToUTF16Hack(path), &result)) |
310 return result; | 310 return result; |
311 | 311 |
312 const Preference* pref = FindPreference(path); | 312 const Preference* pref = FindPreference(path); |
313 if (!pref) { | 313 if (!pref) { |
314 DCHECK(false) << "Trying to read an unregistered pref: " << path; | 314 DCHECK(false) << "Trying to read an unregistered pref: " << path; |
315 return result; | 315 return result; |
316 } | 316 } |
317 bool rv = pref->GetValue()->GetAsInteger(&result); | 317 bool rv = pref->GetValue()->GetAsInteger(&result); |
318 DCHECK(rv); | 318 DCHECK(rv); |
319 return result; | 319 return result; |
320 } | 320 } |
321 | 321 |
322 double PrefService::GetReal(const wchar_t* path) const { | 322 double PrefService::GetReal(const wchar_t* path) const { |
323 DCHECK(CalledOnValidThread()); | 323 DCHECK(CalledOnValidThread()); |
324 | 324 |
325 double result = 0.0; | 325 double result = 0.0; |
326 if (transient_->GetReal(path, &result)) | 326 if (transient_->GetReal(WideToUTF16Hack(path), &result)) |
327 return result; | 327 return result; |
328 | 328 |
329 const Preference* pref = FindPreference(path); | 329 const Preference* pref = FindPreference(path); |
330 if (!pref) { | 330 if (!pref) { |
331 DCHECK(false) << "Trying to read an unregistered pref: " << path; | 331 DCHECK(false) << "Trying to read an unregistered pref: " << path; |
332 return result; | 332 return result; |
333 } | 333 } |
334 bool rv = pref->GetValue()->GetAsReal(&result); | 334 bool rv = pref->GetValue()->GetAsReal(&result); |
335 DCHECK(rv); | 335 DCHECK(rv); |
336 return result; | 336 return result; |
337 } | 337 } |
338 | 338 |
339 std::wstring PrefService::GetString(const wchar_t* path) const { | 339 std::wstring PrefService::GetString(const wchar_t* path) const { |
340 DCHECK(CalledOnValidThread()); | 340 DCHECK(CalledOnValidThread()); |
341 | 341 |
| 342 string16 result16; |
| 343 if (transient_->GetString(WideToUTF16Hack(path), &result16)) |
| 344 return UTF16ToWideHack(result16); |
| 345 |
342 std::wstring result; | 346 std::wstring result; |
343 if (transient_->GetString(path, &result)) | |
344 return result; | |
345 | |
346 const Preference* pref = FindPreference(path); | 347 const Preference* pref = FindPreference(path); |
347 if (!pref) { | 348 if (!pref) { |
348 #if defined(OS_WIN) | 349 #if defined(OS_WIN) |
349 DCHECK(false) << "Trying to read an unregistered pref: " << path; | 350 DCHECK(false) << "Trying to read an unregistered pref: " << path; |
350 #else | 351 #else |
351 // TODO(port): remove this exception | 352 // TODO(port): remove this exception |
352 #endif | 353 #endif |
353 return result; | 354 return result; |
354 } | 355 } |
355 bool rv = pref->GetValue()->GetAsString(&result); | 356 bool rv = pref->GetValue()->GetAsString(&result); |
356 DCHECK(rv); | 357 DCHECK(rv); |
357 return result; | 358 return result; |
358 } | 359 } |
359 | 360 |
360 bool PrefService::HasPrefPath(const wchar_t* path) const { | 361 bool PrefService::HasPrefPath(const wchar_t* path) const { |
361 Value* value = NULL; | 362 Value* value = NULL; |
362 return (transient_->Get(path, &value) || persistent_->Get(path, &value)); | 363 string16 path16 = WideToUTF16Hack(path); |
| 364 return (transient_->Get(path16, &value) || persistent_->Get(path16, &value)); |
363 } | 365 } |
364 | 366 |
365 const PrefService::Preference* PrefService::FindPreference( | 367 const PrefService::Preference* PrefService::FindPreference( |
366 const wchar_t* pref_name) const { | 368 const wchar_t* pref_name) const { |
367 DCHECK(CalledOnValidThread()); | 369 DCHECK(CalledOnValidThread()); |
368 Preference p(NULL, pref_name, NULL); | 370 Preference p(NULL, pref_name, NULL); |
369 PreferenceSet::const_iterator it = prefs_.find(&p); | 371 PreferenceSet::const_iterator it = prefs_.find(&p); |
370 return it == prefs_.end() ? NULL : *it; | 372 return it == prefs_.end() ? NULL : *it; |
371 } | 373 } |
372 | 374 |
373 const DictionaryValue* PrefService::GetDictionary(const wchar_t* path) const { | 375 const DictionaryValue* PrefService::GetDictionary(const wchar_t* path) const { |
374 DCHECK(CalledOnValidThread()); | 376 DCHECK(CalledOnValidThread()); |
375 | 377 |
376 DictionaryValue* result = NULL; | 378 DictionaryValue* result = NULL; |
377 if (transient_->GetDictionary(path, &result)) | 379 if (transient_->GetDictionary(WideToUTF16Hack(path), &result)) |
378 return result; | 380 return result; |
379 | 381 |
380 const Preference* pref = FindPreference(path); | 382 const Preference* pref = FindPreference(path); |
381 if (!pref) { | 383 if (!pref) { |
382 DCHECK(false) << "Trying to read an unregistered pref: " << path; | 384 DCHECK(false) << "Trying to read an unregistered pref: " << path; |
383 return NULL; | 385 return NULL; |
384 } | 386 } |
385 const Value* value = pref->GetValue(); | 387 const Value* value = pref->GetValue(); |
386 if (value->GetType() == Value::TYPE_NULL) | 388 if (value->GetType() == Value::TYPE_NULL) |
387 return NULL; | 389 return NULL; |
388 return static_cast<const DictionaryValue*>(value); | 390 return static_cast<const DictionaryValue*>(value); |
389 } | 391 } |
390 | 392 |
391 const ListValue* PrefService::GetList(const wchar_t* path) const { | 393 const ListValue* PrefService::GetList(const wchar_t* path) const { |
392 DCHECK(CalledOnValidThread()); | 394 DCHECK(CalledOnValidThread()); |
393 | 395 |
394 ListValue* result = NULL; | 396 ListValue* result = NULL; |
395 if (transient_->GetList(path, &result)) | 397 if (transient_->GetList(WideToUTF16Hack(path), &result)) |
396 return result; | 398 return result; |
397 | 399 |
398 const Preference* pref = FindPreference(path); | 400 const Preference* pref = FindPreference(path); |
399 if (!pref) { | 401 if (!pref) { |
400 DCHECK(false) << "Trying to read an unregistered pref: " << path; | 402 DCHECK(false) << "Trying to read an unregistered pref: " << path; |
401 return NULL; | 403 return NULL; |
402 } | 404 } |
403 const Value* value = pref->GetValue(); | 405 const Value* value = pref->GetValue(); |
404 if (value->GetType() == Value::TYPE_NULL) | 406 if (value->GetType() == Value::TYPE_NULL) |
405 return NULL; | 407 return NULL; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 | 468 |
467 void PrefService::ClearPref(const wchar_t* path) { | 469 void PrefService::ClearPref(const wchar_t* path) { |
468 DCHECK(CalledOnValidThread()); | 470 DCHECK(CalledOnValidThread()); |
469 | 471 |
470 const Preference* pref = FindPreference(path); | 472 const Preference* pref = FindPreference(path); |
471 if (!pref) { | 473 if (!pref) { |
472 DCHECK(false) << "Trying to clear an unregistered pref: " << path; | 474 DCHECK(false) << "Trying to clear an unregistered pref: " << path; |
473 return; | 475 return; |
474 } | 476 } |
475 | 477 |
476 transient_->Remove(path, NULL); | 478 string16 path16 = WideToUTF16Hack(path); |
| 479 transient_->Remove(path16, NULL); |
477 Value* value; | 480 Value* value; |
478 bool has_old_value = persistent_->Get(path, &value); | 481 bool has_old_value = persistent_->Get(path16, &value); |
479 persistent_->Remove(path, NULL); | 482 persistent_->Remove(path16, NULL); |
480 | 483 |
481 if (has_old_value) | 484 if (has_old_value) |
482 FireObservers(path); | 485 FireObservers(path); |
483 } | 486 } |
484 | 487 |
485 void PrefService::SetBoolean(const wchar_t* path, bool value) { | 488 void PrefService::SetBoolean(const wchar_t* path, bool value) { |
486 DCHECK(CalledOnValidThread()); | 489 DCHECK(CalledOnValidThread()); |
487 | 490 |
488 const Preference* pref = FindPreference(path); | 491 const Preference* pref = FindPreference(path); |
489 if (!pref) { | 492 if (!pref) { |
490 DCHECK(false) << "Trying to write an unregistered pref: " << path; | 493 DCHECK(false) << "Trying to write an unregistered pref: " << path; |
491 return; | 494 return; |
492 } | 495 } |
493 if (pref->type() != Value::TYPE_BOOLEAN) { | 496 if (pref->type() != Value::TYPE_BOOLEAN) { |
494 DCHECK(false) << "Wrong type for SetBoolean: " << path; | 497 DCHECK(false) << "Wrong type for SetBoolean: " << path; |
495 return; | 498 return; |
496 } | 499 } |
497 | 500 |
498 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 501 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
499 bool rv = persistent_->SetBoolean(path, value); | 502 bool rv = persistent_->SetBoolean(WideToUTF16Hack(path), value); |
500 DCHECK(rv); | 503 DCHECK(rv); |
501 | 504 |
502 FireObserversIfChanged(path, old_value.get()); | 505 FireObserversIfChanged(path, old_value.get()); |
503 } | 506 } |
504 | 507 |
505 void PrefService::SetInteger(const wchar_t* path, int value) { | 508 void PrefService::SetInteger(const wchar_t* path, int value) { |
506 DCHECK(CalledOnValidThread()); | 509 DCHECK(CalledOnValidThread()); |
507 | 510 |
508 const Preference* pref = FindPreference(path); | 511 const Preference* pref = FindPreference(path); |
509 if (!pref) { | 512 if (!pref) { |
510 DCHECK(false) << "Trying to write an unregistered pref: " << path; | 513 DCHECK(false) << "Trying to write an unregistered pref: " << path; |
511 return; | 514 return; |
512 } | 515 } |
513 if (pref->type() != Value::TYPE_INTEGER) { | 516 if (pref->type() != Value::TYPE_INTEGER) { |
514 DCHECK(false) << "Wrong type for SetInteger: " << path; | 517 DCHECK(false) << "Wrong type for SetInteger: " << path; |
515 return; | 518 return; |
516 } | 519 } |
517 | 520 |
518 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 521 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
519 bool rv = persistent_->SetInteger(path, value); | 522 bool rv = persistent_->SetInteger(WideToUTF16Hack(path), value); |
520 DCHECK(rv); | 523 DCHECK(rv); |
521 | 524 |
522 FireObserversIfChanged(path, old_value.get()); | 525 FireObserversIfChanged(path, old_value.get()); |
523 } | 526 } |
524 | 527 |
525 void PrefService::SetReal(const wchar_t* path, double value) { | 528 void PrefService::SetReal(const wchar_t* path, double value) { |
526 DCHECK(CalledOnValidThread()); | 529 DCHECK(CalledOnValidThread()); |
527 | 530 |
528 const Preference* pref = FindPreference(path); | 531 const Preference* pref = FindPreference(path); |
529 if (!pref) { | 532 if (!pref) { |
530 DCHECK(false) << "Trying to write an unregistered pref: " << path; | 533 DCHECK(false) << "Trying to write an unregistered pref: " << path; |
531 return; | 534 return; |
532 } | 535 } |
533 if (pref->type() != Value::TYPE_REAL) { | 536 if (pref->type() != Value::TYPE_REAL) { |
534 DCHECK(false) << "Wrong type for SetReal: " << path; | 537 DCHECK(false) << "Wrong type for SetReal: " << path; |
535 return; | 538 return; |
536 } | 539 } |
537 | 540 |
538 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 541 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
539 bool rv = persistent_->SetReal(path, value); | 542 bool rv = persistent_->SetReal(WideToUTF16Hack(path), value); |
540 DCHECK(rv); | 543 DCHECK(rv); |
541 | 544 |
542 FireObserversIfChanged(path, old_value.get()); | 545 FireObserversIfChanged(path, old_value.get()); |
543 } | 546 } |
544 | 547 |
545 void PrefService::SetString(const wchar_t* path, const std::wstring& value) { | 548 void PrefService::SetString(const wchar_t* path, const std::wstring& value) { |
546 DCHECK(CalledOnValidThread()); | 549 DCHECK(CalledOnValidThread()); |
547 | 550 |
548 const Preference* pref = FindPreference(path); | 551 const Preference* pref = FindPreference(path); |
549 if (!pref) { | 552 if (!pref) { |
550 DCHECK(false) << "Trying to write an unregistered pref: " << path; | 553 DCHECK(false) << "Trying to write an unregistered pref: " << path; |
551 return; | 554 return; |
552 } | 555 } |
553 if (pref->type() != Value::TYPE_STRING) { | 556 if (pref->type() != Value::TYPE_STRING) { |
554 DCHECK(false) << "Wrong type for SetString: " << path; | 557 DCHECK(false) << "Wrong type for SetString: " << path; |
555 return; | 558 return; |
556 } | 559 } |
557 | 560 |
558 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 561 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
559 bool rv = persistent_->SetString(path, value); | 562 bool rv = persistent_->SetString(WideToUTF16Hack(path), |
| 563 WideToUTF16Hack(value)); |
560 DCHECK(rv); | 564 DCHECK(rv); |
561 | 565 |
562 FireObserversIfChanged(path, old_value.get()); | 566 FireObserversIfChanged(path, old_value.get()); |
563 } | 567 } |
564 | 568 |
565 DictionaryValue* PrefService::GetMutableDictionary(const wchar_t* path) { | 569 DictionaryValue* PrefService::GetMutableDictionary(const wchar_t* path) { |
566 DCHECK(CalledOnValidThread()); | 570 DCHECK(CalledOnValidThread()); |
567 | 571 |
568 const Preference* pref = FindPreference(path); | 572 const Preference* pref = FindPreference(path); |
569 if (!pref) { | 573 if (!pref) { |
570 DCHECK(false) << "Trying to get an unregistered pref: " << path; | 574 DCHECK(false) << "Trying to get an unregistered pref: " << path; |
571 return NULL; | 575 return NULL; |
572 } | 576 } |
573 if (pref->type() != Value::TYPE_DICTIONARY) { | 577 if (pref->type() != Value::TYPE_DICTIONARY) { |
574 DCHECK(false) << "Wrong type for GetMutableDictionary: " << path; | 578 DCHECK(false) << "Wrong type for GetMutableDictionary: " << path; |
575 return NULL; | 579 return NULL; |
576 } | 580 } |
577 | 581 |
578 DictionaryValue* dict = NULL; | 582 DictionaryValue* dict = NULL; |
579 bool rv = persistent_->GetDictionary(path, &dict); | 583 string16 path16 = WideToUTF16Hack(path); |
| 584 bool rv = persistent_->GetDictionary(path16, &dict); |
580 if (!rv) { | 585 if (!rv) { |
581 dict = new DictionaryValue; | 586 dict = new DictionaryValue; |
582 rv = persistent_->Set(path, dict); | 587 rv = persistent_->Set(path16, dict); |
583 DCHECK(rv); | 588 DCHECK(rv); |
584 } | 589 } |
585 return dict; | 590 return dict; |
586 } | 591 } |
587 | 592 |
588 ListValue* PrefService::GetMutableList(const wchar_t* path) { | 593 ListValue* PrefService::GetMutableList(const wchar_t* path) { |
589 DCHECK(CalledOnValidThread()); | 594 DCHECK(CalledOnValidThread()); |
590 | 595 |
591 const Preference* pref = FindPreference(path); | 596 const Preference* pref = FindPreference(path); |
592 if (!pref) { | 597 if (!pref) { |
593 DCHECK(false) << "Trying to get an unregistered pref: " << path; | 598 DCHECK(false) << "Trying to get an unregistered pref: " << path; |
594 return NULL; | 599 return NULL; |
595 } | 600 } |
596 if (pref->type() != Value::TYPE_LIST) { | 601 if (pref->type() != Value::TYPE_LIST) { |
597 DCHECK(false) << "Wrong type for GetMutableList: " << path; | 602 DCHECK(false) << "Wrong type for GetMutableList: " << path; |
598 return NULL; | 603 return NULL; |
599 } | 604 } |
600 | 605 |
601 ListValue* list = NULL; | 606 ListValue* list = NULL; |
602 bool rv = persistent_->GetList(path, &list); | 607 string16 path16 = WideToUTF16Hack(path); |
| 608 bool rv = persistent_->GetList(path16, &list); |
603 if (!rv) { | 609 if (!rv) { |
604 list = new ListValue; | 610 list = new ListValue; |
605 rv = persistent_->Set(path, list); | 611 rv = persistent_->Set(path16, list); |
606 DCHECK(rv); | 612 DCHECK(rv); |
607 } | 613 } |
608 return list; | 614 return list; |
609 } | 615 } |
610 | 616 |
611 Value* PrefService::GetPrefCopy(const wchar_t* path) { | 617 Value* PrefService::GetPrefCopy(const wchar_t* path) { |
612 DCHECK(CalledOnValidThread()); | 618 DCHECK(CalledOnValidThread()); |
613 | 619 |
614 const Preference* pref = FindPreference(path); | 620 const Preference* pref = FindPreference(path); |
615 DCHECK(pref); | 621 DCHECK(pref); |
616 return pref->GetValue()->DeepCopy(); | 622 return pref->GetValue()->DeepCopy(); |
617 } | 623 } |
618 | 624 |
619 void PrefService::FireObserversIfChanged(const wchar_t* path, | 625 void PrefService::FireObserversIfChanged(const wchar_t* path, |
620 const Value* old_value) { | 626 const Value* old_value) { |
621 Value* new_value = NULL; | 627 Value* new_value = NULL; |
622 persistent_->Get(path, &new_value); | 628 persistent_->Get(WideToUTF16Hack(path), &new_value); |
623 if (!old_value->Equals(new_value)) | 629 if (!old_value->Equals(new_value)) |
624 FireObservers(path); | 630 FireObservers(path); |
625 } | 631 } |
626 | 632 |
627 void PrefService::FireObservers(const wchar_t* path) { | 633 void PrefService::FireObservers(const wchar_t* path) { |
628 DCHECK(CalledOnValidThread()); | 634 DCHECK(CalledOnValidThread()); |
629 | 635 |
630 // Convert path to a std::wstring because the Details constructor requires a | 636 // Convert path to a std::wstring because the Details constructor requires a |
631 // class. | 637 // class. |
632 std::wstring path_str(path); | 638 std::wstring path_str(path); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 // easier for callers to check for empty list/dict prefs. | 671 // easier for callers to check for empty list/dict prefs. |
666 if (Value::TYPE_LIST == type_ || Value::TYPE_DICTIONARY == type_) | 672 if (Value::TYPE_LIST == type_ || Value::TYPE_DICTIONARY == type_) |
667 default_value_.reset(Value::CreateNullValue()); | 673 default_value_.reset(Value::CreateNullValue()); |
668 } | 674 } |
669 | 675 |
670 const Value* PrefService::Preference::GetValue() const { | 676 const Value* PrefService::Preference::GetValue() const { |
671 DCHECK(NULL != root_pref_) << | 677 DCHECK(NULL != root_pref_) << |
672 "Must register pref before getting its value"; | 678 "Must register pref before getting its value"; |
673 | 679 |
674 Value* temp_value = NULL; | 680 Value* temp_value = NULL; |
675 if (root_pref_->Get(name_.c_str(), &temp_value) && | 681 if (root_pref_->Get(WideToUTF16Hack(name_), &temp_value) && |
676 temp_value->GetType() == type_) { | 682 temp_value->GetType() == type_) { |
677 return temp_value; | 683 return temp_value; |
678 } | 684 } |
679 | 685 |
680 // Pref not found, just return the app default. | 686 // Pref not found, just return the app default. |
681 return default_value_.get(); | 687 return default_value_.get(); |
682 } | 688 } |
683 | 689 |
684 bool PrefService::Preference::IsDefaultValue() const { | 690 bool PrefService::Preference::IsDefaultValue() const { |
685 DCHECK(default_value_.get()); | 691 DCHECK(default_value_.get()); |
686 return default_value_->Equals(GetValue()); | 692 return default_value_->Equals(GetValue()); |
687 } | 693 } |
OLD | NEW |