Chromium Code Reviews| Index: components/translate/core/browser/translate_prefs.cc |
| diff --git a/components/translate/core/browser/translate_prefs.cc b/components/translate/core/browser/translate_prefs.cc |
| index 98419c0c91474d127ccef774ead2cf8f0f123a91..4ac0838ddd56a19d863d4842f33cdc2a7cdfe49a 100644 |
| --- a/components/translate/core/browser/translate_prefs.cc |
| +++ b/components/translate/core/browser/translate_prefs.cc |
| @@ -20,6 +20,7 @@ |
| namespace translate { |
| +const char TranslatePrefs::kPrefLanguageProfile[] = "language_profile"; |
| const char TranslatePrefs::kPrefTranslateSiteBlacklist[] = |
| "translate_site_blacklist"; |
| const char TranslatePrefs::kPrefTranslateWhitelists[] = "translate_whitelists"; |
| @@ -35,10 +36,18 @@ const char TranslatePrefs::kPrefTranslateLastDeniedTimeForLanguage[] = |
| "translate_last_denied_time_for_language"; |
| const char TranslatePrefs::kPrefTranslateTooOftenDeniedForLanguage[] = |
| "translate_too_often_denied_for_language"; |
| + |
| const char kTranslateUI2016Q2TrialName[] = "TranslateUI2016Q2"; |
| const char kAlwaysTranslateOfferThreshold[] = |
| "always_translate_offer_threshold"; |
| +// For reading ULP prefs. |
| +const char kConfidence[] = "confidence"; |
| +const char kLanguage[] = "language"; |
| +const char kPreference[] = "preference"; |
| +const char kProbability[] = "probability"; |
| +const char kReading[] = "reading"; |
| + |
| // The below properties used to be used but now are deprecated. Don't use them |
| // since an old profile might have some values there. |
| // |
| @@ -474,6 +483,8 @@ void TranslatePrefs::RegisterProfilePrefs( |
| registry->RegisterDictionaryPref( |
| kPrefTranslateTooOftenDeniedForLanguage, |
| user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| + registry->RegisterDictionaryPref( |
| + kPrefLanguageProfile, user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| } |
| // static |
| @@ -569,4 +580,92 @@ bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const { |
| return (dict == NULL || dict->empty()); |
| } |
| +// Functions to read from User Language Profile |
| +// 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.
|
| +// language_profile { |
| +// reading: { |
| +// confidence: 0.7, |
| +// items: [ |
| +// { |
| +// language: 'en', |
| +// probability: 0.45 |
| +// }, { |
| +// language: 'zh-TW', |
| +// probability: 0.33 |
| +// }, { |
| +// language: 'pt-BR', |
| +// probability: 0.22 |
| +// } |
| +// ] |
| +// }, |
| +// writing: { |
| +// confidence: 0.35, |
| +// items: [ |
| +// { |
| +// language: 'en', |
| +// probability: 0.75 |
| +// }, { |
| +// language: 'pt-BR', |
| +// probability: 0.25 |
| +// } |
| +// ] |
| +// }, |
| +// settings: ["pt-BR", "en", "zh-TW" ] |
| +// } |
| +double TranslatePrefs::GetReadingFromUserLanguageProfile( |
| + LanguageAndProbabilityList* out_value) const { |
| + const base::DictionaryValue* dict = |
| + prefs_->GetDictionary(kPrefLanguageProfile); |
| + const base::DictionaryValue* entries = nullptr; |
| + |
| + // Return 0.0 if no ULP prefs. |
| + if (!dict) |
| + return 0.0; |
| + |
| + // Return 0.0 if no such list. |
| + if (!dict->GetDictionary(kReading, &entries)) |
| + return 0.0; |
| + |
| + double confidence = 0.0; |
| + 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.
|
| + |
| + // Return 0.0 if cannot find confidence. |
| + if (!entries->GetDouble(kConfidence, &confidence)) |
| + return 0.0; |
| + |
| + // Return the confidence if there are no item on the 'preference' field. |
| + if (!entries->GetList(kPreference, &preference)) |
| + return confidence; |
| + |
| + std::string language; |
| + double probability; |
| + |
| + // Use a map to fold the probability of all the same normalized language |
| + // code together. |
| + std::map<std::string, double> probability_map; |
| + // Iterate through the preference. |
| + for (const auto& entry : *preference) { |
| + const base::DictionaryValue* item = nullptr; |
| + if (entry->GetAsDictionary(&item) && |
| + item->GetString(kLanguage, &language) && |
| + item->GetDouble(kProbability, &probability)) { |
| + // Normalize the the language code known and supported by |
| + // Translate. |
| + translate::ToTranslateLanguageSynonym(&language); |
| + // 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.
|
| + if (TranslateDownloadManager::IsSupportedLanguage(language)) { |
| + 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.
|
| + } |
| + } |
| + } |
| + for (const auto& it : probability_map) |
| + out_value->push_back(it); |
| + std::sort(out_value->begin(), out_value->end(), |
| + [](const LanguageAndProbability& left, |
| + const LanguageAndProbability& right) { |
| + return left.second > right.second; |
| + }); |
| + return confidence; |
| +} |
| + |
| } // namespace translate |