| 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; |
| 23 class TranslateDriver; |
| 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, | |
| 65 bool triggered_from_menu); | 63 bool triggered_from_menu); |
| 66 | 64 |
| 67 // Returns the number of languages supported. | 65 // Returns the number of languages supported. |
| 68 size_t num_languages() const { return ui_delegate_.GetNumberOfLanguages(); } | 66 size_t num_languages() const { return ui_delegate_.GetNumberOfLanguages(); } |
| 69 | 67 |
| 70 // Returns the ISO code for the language at |index|. | 68 // Returns the ISO code for the language at |index|. |
| 71 std::string language_code_at(size_t index) const { | 69 std::string language_code_at(size_t index) const { |
| 72 return ui_delegate_.GetLanguageCodeAt(index); | 70 return ui_delegate_.GetLanguageCodeAt(index); |
| 73 } | 71 } |
| 74 | 72 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 bool ShouldShowMessageInfoBarButton(); | 147 bool ShouldShowMessageInfoBarButton(); |
| 150 | 148 |
| 151 // Called by the before translate infobar to figure-out if it should show | 149 // 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 | 150 // an extra shortcut to let the user black-list/white-list that language |
| 153 // (based on how many times the user accepted/declined translation). | 151 // (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 | 152 // The shortcut itself is platform specific, it can be a button or a new bar |
| 155 // for example. | 153 // for example. |
| 156 bool ShouldShowNeverTranslateShortcut(); | 154 bool ShouldShowNeverTranslateShortcut(); |
| 157 bool ShouldShowAlwaysTranslateShortcut(); | 155 bool ShouldShowAlwaysTranslateShortcut(); |
| 158 | 156 |
| 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 | 157 // 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 | 158 // |strings|. If |autodetermined_source_language| is false, the text in that |
| 164 // infobar is: | 159 // infobar is: |
| 165 // "The page has been translated from <lang1> to <lang2>." | 160 // "The page has been translated from <lang1> to <lang2>." |
| 166 // Otherwise: | 161 // Otherwise: |
| 167 // "The page has been translated to <lang1>." | 162 // "The page has been translated to <lang1>." |
| 168 // Because <lang1>, or <lang1> and <lang2> are displayed in menu buttons, the | 163 // 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 | 164 // 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> | 165 // |autodetermined_source_language| is false, and <lang1> and <lang2> |
| 171 // should be inverted (some languages express the sentense as "The page has | 166 // should be inverted (some languages express the sentense as "The page has |
| 172 // been translate to <lang2> from <lang1>."). It is ignored if | 167 // been translate to <lang2> from <lang1>."). It is ignored if |
| 173 // |autodetermined_source_language| is true. | 168 // |autodetermined_source_language| is true. |
| 174 static void GetAfterTranslateStrings(std::vector<base::string16>* strings, | 169 static void GetAfterTranslateStrings(std::vector<base::string16>* strings, |
| 175 bool* swap_languages, | 170 bool* swap_languages, |
| 176 bool autodetermined_source_language); | 171 bool autodetermined_source_language); |
| 177 | 172 |
| 173 // Gets the TranslateDriver associated with this object. |
| 174 // May return NULL if the driver has been destroyed. |
| 175 TranslateDriver* GetTranslateDriver(); |
| 176 |
| 178 protected: | 177 protected: |
| 179 TranslateInfoBarDelegate( | 178 TranslateInfoBarDelegate( |
| 180 const base::WeakPtr<TranslateManager>& translate_manager, | 179 const base::WeakPtr<TranslateManager>& translate_manager, |
| 181 bool is_off_the_record, | 180 bool is_off_the_record, |
| 182 translate::TranslateStep step, | 181 translate::TranslateStep step, |
| 183 TranslateInfoBarDelegate* old_delegate, | 182 TranslateInfoBarDelegate* old_delegate, |
| 184 const std::string& original_language, | 183 const std::string& original_language, |
| 185 const std::string& target_language, | 184 const std::string& target_language, |
| 186 TranslateErrors::Type error_type, | 185 TranslateErrors::Type error_type, |
| 187 PrefService* prefs, | |
| 188 bool triggered_from_menu); | 186 bool triggered_from_menu); |
| 189 | 187 |
| 190 private: | 188 private: |
| 191 friend class TranslationInfoBarTest; | 189 friend class TranslationInfoBarTest; |
| 192 typedef std::pair<std::string, base::string16> LanguageNamePair; | 190 typedef std::pair<std::string, base::string16> LanguageNamePair; |
| 193 | 191 |
| 194 // Returns a translate infobar that owns |delegate|. | 192 // Returns a translate infobar that owns |delegate|. |
| 195 static scoped_ptr<infobars::InfoBar> CreateInfoBar( | 193 static scoped_ptr<infobars::InfoBar> CreateInfoBar( |
| 196 scoped_ptr<TranslateInfoBarDelegate> delegate); | 194 scoped_ptr<TranslateInfoBarDelegate> delegate); |
| 197 | 195 |
| 196 // Gets the TranslateClient associated with this object. |
| 198 // May return NULL if the client has been destroyed. | 197 // May return NULL if the client has been destroyed. |
| 199 TranslateClient* GetTranslateClient(); | 198 TranslateClient* GetTranslateClient(); |
| 200 | 199 |
| 201 // InfoBarDelegate: | 200 // InfoBarDelegate: |
| 202 virtual void InfoBarDismissed() OVERRIDE; | 201 virtual void InfoBarDismissed() OVERRIDE; |
| 203 virtual int GetIconID() const OVERRIDE; | 202 virtual int GetIconID() const OVERRIDE; |
| 204 virtual infobars::InfoBarDelegate::Type GetInfoBarType() const OVERRIDE; | 203 virtual infobars::InfoBarDelegate::Type GetInfoBarType() const OVERRIDE; |
| 205 virtual bool ShouldExpire(const NavigationDetails& details) const OVERRIDE; | 204 virtual bool ShouldExpire(const NavigationDetails& details) const OVERRIDE; |
| 206 virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() OVERRIDE; | 205 virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() OVERRIDE; |
| 207 | 206 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 221 // The translation related preferences. | 220 // The translation related preferences. |
| 222 scoped_ptr<TranslatePrefs> prefs_; | 221 scoped_ptr<TranslatePrefs> prefs_; |
| 223 | 222 |
| 224 // Whether the translation was triggered via a menu click vs automatically | 223 // Whether the translation was triggered via a menu click vs automatically |
| 225 // (due to language detection, preferences...) | 224 // (due to language detection, preferences...) |
| 226 bool triggered_from_menu_; | 225 bool triggered_from_menu_; |
| 227 DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate); | 226 DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate); |
| 228 }; | 227 }; |
| 229 | 228 |
| 230 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_ | 229 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_ |
| OLD | NEW |