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 int always_translate_offer_threshold = -1; | |
312 if (variations::GetVariationParams(translate::kTranslateUI2016Q2TrialName, | |
313 ¶ms)) { | |
314 auto it = params.find(translate::kAlwaysTranslateOfferThreshold); | |
groby-ooo-7-16
2016/06/28 17:35:20
Why not the shorter variant? (I'm fine with using
ftang
2016/06/28 21:51:12
Done.
| |
315 if (it != params.end()) { | |
316 if (!base::StringToInt(it->second, &always_translate_offer_threshold)) { | |
317 // if we cannot parse the number, return false. | |
318 return false; | |
319 } | |
320 } | |
321 } | |
322 | |
323 if (always_translate_offer_threshold < 0) { | |
groby-ooo-7-16
2016/06/28 17:35:20
<= 0?
ftang
2016/06/28 21:51:12
Done.
| |
324 // if the number is negative, we never set the checked by default. | |
325 return false; | |
326 } | |
327 | |
328 // After N clicks on Translate for the same language. | |
329 // 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. | 330 // "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 | 331 // 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, | 332 // "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. | 333 // we should show as it as unchecked so we only check == N instead of >= N. |
311 return ShouldAlwaysTranslate() || | 334 return prefs_->GetTranslationAcceptedCount(GetOriginalLanguageCode()) == |
groby-ooo-7-16
2016/06/28 17:35:20
You really want greater-equals here. Otherwise, yo
ftang
2016/06/28 21:51:12
The problem of that is, then if the user explicitl
| |
312 prefs_->GetTranslationAcceptedCount(GetOriginalLanguageCode()) == 2; | 335 always_translate_offer_threshold; |
313 } | 336 } |
314 | 337 |
315 void TranslateUIDelegate::SetAlwaysTranslate(bool value) { | 338 void TranslateUIDelegate::SetAlwaysTranslate(bool value) { |
316 const std::string& original_lang = GetOriginalLanguageCode(); | 339 const std::string& original_lang = GetOriginalLanguageCode(); |
317 const std::string& target_lang = GetTargetLanguageCode(); | 340 const std::string& target_lang = GetTargetLanguageCode(); |
318 if (value) | 341 if (value) |
319 prefs_->WhitelistLanguagePair(original_lang, target_lang); | 342 prefs_->WhitelistLanguagePair(original_lang, target_lang); |
320 else | 343 else |
321 prefs_->RemoveLanguagePairFromWhitelist(original_lang, target_lang); | 344 prefs_->RemoveLanguagePairFromWhitelist(original_lang, target_lang); |
322 | 345 |
323 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, value); | 346 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, value); |
324 } | 347 } |
325 | 348 |
326 std::string TranslateUIDelegate::GetPageHost() { | 349 std::string TranslateUIDelegate::GetPageHost() { |
327 if (!translate_driver_->HasCurrentPage()) | 350 if (!translate_driver_->HasCurrentPage()) |
328 return std::string(); | 351 return std::string(); |
329 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); | 352 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); |
330 } | 353 } |
331 | 354 |
332 } // namespace translate | 355 } // namespace translate |
OLD | NEW |