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