OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "chrome/browser/translate/translate_infobar_delegate.h" | 5 #include "components/translate/core/browser/translate_infobar_delegate.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/string_compare.h" | 9 #include "base/i18n/string_compare.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "components/infobars/core/infobar.h" | 11 #include "components/infobars/core/infobar.h" |
12 #include "components/infobars/core/infobar_manager.h" | 12 #include "components/infobars/core/infobar_manager.h" |
13 #include "components/translate/core/browser/language_state.h" | 13 #include "components/translate/core/browser/language_state.h" |
14 #include "components/translate/core/browser/translate_accept_languages.h" | 14 #include "components/translate/core/browser/translate_accept_languages.h" |
15 #include "components/translate/core/browser/translate_client.h" | 15 #include "components/translate/core/browser/translate_client.h" |
16 #include "components/translate/core/browser/translate_download_manager.h" | 16 #include "components/translate/core/browser/translate_download_manager.h" |
17 #include "components/translate/core/browser/translate_driver.h" | 17 #include "components/translate/core/browser/translate_driver.h" |
18 #include "components/translate/core/browser/translate_manager.h" | 18 #include "components/translate/core/browser/translate_manager.h" |
19 #include "components/translate/core/common/translate_constants.h" | 19 #include "components/translate/core/common/translate_constants.h" |
20 #include "grit/components_strings.h" | 20 #include "grit/components_strings.h" |
21 #include "grit/theme_resources.h" | |
22 #include "third_party/icu/source/i18n/unicode/coll.h" | |
23 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
24 | 22 |
25 namespace { | 23 namespace { |
26 | 24 |
27 // Counts used to decide whether infobars should be shown. | 25 // Counts used to decide whether infobars should be shown. |
28 // Android and iOS implementations do not offer a drop down (for space reasons), | 26 // Android and iOS implementations do not offer a drop down (for space reasons), |
29 // so we are more aggressive about showing the shortcut to never translate. | 27 // so we are more aggressive about showing the shortcut to never translate. |
30 // The "Always Translate" option is always shown on iOS and Android. | 28 // The "Always Translate" option is always shown on iOS and Android. |
31 #if defined(OS_ANDROID) | 29 #if defined(OS_ANDROID) |
32 const int kAlwaysTranslateMinCount = 1; | 30 const int kAlwaysTranslateMinCount = 1; |
33 const int kNeverTranslateMinCount = 1; | 31 const int kNeverTranslateMinCount = 1; |
34 #elif defined(OS_IOS) | 32 #elif defined(OS_IOS) |
35 // The iOS implementation, like the Android implementation, shows the "Never | 33 // The iOS implementation, like the Android implementation, shows the "Never |
36 // translate" infobar after two denials. There is an offset of one because on | 34 // translate" infobar after two denials. There is an offset of one because on |
37 // Android the last event is not counted. | 35 // Android the last event is not counted. |
38 const int kAlwaysTranslateMinCount = 1; | 36 const int kAlwaysTranslateMinCount = 1; |
39 const int kNeverTranslateMinCount = 2; | 37 const int kNeverTranslateMinCount = 2; |
40 #else | 38 #else |
41 const int kAlwaysTranslateMinCount = 3; | 39 const int kAlwaysTranslateMinCount = 3; |
42 const int kNeverTranslateMinCount = 3; | 40 const int kNeverTranslateMinCount = 3; |
43 #endif | 41 #endif |
44 | 42 |
45 } // namespace | 43 } // namespace |
46 | 44 |
47 const size_t TranslateInfoBarDelegate::kNoIndex = TranslateUIDelegate::NO_INDEX; | 45 const size_t TranslateInfoBarDelegate::kNoIndex = TranslateUIDelegate::NO_INDEX; |
48 | 46 |
49 TranslateInfoBarDelegate::~TranslateInfoBarDelegate() { | 47 TranslateInfoBarDelegate::~TranslateInfoBarDelegate() { |
50 } | 48 } |
51 | 49 |
52 // static | 50 // static |
(...skipping 17 matching lines...) Expand all Loading... |
70 // The original language can only be "unknown" for the "translating" | 68 // The original language can only be "unknown" for the "translating" |
71 // infobar, which is the case when the user started a translation from the | 69 // infobar, which is the case when the user started a translation from the |
72 // context menu. | 70 // context menu. |
73 DCHECK(step == translate::TRANSLATE_STEP_TRANSLATING || | 71 DCHECK(step == translate::TRANSLATE_STEP_TRANSLATING || |
74 step == translate::TRANSLATE_STEP_AFTER_TRANSLATE); | 72 step == translate::TRANSLATE_STEP_AFTER_TRANSLATE); |
75 DCHECK_EQ(translate::kUnknownLanguageCode, original_language); | 73 DCHECK_EQ(translate::kUnknownLanguageCode, original_language); |
76 } | 74 } |
77 } | 75 } |
78 | 76 |
79 // Do not create the after translate infobar if we are auto translating. | 77 // Do not create the after translate infobar if we are auto translating. |
| 78 TranslateClient* translate_client = translate_manager->translate_client(); |
80 if (((step == translate::TRANSLATE_STEP_AFTER_TRANSLATE) || | 79 if (((step == translate::TRANSLATE_STEP_AFTER_TRANSLATE) || |
81 (step == translate::TRANSLATE_STEP_TRANSLATING)) && | 80 (step == translate::TRANSLATE_STEP_TRANSLATING)) && |
82 translate_manager->translate_client()->GetTranslateDriver() | 81 translate_client->GetTranslateDriver()->GetLanguageState() |
83 ->GetLanguageState().InTranslateNavigation()) { | 82 .InTranslateNavigation()) { |
84 return; | 83 return; |
85 } | 84 } |
86 | 85 |
87 // Find any existing translate infobar delegate. | 86 // Find any existing translate infobar delegate. |
88 infobars::InfoBar* old_infobar = NULL; | 87 infobars::InfoBar* old_infobar = NULL; |
89 TranslateInfoBarDelegate* old_delegate = NULL; | 88 TranslateInfoBarDelegate* old_delegate = NULL; |
90 for (size_t i = 0; i < infobar_manager->infobar_count(); ++i) { | 89 for (size_t i = 0; i < infobar_manager->infobar_count(); ++i) { |
91 old_infobar = infobar_manager->infobar_at(i); | 90 old_infobar = infobar_manager->infobar_at(i); |
92 old_delegate = old_infobar->delegate()->AsTranslateInfoBarDelegate(); | 91 old_delegate = old_infobar->delegate()->AsTranslateInfoBarDelegate(); |
93 if (old_delegate) { | 92 if (old_delegate) { |
94 if (!replace_existing_infobar) | 93 if (!replace_existing_infobar) |
95 return; | 94 return; |
96 break; | 95 break; |
97 } | 96 } |
98 } | 97 } |
99 | 98 |
100 // Add the new delegate. | 99 // Add the new delegate. |
101 scoped_ptr<infobars::InfoBar> infobar(CreateInfoBar( | 100 scoped_ptr<infobars::InfoBar> infobar(translate_client->CreateInfoBar( |
102 scoped_ptr<TranslateInfoBarDelegate>(new TranslateInfoBarDelegate( | 101 scoped_ptr<TranslateInfoBarDelegate>(new TranslateInfoBarDelegate( |
103 translate_manager, is_off_the_record, step, old_delegate, | 102 translate_manager, is_off_the_record, step, old_delegate, |
104 original_language, target_language, error_type, | 103 original_language, target_language, error_type, |
105 triggered_from_menu)))); | 104 triggered_from_menu)))); |
106 if (old_delegate) | 105 if (old_delegate) |
107 infobar_manager->ReplaceInfoBar(old_infobar, infobar.Pass()); | 106 infobar_manager->ReplaceInfoBar(old_infobar, infobar.Pass()); |
108 else | 107 else |
109 infobar_manager->AddInfoBar(infobar.Pass()); | 108 infobar_manager->AddInfoBar(infobar.Pass()); |
110 } | 109 } |
111 | 110 |
112 | |
113 void TranslateInfoBarDelegate::UpdateOriginalLanguageIndex( | 111 void TranslateInfoBarDelegate::UpdateOriginalLanguageIndex( |
114 size_t language_index) { | 112 size_t language_index) { |
115 ui_delegate_.UpdateOriginalLanguageIndex(language_index); | 113 ui_delegate_.UpdateOriginalLanguageIndex(language_index); |
116 } | 114 } |
117 | 115 |
118 void TranslateInfoBarDelegate::UpdateTargetLanguageIndex( | 116 void TranslateInfoBarDelegate::UpdateTargetLanguageIndex( |
119 size_t language_index) { | 117 size_t language_index) { |
120 ui_delegate_.UpdateTargetLanguageIndex(language_index); | 118 ui_delegate_.UpdateTargetLanguageIndex(language_index); |
121 } | 119 } |
122 | 120 |
123 void TranslateInfoBarDelegate::Translate() { | 121 void TranslateInfoBarDelegate::Translate() { |
124 ui_delegate_.Translate(); | 122 ui_delegate_.Translate(); |
125 } | 123 } |
126 | 124 |
127 void TranslateInfoBarDelegate::RevertTranslation() { | 125 void TranslateInfoBarDelegate::RevertTranslation() { |
128 ui_delegate_.RevertTranslation(); | 126 ui_delegate_.RevertTranslation(); |
129 infobar()->RemoveSelf(); | 127 infobar()->RemoveSelf(); |
130 } | 128 } |
131 | 129 |
132 void TranslateInfoBarDelegate::ReportLanguageDetectionError() { | 130 void TranslateInfoBarDelegate::ReportLanguageDetectionError() { |
133 if (translate_manager_) | 131 if (translate_manager_) |
134 translate_manager_->ReportLanguageDetectionError(); | 132 translate_manager_->ReportLanguageDetectionError(); |
135 } | 133 } |
136 | 134 |
137 void TranslateInfoBarDelegate::TranslationDeclined() { | 135 void TranslateInfoBarDelegate::TranslationDeclined() { |
138 ui_delegate_.TranslationDeclined(false); | 136 ui_delegate_.TranslationDeclined(false); |
139 } | 137 } |
140 | 138 |
141 bool TranslateInfoBarDelegate::IsTranslatableLanguageByPrefs() { | 139 bool TranslateInfoBarDelegate::IsTranslatableLanguageByPrefs() { |
142 TranslateClient* client = GetTranslateClient(); | 140 TranslateClient* client = translate_manager_->translate_client(); |
143 scoped_ptr<TranslatePrefs> translate_prefs(client->GetTranslatePrefs()); | 141 scoped_ptr<TranslatePrefs> translate_prefs(client->GetTranslatePrefs()); |
144 TranslateAcceptLanguages* accept_languages = | 142 TranslateAcceptLanguages* accept_languages = |
145 client->GetTranslateAcceptLanguages(); | 143 client->GetTranslateAcceptLanguages(); |
146 return translate_prefs->CanTranslateLanguage(accept_languages, | 144 return translate_prefs->CanTranslateLanguage(accept_languages, |
147 original_language_code()); | 145 original_language_code()); |
148 } | 146 } |
149 | 147 |
150 void TranslateInfoBarDelegate::ToggleTranslatableLanguageByPrefs() { | 148 void TranslateInfoBarDelegate::ToggleTranslatableLanguageByPrefs() { |
151 if (ui_delegate_.IsLanguageBlocked()) { | 149 if (ui_delegate_.IsLanguageBlocked()) { |
152 ui_delegate_.SetLanguageBlocked(false); | 150 ui_delegate_.SetLanguageBlocked(false); |
(...skipping 26 matching lines...) Expand all Loading... |
179 | 177 |
180 void TranslateInfoBarDelegate::AlwaysTranslatePageLanguage() { | 178 void TranslateInfoBarDelegate::AlwaysTranslatePageLanguage() { |
181 DCHECK(!ui_delegate_.ShouldAlwaysTranslate()); | 179 DCHECK(!ui_delegate_.ShouldAlwaysTranslate()); |
182 ui_delegate_.SetAlwaysTranslate(true); | 180 ui_delegate_.SetAlwaysTranslate(true); |
183 Translate(); | 181 Translate(); |
184 } | 182 } |
185 | 183 |
186 void TranslateInfoBarDelegate::NeverTranslatePageLanguage() { | 184 void TranslateInfoBarDelegate::NeverTranslatePageLanguage() { |
187 DCHECK(!ui_delegate_.IsLanguageBlocked()); | 185 DCHECK(!ui_delegate_.IsLanguageBlocked()); |
188 ui_delegate_.SetLanguageBlocked(true); | 186 ui_delegate_.SetLanguageBlocked(true); |
189 infobar()->RemoveSelf(); | 187 infobar()->RemoveSelf(); |
190 } | 188 } |
191 | 189 |
192 base::string16 TranslateInfoBarDelegate::GetMessageInfoBarText() { | 190 base::string16 TranslateInfoBarDelegate::GetMessageInfoBarText() { |
193 if (step_ == translate::TRANSLATE_STEP_TRANSLATING) { | 191 if (step_ == translate::TRANSLATE_STEP_TRANSLATING) { |
194 base::string16 target_language_name = | 192 base::string16 target_language_name = |
195 language_name_at(target_language_index()); | 193 language_name_at(target_language_index()); |
196 return l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_TRANSLATING_TO, | 194 return l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_TRANSLATING_TO, |
197 target_language_name); | 195 target_language_name); |
198 } | 196 } |
199 | 197 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 prefs_(translate_manager->translate_client()->GetTranslatePrefs()), | 331 prefs_(translate_manager->translate_client()->GetTranslatePrefs()), |
334 triggered_from_menu_(triggered_from_menu) { | 332 triggered_from_menu_(triggered_from_menu) { |
335 DCHECK_NE((step_ == translate::TRANSLATE_STEP_TRANSLATE_ERROR), | 333 DCHECK_NE((step_ == translate::TRANSLATE_STEP_TRANSLATE_ERROR), |
336 (error_type_ == TranslateErrors::NONE)); | 334 (error_type_ == TranslateErrors::NONE)); |
337 DCHECK(translate_manager_); | 335 DCHECK(translate_manager_); |
338 | 336 |
339 if (old_delegate && (old_delegate->is_error() != is_error())) | 337 if (old_delegate && (old_delegate->is_error() != is_error())) |
340 background_animation_ = is_error() ? NORMAL_TO_ERROR : ERROR_TO_NORMAL; | 338 background_animation_ = is_error() ? NORMAL_TO_ERROR : ERROR_TO_NORMAL; |
341 } | 339 } |
342 | 340 |
343 TranslateClient* TranslateInfoBarDelegate::GetTranslateClient() { | |
344 if (!translate_manager_) | |
345 return NULL; | |
346 | |
347 return translate_manager_->translate_client(); | |
348 } | |
349 | |
350 // TranslateInfoBarDelegate::CreateInfoBar() is implemented in platform-specific | |
351 // files. | |
352 | |
353 void TranslateInfoBarDelegate::InfoBarDismissed() { | 341 void TranslateInfoBarDelegate::InfoBarDismissed() { |
354 if (step_ != translate::TRANSLATE_STEP_BEFORE_TRANSLATE) | 342 if (step_ != translate::TRANSLATE_STEP_BEFORE_TRANSLATE) |
355 return; | 343 return; |
356 | 344 |
357 // The user closed the infobar without clicking the translate button. | 345 // The user closed the infobar without clicking the translate button. |
358 TranslationDeclined(); | 346 TranslationDeclined(); |
359 UMA_HISTOGRAM_BOOLEAN("Translate.DeclineTranslateCloseInfobar", true); | 347 UMA_HISTOGRAM_BOOLEAN("Translate.DeclineTranslateCloseInfobar", true); |
360 } | 348 } |
361 | 349 |
362 int TranslateInfoBarDelegate::GetIconID() const { | 350 int TranslateInfoBarDelegate::GetIconID() const { |
363 return IDR_INFOBAR_TRANSLATE; | 351 return translate_manager_->translate_client()->GetInfobarIconID(); |
364 } | 352 } |
365 | 353 |
366 infobars::InfoBarDelegate::Type TranslateInfoBarDelegate::GetInfoBarType() | 354 infobars::InfoBarDelegate::Type TranslateInfoBarDelegate::GetInfoBarType() |
367 const { | 355 const { |
368 return PAGE_ACTION_TYPE; | 356 return PAGE_ACTION_TYPE; |
369 } | 357 } |
370 | 358 |
371 bool TranslateInfoBarDelegate::ShouldExpire( | 359 bool TranslateInfoBarDelegate::ShouldExpire( |
372 const NavigationDetails& details) const { | 360 const NavigationDetails& details) const { |
373 // Note: we allow closing this infobar even if the main frame navigation | 361 // Note: we allow closing this infobar even if the main frame navigation |
374 // was programmatic and not initiated by the user - crbug.com/70261 . | 362 // was programmatic and not initiated by the user - crbug.com/70261 . |
375 if (!details.is_navigation_to_different_page && !details.is_main_frame) | 363 if (!details.is_navigation_to_different_page && !details.is_main_frame) |
376 return false; | 364 return false; |
377 | 365 |
378 return infobars::InfoBarDelegate::ShouldExpireInternal(details); | 366 return infobars::InfoBarDelegate::ShouldExpireInternal(details); |
379 } | 367 } |
380 | 368 |
381 TranslateInfoBarDelegate* | 369 TranslateInfoBarDelegate* |
382 TranslateInfoBarDelegate::AsTranslateInfoBarDelegate() { | 370 TranslateInfoBarDelegate::AsTranslateInfoBarDelegate() { |
383 return this; | 371 return this; |
384 } | 372 } |
OLD | NEW |