OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/translate/translate_infobar_delegate.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // The original language can only be "unknown" for the "translating" | 43 // The original language can only be "unknown" for the "translating" |
44 // infobar, which is the case when the user started a translation from the | 44 // infobar, which is the case when the user started a translation from the |
45 // context menu. | 45 // context menu. |
46 DCHECK_EQ(TRANSLATING, infobar_type); | 46 DCHECK_EQ(TRANSLATING, infobar_type); |
47 DCHECK_EQ(chrome::kUnknownLanguageCode, original_language); | 47 DCHECK_EQ(chrome::kUnknownLanguageCode, original_language); |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 // Find any existing translate infobar delegate. | 51 // Find any existing translate infobar delegate. |
52 TranslateInfoBarDelegate* old_delegate = NULL; | 52 TranslateInfoBarDelegate* old_delegate = NULL; |
53 for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { | 53 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { |
54 old_delegate = | 54 old_delegate = infobar_service->infobar_at(i)->AsTranslateInfoBarDelegate(); |
55 infobar_service->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | |
56 if (old_delegate) | 55 if (old_delegate) |
57 break; | 56 break; |
58 } | 57 } |
59 | 58 |
60 // Create the new delegate. | 59 // Create the new delegate. |
61 scoped_ptr<TranslateInfoBarDelegate> infobar( | 60 scoped_ptr<TranslateInfoBarDelegate> infobar( |
62 new TranslateInfoBarDelegate(infobar_type, error_type, infobar_service, | 61 new TranslateInfoBarDelegate(infobar_type, error_type, infobar_service, |
63 prefs, original_language, target_language)); | 62 prefs, original_language, target_language)); |
64 infobar->UpdateBackgroundAnimation(old_delegate); | 63 infobar->UpdateBackgroundAnimation(old_delegate); |
65 | 64 |
66 // Add the new delegate if necessary. | 65 // Add the new delegate if necessary. |
67 if (!old_delegate) { | 66 if (!old_delegate) { |
68 infobar_service->AddInfoBar(infobar.PassAs<InfoBarDelegate>()); | 67 infobar_service->AddInfoBar(infobar.PassAs<InfoBarDelegate>()); |
69 } else if (replace_existing_infobar) { | 68 } else if (replace_existing_infobar) { |
70 infobar_service->ReplaceInfoBar(old_delegate, | 69 infobar_service->ReplaceInfoBar(old_delegate, |
71 infobar.PassAs<InfoBarDelegate>()); | 70 infobar.PassAs<InfoBarDelegate>()); |
72 } | 71 } |
73 } | 72 } |
74 | 73 |
75 void TranslateInfoBarDelegate::Translate() { | 74 void TranslateInfoBarDelegate::Translate() { |
76 if (!owner()->GetWebContents()->GetBrowserContext()->IsOffTheRecord()) { | 75 if (!owner()->web_contents()->GetBrowserContext()->IsOffTheRecord()) { |
77 prefs_.ResetTranslationDeniedCount(original_language_code()); | 76 prefs_.ResetTranslationDeniedCount(original_language_code()); |
78 prefs_.IncrementTranslationAcceptedCount(original_language_code()); | 77 prefs_.IncrementTranslationAcceptedCount(original_language_code()); |
79 } | 78 } |
80 | 79 |
81 TranslateManager::GetInstance()->TranslatePage(owner()->GetWebContents(), | 80 TranslateManager::GetInstance()->TranslatePage(owner()->web_contents(), |
82 original_language_code(), | 81 original_language_code(), |
83 target_language_code()); | 82 target_language_code()); |
84 | 83 |
85 UMA_HISTOGRAM_COUNTS("Translate.Translate", 1); | 84 UMA_HISTOGRAM_COUNTS("Translate.Translate", 1); |
86 } | 85 } |
87 | 86 |
88 void TranslateInfoBarDelegate::RevertTranslation() { | 87 void TranslateInfoBarDelegate::RevertTranslation() { |
89 TranslateManager::GetInstance()->RevertTranslation(owner()->GetWebContents()); | 88 TranslateManager::GetInstance()->RevertTranslation(owner()->web_contents()); |
90 RemoveSelf(); | 89 RemoveSelf(); |
91 } | 90 } |
92 | 91 |
93 void TranslateInfoBarDelegate::ReportLanguageDetectionError() { | 92 void TranslateInfoBarDelegate::ReportLanguageDetectionError() { |
94 TranslateManager::GetInstance()-> | 93 TranslateManager::GetInstance()-> |
95 ReportLanguageDetectionError(owner()->GetWebContents()); | 94 ReportLanguageDetectionError(owner()->web_contents()); |
96 } | 95 } |
97 | 96 |
98 void TranslateInfoBarDelegate::TranslationDeclined() { | 97 void TranslateInfoBarDelegate::TranslationDeclined() { |
99 if (!owner()->GetWebContents()->GetBrowserContext()->IsOffTheRecord()) { | 98 if (!owner()->web_contents()->GetBrowserContext()->IsOffTheRecord()) { |
100 prefs_.ResetTranslationAcceptedCount(original_language_code()); | 99 prefs_.ResetTranslationAcceptedCount(original_language_code()); |
101 prefs_.IncrementTranslationDeniedCount(original_language_code()); | 100 prefs_.IncrementTranslationDeniedCount(original_language_code()); |
102 } | 101 } |
103 | 102 |
104 // Remember that the user declined the translation so as to prevent showing a | 103 // Remember that the user declined the translation so as to prevent showing a |
105 // translate infobar for that page again. (TranslateManager initiates | 104 // translate infobar for that page again. (TranslateManager initiates |
106 // translations when getting a LANGUAGE_DETERMINED from the page, which | 105 // translations when getting a LANGUAGE_DETERMINED from the page, which |
107 // happens when a load stops. That could happen multiple times, including | 106 // happens when a load stops. That could happen multiple times, including |
108 // after the user already declined the translation.) | 107 // after the user already declined the translation.) |
109 TranslateTabHelper* translate_tab_helper = | 108 TranslateTabHelper* translate_tab_helper = |
110 TranslateTabHelper::FromWebContents(owner()->GetWebContents()); | 109 TranslateTabHelper::FromWebContents(owner()->web_contents()); |
111 translate_tab_helper->language_state().set_translation_declined(true); | 110 translate_tab_helper->language_state().set_translation_declined(true); |
112 | 111 |
113 UMA_HISTOGRAM_COUNTS("Translate.DeclineTranslate", 1); | 112 UMA_HISTOGRAM_COUNTS("Translate.DeclineTranslate", 1); |
114 } | 113 } |
115 | 114 |
116 bool TranslateInfoBarDelegate::IsLanguageBlacklisted() { | 115 bool TranslateInfoBarDelegate::IsLanguageBlacklisted() { |
117 return prefs_.IsLanguageBlacklisted(original_language_code()); | 116 return prefs_.IsLanguageBlacklisted(original_language_code()); |
118 } | 117 } |
119 | 118 |
120 void TranslateInfoBarDelegate::ToggleLanguageBlacklist() { | 119 void TranslateInfoBarDelegate::ToggleLanguageBlacklist() { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 return string16(); | 217 return string16(); |
219 } | 218 } |
220 | 219 |
221 void TranslateInfoBarDelegate::MessageInfoBarButtonPressed() { | 220 void TranslateInfoBarDelegate::MessageInfoBarButtonPressed() { |
222 DCHECK_EQ(TRANSLATION_ERROR, infobar_type_); | 221 DCHECK_EQ(TRANSLATION_ERROR, infobar_type_); |
223 if (error_type_ == TranslateErrors::UNSUPPORTED_LANGUAGE) { | 222 if (error_type_ == TranslateErrors::UNSUPPORTED_LANGUAGE) { |
224 RevertTranslation(); | 223 RevertTranslation(); |
225 return; | 224 return; |
226 } | 225 } |
227 // This is the "Try again..." case. | 226 // This is the "Try again..." case. |
228 TranslateManager::GetInstance()->TranslatePage(owner()->GetWebContents(), | 227 TranslateManager::GetInstance()->TranslatePage(owner()->web_contents(), |
229 original_language_code(), target_language_code()); | 228 original_language_code(), |
| 229 target_language_code()); |
230 } | 230 } |
231 | 231 |
232 bool TranslateInfoBarDelegate::ShouldShowMessageInfoBarButton() { | 232 bool TranslateInfoBarDelegate::ShouldShowMessageInfoBarButton() { |
233 return !GetMessageInfoBarButtonText().empty(); | 233 return !GetMessageInfoBarButtonText().empty(); |
234 } | 234 } |
235 | 235 |
236 bool TranslateInfoBarDelegate::ShouldShowNeverTranslateButton() { | 236 bool TranslateInfoBarDelegate::ShouldShowNeverTranslateButton() { |
237 DCHECK_EQ(BEFORE_TRANSLATE, infobar_type_); | 237 DCHECK_EQ(BEFORE_TRANSLATE, infobar_type_); |
238 return !owner()->GetWebContents()->GetBrowserContext()->IsOffTheRecord() && | 238 return !owner()->web_contents()->GetBrowserContext()->IsOffTheRecord() && |
239 (prefs_.GetTranslationDeniedCount(original_language_code()) >= 3); | 239 (prefs_.GetTranslationDeniedCount(original_language_code()) >= 3); |
240 } | 240 } |
241 | 241 |
242 bool TranslateInfoBarDelegate::ShouldShowAlwaysTranslateButton() { | 242 bool TranslateInfoBarDelegate::ShouldShowAlwaysTranslateButton() { |
243 DCHECK_EQ(BEFORE_TRANSLATE, infobar_type_); | 243 DCHECK_EQ(BEFORE_TRANSLATE, infobar_type_); |
244 return !owner()->GetWebContents()->GetBrowserContext()->IsOffTheRecord() && | 244 return !owner()->web_contents()->GetBrowserContext()->IsOffTheRecord() && |
245 (prefs_.GetTranslationAcceptedCount(original_language_code()) >= 3); | 245 (prefs_.GetTranslationAcceptedCount(original_language_code()) >= 3); |
246 } | 246 } |
247 | 247 |
248 void TranslateInfoBarDelegate::UpdateBackgroundAnimation( | 248 void TranslateInfoBarDelegate::UpdateBackgroundAnimation( |
249 TranslateInfoBarDelegate* previous_infobar) { | 249 TranslateInfoBarDelegate* previous_infobar) { |
250 if (!previous_infobar || previous_infobar->IsError() == IsError()) | 250 if (!previous_infobar || previous_infobar->IsError() == IsError()) |
251 background_animation_ = NONE; | 251 background_animation_ = NONE; |
252 else | 252 else |
253 background_animation_ = IsError() ? NORMAL_TO_ERROR : ERROR_TO_NORMAL; | 253 background_animation_ = IsError() ? NORMAL_TO_ERROR : ERROR_TO_NORMAL; |
254 } | 254 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 return PAGE_ACTION_TYPE; | 357 return PAGE_ACTION_TYPE; |
358 } | 358 } |
359 | 359 |
360 TranslateInfoBarDelegate* | 360 TranslateInfoBarDelegate* |
361 TranslateInfoBarDelegate::AsTranslateInfoBarDelegate() { | 361 TranslateInfoBarDelegate::AsTranslateInfoBarDelegate() { |
362 return this; | 362 return this; |
363 } | 363 } |
364 | 364 |
365 std::string TranslateInfoBarDelegate::GetPageHost() { | 365 std::string TranslateInfoBarDelegate::GetPageHost() { |
366 NavigationEntry* entry = | 366 NavigationEntry* entry = |
367 owner()->GetWebContents()->GetController().GetActiveEntry(); | 367 owner()->web_contents()->GetController().GetActiveEntry(); |
368 return entry ? entry->GetURL().HostNoBrackets() : std::string(); | 368 return entry ? entry->GetURL().HostNoBrackets() : std::string(); |
369 } | 369 } |
OLD | NEW |