| 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.h" | 16 #include "base/time.h" |
| 17 #include "chrome/common/translate/translate_errors.h" | 17 #include "chrome/common/translate/translate_errors.h" |
| 18 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 20 #include "net/url_request/url_fetcher_delegate.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; | 30 struct TranslateEventDetails; |
| 32 class TranslateInfoBarDelegate; | 31 class TranslateInfoBarDelegate; |
| 33 class TranslateLanguageList; | 32 class TranslateLanguageList; |
| 33 class TranslateScript; |
| 34 | 34 |
| 35 namespace content { | 35 namespace content { |
| 36 class WebContents; | 36 class WebContents; |
| 37 } | 37 } |
| 38 | 38 |
| 39 namespace net { | 39 namespace net { |
| 40 class URLFetcher; | 40 class URLFetcher; |
| 41 } | 41 } |
| 42 | 42 |
| 43 // The TranslateManager class is responsible for showing an info-bar when a page | 43 // The TranslateManager class is responsible for showing an info-bar when a page |
| 44 // in a language different than the user language is loaded. It triggers the | 44 // in a language different than the user language is loaded. It triggers the |
| 45 // page translation the user requests. | 45 // page translation the user requests. |
| 46 // It is a singleton. | 46 // It is a singleton. |
| 47 | 47 |
| 48 class TranslateManager : public content::NotificationObserver, | 48 class TranslateManager : public content::NotificationObserver { |
| 49 public net::URLFetcherDelegate { | |
| 50 public: | 49 public: |
| 51 // Returns the singleton instance. | 50 // Returns the singleton instance. |
| 52 static TranslateManager* GetInstance(); | 51 static TranslateManager* GetInstance(); |
| 53 | 52 |
| 54 virtual ~TranslateManager(); | 53 virtual ~TranslateManager(); |
| 55 | 54 |
| 56 // Returns true if the URL can be translated. | 55 // Returns true if the URL can be translated. |
| 57 static bool IsTranslatableURL(const GURL& url); | 56 static bool IsTranslatableURL(const GURL& url); |
| 58 | 57 |
| 59 // Fills |languages| with the list of languages that the translate server can | 58 // Fills |languages| with the list of languages that the translate server can |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // Reverts the contents of the page in |web_contents| to its original | 96 // Reverts the contents of the page in |web_contents| to its original |
| 98 // language. | 97 // language. |
| 99 void RevertTranslation(content::WebContents* web_contents); | 98 void RevertTranslation(content::WebContents* web_contents); |
| 100 | 99 |
| 101 // Reports to the Google translate server that a page language was incorrectly | 100 // Reports to the Google translate server that a page language was incorrectly |
| 102 // detected. This call is initiated by the user selecting the "report" menu | 101 // detected. This call is initiated by the user selecting the "report" menu |
| 103 // under options in the translate infobar. | 102 // under options in the translate infobar. |
| 104 void ReportLanguageDetectionError(content::WebContents* web_contents); | 103 void ReportLanguageDetectionError(content::WebContents* web_contents); |
| 105 | 104 |
| 106 // Clears the translate script, so it will be fetched next time we translate. | 105 // Clears the translate script, so it will be fetched next time we translate. |
| 107 void ClearTranslateScript() { translate_script_.clear(); } | 106 void ClearTranslateScript(); |
| 108 | 107 |
| 109 // content::NotificationObserver implementation: | 108 // content::NotificationObserver implementation: |
| 110 virtual void Observe(int type, | 109 virtual void Observe(int type, |
| 111 const content::NotificationSource& source, | 110 const content::NotificationSource& source, |
| 112 const content::NotificationDetails& details) OVERRIDE; | 111 const content::NotificationDetails& details) OVERRIDE; |
| 113 | 112 |
| 114 // net::URLFetcherDelegate implementation: | |
| 115 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 116 | |
| 117 // Used by unit-tests to override some defaults: | 113 // Used by unit-tests to override some defaults: |
| 118 // Delay after which the translate script is fetched again from the | 114 // Delay after which the translate script is fetched again from the |
| 119 // translation server. | 115 // translation server. |
| 120 void set_translate_script_expiration_delay(int delay_ms) { | 116 void SetTranslateScriptExpirationDelay(int delay_ms); |
| 121 translate_script_expiration_delay_ = | |
| 122 base::TimeDelta::FromMilliseconds(delay_ms); | |
| 123 } | |
| 124 | 117 |
| 125 // Number of attempts before waiting for a page to be fully reloaded. | 118 // Number of attempts before waiting for a page to be fully reloaded. |
| 126 void set_translate_max_reload_attemps(int attempts) { | 119 void set_translate_max_reload_attemps(int attempts) { |
| 127 max_reload_check_attempts_ = attempts; | 120 max_reload_check_attempts_ = attempts; |
| 128 } | 121 } |
| 129 | 122 |
| 130 // The observer class for TranslateManager. | 123 // The observer class for TranslateManager. |
| 131 class Observer { | 124 class Observer { |
| 132 public: | 125 public: |
| 133 virtual void OnLanguageDetection( | 126 virtual void OnLanguageDetection( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // Sends a translation request to the RenderView of |web_contents|. | 168 // Sends a translation request to the RenderView of |web_contents|. |
| 176 void DoTranslatePage(content::WebContents* web_contents, | 169 void DoTranslatePage(content::WebContents* web_contents, |
| 177 const std::string& translate_script, | 170 const std::string& translate_script, |
| 178 const std::string& source_lang, | 171 const std::string& source_lang, |
| 179 const std::string& target_lang); | 172 const std::string& target_lang); |
| 180 | 173 |
| 181 // Shows the after translate or error infobar depending on the details. | 174 // Shows the after translate or error infobar depending on the details. |
| 182 void PageTranslated(content::WebContents* web_contents, | 175 void PageTranslated(content::WebContents* web_contents, |
| 183 PageTranslatedDetails* details); | 176 PageTranslatedDetails* details); |
| 184 | 177 |
| 185 // Fetches the JS translate script (the script that is injected in the page | 178 void OnTranslateScriptFetchComplete(bool success, const std::string& data); |
| 186 // to translate it). | 179 |
| 187 void RequestTranslateScript(); | 180 // Processes any pending requests. |
| 181 void ProcessPendingRequests(bool error); |
| 188 | 182 |
| 189 // Notifies to the observers when a language is detected. | 183 // Notifies to the observers when a language is detected. |
| 190 void NotifyLanguageDetection(const LanguageDetectionDetails& details); | 184 void NotifyLanguageDetection(const LanguageDetectionDetails& details); |
| 191 | 185 |
| 192 // Notifies to the observers when translate failed. | 186 // Notifies to the observers when translate failed. |
| 193 void NotifyTranslateError(const TranslateErrorDetails& details); | 187 void NotifyTranslateError(const TranslateErrorDetails& details); |
| 194 | 188 |
| 195 // Returns the language to translate to. The language returned is the | 189 // Returns the language to translate to. The language returned is the |
| 196 // first language found in the following list that is supported by the | 190 // first language found in the following list that is supported by the |
| 197 // translation service: | 191 // translation service: |
| 198 // the UI language | 192 // the UI language |
| 199 // the accept-language list | 193 // the accept-language list |
| 200 // If no language is found then an empty string is returned. | 194 // If no language is found then an empty string is returned. |
| 201 static std::string GetTargetLanguage(PrefService* prefs); | 195 static std::string GetTargetLanguage(PrefService* prefs); |
| 202 | 196 |
| 203 // Returns the different parameters used to decide whether extra shortcuts | 197 // Returns the different parameters used to decide whether extra shortcuts |
| 204 // are needed. | 198 // are needed. |
| 205 static ShortcutConfiguration ShortcutConfig(); | 199 static ShortcutConfiguration ShortcutConfig(); |
| 206 | 200 |
| 207 content::NotificationRegistrar notification_registrar_; | 201 content::NotificationRegistrar notification_registrar_; |
| 208 | 202 |
| 209 base::WeakPtrFactory<TranslateManager> weak_method_factory_; | 203 base::WeakPtrFactory<TranslateManager> weak_method_factory_; |
| 210 | 204 |
| 211 // The JS injected in the page to do the translation. | |
| 212 std::string translate_script_; | |
| 213 | |
| 214 // Delay after which the translate script is fetched again | |
| 215 // from the translate server. | |
| 216 base::TimeDelta translate_script_expiration_delay_; | |
| 217 | |
| 218 // Max number of attempts before checking if a page has been reloaded. | 205 // Max number of attempts before checking if a page has been reloaded. |
| 219 int max_reload_check_attempts_; | 206 int max_reload_check_attempts_; |
| 220 | 207 |
| 221 // Set when the translate JS is currently being retrieved. NULL otherwise. | |
| 222 scoped_ptr<net::URLFetcher> translate_script_request_pending_; | |
| 223 | |
| 224 // The list of pending translate requests. Translate requests are queued when | 208 // The list of pending translate requests. Translate requests are queued when |
| 225 // the translate script is not ready and has to be fetched from the translate | 209 // the translate script is not ready and has to be fetched from the translate |
| 226 // server. | 210 // server. |
| 227 std::vector<PendingRequest> pending_requests_; | 211 std::vector<PendingRequest> pending_requests_; |
| 228 | 212 |
| 229 // List of registered observers. | 213 // List of registered observers. |
| 230 ObserverList<Observer> observer_list_; | 214 ObserverList<Observer> observer_list_; |
| 231 | 215 |
| 232 // An instance of TranslateLanguageList which manages supported language list. | 216 // An instance of TranslateLanguageList which manages supported language list. |
| 233 scoped_ptr<TranslateLanguageList> language_list_; | 217 scoped_ptr<TranslateLanguageList> language_list_; |
| 234 | 218 |
| 219 // An instance of TranslateScript which manages JavaScript source for |
| 220 // Translate. |
| 221 scoped_ptr<TranslateScript> script_; |
| 222 |
| 235 // An instance of TranslateAcceptLanguages which manages Accept languages of | 223 // An instance of TranslateAcceptLanguages which manages Accept languages of |
| 236 // each profiles. | 224 // each profiles. |
| 237 scoped_ptr<TranslateAcceptLanguages> accept_languages_; | 225 scoped_ptr<TranslateAcceptLanguages> accept_languages_; |
| 238 | 226 |
| 239 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 227 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 240 }; | 228 }; |
| 241 | 229 |
| 242 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 230 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| OLD | NEW |