OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 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 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_ | 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_ |
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_ | 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "components/infobars/core/infobar_delegate.h" | 15 #include "components/infobars/core/infobar_delegate.h" |
16 #include "components/translate/core/browser/translate_prefs.h" | 16 #include "components/translate/core/browser/translate_prefs.h" |
17 #include "components/translate/core/browser/translate_step.h" | 17 #include "components/translate/core/browser/translate_step.h" |
18 #include "components/translate/core/browser/translate_ui_delegate.h" | 18 #include "components/translate/core/browser/translate_ui_delegate.h" |
19 #include "components/translate/core/common/translate_constants.h" | 19 #include "components/translate/core/common/translate_constants.h" |
20 #include "components/translate/core/common/translate_errors.h" | 20 #include "components/translate/core/common/translate_errors.h" |
21 | 21 |
22 class InfoBarService; | |
23 class PrefService; | 22 class PrefService; |
24 class TranslateClient; | 23 class TranslateClient; |
25 class TranslateManager; | 24 class TranslateManager; |
26 | 25 |
27 namespace content { | 26 namespace infobars { |
28 class WebContents; | 27 class InfoBarManager; |
29 } | 28 } |
30 | 29 |
31 class TranslateInfoBarDelegate : public infobars::InfoBarDelegate { | 30 class TranslateInfoBarDelegate : public infobars::InfoBarDelegate { |
32 public: | 31 public: |
33 // The types of background color animations. | 32 // The types of background color animations. |
34 enum BackgroundAnimationType { | 33 enum BackgroundAnimationType { |
35 NONE, | 34 NONE, |
36 NORMAL_TO_ERROR, | 35 NORMAL_TO_ERROR, |
37 ERROR_TO_NORMAL | 36 ERROR_TO_NORMAL |
38 }; | 37 }; |
39 | 38 |
40 static const size_t kNoIndex; | 39 static const size_t kNoIndex; |
41 | 40 |
42 virtual ~TranslateInfoBarDelegate(); | 41 virtual ~TranslateInfoBarDelegate(); |
43 | 42 |
44 // Factory method to create a translate infobar. |error_type| must be | 43 // Factory method to create a translate infobar. |error_type| must be |
45 // specified iff |step| == TRANSLATION_ERROR. For other translate steps, | 44 // specified iff |step| == TRANSLATION_ERROR. For other translate steps, |
46 // |original_language| and |target_language| must be ASCII language codes | 45 // |original_language| and |target_language| must be ASCII language codes |
47 // (e.g. "en", "fr", etc.) for languages the TranslateManager supports | 46 // (e.g. "en", "fr", etc.) for languages the TranslateManager supports |
48 // translating. The lone exception is when the user initiates translation | 47 // translating. The lone exception is when the user initiates translation |
49 // from the context menu, in which case it's legal to call this with | 48 // from the context menu, in which case it's legal to call this with |
50 // |step| == TRANSLATING and |original_language| == kUnknownLanguageCode. | 49 // |step| == TRANSLATING and |original_language| == kUnknownLanguageCode. |
51 // | 50 // |
52 // If |replace_existing_infobar| is true, the infobar is created and added to | 51 // If |replace_existing_infobar| is true, the infobar is created and added to |
53 // the infobar service for |web_contents|, replacing any other translate | 52 // the infobar manager, replacing any other translate infobar already present |
54 // infobar already present there. Otherwise, the infobar will only be added | 53 // there. Otherwise, the infobar will only be added if there is no other |
55 // if there is no other translate infobar already present. | 54 // translate infobar already present. |
56 static void Create(bool replace_existing_infobar, | 55 static void Create(bool replace_existing_infobar, |
57 const base::WeakPtr<TranslateManager>& translate_manager, | 56 const base::WeakPtr<TranslateManager>& translate_manager, |
58 InfoBarService* infobar_service, | 57 infobars::InfoBarManager* infobar_manager, |
59 bool is_off_the_record, | 58 bool is_off_the_record, |
60 translate::TranslateStep step, | 59 translate::TranslateStep step, |
61 const std::string& original_language, | 60 const std::string& original_language, |
62 const std::string& target_language, | 61 const std::string& target_language, |
63 TranslateErrors::Type error_type, | 62 TranslateErrors::Type error_type, |
64 PrefService* prefs, | 63 PrefService* prefs, |
65 bool triggered_from_menu); | 64 bool triggered_from_menu); |
66 | 65 |
67 // Returns the number of languages supported. | 66 // Returns the number of languages supported. |
68 size_t num_languages() const { return ui_delegate_.GetNumberOfLanguages(); } | 67 size_t num_languages() const { return ui_delegate_.GetNumberOfLanguages(); } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 bool ShouldShowMessageInfoBarButton(); | 148 bool ShouldShowMessageInfoBarButton(); |
150 | 149 |
151 // Called by the before translate infobar to figure-out if it should show | 150 // 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 | 151 // an extra shortcut to let the user black-list/white-list that language |
153 // (based on how many times the user accepted/declined translation). | 152 // (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 | 153 // The shortcut itself is platform specific, it can be a button or a new bar |
155 // for example. | 154 // for example. |
156 bool ShouldShowNeverTranslateShortcut(); | 155 bool ShouldShowNeverTranslateShortcut(); |
157 bool ShouldShowAlwaysTranslateShortcut(); | 156 bool ShouldShowAlwaysTranslateShortcut(); |
158 | 157 |
159 // Returns the WebContents associated with the TranslateInfoBarDelegate. | |
160 content::WebContents* GetWebContents(); | |
161 | |
162 // Adds the strings that should be displayed in the after translate infobar to | 158 // Adds the strings that should be displayed in the after translate infobar to |
163 // |strings|. If |autodetermined_source_language| is false, the text in that | 159 // |strings|. If |autodetermined_source_language| is false, the text in that |
164 // infobar is: | 160 // infobar is: |
165 // "The page has been translated from <lang1> to <lang2>." | 161 // "The page has been translated from <lang1> to <lang2>." |
166 // Otherwise: | 162 // Otherwise: |
167 // "The page has been translated to <lang1>." | 163 // "The page has been translated to <lang1>." |
168 // Because <lang1>, or <lang1> and <lang2> are displayed in menu buttons, the | 164 // Because <lang1>, or <lang1> and <lang2> are displayed in menu buttons, the |
169 // text is split in 2 or 3 chunks. |swap_languages| is set to true if | 165 // text is split in 2 or 3 chunks. |swap_languages| is set to true if |
170 // |autodetermined_source_language| is false, and <lang1> and <lang2> | 166 // |autodetermined_source_language| is false, and <lang1> and <lang2> |
171 // should be inverted (some languages express the sentense as "The page has | 167 // should be inverted (some languages express the sentense as "The page has |
172 // been translate to <lang2> from <lang1>."). It is ignored if | 168 // been translate to <lang2> from <lang1>."). It is ignored if |
173 // |autodetermined_source_language| is true. | 169 // |autodetermined_source_language| is true. |
174 static void GetAfterTranslateStrings(std::vector<base::string16>* strings, | 170 static void GetAfterTranslateStrings(std::vector<base::string16>* strings, |
175 bool* swap_languages, | 171 bool* swap_languages, |
176 bool autodetermined_source_language); | 172 bool autodetermined_source_language); |
177 | 173 |
| 174 // Gets the TranslateClient associated with this object. |
| 175 // May return NULL if the client has been destroyed. |
| 176 TranslateClient* GetTranslateClient(); |
| 177 |
178 protected: | 178 protected: |
179 TranslateInfoBarDelegate( | 179 TranslateInfoBarDelegate( |
180 const base::WeakPtr<TranslateManager>& translate_manager, | 180 const base::WeakPtr<TranslateManager>& translate_manager, |
181 bool is_off_the_record, | 181 bool is_off_the_record, |
182 translate::TranslateStep step, | 182 translate::TranslateStep step, |
183 TranslateInfoBarDelegate* old_delegate, | 183 TranslateInfoBarDelegate* old_delegate, |
184 const std::string& original_language, | 184 const std::string& original_language, |
185 const std::string& target_language, | 185 const std::string& target_language, |
186 TranslateErrors::Type error_type, | 186 TranslateErrors::Type error_type, |
187 PrefService* prefs, | 187 PrefService* prefs, |
188 bool triggered_from_menu); | 188 bool triggered_from_menu); |
189 | 189 |
190 private: | 190 private: |
191 friend class TranslationInfoBarTest; | 191 friend class TranslationInfoBarTest; |
192 typedef std::pair<std::string, base::string16> LanguageNamePair; | 192 typedef std::pair<std::string, base::string16> LanguageNamePair; |
193 | 193 |
194 // Returns a translate infobar that owns |delegate|. | 194 // Returns a translate infobar that owns |delegate|. |
195 static scoped_ptr<infobars::InfoBar> CreateInfoBar( | 195 static scoped_ptr<infobars::InfoBar> CreateInfoBar( |
196 scoped_ptr<TranslateInfoBarDelegate> delegate); | 196 scoped_ptr<TranslateInfoBarDelegate> delegate); |
197 | 197 |
198 // May return NULL if the client has been destroyed. | |
199 TranslateClient* GetTranslateClient(); | |
200 | |
201 // InfoBarDelegate: | 198 // InfoBarDelegate: |
202 virtual void InfoBarDismissed() OVERRIDE; | 199 virtual void InfoBarDismissed() OVERRIDE; |
203 virtual int GetIconID() const OVERRIDE; | 200 virtual int GetIconID() const OVERRIDE; |
204 virtual infobars::InfoBarDelegate::Type GetInfoBarType() const OVERRIDE; | 201 virtual infobars::InfoBarDelegate::Type GetInfoBarType() const OVERRIDE; |
205 virtual bool ShouldExpire(const NavigationDetails& details) const OVERRIDE; | 202 virtual bool ShouldExpire(const NavigationDetails& details) const OVERRIDE; |
206 virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() OVERRIDE; | 203 virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() OVERRIDE; |
207 | 204 |
208 bool is_off_the_record_; | 205 bool is_off_the_record_; |
209 translate::TranslateStep step_; | 206 translate::TranslateStep step_; |
210 | 207 |
(...skipping 10 matching lines...) Expand all Loading... |
221 // The translation related preferences. | 218 // The translation related preferences. |
222 scoped_ptr<TranslatePrefs> prefs_; | 219 scoped_ptr<TranslatePrefs> prefs_; |
223 | 220 |
224 // Whether the translation was triggered via a menu click vs automatically | 221 // Whether the translation was triggered via a menu click vs automatically |
225 // (due to language detection, preferences...) | 222 // (due to language detection, preferences...) |
226 bool triggered_from_menu_; | 223 bool triggered_from_menu_; |
227 DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate); | 224 DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate); |
228 }; | 225 }; |
229 | 226 |
230 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_ | 227 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_ |
OLD | NEW |