| 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_ui_delegate.h" | 5 #include "components/translate/core/browser/translate_ui_delegate.h" |
| 6 | 6 |
| 7 #include "base/i18n/string_compare.h" | 7 #include "base/i18n/string_compare.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 9 #include "components/translate/core/browser/language_state.h" | 10 #include "components/translate/core/browser/language_state.h" |
| 10 #include "components/translate/core/browser/translate_client.h" | 11 #include "components/translate/core/browser/translate_client.h" |
| 11 #include "components/translate/core/browser/translate_download_manager.h" | 12 #include "components/translate/core/browser/translate_download_manager.h" |
| 12 #include "components/translate/core/browser/translate_driver.h" | 13 #include "components/translate/core/browser/translate_driver.h" |
| 13 #include "components/translate/core/browser/translate_manager.h" | 14 #include "components/translate/core/browser/translate_manager.h" |
| 14 #include "components/translate/core/browser/translate_prefs.h" | 15 #include "components/translate/core/browser/translate_prefs.h" |
| 15 #include "components/translate/core/common/translate_constants.h" | 16 #include "components/translate/core/common/translate_constants.h" |
| 17 #include "components/variations/variations_associated_data.h" |
| 16 #include "third_party/icu/source/i18n/unicode/coll.h" | 18 #include "third_party/icu/source/i18n/unicode/coll.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 const char kDeclineTranslate[] = "Translate.DeclineTranslate"; | 23 const char kDeclineTranslate[] = "Translate.DeclineTranslate"; |
| 22 const char kRevertTranslation[] = "Translate.RevertTranslation"; | 24 const char kRevertTranslation[] = "Translate.RevertTranslation"; |
| 23 const char kPerformTranslate[] = "Translate.Translate"; | 25 const char kPerformTranslate[] = "Translate.Translate"; |
| 24 const char kNeverTranslateLang[] = "Translate.NeverTranslateLang"; | 26 const char kNeverTranslateLang[] = "Translate.NeverTranslateLang"; |
| 25 const char kNeverTranslateSite[] = "Translate.NeverTranslateSite"; | 27 const char kNeverTranslateSite[] = "Translate.NeverTranslateSite"; |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 | 297 |
| 296 UMA_HISTOGRAM_BOOLEAN(kNeverTranslateSite, value); | 298 UMA_HISTOGRAM_BOOLEAN(kNeverTranslateSite, value); |
| 297 } | 299 } |
| 298 | 300 |
| 299 bool TranslateUIDelegate::ShouldAlwaysTranslate() { | 301 bool TranslateUIDelegate::ShouldAlwaysTranslate() { |
| 300 return prefs_->IsLanguagePairWhitelisted(GetOriginalLanguageCode(), | 302 return prefs_->IsLanguagePairWhitelisted(GetOriginalLanguageCode(), |
| 301 GetTargetLanguageCode()); | 303 GetTargetLanguageCode()); |
| 302 } | 304 } |
| 303 | 305 |
| 304 bool TranslateUIDelegate::ShouldAlwaysTranslateBeCheckedByDefault() { | 306 bool TranslateUIDelegate::ShouldAlwaysTranslateBeCheckedByDefault() { |
| 305 // After 2 clicks on Translate for the same language. | 307 if (ShouldAlwaysTranslate()) |
| 306 // We check for == 2 instead of >= 2 because if the user translates with the | 308 return true; |
| 309 |
| 310 std::map<std::string, std::string> params; |
| 311 if (!variations::GetVariationParams(translate::kTranslateUI2016Q2TrialName, |
| 312 ¶ms)) |
| 313 return false; |
| 314 int threshold = 0; |
| 315 base::StringToInt(params[translate::kAlwaysTranslateOfferThreshold], |
| 316 &threshold); |
| 317 if (threshold <= 0) |
| 318 return false; |
| 319 |
| 320 // After N clicks on Translate for the same language. |
| 321 // We check for == N instead of >= N because if the user translates with the |
| 307 // "Always do this?" on, then the next time the bubble won't show up. | 322 // "Always do this?" on, then the next time the bubble won't show up. |
| 308 // The only chance the bubble will show up is after the user manually unchecks | 323 // The only chance the bubble will show up is after the user manually unchecks |
| 309 // "Always do this?". In that case, since it is after user explictly unchecks, | 324 // "Always do this?". In that case, since it is after user explictly unchecks, |
| 310 // we should show as it as unchecked so we only check == 2 instead of >= 2. | 325 // we should show as it as unchecked so we only check == N instead of >= N. |
| 311 return ShouldAlwaysTranslate() || | 326 return prefs_->GetTranslationAcceptedCount(GetOriginalLanguageCode()) == |
| 312 prefs_->GetTranslationAcceptedCount(GetOriginalLanguageCode()) == 2; | 327 threshold; |
| 313 } | 328 } |
| 314 | 329 |
| 315 void TranslateUIDelegate::SetAlwaysTranslate(bool value) { | 330 void TranslateUIDelegate::SetAlwaysTranslate(bool value) { |
| 316 const std::string& original_lang = GetOriginalLanguageCode(); | 331 const std::string& original_lang = GetOriginalLanguageCode(); |
| 317 const std::string& target_lang = GetTargetLanguageCode(); | 332 const std::string& target_lang = GetTargetLanguageCode(); |
| 318 if (value) | 333 if (value) |
| 319 prefs_->WhitelistLanguagePair(original_lang, target_lang); | 334 prefs_->WhitelistLanguagePair(original_lang, target_lang); |
| 320 else | 335 else |
| 321 prefs_->RemoveLanguagePairFromWhitelist(original_lang, target_lang); | 336 prefs_->RemoveLanguagePairFromWhitelist(original_lang, target_lang); |
| 322 | 337 |
| 323 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, value); | 338 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, value); |
| 324 } | 339 } |
| 325 | 340 |
| 326 std::string TranslateUIDelegate::GetPageHost() { | 341 std::string TranslateUIDelegate::GetPageHost() { |
| 327 if (!translate_driver_->HasCurrentPage()) | 342 if (!translate_driver_->HasCurrentPage()) |
| 328 return std::string(); | 343 return std::string(); |
| 329 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); | 344 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); |
| 330 } | 345 } |
| 331 | 346 |
| 332 } // namespace translate | 347 } // namespace translate |
| OLD | NEW |