| 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 <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 void ClearTranslateScript() { translate_script_.clear(); } | 79 void ClearTranslateScript() { translate_script_.clear(); } |
| 80 | 80 |
| 81 // content::NotificationObserver implementation: | 81 // content::NotificationObserver implementation: |
| 82 virtual void Observe(int type, | 82 virtual void Observe(int type, |
| 83 const content::NotificationSource& source, | 83 const content::NotificationSource& source, |
| 84 const content::NotificationDetails& details) OVERRIDE; | 84 const content::NotificationDetails& details) OVERRIDE; |
| 85 | 85 |
| 86 // net::URLFetcherDelegate implementation: | 86 // net::URLFetcherDelegate implementation: |
| 87 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 87 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 88 | 88 |
| 89 // Used by unit-tests to override the default delay after which the translate | 89 // Used by unit-tests to override some defaults: |
| 90 // script is fetched again from the translation server. | 90 // Delay after which the translate script is fetched again from the |
| 91 // translation server. |
| 91 void set_translate_script_expiration_delay(int delay_ms) { | 92 void set_translate_script_expiration_delay(int delay_ms) { |
| 92 translate_script_expiration_delay_ = | 93 translate_script_expiration_delay_ = |
| 93 base::TimeDelta::FromMilliseconds(delay_ms); | 94 base::TimeDelta::FromMilliseconds(delay_ms); |
| 94 } | 95 } |
| 95 | 96 |
| 97 // Number of attempts before waiting for a page to be fully reloaded. |
| 98 void set_translate_max_reload_attemps(int attempts) { |
| 99 max_reload_check_attempts_ = attempts; |
| 100 } |
| 101 |
| 96 // Returns true if the URL can be translated. | 102 // Returns true if the URL can be translated. |
| 97 static bool IsTranslatableURL(const GURL& url); | 103 static bool IsTranslatableURL(const GURL& url); |
| 98 | 104 |
| 99 // Fills |languages| with the list of languages that the translate server can | 105 // Fills |languages| with the list of languages that the translate server can |
| 100 // translate to and from. | 106 // translate to and from. |
| 101 static void GetSupportedLanguages(std::vector<std::string>* languages); | 107 static void GetSupportedLanguages(std::vector<std::string>* languages); |
| 102 | 108 |
| 103 // Returns the language code that can be used with the Translate method for a | 109 // Returns the language code that can be used with the Translate method for a |
| 104 // specified |chrome_locale|. | 110 // specified |chrome_locale|. |
| 105 static std::string GetLanguageCode(const std::string& chrome_locale); | 111 static std::string GetLanguageCode(const std::string& chrome_locale); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 135 // in case we failed to get them from the server, or didn't get them just yet. | 141 // in case we failed to get them from the server, or didn't get them just yet. |
| 136 static void InitSupportedLanguages(); | 142 static void InitSupportedLanguages(); |
| 137 | 143 |
| 138 // Starts the translation process on |tab| containing the page in the | 144 // Starts the translation process on |tab| containing the page in the |
| 139 // |page_lang| language. | 145 // |page_lang| language. |
| 140 void InitiateTranslation(content::WebContents* web_contents, | 146 void InitiateTranslation(content::WebContents* web_contents, |
| 141 const std::string& page_lang); | 147 const std::string& page_lang); |
| 142 | 148 |
| 143 // If the tab identified by |process_id| and |render_id| has been closed, this | 149 // If the tab identified by |process_id| and |render_id| has been closed, this |
| 144 // does nothing, otherwise it calls InitiateTranslation. | 150 // does nothing, otherwise it calls InitiateTranslation. |
| 145 void InitiateTranslationPosted(int process_id, | 151 void InitiateTranslationPosted(int process_id, int render_id, |
| 146 int render_id, | 152 const std::string& page_lang, int attempt); |
| 147 const std::string& page_lang); | |
| 148 | 153 |
| 149 // Sends a translation request to the RenderView of |web_contents|. | 154 // Sends a translation request to the RenderView of |web_contents|. |
| 150 void DoTranslatePage(content::WebContents* web_contents, | 155 void DoTranslatePage(content::WebContents* web_contents, |
| 151 const std::string& translate_script, | 156 const std::string& translate_script, |
| 152 const std::string& source_lang, | 157 const std::string& source_lang, |
| 153 const std::string& target_lang); | 158 const std::string& target_lang); |
| 154 | 159 |
| 155 // Shows the after translate or error infobar depending on the details. | 160 // Shows the after translate or error infobar depending on the details. |
| 156 void PageTranslated(content::WebContents* web_contents, | 161 void PageTranslated(content::WebContents* web_contents, |
| 157 PageTranslatedDetails* details); | 162 PageTranslatedDetails* details); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 200 |
| 196 base::WeakPtrFactory<TranslateManager> weak_method_factory_; | 201 base::WeakPtrFactory<TranslateManager> weak_method_factory_; |
| 197 | 202 |
| 198 // The JS injected in the page to do the translation. | 203 // The JS injected in the page to do the translation. |
| 199 std::string translate_script_; | 204 std::string translate_script_; |
| 200 | 205 |
| 201 // Delay after which the translate script is fetched again | 206 // Delay after which the translate script is fetched again |
| 202 // from the translate server. | 207 // from the translate server. |
| 203 base::TimeDelta translate_script_expiration_delay_; | 208 base::TimeDelta translate_script_expiration_delay_; |
| 204 | 209 |
| 210 // Max number of attempts before checking if a page has been reloaded. |
| 211 int max_reload_check_attempts_; |
| 212 |
| 205 // Set when the translate JS is currently being retrieved. NULL otherwise. | 213 // Set when the translate JS is currently being retrieved. NULL otherwise. |
| 206 scoped_ptr<net::URLFetcher> translate_script_request_pending_; | 214 scoped_ptr<net::URLFetcher> translate_script_request_pending_; |
| 207 | 215 |
| 208 // Set when the list of languages is currently being retrieved. | 216 // Set when the list of languages is currently being retrieved. |
| 209 // NULL otherwise. | 217 // NULL otherwise. |
| 210 scoped_ptr<net::URLFetcher> language_list_request_pending_; | 218 scoped_ptr<net::URLFetcher> language_list_request_pending_; |
| 211 | 219 |
| 212 // The list of pending translate requests. Translate requests are queued when | 220 // The list of pending translate requests. Translate requests are queued when |
| 213 // the translate script is not ready and has to be fetched from the translate | 221 // the translate script is not ready and has to be fetched from the translate |
| 214 // server. | 222 // server. |
| 215 std::vector<PendingRequest> pending_requests_; | 223 std::vector<PendingRequest> pending_requests_; |
| 216 | 224 |
| 217 // The languages supported by the translation server. | 225 // The languages supported by the translation server. |
| 218 static base::LazyInstance<std::set<std::string> > supported_languages_; | 226 static base::LazyInstance<std::set<std::string> > supported_languages_; |
| 219 | 227 |
| 220 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 228 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 221 }; | 229 }; |
| 222 | 230 |
| 223 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 231 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| OLD | NEW |