1 // Copyright 2014 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 COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_DELEGATE_H_ 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_DELEGATE_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/logging.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/strings/string16.h" 15 #include "components/translate/core/common/translate_errors.h" 16 17 class LanguageState; 18 class TranslateClient; 19 class TranslateDriver; 20 class TranslateManager; 21 class TranslatePrefs; 22 23 // The TranslateUIDelegate is a generic delegate for UI which offers Translate 24 // feature to the user. 25 class TranslateUIDelegate { 26 public: 27 enum { NO_INDEX = -1, }; 28 29 TranslateUIDelegate(const base::WeakPtr<TranslateManager>& translate_manager, 30 const std::string& original_language, 31 const std::string& target_language); 32 virtual ~TranslateUIDelegate(); 33 34 // Handles when an error message is shown. 35 void OnErrorShown(TranslateErrors::Type error_type); 36 37 // Returns the LanguageState associated with this object. 38 const LanguageState& GetLanguageState(); 39 40 // Returns the number of languages supported. 41 size_t GetNumberOfLanguages() const; 42 43 // Returns the original language index. 44 size_t GetOriginalLanguageIndex() const; 45 46 // Updates the original language index. 47 void UpdateOriginalLanguageIndex(size_t language_index); 48 49 // Returns the target language index. 50 size_t GetTargetLanguageIndex() const; 51 52 // Updates the target language index. 53 void UpdateTargetLanguageIndex(size_t language_index); 54 55 // Returns the ISO code for the language at |index|. 56 std::string GetLanguageCodeAt(size_t index) const; 57 58 // Returns the displayable name for the language at |index|. 59 base::string16 GetLanguageNameAt(size_t index) const; 60 61 // The original language for Translate. 62 std::string GetOriginalLanguageCode() const; 63 64 // The target language for Translate. 65 std::string GetTargetLanguageCode() const; 66 67 // Starts translating the current page. 68 void Translate(); 69 70 // Reverts translation. 71 void RevertTranslation(); 72 73 // Processes when the user declines translation. 74 void TranslationDeclined(bool explicitly_closed); 75 76 // Returns true if the current language is blocked. 77 bool IsLanguageBlocked(); 78 79 // Sets the value if the current language is blocked. 80 void SetLanguageBlocked(bool value); 81 82 // Returns true if the current webpage is blacklisted. 83 bool IsSiteBlacklisted(); 84 85 // Sets the value if the current webpage is blacklisted. 86 void SetSiteBlacklist(bool value); 87 88 // Returns true if the webpage in the current original language should be 89 // translated into the current target language automatically. 90 bool ShouldAlwaysTranslate(); 91 92 // Sets the value if the webpage in the current original language should be 93 // translated into the current target language automatically. 94 void SetAlwaysTranslate(bool value); 95 96 private: 97 // Gets the host of the page being translated, or an empty string if no URL is 98 // associated with the current page. 99 std::string GetPageHost(); 100 101 TranslateDriver* translate_driver_; 102 base::WeakPtr<TranslateManager> translate_manager_; 103 104 typedef std::pair<std::string, base::string16> LanguageNamePair; 105 106 // The list supported languages for translation. 107 // The pair first string is the language ISO code (ex: en, fr...), the second 108 // string is the displayable name on the current locale. 109 // The languages are sorted alphabetically based on the displayable name. 110 std::vector<LanguageNamePair> languages_; 111 112 // The index for language the page is originally in. 113 size_t original_language_index_; 114 115 // The index for language the page is originally in that was originally 116 // reported (original_language_index_ changes if the user selects a new 117 // original language, but this one does not). This is necessary to report 118 // language detection errors with the right original language even if the user 119 // changed the original language. 120 size_t initial_original_language_index_; 121 122 // The index for language the page should be translated to. 123 size_t target_language_index_; 124 125 // The translation related preferences. 126 scoped_ptr<TranslatePrefs> prefs_; 127 128 DISALLOW_COPY_AND_ASSIGN(TranslateUIDelegate); 129 }; 130 131 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_DELEGATE_H_ 132