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 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_INFOBAR_DELEGATE_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_INFOBAR_DELEGATE_H_
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "components/infobars/core/infobar_delegate.h"
16 #include "components/translate/core/browser/translate_prefs.h"
17 #include "components/translate/core/browser/translate_step.h"
18 #include "components/translate/core/browser/translate_ui_delegate.h"
19 #include "components/translate/core/common/translate_constants.h"
20 #include "components/translate/core/common/translate_errors.h"
28 class TranslateClient
;
29 class TranslateDriver
;
30 class TranslateManager
;
32 class TranslateInfoBarDelegate
: public infobars::InfoBarDelegate
{
34 // The types of background color animations.
35 enum BackgroundAnimationType
{
41 static const size_t kNoIndex
;
43 ~TranslateInfoBarDelegate() override
;
45 // Factory method to create a translate infobar. |error_type| must be
46 // specified iff |step| == TRANSLATION_ERROR. For other translate steps,
47 // |original_language| and |target_language| must be ASCII language codes
48 // (e.g. "en", "fr", etc.) for languages the TranslateManager supports
49 // translating. The lone exception is when the user initiates translation
50 // from the context menu, in which case it's legal to call this with
51 // |step| == TRANSLATING and |original_language| == kUnknownLanguageCode.
53 // If |replace_existing_infobar| is true, the infobar is created and added to
54 // the infobar manager, replacing any other translate infobar already present
55 // there. Otherwise, the infobar will only be added if there is no other
56 // translate infobar already present.
57 static void Create(bool replace_existing_infobar
,
58 const base::WeakPtr
<TranslateManager
>& translate_manager
,
59 infobars::InfoBarManager
* infobar_manager
,
60 bool is_off_the_record
,
61 translate::TranslateStep step
,
62 const std::string
& original_language
,
63 const std::string
& target_language
,
64 TranslateErrors::Type error_type
,
65 bool triggered_from_menu
);
67 // Returns the number of languages supported.
68 size_t num_languages() const { return ui_delegate_
.GetNumberOfLanguages(); }
70 // Returns the ISO code for the language at |index|.
71 std::string
language_code_at(size_t index
) const {
72 return ui_delegate_
.GetLanguageCodeAt(index
);
75 // Returns the displayable name for the language at |index|.
76 base::string16
language_name_at(size_t index
) const {
77 return ui_delegate_
.GetLanguageNameAt(index
);
80 translate::TranslateStep
translate_step() const { return step_
; }
82 bool is_off_the_record() { return is_off_the_record_
; }
84 TranslateErrors::Type
error_type() const { return error_type_
; }
86 size_t original_language_index() const {
87 return ui_delegate_
.GetOriginalLanguageIndex();
89 void UpdateOriginalLanguageIndex(size_t language_index
);
91 size_t target_language_index() const {
92 return ui_delegate_
.GetTargetLanguageIndex();
94 void UpdateTargetLanguageIndex(size_t language_index
);
96 // Convenience methods.
97 std::string
original_language_code() const {
98 return ui_delegate_
.GetOriginalLanguageCode();
100 std::string
target_language_code() const {
101 return ui_delegate_
.GetTargetLanguageCode();
104 // Returns true if the current infobar indicates an error (in which case it
105 // should get a yellow background instead of a blue one).
106 bool is_error() const {
107 return step_
== translate::TRANSLATE_STEP_TRANSLATE_ERROR
;
110 // Return true if the translation was triggered by a menu entry instead of
111 // via an infobar/bubble or preference.
112 bool triggered_from_menu() const {
113 return triggered_from_menu_
;
116 // Returns what kind of background fading effect the infobar should use when
118 BackgroundAnimationType
background_animation_type() const {
119 return background_animation_
;
122 virtual void Translate();
123 virtual void RevertTranslation();
124 void ReportLanguageDetectionError();
126 // Called when the user declines to translate a page, by either closing the
127 // infobar or pressing the "Don't translate" button.
128 virtual void TranslationDeclined();
130 // Methods called by the Options menu delegate.
131 virtual bool IsTranslatableLanguageByPrefs();
132 virtual void ToggleTranslatableLanguageByPrefs();
133 virtual bool IsSiteBlacklisted();
134 virtual void ToggleSiteBlacklist();
135 virtual bool ShouldAlwaysTranslate();
136 virtual void ToggleAlwaysTranslate();
138 // Methods called by the extra-buttons that can appear on the "before
139 // translate" infobar (when the user has accepted/declined the translation
141 void AlwaysTranslatePageLanguage();
142 void NeverTranslatePageLanguage();
144 // The following methods are called by the infobar that displays the status
145 // while translating and also the one displaying the error message.
146 base::string16
GetMessageInfoBarText();
147 base::string16
GetMessageInfoBarButtonText();
148 void MessageInfoBarButtonPressed();
149 bool ShouldShowMessageInfoBarButton();
151 // Called by the before translate infobar to figure-out if it should show
152 // an extra shortcut to let the user black-list/white-list that language
153 // (based on how many times the user accepted/declined translation).
154 // The shortcut itself is platform specific, it can be a button or a new bar
156 bool ShouldShowNeverTranslateShortcut();
157 bool ShouldShowAlwaysTranslateShortcut();
160 // Shows the Infobar offering to never translate the language or the site.
161 void ShowNeverTranslateInfobar();
164 // Adds the strings that should be displayed in the after translate infobar to
165 // |strings|. If |autodetermined_source_language| is false, the text in that
167 // "The page has been translated from <lang1> to <lang2>."
169 // "The page has been translated to <lang1>."
170 // Because <lang1>, or <lang1> and <lang2> are displayed in menu buttons, the
171 // text is split in 2 or 3 chunks. |swap_languages| is set to true if
172 // |autodetermined_source_language| is false, and <lang1> and <lang2>
173 // should be inverted (some languages express the sentense as "The page has
174 // been translate to <lang2> from <lang1>."). It is ignored if
175 // |autodetermined_source_language| is true.
176 static void GetAfterTranslateStrings(std::vector
<base::string16
>* strings
,
177 bool* swap_languages
,
178 bool autodetermined_source_language
);
180 // Gets the TranslateDriver associated with this object.
181 // May return NULL if the driver has been destroyed.
182 TranslateDriver
* GetTranslateDriver();
185 TranslateInfoBarDelegate(
186 const base::WeakPtr
<TranslateManager
>& translate_manager
,
187 bool is_off_the_record
,
188 translate::TranslateStep step
,
189 TranslateInfoBarDelegate
* old_delegate
,
190 const std::string
& original_language
,
191 const std::string
& target_language
,
192 TranslateErrors::Type error_type
,
193 bool triggered_from_menu
);
196 friend class TranslationInfoBarTest
;
197 typedef std::pair
<std::string
, base::string16
> LanguageNamePair
;
200 Type
GetInfoBarType() const override
;
201 int GetIconID() const override
;
202 bool ShouldExpire(const NavigationDetails
& details
) const override
;
203 void InfoBarDismissed() override
;
204 TranslateInfoBarDelegate
* AsTranslateInfoBarDelegate() override
;
206 bool is_off_the_record_
;
207 translate::TranslateStep step_
;
209 // The type of fading animation if any that should be used when showing this
211 BackgroundAnimationType background_animation_
;
213 TranslateUIDelegate ui_delegate_
;
214 base::WeakPtr
<TranslateManager
> translate_manager_
;
216 // The error that occurred when trying to translate (NONE if no error).
217 TranslateErrors::Type error_type_
;
219 // The translation related preferences.
220 scoped_ptr
<TranslatePrefs
> prefs_
;
222 // Whether the translation was triggered via a menu click vs automatically
223 // (due to language detection, preferences...)
224 bool triggered_from_menu_
;
225 DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate
);
228 } // namespace translate
230 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_INFOBAR_DELEGATE_H_