| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.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 "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/time/time.h" | |
| 17 #include "chrome/browser/ui/translate/translate_bubble_model.h" | 16 #include "chrome/browser/ui/translate/translate_bubble_model.h" |
| 18 #include "components/translate/core/common/translate_errors.h" | 17 #include "components/translate/core/common/translate_errors.h" |
| 19 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 20 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 21 | 20 |
| 22 template <typename T> struct DefaultSingletonTraits; | 21 template <typename T> struct DefaultSingletonTraits; |
| 23 class GURL; | 22 class GURL; |
| 24 struct LanguageDetectionDetails; | 23 struct LanguageDetectionDetails; |
| 25 struct PageTranslatedDetails; | 24 struct PageTranslatedDetails; |
| 26 class PrefService; | 25 class PrefService; |
| 27 class Profile; | 26 class Profile; |
| 28 struct ShortcutConfiguration; | 27 struct ShortcutConfiguration; |
| 29 class TranslateAcceptLanguages; | 28 class TranslateAcceptLanguages; |
| 30 struct TranslateErrorDetails; | 29 struct TranslateErrorDetails; |
| 31 struct TranslateEventDetails; | |
| 32 class TranslateInfoBarDelegate; | 30 class TranslateInfoBarDelegate; |
| 33 class TranslateLanguageList; | |
| 34 class TranslateScript; | 31 class TranslateScript; |
| 35 | 32 |
| 36 namespace content { | 33 namespace content { |
| 37 class WebContents; | 34 class WebContents; |
| 38 } | 35 } |
| 39 | 36 |
| 40 namespace net { | 37 namespace net { |
| 41 class URLFetcher; | 38 class URLFetcher; |
| 42 } | 39 } |
| 43 | 40 |
| 44 // The TranslateManager class is responsible for showing an info-bar when a page | 41 // The TranslateManager class is responsible for showing an info-bar when a page |
| 45 // in a language different than the user language is loaded. It triggers the | 42 // in a language different than the user language is loaded. It triggers the |
| 46 // page translation the user requests. | 43 // page translation the user requests. |
| 47 // It is a singleton. | 44 // It is a singleton. |
| 48 | 45 |
| 49 class TranslateManager : public content::NotificationObserver { | 46 class TranslateManager : public content::NotificationObserver { |
| 50 public: | 47 public: |
| 51 // Returns the singleton instance. | 48 // Returns the singleton instance. |
| 52 static TranslateManager* GetInstance(); | 49 static TranslateManager* GetInstance(); |
| 53 | 50 |
| 54 virtual ~TranslateManager(); | 51 virtual ~TranslateManager(); |
| 55 | 52 |
| 56 // Returns true if the URL can be translated. | 53 // Returns true if the URL can be translated. |
| 57 static bool IsTranslatableURL(const GURL& url); | 54 static bool IsTranslatableURL(const GURL& url); |
| 58 | 55 |
| 59 // Fills |languages| with the list of languages that the translate server can | |
| 60 // translate to and from. | |
| 61 static void GetSupportedLanguages(std::vector<std::string>* languages); | |
| 62 | |
| 63 // Returns the last-updated time when Chrome receives a language list from a | |
| 64 // Translate server. Returns null time if Chrome hasn't received any lists. | |
| 65 static base::Time GetSupportedLanguagesLastUpdated(); | |
| 66 | |
| 67 // Returns the language code that can be used with the Translate method for a | |
| 68 // specified |chrome_locale|. | |
| 69 static std::string GetLanguageCode(const std::string& chrome_locale); | |
| 70 | |
| 71 // Returns true if |language| is supported by the translation server. | |
| 72 static bool IsSupportedLanguage(const std::string& language); | |
| 73 | |
| 74 // Returns true if |language| is supported by the translation server as a | |
| 75 // alpha language. | |
| 76 static bool IsAlphaLanguage(const std::string& language); | |
| 77 | |
| 78 // Returns true if |language| is an Accept language for the user profile. | 56 // Returns true if |language| is an Accept language for the user profile. |
| 79 static bool IsAcceptLanguage(Profile* profile, const std::string& language); | 57 static bool IsAcceptLanguage(Profile* profile, const std::string& language); |
| 80 | 58 |
| 81 // Returns the language to translate to. The language returned is the | 59 // Returns the language to translate to. The language returned is the |
| 82 // first language found in the following list that is supported by the | 60 // first language found in the following list that is supported by the |
| 83 // translation service: | 61 // translation service: |
| 84 // the UI language | 62 // the UI language |
| 85 // the accept-language list | 63 // the accept-language list |
| 86 // If no language is found then an empty string is returned. | 64 // If no language is found then an empty string is returned. |
| 87 static std::string GetTargetLanguage(PrefService* prefs); | 65 static std::string GetTargetLanguage(PrefService* prefs); |
| 88 | 66 |
| 89 // Returns the language to automatically translate to. |original_language| is | 67 // Returns the language to automatically translate to. |original_language| is |
| 90 // the webpage's original language. | 68 // the webpage's original language. |
| 91 static std::string GetAutoTargetLanguage(const std::string& original_language, | 69 static std::string GetAutoTargetLanguage(const std::string& original_language, |
| 92 PrefService* prefs); | 70 PrefService* prefs); |
| 93 | 71 |
| 94 // Returns true if the new translate bubble is enabled. | 72 // Returns true if the new translate bubble is enabled. |
| 95 static bool IsTranslateBubbleEnabled(); | 73 static bool IsTranslateBubbleEnabled(); |
| 96 | 74 |
| 97 // Sets whether of not the infobar UI is used. This method is intented to be | 75 // Sets whether of not the infobar UI is used. This method is intented to be |
| 98 // used only for tests. | 76 // used only for tests. |
| 99 static void SetUseInfobar(bool value); | 77 static void SetUseInfobar(bool value); |
| 100 | 78 |
| 101 // Let the caller decide if and when we should fetch the language list from | |
| 102 // the translate server. This is a NOOP if switches::kDisableTranslate is set | |
| 103 // or if prefs::kEnableTranslate is set to false. | |
| 104 void FetchLanguageListFromTranslateServer(PrefService* prefs); | |
| 105 | |
| 106 // Allows caller to cleanup pending URLFetcher objects to make sure they | 79 // Allows caller to cleanup pending URLFetcher objects to make sure they |
| 107 // get released in the appropriate thread... Mainly for tests. | 80 // get released in the appropriate thread... Mainly for tests. |
| 108 void CleanupPendingUlrFetcher(); | 81 void CleanupPendingUlrFetcher(); |
| 109 | 82 |
| 110 // Translates the page contents from |source_lang| to |target_lang|. | 83 // Translates the page contents from |source_lang| to |target_lang|. |
| 111 // The actual translation might be performed asynchronously if the translate | 84 // The actual translation might be performed asynchronously if the translate |
| 112 // script is not yet available. | 85 // script is not yet available. |
| 113 void TranslatePage(content::WebContents* web_contents, | 86 void TranslatePage(content::WebContents* web_contents, |
| 114 const std::string& source_lang, | 87 const std::string& source_lang, |
| 115 const std::string& target_lang); | 88 const std::string& target_lang); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 141 max_reload_check_attempts_ = attempts; | 114 max_reload_check_attempts_ = attempts; |
| 142 } | 115 } |
| 143 | 116 |
| 144 // The observer class for TranslateManager. | 117 // The observer class for TranslateManager. |
| 145 class Observer { | 118 class Observer { |
| 146 public: | 119 public: |
| 147 virtual void OnLanguageDetection( | 120 virtual void OnLanguageDetection( |
| 148 const LanguageDetectionDetails& details) = 0; | 121 const LanguageDetectionDetails& details) = 0; |
| 149 virtual void OnTranslateError( | 122 virtual void OnTranslateError( |
| 150 const TranslateErrorDetails& details) = 0; | 123 const TranslateErrorDetails& details) = 0; |
| 151 virtual void OnTranslateEvent( | |
| 152 const TranslateEventDetails& details) = 0; | |
| 153 }; | 124 }; |
| 154 | 125 |
| 155 // Adds/removes observer. | 126 // Adds/removes observer. |
| 156 void AddObserver(Observer* obs); | 127 void AddObserver(Observer* obs); |
| 157 void RemoveObserver(Observer* obs); | 128 void RemoveObserver(Observer* obs); |
| 158 | 129 |
| 159 // Notifies to the observers when translate event happens. | |
| 160 void NotifyTranslateEvent(const TranslateEventDetails& details); | |
| 161 | |
| 162 protected: | 130 protected: |
| 163 TranslateManager(); | 131 TranslateManager(); |
| 164 | 132 |
| 165 private: | 133 private: |
| 166 friend struct DefaultSingletonTraits<TranslateManager>; | 134 friend struct DefaultSingletonTraits<TranslateManager>; |
| 167 | 135 |
| 168 // Structure that describes a translate request. | 136 // Structure that describes a translate request. |
| 169 // Translation may be deferred while the translate script is being retrieved | 137 // Translation may be deferred while the translate script is being retrieved |
| 170 // from the translate server. | 138 // from the translate server. |
| 171 struct PendingRequest { | 139 struct PendingRequest { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 int max_reload_check_attempts_; | 191 int max_reload_check_attempts_; |
| 224 | 192 |
| 225 // The list of pending translate requests. Translate requests are queued when | 193 // The list of pending translate requests. Translate requests are queued when |
| 226 // the translate script is not ready and has to be fetched from the translate | 194 // the translate script is not ready and has to be fetched from the translate |
| 227 // server. | 195 // server. |
| 228 std::vector<PendingRequest> pending_requests_; | 196 std::vector<PendingRequest> pending_requests_; |
| 229 | 197 |
| 230 // List of registered observers. | 198 // List of registered observers. |
| 231 ObserverList<Observer> observer_list_; | 199 ObserverList<Observer> observer_list_; |
| 232 | 200 |
| 233 // An instance of TranslateLanguageList which manages supported language list. | |
| 234 scoped_ptr<TranslateLanguageList> language_list_; | |
| 235 | |
| 236 // An instance of TranslateScript which manages JavaScript source for | 201 // An instance of TranslateScript which manages JavaScript source for |
| 237 // Translate. | 202 // Translate. |
| 238 scoped_ptr<TranslateScript> script_; | 203 scoped_ptr<TranslateScript> script_; |
| 239 | 204 |
| 240 // An instance of TranslateAcceptLanguages which manages Accept languages of | 205 // An instance of TranslateAcceptLanguages which manages Accept languages of |
| 241 // each profiles. | 206 // each profiles. |
| 242 scoped_ptr<TranslateAcceptLanguages> accept_languages_; | 207 scoped_ptr<TranslateAcceptLanguages> accept_languages_; |
| 243 | 208 |
| 244 base::WeakPtrFactory<TranslateManager> weak_method_factory_; | 209 base::WeakPtrFactory<TranslateManager> weak_method_factory_; |
| 245 | 210 |
| 246 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 211 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 247 }; | 212 }; |
| 248 | 213 |
| 249 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 214 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| OLD | NEW |