Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/translate/core/browser/translate_prefs.h" | 5 #include "components/translate/core/browser/translate_prefs.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "components/pref_registry/pref_registry_syncable.h" | 13 #include "components/pref_registry/pref_registry_syncable.h" |
| 14 #include "components/prefs/pref_service.h" | 14 #include "components/prefs/pref_service.h" |
| 15 #include "components/prefs/scoped_user_pref_update.h" | 15 #include "components/prefs/scoped_user_pref_update.h" |
| 16 #include "components/translate/core/browser/translate_accept_languages.h" | 16 #include "components/translate/core/browser/translate_accept_languages.h" |
| 17 #include "components/translate/core/browser/translate_download_manager.h" | 17 #include "components/translate/core/browser/translate_download_manager.h" |
| 18 #include "components/translate/core/browser/translate_experiment.h" | 18 #include "components/translate/core/browser/translate_experiment.h" |
| 19 #include "components/translate/core/common/translate_util.h" | 19 #include "components/translate/core/common/translate_util.h" |
| 20 | 20 |
| 21 namespace translate { | 21 namespace translate { |
| 22 | 22 |
| 23 const char TranslatePrefs::kPrefLanguageProfile[] = "language_profile"; | |
|
groby-ooo-7-16
2016/08/02 00:38:21
If ULP is translate specific, it should be transla
| |
| 23 const char TranslatePrefs::kPrefTranslateSiteBlacklist[] = | 24 const char TranslatePrefs::kPrefTranslateSiteBlacklist[] = |
| 24 "translate_site_blacklist"; | 25 "translate_site_blacklist"; |
| 25 const char TranslatePrefs::kPrefTranslateWhitelists[] = "translate_whitelists"; | 26 const char TranslatePrefs::kPrefTranslateWhitelists[] = "translate_whitelists"; |
| 26 const char TranslatePrefs::kPrefTranslateDeniedCount[] = | 27 const char TranslatePrefs::kPrefTranslateDeniedCount[] = |
| 27 "translate_denied_count_for_language"; | 28 "translate_denied_count_for_language"; |
| 28 const char TranslatePrefs::kPrefTranslateIgnoredCount[] = | 29 const char TranslatePrefs::kPrefTranslateIgnoredCount[] = |
| 29 "translate_ignored_count_for_language"; | 30 "translate_ignored_count_for_language"; |
| 30 const char TranslatePrefs::kPrefTranslateAcceptedCount[] = | 31 const char TranslatePrefs::kPrefTranslateAcceptedCount[] = |
| 31 "translate_accepted_count"; | 32 "translate_accepted_count"; |
| 32 const char TranslatePrefs::kPrefTranslateBlockedLanguages[] = | 33 const char TranslatePrefs::kPrefTranslateBlockedLanguages[] = |
| 33 "translate_blocked_languages"; | 34 "translate_blocked_languages"; |
| 34 const char TranslatePrefs::kPrefTranslateLastDeniedTimeForLanguage[] = | 35 const char TranslatePrefs::kPrefTranslateLastDeniedTimeForLanguage[] = |
| 35 "translate_last_denied_time_for_language"; | 36 "translate_last_denied_time_for_language"; |
| 36 const char TranslatePrefs::kPrefTranslateTooOftenDeniedForLanguage[] = | 37 const char TranslatePrefs::kPrefTranslateTooOftenDeniedForLanguage[] = |
| 37 "translate_too_often_denied_for_language"; | 38 "translate_too_often_denied_for_language"; |
| 39 | |
| 38 const char kTranslateUI2016Q2TrialName[] = "TranslateUI2016Q2"; | 40 const char kTranslateUI2016Q2TrialName[] = "TranslateUI2016Q2"; |
| 39 const char kAlwaysTranslateOfferThreshold[] = | 41 const char kAlwaysTranslateOfferThreshold[] = |
| 40 "always_translate_offer_threshold"; | 42 "always_translate_offer_threshold"; |
| 41 | 43 |
| 44 // For reading ULP prefs. | |
| 45 const char kConfidence[] = "confidence"; | |
| 46 const char kItems[] = "items"; | |
| 47 const char kLanguage[] = "language"; | |
| 48 const char kProbability[] = "probability"; | |
| 49 const char kReading[] = "reading"; | |
| 50 const char kSettings[] = "settings"; | |
| 51 const char kWriting[] = "writing"; | |
| 52 | |
| 42 // The below properties used to be used but now are deprecated. Don't use them | 53 // The below properties used to be used but now are deprecated. Don't use them |
| 43 // since an old profile might have some values there. | 54 // since an old profile might have some values there. |
| 44 // | 55 // |
| 45 // * translate_last_denied_time | 56 // * translate_last_denied_time |
| 46 // * translate_too_often_denied | 57 // * translate_too_often_denied |
| 47 // * translate_language_blacklist | 58 // * translate_language_blacklist |
| 48 | 59 |
| 49 namespace { | 60 namespace { |
| 50 | 61 |
| 51 // Expands language codes to make these more suitable for Accept-Language. | 62 // Expands language codes to make these more suitable for Accept-Language. |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 467 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 478 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 468 registry->RegisterDictionaryPref( | 479 registry->RegisterDictionaryPref( |
| 469 kPrefTranslateAcceptedCount, | 480 kPrefTranslateAcceptedCount, |
| 470 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 481 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 471 registry->RegisterListPref(kPrefTranslateBlockedLanguages, | 482 registry->RegisterListPref(kPrefTranslateBlockedLanguages, |
| 472 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 483 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 473 registry->RegisterDictionaryPref(kPrefTranslateLastDeniedTimeForLanguage); | 484 registry->RegisterDictionaryPref(kPrefTranslateLastDeniedTimeForLanguage); |
| 474 registry->RegisterDictionaryPref( | 485 registry->RegisterDictionaryPref( |
| 475 kPrefTranslateTooOftenDeniedForLanguage, | 486 kPrefTranslateTooOftenDeniedForLanguage, |
| 476 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 487 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 488 registry->RegisterDictionaryPref( | |
| 489 kPrefLanguageProfile, user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
| 477 } | 490 } |
| 478 | 491 |
| 479 // static | 492 // static |
| 480 void TranslatePrefs::MigrateUserPrefs(PrefService* user_prefs, | 493 void TranslatePrefs::MigrateUserPrefs(PrefService* user_prefs, |
| 481 const char* accept_languages_pref) { | 494 const char* accept_languages_pref) { |
| 482 // Old format of kPrefTranslateWhitelists | 495 // Old format of kPrefTranslateWhitelists |
| 483 // - original language -> list of target langs to auto-translate | 496 // - original language -> list of target langs to auto-translate |
| 484 // - list of langs is in order of being enabled i.e. last in list is the | 497 // - list of langs is in order of being enabled i.e. last in list is the |
| 485 // most recent language that user enabled via | 498 // most recent language that user enabled via |
| 486 // Always translate |source_lang| to |target_lang|" | 499 // Always translate |source_lang| to |target_lang|" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 562 bool TranslatePrefs::IsListEmpty(const char* pref_id) const { | 575 bool TranslatePrefs::IsListEmpty(const char* pref_id) const { |
| 563 const base::ListValue* blacklist = prefs_->GetList(pref_id); | 576 const base::ListValue* blacklist = prefs_->GetList(pref_id); |
| 564 return (blacklist == NULL || blacklist->empty()); | 577 return (blacklist == NULL || blacklist->empty()); |
| 565 } | 578 } |
| 566 | 579 |
| 567 bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const { | 580 bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const { |
| 568 const base::DictionaryValue* dict = prefs_->GetDictionary(pref_id); | 581 const base::DictionaryValue* dict = prefs_->GetDictionary(pref_id); |
| 569 return (dict == NULL || dict->empty()); | 582 return (dict == NULL || dict->empty()); |
| 570 } | 583 } |
| 571 | 584 |
| 585 // Functions to read from User Language Profile | |
|
groby-ooo-7-16
2016/08/02 00:38:21
Probably might want to document that via a test in
ftang
2016/08/03 02:01:18
Acknowledged.
| |
| 586 // The structure of User Language Profile looks like below: | |
| 587 // language_profile { | |
| 588 // reading: { | |
| 589 // confidence: 0.7, | |
| 590 // items: [ | |
| 591 // { | |
| 592 // language: 'en', | |
| 593 // probability: 0.45 | |
| 594 // }, { | |
| 595 // language: 'zh-TW', | |
| 596 // probability: 0.33 | |
| 597 // }, { | |
| 598 // language: 'pt-BR', | |
| 599 // probability: 0.22 | |
| 600 // } | |
| 601 // ] | |
| 602 // }, | |
| 603 // writing: { | |
| 604 // confidence: 0.35, | |
| 605 // items: [ | |
| 606 // { | |
| 607 // language: 'en', | |
| 608 // probability: 0.75 | |
| 609 // }, { | |
| 610 // language: 'pt-BR', | |
| 611 // probability: 0.25 | |
| 612 // } | |
| 613 // ] | |
| 614 // }, | |
| 615 // settings: ["pt-BR", "en", "zh-TW" ] | |
| 616 // } | |
| 617 double TranslatePrefs::GetFromUserLanguageProfile( | |
| 618 base::StringPiece path, | |
| 619 LanguageProbabilityList* out_value) const { | |
|
groby-ooo-7-16
2016/08/02 00:38:21
That's a lot of ad-hoc work - a JSONValueConverter
| |
| 620 const base::DictionaryValue* dict = | |
| 621 prefs_->GetDictionary(kPrefLanguageProfile); | |
| 622 const base::DictionaryValue* entries = nullptr; | |
| 623 if (dict == nullptr || dict->empty() || | |
|
groby-ooo-7-16
2016/08/02 00:38:21
1) No need to check dict->empty, because GetDictio
ftang
2016/08/03 02:01:18
Done.
| |
| 624 !dict->GetDictionary(path, &entries) || entries == nullptr || | |
| 625 entries->empty()) | |
| 626 return 0.0; | |
| 627 double confidence = 0.0; | |
| 628 const base::ListValue* list = nullptr; | |
| 629 if (!entries->GetDouble(kConfidence, &confidence) || | |
| 630 !entries->GetList(kItems, &list) || list == nullptr || list->empty()) | |
|
groby-ooo-7-16
2016/08/02 00:38:21
Same here - if GetList is true, list is not null.
ftang
2016/08/03 02:01:18
Done.
| |
| 631 return confidence; | |
| 632 std::string language; | |
| 633 double probability; | |
| 634 for (size_t i = 0; i < list->GetSize(); i++) { | |
|
groby-ooo-7-16
2016/08/02 00:38:21
ListValue is iterable, so C++11 iteration works
| |
| 635 const base::DictionaryValue* item = nullptr; | |
| 636 if (list->GetDictionary(i, &item) && item != nullptr && !item->empty() && | |
|
groby-ooo-7-16
2016/08/02 00:38:21
See above :)
ftang
2016/08/03 02:01:18
Done.
| |
| 637 item->GetString(kLanguage, &language) && | |
| 638 item->GetDouble(kProbability, &probability)) { | |
| 639 out_value->push_back(std::make_pair(language, probability)); | |
| 640 } | |
| 641 } | |
| 642 return confidence; | |
| 643 } | |
| 644 | |
| 645 double TranslatePrefs::GetReadingFromUserLanguageProfile( | |
| 646 LanguageProbabilityList* list) const { | |
| 647 return GetFromUserLanguageProfile(kReading, list); | |
| 648 } | |
| 649 | |
| 650 double TranslatePrefs::GetWritingFromUserLanguageProfile( | |
| 651 LanguageProbabilityList* list) const { | |
| 652 return GetFromUserLanguageProfile(kWriting, list); | |
| 653 } | |
| 654 | |
| 655 int TranslatePrefs::GetUniversialLanguageSettings( | |
|
groby-ooo-7-16
2016/08/02 00:38:21
I didn't see any consumer of this function yet?
ftang
2016/08/03 02:01:18
you are right. Removed. We should keep the ULP pre
| |
| 656 std::vector<std::string>* out_value) const { | |
|
groby-ooo-7-16
2016/08/02 00:38:21
Why use an outvalue? Just return std::vector<std::
ftang
2016/08/03 02:01:18
since I remove this function. It does not matter a
| |
| 657 const base::DictionaryValue* dict = | |
| 658 prefs_->GetDictionary(kPrefLanguageProfile); | |
| 659 const base::ListValue* list = nullptr; | |
| 660 if (dict == nullptr || dict->empty() || !dict->GetList(kSettings, &list) || | |
|
groby-ooo-7-16
2016/08/02 00:38:21
See above.
ftang
2016/08/03 02:01:18
Acknowledged.
| |
| 661 list == nullptr || list->empty()) | |
| 662 return 0; | |
| 663 std::string language; | |
| 664 for (size_t i = 0; i < list->GetSize(); i++) { | |
| 665 if (list->GetString(i, &language)) { | |
| 666 out_value->push_back(language); | |
| 667 } | |
| 668 } | |
| 669 return out_value->size(); | |
| 670 } | |
| 671 | |
| 572 } // namespace translate | 672 } // namespace translate |
| OLD | NEW |