Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / components / translate / core / browser / translate_ui_delegate.cc
blobfb33f7a755190a32d49f905fcfcafbfcac70ab9e
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
3 // found in the LICENSE file.
5 #include "components/translate/core/browser/translate_ui_delegate.h"
7 #include "base/i18n/string_compare.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "components/translate/core/browser/language_state.h"
10 #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_driver.h"
13 #include "components/translate/core/browser/translate_manager.h"
14 #include "components/translate/core/browser/translate_prefs.h"
15 #include "components/translate/core/common/translate_constants.h"
16 #include "third_party/icu/source/i18n/unicode/coll.h"
17 #include "ui/base/l10n/l10n_util.h"
19 namespace {
21 const char kDeclineTranslate[] = "Translate.DeclineTranslate";
22 const char kRevertTranslation[] = "Translate.RevertTranslation";
23 const char kPerformTranslate[] = "Translate.Translate";
24 const char kNeverTranslateLang[] = "Translate.NeverTranslateLang";
25 const char kNeverTranslateSite[] = "Translate.NeverTranslateSite";
26 const char kAlwaysTranslateLang[] = "Translate.AlwaysTranslateLang";
27 const char kModifyOriginalLang[] = "Translate.ModifyOriginalLang";
28 const char kModifyTargetLang[] = "Translate.ModifyTargetLang";
29 const char kDeclineTranslateDismissUI[] = "Translate.DeclineTranslateDismissUI";
30 const char kShowErrorUI[] = "Translate.ShowErrorUI";
32 } // namespace
34 namespace translate {
36 TranslateUIDelegate::TranslateUIDelegate(
37 const base::WeakPtr<TranslateManager>& translate_manager,
38 const std::string& original_language,
39 const std::string& target_language)
40 : translate_driver_(
41 translate_manager->translate_client()->GetTranslateDriver()),
42 translate_manager_(translate_manager),
43 original_language_index_(kNoIndex),
44 initial_original_language_index_(kNoIndex),
45 target_language_index_(kNoIndex) {
46 DCHECK(translate_driver_);
47 DCHECK(translate_manager_);
49 std::vector<std::string> language_codes;
50 TranslateDownloadManager::GetSupportedLanguages(&language_codes);
52 // Preparing for the alphabetical order in the locale.
53 UErrorCode error = U_ZERO_ERROR;
54 std::string locale =
55 TranslateDownloadManager::GetInstance()->application_locale();
56 icu::Locale loc(locale.c_str());
57 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error));
58 collator->setStrength(icu::Collator::PRIMARY);
60 languages_.reserve(language_codes.size());
61 for (std::vector<std::string>::const_iterator iter = language_codes.begin();
62 iter != language_codes.end();
63 ++iter) {
64 std::string language_code = *iter;
66 base::string16 language_name =
67 l10n_util::GetDisplayNameForLocale(language_code, locale, true);
68 // Insert the language in languages_ in alphabetical order.
69 std::vector<LanguageNamePair>::iterator iter2;
70 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) {
71 if (base::i18n::CompareString16WithCollator(*collator, language_name,
72 iter2->second) == UCOL_LESS) {
73 break;
76 languages_.insert(iter2, LanguageNamePair(language_code, language_name));
78 for (std::vector<LanguageNamePair>::const_iterator iter = languages_.begin();
79 iter != languages_.end();
80 ++iter) {
81 std::string language_code = iter->first;
82 if (language_code == original_language) {
83 original_language_index_ = iter - languages_.begin();
84 initial_original_language_index_ = original_language_index_;
86 if (language_code == target_language)
87 target_language_index_ = iter - languages_.begin();
90 prefs_ = translate_manager_->translate_client()->GetTranslatePrefs();
93 TranslateUIDelegate::~TranslateUIDelegate() {}
95 void TranslateUIDelegate::OnErrorShown(TranslateErrors::Type error_type) {
96 DCHECK_LE(TranslateErrors::NONE, error_type);
97 DCHECK_LT(error_type, TranslateErrors::TRANSLATE_ERROR_MAX);
99 if (error_type == TranslateErrors::NONE)
100 return;
102 UMA_HISTOGRAM_ENUMERATION(
103 kShowErrorUI, error_type, TranslateErrors::TRANSLATE_ERROR_MAX);
106 const LanguageState& TranslateUIDelegate::GetLanguageState() {
107 return translate_manager_->GetLanguageState();
110 size_t TranslateUIDelegate::GetNumberOfLanguages() const {
111 return languages_.size();
114 size_t TranslateUIDelegate::GetOriginalLanguageIndex() const {
115 return original_language_index_;
118 void TranslateUIDelegate::UpdateOriginalLanguageIndex(size_t language_index) {
119 if (original_language_index_ == language_index)
120 return;
122 UMA_HISTOGRAM_BOOLEAN(kModifyOriginalLang, true);
123 original_language_index_ = language_index;
126 size_t TranslateUIDelegate::GetTargetLanguageIndex() const {
127 return target_language_index_;
130 void TranslateUIDelegate::UpdateTargetLanguageIndex(size_t language_index) {
131 if (target_language_index_ == language_index)
132 return;
134 DCHECK_LT(language_index, GetNumberOfLanguages());
135 UMA_HISTOGRAM_BOOLEAN(kModifyTargetLang, true);
136 target_language_index_ = language_index;
139 std::string TranslateUIDelegate::GetLanguageCodeAt(size_t index) const {
140 DCHECK_LT(index, GetNumberOfLanguages());
141 return languages_[index].first;
144 base::string16 TranslateUIDelegate::GetLanguageNameAt(size_t index) const {
145 if (index == kNoIndex)
146 return base::string16();
147 DCHECK_LT(index, GetNumberOfLanguages());
148 return languages_[index].second;
151 std::string TranslateUIDelegate::GetOriginalLanguageCode() const {
152 return (GetOriginalLanguageIndex() == kNoIndex) ?
153 translate::kUnknownLanguageCode :
154 GetLanguageCodeAt(GetOriginalLanguageIndex());
157 std::string TranslateUIDelegate::GetTargetLanguageCode() const {
158 return GetLanguageCodeAt(GetTargetLanguageIndex());
161 void TranslateUIDelegate::Translate() {
162 if (!translate_driver_->IsOffTheRecord()) {
163 prefs_->ResetTranslationDeniedCount(GetOriginalLanguageCode());
164 prefs_->IncrementTranslationAcceptedCount(GetOriginalLanguageCode());
167 if (translate_manager_) {
168 translate_manager_->TranslatePage(
169 GetOriginalLanguageCode(), GetTargetLanguageCode(), false);
170 UMA_HISTOGRAM_BOOLEAN(kPerformTranslate, true);
174 void TranslateUIDelegate::RevertTranslation() {
175 if (translate_manager_) {
176 translate_manager_->RevertTranslation();
177 UMA_HISTOGRAM_BOOLEAN(kRevertTranslation, true);
181 void TranslateUIDelegate::TranslationDeclined(bool explicitly_closed) {
182 if (!translate_driver_->IsOffTheRecord()) {
183 const std::string& language = GetOriginalLanguageCode();
184 prefs_->ResetTranslationAcceptedCount(language);
185 prefs_->IncrementTranslationDeniedCount(language);
186 prefs_->UpdateLastDeniedTime(language);
189 // Remember that the user declined the translation so as to prevent showing a
190 // translate UI for that page again. (TranslateManager initiates translations
191 // when getting a LANGUAGE_DETERMINED from the page, which happens when a load
192 // stops. That could happen multiple times, including after the user already
193 // declined the translation.)
194 if (translate_manager_) {
195 translate_manager_->GetLanguageState().set_translation_declined(true);
196 UMA_HISTOGRAM_BOOLEAN(kDeclineTranslate, true);
199 if (!explicitly_closed)
200 UMA_HISTOGRAM_BOOLEAN(kDeclineTranslateDismissUI, true);
203 bool TranslateUIDelegate::IsLanguageBlocked() {
204 return prefs_->IsBlockedLanguage(GetOriginalLanguageCode());
207 void TranslateUIDelegate::SetLanguageBlocked(bool value) {
208 if (value) {
209 prefs_->BlockLanguage(GetOriginalLanguageCode());
210 if (translate_manager_) {
211 translate_manager_->GetLanguageState().SetTranslateEnabled(false);
213 } else {
214 prefs_->UnblockLanguage(GetOriginalLanguageCode());
217 UMA_HISTOGRAM_BOOLEAN(kNeverTranslateLang, true);
220 bool TranslateUIDelegate::IsSiteBlacklisted() {
221 std::string host = GetPageHost();
222 return !host.empty() && prefs_->IsSiteBlacklisted(host);
225 void TranslateUIDelegate::SetSiteBlacklist(bool value) {
226 std::string host = GetPageHost();
227 if (host.empty())
228 return;
230 if (value) {
231 prefs_->BlacklistSite(host);
232 if (translate_manager_) {
233 translate_manager_->GetLanguageState().SetTranslateEnabled(false);
235 } else {
236 prefs_->RemoveSiteFromBlacklist(host);
239 UMA_HISTOGRAM_BOOLEAN(kNeverTranslateSite, true);
242 bool TranslateUIDelegate::ShouldAlwaysTranslate() {
243 return prefs_->IsLanguagePairWhitelisted(GetOriginalLanguageCode(),
244 GetTargetLanguageCode());
247 void TranslateUIDelegate::SetAlwaysTranslate(bool value) {
248 const std::string& original_lang = GetOriginalLanguageCode();
249 const std::string& target_lang = GetTargetLanguageCode();
250 if (value)
251 prefs_->WhitelistLanguagePair(original_lang, target_lang);
252 else
253 prefs_->RemoveLanguagePairFromWhitelist(original_lang, target_lang);
255 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, true);
258 std::string TranslateUIDelegate::GetPageHost() {
259 if (!translate_driver_->HasCurrentPage())
260 return std::string();
261 return translate_driver_->GetLastCommittedURL().HostNoBrackets();
264 } // namespace translate