| 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 double TranslatePrefs::GetReadingFromUserLanguageProfile( |
| 584 LanguageAndProbabilityList* out_value) const { |
| 585 const base::DictionaryValue* dict = |
| 586 prefs_->GetDictionary(kPrefLanguageProfile); |
| 587 const base::DictionaryValue* entries = nullptr; |
| 588 |
| 589 // Return 0.0 if no ULP prefs. |
| 590 if (!dict) |
| 591 return 0.0; |
| 592 |
| 593 // Return 0.0 if no such list. |
| 594 if (!dict->GetDictionary(kReading, &entries)) |
| 595 return 0.0; |
| 596 |
| 597 double confidence = 0.0; |
| 598 // Return 0.0 if cannot find confidence. |
| 599 if (!entries->GetDouble(kConfidence, &confidence)) |
| 600 return 0.0; |
| 601 |
| 602 const base::ListValue* preference = nullptr; |
| 603 // Return the confidence if there are no item on the 'preference' field. |
| 604 if (!entries->GetList(kPreference, &preference)) |
| 605 return confidence; |
| 606 |
| 607 // Use a map to fold the probability of all the same normalized language |
| 608 // code together. |
| 609 std::map<std::string, double> probability_map; |
| 610 // Iterate through the preference. |
| 611 for (const auto& entry : *preference) { |
| 612 const base::DictionaryValue* item = nullptr; |
| 613 std::string language; |
| 614 double probability = 0.0; |
| 615 if (entry->GetAsDictionary(&item) && |
| 616 item->GetString(kLanguage, &language) && |
| 617 item->GetDouble(kProbability, &probability)) { |
| 618 // Normalize the the language code known and supported by |
| 619 // Translate. |
| 620 translate::ToTranslateLanguageSynonym(&language); |
| 621 // Discard if the normalized version is unsupported. |
| 622 if (TranslateDownloadManager::IsSupportedLanguage(language)) { |
| 623 probability_map[language] += probability; |
| 624 } |
| 625 } |
| 626 } |
| 627 for (const auto& it : probability_map) |
| 628 out_value->push_back(it); |
| 629 std::sort(out_value->begin(), out_value->end(), |
| 630 [](const LanguageAndProbability& left, |
| 631 const LanguageAndProbability& right) { |
| 632 return left.second > right.second; |
| 633 }); |
| 634 return confidence; |
| 635 } |
| 636 |
| 572 } // namespace translate | 637 } // namespace translate |
| OLD | NEW |