- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / translate / translate_infobar_delegate.h
1 // Copyright (c) 2011 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.
4
5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_
7
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/compiler_specific.h"
13 #include "base/logging.h"
14 #include "chrome/browser/infobars/infobar_delegate.h"
15 #include "chrome/browser/translate/translate_prefs.h"
16 #include "chrome/browser/translate/translate_ui_delegate.h"
17 #include "chrome/common/translate/translate_errors.h"
18 #include "components/translate/common/translate_constants.h"
19
20 class PrefService;
21
22 // The defaults after which extra shortcuts for options
23 // can be shown.
24 struct ShortcutConfiguration {
25   int always_translate_min_count;
26   int never_translate_min_count;
27 };
28
29 class TranslateInfoBarDelegate : public InfoBarDelegate {
30  public:
31   // The different types of infobars that can be shown for translation.
32   enum Type {
33     BEFORE_TRANSLATE,
34     TRANSLATING,
35     AFTER_TRANSLATE,
36     TRANSLATION_ERROR
37   };
38
39   // The types of background color animations.
40   enum BackgroundAnimationType {
41     NONE,
42     NORMAL_TO_ERROR,
43     ERROR_TO_NORMAL
44   };
45
46   static const size_t kNoIndex;
47
48   virtual ~TranslateInfoBarDelegate();
49
50   // Factory method to create a translate infobar.  |error_type| must be
51   // specified iff |infobar_type| == TRANSLATION_ERROR.  For other infobar
52   // types, |original_language| and |target_language| must be ASCII language
53   // codes (e.g. "en", "fr", etc.) for languages the TranslateManager supports
54   // translating.  The lone exception is when the user initiates translation
55   // from the context menu, in which case it's legal to call this with
56   // |infobar_type| == TRANSLATING and
57   // |original_language| == kUnknownLanguageCode.
58   //
59   // If |replace_existing_infobar| is true, the infobar is created and added to
60   // |infobar_service|, replacing any other translate infobar already present
61   // there.  Otherwise, the infobar will only be added if there is no other
62   // translate infobar already present.
63   static void Create(bool replace_existing_infobar,
64                      InfoBarService* infobar_service,
65                      Type infobar_type,
66                      const std::string& original_language,
67                      const std::string& target_language,
68                      TranslateErrors::Type error_type,
69                      PrefService* prefs,
70                      const ShortcutConfiguration& shortcut_config);
71
72   // Returns the number of languages supported.
73   size_t num_languages() const { return ui_delegate_.GetNumberOfLanguages(); }
74
75   // Returns the ISO code for the language at |index|.
76   std::string language_code_at(size_t index) const {
77     return ui_delegate_.GetLanguageCodeAt(index);
78   }
79
80   // Returns the displayable name for the language at |index|.
81   string16 language_name_at(size_t index) const {
82     return ui_delegate_.GetLanguageNameAt(index);
83   }
84
85   Type infobar_type() const { return infobar_type_; }
86
87   TranslateErrors::Type error_type() const { return error_type_; }
88
89   size_t original_language_index() const {
90     return ui_delegate_.GetOriginalLanguageIndex();
91   }
92   void UpdateOriginalLanguageIndex(size_t language_index);
93
94   size_t target_language_index() const {
95     return ui_delegate_.GetTargetLanguageIndex();
96   }
97   void UpdateTargetLanguageIndex(size_t language_index);
98
99   // Convenience methods.
100   std::string original_language_code() const {
101     return ui_delegate_.GetOriginalLanguageCode();
102   }
103   std::string target_language_code() const {
104     return ui_delegate_.GetTargetLanguageCode();
105   }
106
107   // Returns true if the current infobar indicates an error (in which case it
108   // should get a yellow background instead of a blue one).
109   bool is_error() const { return infobar_type_ == TRANSLATION_ERROR; }
110
111   // Returns what kind of background fading effect the infobar should use when
112   // its is shown.
113   BackgroundAnimationType background_animation_type() const {
114     return background_animation_;
115   }
116
117   virtual void Translate();
118   virtual void RevertTranslation();
119   void ReportLanguageDetectionError();
120
121   // Called when the user declines to translate a page, by either closing the
122   // infobar or pressing the "Don't translate" button.
123   virtual void TranslationDeclined();
124
125   // Methods called by the Options menu delegate.
126   virtual bool IsTranslatableLanguageByPrefs();
127   virtual void ToggleTranslatableLanguageByPrefs();
128   virtual bool IsSiteBlacklisted();
129   virtual void ToggleSiteBlacklist();
130   virtual bool ShouldAlwaysTranslate();
131   virtual void ToggleAlwaysTranslate();
132
133   // Methods called by the extra-buttons that can appear on the "before
134   // translate" infobar (when the user has accepted/declined the translation
135   // several times).
136   void AlwaysTranslatePageLanguage();
137   void NeverTranslatePageLanguage();
138
139   // The following methods are called by the infobar that displays the status
140   // while translating and also the one displaying the error message.
141   string16 GetMessageInfoBarText();
142   string16 GetMessageInfoBarButtonText();
143   void MessageInfoBarButtonPressed();
144   bool ShouldShowMessageInfoBarButton();
145
146   // Called by the before translate infobar to figure-out if it should show
147   // an extra shortcut to let the user black-list/white-list that language
148   // (based on how many times the user accepted/declined translation).
149   // The shortcut itself is platform specific, it can be a button or a new bar
150   // for example.
151   bool ShouldShowNeverTranslateShortcut();
152   bool ShouldShowAlwaysTranslateShortcut();
153
154   // Convenience method that returns the displayable language name for
155   // |language_code| in the current application locale.
156   static string16 GetLanguageDisplayableName(const std::string& language_code);
157
158   // Adds the strings that should be displayed in the after translate infobar to
159   // |strings|. If |autodetermined_source_language| is false, the text in that
160   // infobar is:
161   // "The page has been translated from <lang1> to <lang2>."
162   // Otherwise:
163   // "The page has been translated to <lang1>."
164   // Because <lang1>, or <lang1> and <lang2> are displayed in menu buttons, the
165   // text is split in 2 or 3 chunks. |swap_languages| is set to true if
166   // |autodetermined_source_language| is false, and <lang1> and <lang2>
167   // should be inverted (some languages express the sentense as "The page has
168   // been translate to <lang2> from <lang1>."). It is ignored if
169   // |autodetermined_source_language| is true.
170   static void GetAfterTranslateStrings(std::vector<string16>* strings,
171                                        bool* swap_languages,
172                                        bool autodetermined_source_language);
173
174  protected:
175   TranslateInfoBarDelegate(InfoBarService* infobar_service,
176                            Type infobar_type,
177                            TranslateInfoBarDelegate* old_delegate,
178                            const std::string& original_language,
179                            const std::string& target_language,
180                            TranslateErrors::Type error_type,
181                            PrefService* prefs,
182                            ShortcutConfiguration shortcut_config);
183
184  private:
185   typedef std::pair<std::string, string16> LanguageNamePair;
186
187   // InfoBarDelegate:
188   virtual InfoBar* CreateInfoBar(InfoBarService* infobar_service) OVERRIDE;
189   virtual void InfoBarDismissed() OVERRIDE;
190   virtual int GetIconID() const OVERRIDE;
191   virtual InfoBarDelegate::Type GetInfoBarType() const OVERRIDE;
192   virtual bool ShouldExpire(
193        const content::LoadCommittedDetails& details) const OVERRIDE;
194   virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() OVERRIDE;
195
196   Type infobar_type_;
197
198   // The type of fading animation if any that should be used when showing this
199   // infobar.
200   BackgroundAnimationType background_animation_;
201
202   TranslateUIDelegate ui_delegate_;
203
204   // The error that occurred when trying to translate (NONE if no error).
205   TranslateErrors::Type error_type_;
206
207   // The translation related preferences.
208   TranslatePrefs prefs_;
209
210   // Translation shortcut configuration
211   ShortcutConfiguration shortcut_config_;
212   DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate);
213 };
214
215 #endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_