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 virtual ~TranslateInfoBarDelegate();
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();
159 // Adds the strings that should be displayed in the after translate infobar to
160 // |strings|. If |autodetermined_source_language| is false, the text in that
162 // "The page has been translated from <lang1> to <lang2>."
164 // "The page has been translated to <lang1>."
165 // Because <lang1>, or <lang1> and <lang2> are displayed in menu buttons, the
166 // text is split in 2 or 3 chunks. |swap_languages| is set to true if
167 // |autodetermined_source_language| is false, and <lang1> and <lang2>
168 // should be inverted (some languages express the sentense as "The page has
169 // been translate to <lang2> from <lang1>."). It is ignored if
170 // |autodetermined_source_language| is true.
171 static void GetAfterTranslateStrings(std::vector
<base::string16
>* strings
,
172 bool* swap_languages
,
173 bool autodetermined_source_language
);
175 // Gets the TranslateDriver associated with this object.
176 // May return NULL if the driver has been destroyed.
177 TranslateDriver
* GetTranslateDriver();
180 TranslateInfoBarDelegate(
181 const base::WeakPtr
<TranslateManager
>& translate_manager
,
182 bool is_off_the_record
,
183 translate::TranslateStep step
,
184 TranslateInfoBarDelegate
* old_delegate
,
185 const std::string
& original_language
,
186 const std::string
& target_language
,
187 TranslateErrors::Type error_type
,
188 bool triggered_from_menu
);
191 friend class TranslationInfoBarTest
;
192 typedef std::pair
<std::string
, base::string16
> LanguageNamePair
;
195 virtual void InfoBarDismissed() override
;
196 virtual int GetIconID() const override
;
197 virtual infobars::InfoBarDelegate::Type
GetInfoBarType() const override
;
198 virtual bool ShouldExpire(const NavigationDetails
& details
) const override
;
199 virtual TranslateInfoBarDelegate
* AsTranslateInfoBarDelegate() override
;
201 bool is_off_the_record_
;
202 translate::TranslateStep step_
;
204 // The type of fading animation if any that should be used when showing this
206 BackgroundAnimationType background_animation_
;
208 TranslateUIDelegate ui_delegate_
;
209 base::WeakPtr
<TranslateManager
> translate_manager_
;
211 // The error that occurred when trying to translate (NONE if no error).
212 TranslateErrors::Type error_type_
;
214 // The translation related preferences.
215 scoped_ptr
<TranslatePrefs
> prefs_
;
217 // Whether the translation was triggered via a menu click vs automatically
218 // (due to language detection, preferences...)
219 bool triggered_from_menu_
;
220 DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate
);
223 } // namespace translate
225 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_INFOBAR_DELEGATE_H_