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