translate_infobar_delegate.h revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2010 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#pragma once
8
9#include <string>
10#include <vector>
11
12#include "chrome/browser/tab_contents/infobar_delegate.h"
13#include "chrome/browser/translate/translate_prefs.h"
14#include "chrome/common/translate_errors.h"
15
16class SkBitmap;
17class TranslateInfoBarView;
18
19class TranslateInfoBarDelegate : public InfoBarDelegate {
20 public:
21  // The different types of infobars that can be shown for translation.
22  enum Type {
23    BEFORE_TRANSLATE,
24    TRANSLATING,
25    AFTER_TRANSLATE,
26    TRANSLATION_ERROR
27  };
28
29  // The types of background color animations.
30  enum BackgroundAnimationType {
31    NONE,
32    NORMAL_TO_ERROR,
33    ERROR_TO_NORMAL
34  };
35
36  // Factory method to create a non-error translate infobar.
37  // The original and target language specified are the ASCII language codes
38  // (ex: en, fr...).
39  // Returns NULL if it failed, typically if |original_language| or
40  // |target_language| is not a supported language.
41  static TranslateInfoBarDelegate* CreateDelegate(
42      Type infobar_type,
43      TabContents* tab_contents,
44      const std::string& original_language,
45      const std::string& target_language);
46
47  // Factory method to create an error translate infobar.
48  static TranslateInfoBarDelegate* CreateErrorDelegate(
49      TranslateErrors::Type error_type,
50      TabContents* tab_contents,
51      const std::string& original_language,
52      const std::string& target_language);
53
54  virtual ~TranslateInfoBarDelegate();
55
56  // Returns the number of languages supported.
57  int GetLanguageCount() const;
58
59  // Returns the ISO code for the language at |index|.
60  std::string GetLanguageCodeAt(int index) const;
61
62  // Returns the displayable name for the language at |index|.
63  string16 GetLanguageDisplayableNameAt(int index) const;
64
65  TabContents* tab_contents() const { return tab_contents_; }
66
67  Type type() const { return type_; }
68
69  TranslateErrors::Type error() const { return error_; }
70
71  int original_language_index() const { return original_language_index_; }
72  int target_language_index() const { return target_language_index_; }
73
74  // Convenience methods.
75  std::string GetOriginalLanguageCode() const;
76  std::string GetTargetLanguageCode() const;
77
78  // Called by the InfoBar to notify that the original/target language has
79  // changed and is now the language at |language_index|.
80  virtual void SetOriginalLanguage(int language_index);
81  virtual void SetTargetLanguage(int language_index);
82
83  // Returns true if the current infobar indicates an error (in which case it
84  // should get a yellow background instead of a blue one).
85  bool IsError();
86
87  // Returns what kind of background fading effect the infobar should use when
88  // its is shown.
89  BackgroundAnimationType background_animation_type() const {
90    return background_animation_;
91  }
92
93  virtual void Translate();
94  virtual void RevertTranslation();
95  virtual void ReportLanguageDetectionError();
96
97  // Called when the user declines to translate a page, by either closing the
98  // infobar or pressing the "Don't translate" button.
99  void TranslationDeclined();
100
101  // InfoBarDelegate implementation:
102  virtual InfoBar* CreateInfoBar();
103  virtual void InfoBarDismissed();
104  virtual void InfoBarClosed();
105  virtual SkBitmap* GetIcon() const;
106  virtual InfoBarDelegate::Type GetInfoBarType();
107  virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate();
108
109  // Methods called by the Options menu delegate.
110  virtual bool IsLanguageBlacklisted();
111  virtual void ToggleLanguageBlacklist();
112  virtual bool IsSiteBlacklisted();
113  virtual void ToggleSiteBlacklist();
114  virtual bool ShouldAlwaysTranslate();
115  virtual void ToggleAlwaysTranslate();
116
117  // Methods called by the extra-buttons that can appear on the "before
118  // translate" infobar (when the user has accepted/declined the translation
119  // several times).
120  virtual void AlwaysTranslatePageLanguage();
121  virtual void NeverTranslatePageLanguage();
122
123  // The following methods are called by the infobar that displays the status
124  // while translating and also the one displaying the error message.
125  string16 GetMessageInfoBarText();
126  string16 GetMessageInfoBarButtonText();
127  void MessageInfoBarButtonPressed();
128  bool ShouldShowMessageInfoBarButton();
129
130  // Called by the before translate infobar to figure-out if it should show
131  // an extra button to let the user black-list/white-list that language (based
132  // on how many times the user accepted/declined translation).
133  bool ShouldShowNeverTranslateButton();
134  bool ShouldShowAlwaysTranslateButton();
135
136  // Sets this infobar background animation based on the previous infobar shown.
137  // A fading background effect is used when transitioning from a normal state
138  // to an error state (and vice-versa).
139  void UpdateBackgroundAnimation(TranslateInfoBarDelegate* previous_infobar);
140
141  // Convenience method that returns the displayable language name for
142  // |language_code| in the current application locale.
143  static string16 GetLanguageDisplayableName(const std::string& language_code);
144
145  // Adds the strings that should be displayed in the after translate infobar to
146  // |strings|. The text in that infobar is:
147  // "The page has been translated from <lang1> to <lang2>."
148  // Because <lang1> and <lang2> are displayed in menu buttons, the text is
149  // split in 3 chunks.  |swap_languages| is set to true if <lang1> and <lang2>
150  // should be inverted (some languages express the sentense as "The page has
151  // been translate to <lang2> from <lang1>.").
152  static void GetAfterTranslateStrings(std::vector<string16>* strings,
153                                       bool* swap_languages);
154
155 protected:
156  // For testing.
157  TranslateInfoBarDelegate(Type infobar_type,
158                           TranslateErrors::Type error,
159                           TabContents* tab_contents,
160                           const std::string& original_language,
161                           const std::string& target_language);
162  Type type_;
163
164 private:
165  typedef std::pair<std::string, string16> LanguageNamePair;
166
167  // Gets the host of the page being translated, or an empty string if no URL is
168  // associated with the current page.
169  std::string GetPageHost();
170
171  // The type of fading animation if any that should be used when showing this
172  // infobar.
173  BackgroundAnimationType background_animation_;
174
175  TabContents* tab_contents_;
176
177  // The list supported languages for translation.
178  // The pair first string is the language ISO code (ex: en, fr...), the second
179  // string is the displayable name on the current locale.
180  // The languages are sorted alphabetically based on the displayable name.
181  std::vector<LanguageNamePair> languages_;
182
183  // The index for language the page is originally in.
184  int original_language_index_;
185
186  // The index for language the page is originally in that was originally
187  // reported (original_language_index_ changes if the user selects a new
188  // original language, but this one does not).  This is necessary to report
189  // language detection errors with the right original language even if the user
190  // changed the original language.
191  int initial_original_language_index_;
192
193  // The index for language the page should be translated to.
194  int target_language_index_;
195
196  // The error that occurred when trying to translate (NONE if no error).
197  TranslateErrors::Type error_;
198
199  // The current infobar view.
200  TranslateInfoBarView* infobar_view_;
201
202  // The translation related preferences.
203  TranslatePrefs prefs_;
204
205  DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate);
206};
207
208#endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_
209