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"; | |
| 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 kLanguage[] = "language"; | |
| 47 const char kPreference[] = "preference"; | |
| 48 const char kProbability[] = "probability"; | |
| 49 const char kReading[] = "reading"; | |
| 50 | |
| 42 // The below properties used to be used but now are deprecated. Don't use them | 51 // 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. | 52 // since an old profile might have some values there. |
| 44 // | 53 // |
| 45 // * translate_last_denied_time | 54 // * translate_last_denied_time |
| 46 // * translate_too_often_denied | 55 // * translate_too_often_denied |
| 47 // * translate_language_blacklist | 56 // * translate_language_blacklist |
| 48 | 57 |
| 49 namespace { | 58 namespace { |
| 50 | 59 |
| 51 // Expands language codes to make these more suitable for Accept-Language. | 60 // 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); | 476 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 468 registry->RegisterDictionaryPref( | 477 registry->RegisterDictionaryPref( |
| 469 kPrefTranslateAcceptedCount, | 478 kPrefTranslateAcceptedCount, |
| 470 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 479 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 471 registry->RegisterListPref(kPrefTranslateBlockedLanguages, | 480 registry->RegisterListPref(kPrefTranslateBlockedLanguages, |
| 472 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 481 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 473 registry->RegisterDictionaryPref(kPrefTranslateLastDeniedTimeForLanguage); | 482 registry->RegisterDictionaryPref(kPrefTranslateLastDeniedTimeForLanguage); |
| 474 registry->RegisterDictionaryPref( | 483 registry->RegisterDictionaryPref( |
| 475 kPrefTranslateTooOftenDeniedForLanguage, | 484 kPrefTranslateTooOftenDeniedForLanguage, |
| 476 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 485 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 486 registry->RegisterDictionaryPref( | |
| 487 kPrefLanguageProfile, user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
| 477 } | 488 } |
| 478 | 489 |
| 479 // static | 490 // static |
| 480 void TranslatePrefs::MigrateUserPrefs(PrefService* user_prefs, | 491 void TranslatePrefs::MigrateUserPrefs(PrefService* user_prefs, |
| 481 const char* accept_languages_pref) { | 492 const char* accept_languages_pref) { |
| 482 // Old format of kPrefTranslateWhitelists | 493 // Old format of kPrefTranslateWhitelists |
| 483 // - original language -> list of target langs to auto-translate | 494 // - 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 | 495 // - list of langs is in order of being enabled i.e. last in list is the |
| 485 // most recent language that user enabled via | 496 // most recent language that user enabled via |
| 486 // Always translate |source_lang| to |target_lang|" | 497 // 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 { | 573 bool TranslatePrefs::IsListEmpty(const char* pref_id) const { |
| 563 const base::ListValue* blacklist = prefs_->GetList(pref_id); | 574 const base::ListValue* blacklist = prefs_->GetList(pref_id); |
| 564 return (blacklist == NULL || blacklist->empty()); | 575 return (blacklist == NULL || blacklist->empty()); |
| 565 } | 576 } |
| 566 | 577 |
| 567 bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const { | 578 bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const { |
| 568 const base::DictionaryValue* dict = prefs_->GetDictionary(pref_id); | 579 const base::DictionaryValue* dict = prefs_->GetDictionary(pref_id); |
| 569 return (dict == NULL || dict->empty()); | 580 return (dict == NULL || dict->empty()); |
| 570 } | 581 } |
| 571 | 582 |
| 583 // Functions to read from User Language Profile | |
| 584 // The structure of User Language Profile looks like below: | |
|
groby-ooo-7-16
2016/08/04 02:33:42
The structure is already explained in hte test, no
ftang
2016/08/05 04:45:28
Done.
| |
| 585 // language_profile { | |
| 586 // reading: { | |
| 587 // confidence: 0.7, | |
| 588 // items: [ | |
| 589 // { | |
| 590 // language: 'en', | |
| 591 // probability: 0.45 | |
| 592 // }, { | |
| 593 // language: 'zh-TW', | |
| 594 // probability: 0.33 | |
| 595 // }, { | |
| 596 // language: 'pt-BR', | |
| 597 // probability: 0.22 | |
| 598 // } | |
| 599 // ] | |
| 600 // }, | |
| 601 // writing: { | |
| 602 // confidence: 0.35, | |
| 603 // items: [ | |
| 604 // { | |
| 605 // language: 'en', | |
| 606 // probability: 0.75 | |
| 607 // }, { | |
| 608 // language: 'pt-BR', | |
| 609 // probability: 0.25 | |
| 610 // } | |
| 611 // ] | |
| 612 // }, | |
| 613 // settings: ["pt-BR", "en", "zh-TW" ] | |
| 614 // } | |
| 615 double TranslatePrefs::GetReadingFromUserLanguageProfile( | |
| 616 LanguageAndProbabilityList* out_value) const { | |
| 617 const base::DictionaryValue* dict = | |
| 618 prefs_->GetDictionary(kPrefLanguageProfile); | |
| 619 const base::DictionaryValue* entries = nullptr; | |
| 620 | |
| 621 // Return 0.0 if no ULP prefs. | |
| 622 if (!dict) | |
| 623 return 0.0; | |
| 624 | |
| 625 // Return 0.0 if no such list. | |
| 626 if (!dict->GetDictionary(kReading, &entries)) | |
| 627 return 0.0; | |
| 628 | |
| 629 double confidence = 0.0; | |
| 630 const base::ListValue* preference = nullptr; | |
|
groby-ooo-7-16
2016/08/04 02:33:42
please move ctor to where this is used (after next
ftang
2016/08/05 04:45:28
Done.
| |
| 631 | |
| 632 // Return 0.0 if cannot find confidence. | |
| 633 if (!entries->GetDouble(kConfidence, &confidence)) | |
| 634 return 0.0; | |
| 635 | |
| 636 // Return the confidence if there are no item on the 'preference' field. | |
| 637 if (!entries->GetList(kPreference, &preference)) | |
| 638 return confidence; | |
| 639 | |
| 640 std::string language; | |
| 641 double probability; | |
| 642 | |
| 643 // Use a map to fold the probability of all the same normalized language | |
| 644 // code together. | |
| 645 std::map<std::string, double> probability_map; | |
| 646 // Iterate through the preference. | |
| 647 for (const auto& entry : *preference) { | |
| 648 const base::DictionaryValue* item = nullptr; | |
| 649 if (entry->GetAsDictionary(&item) && | |
| 650 item->GetString(kLanguage, &language) && | |
| 651 item->GetDouble(kProbability, &probability)) { | |
| 652 // Normalize the the language code known and supported by | |
| 653 // Translate. | |
| 654 translate::ToTranslateLanguageSynonym(&language); | |
| 655 // Put into the vector only if the normalized version is supported. | |
|
groby-ooo-7-16
2016/08/04 02:33:42
It's not a vector yet :) Maybe "discard if the nor
ftang
2016/08/05 04:45:28
Done.
| |
| 656 if (TranslateDownloadManager::IsSupportedLanguage(language)) { | |
| 657 probability_map[language] += probability; | |
|
groby-ooo-7-16
2016/08/04 02:33:42
I'm not sure why the bots think probability might
ftang
2016/08/05 04:45:28
Done.
| |
| 658 } | |
| 659 } | |
| 660 } | |
| 661 for (const auto& it : probability_map) | |
| 662 out_value->push_back(it); | |
| 663 std::sort(out_value->begin(), out_value->end(), | |
| 664 [](const LanguageAndProbability& left, | |
| 665 const LanguageAndProbability& right) { | |
| 666 return left.second > right.second; | |
| 667 }); | |
| 668 return confidence; | |
| 669 } | |
| 670 | |
| 572 } // namespace translate | 671 } // namespace translate |
| OLD | NEW |