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> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/lazy_instance.h" | |
15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
17 #include "base/prefs/pref_change_registrar.h" | 16 #include "base/prefs/pref_change_registrar.h" |
18 #include "base/time.h" | 17 #include "base/time.h" |
19 #include "chrome/common/translate_errors.h" | 18 #include "chrome/common/translate_errors.h" |
20 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
21 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
22 #include "net/url_request/url_fetcher_delegate.h" | 21 #include "net/url_request/url_fetcher_delegate.h" |
23 | 22 |
24 template <typename T> struct DefaultSingletonTraits; | 23 template <typename T> struct DefaultSingletonTraits; |
(...skipping 18 matching lines...) Expand all Loading... |
43 | 42 |
44 class TranslateManager : public content::NotificationObserver, | 43 class TranslateManager : public content::NotificationObserver, |
45 public net::URLFetcherDelegate { | 44 public net::URLFetcherDelegate { |
46 public: | 45 public: |
47 // Returns the singleton instance. | 46 // Returns the singleton instance. |
48 static TranslateManager* GetInstance(); | 47 static TranslateManager* GetInstance(); |
49 | 48 |
50 virtual ~TranslateManager(); | 49 virtual ~TranslateManager(); |
51 | 50 |
52 // Let the caller decide if and when we should fetch the language list from | 51 // Let the caller decide if and when we should fetch the language list from |
53 // the translate server. This is a NOOP if switches::kDisableTranslate is | 52 // the translate server. This is a NOOP if switches::kDisableTranslate is set |
54 // set or if prefs::kEnableTranslate is set to false. | 53 // or if prefs::kEnableTranslate is set to false. |
55 // It will not retry more than kMaxRetryLanguageListFetch times. | |
56 void FetchLanguageListFromTranslateServer(PrefService* prefs); | 54 void FetchLanguageListFromTranslateServer(PrefService* prefs); |
57 | 55 |
58 // Allows caller to cleanup pending URLFetcher objects to make sure they | 56 // Allows caller to cleanup pending URLFetcher objects to make sure they |
59 // get released in the appropriate thread... Mainly for tests. | 57 // get released in the appropriate thread... Mainly for tests. |
60 void CleanupPendingUlrFetcher(); | 58 void CleanupPendingUlrFetcher(); |
61 | 59 |
62 // Translates the page contents from |source_lang| to |target_lang|. | 60 // Translates the page contents from |source_lang| to |target_lang|. |
63 // The actual translation might be performed asynchronously if the translate | 61 // The actual translation might be performed asynchronously if the translate |
64 // script is not yet available. | 62 // script is not yet available. |
65 void TranslatePage(content::WebContents* web_contents, | 63 void TranslatePage(content::WebContents* web_contents, |
(...skipping 29 matching lines...) Expand all Loading... |
95 } | 93 } |
96 | 94 |
97 // Number of attempts before waiting for a page to be fully reloaded. | 95 // Number of attempts before waiting for a page to be fully reloaded. |
98 void set_translate_max_reload_attemps(int attempts) { | 96 void set_translate_max_reload_attemps(int attempts) { |
99 max_reload_check_attempts_ = attempts; | 97 max_reload_check_attempts_ = attempts; |
100 } | 98 } |
101 | 99 |
102 // Returns true if the URL can be translated. | 100 // Returns true if the URL can be translated. |
103 static bool IsTranslatableURL(const GURL& url); | 101 static bool IsTranslatableURL(const GURL& url); |
104 | 102 |
105 // Fills |languages| with the list of languages that the translate server can | |
106 // translate to and from. | |
107 static void GetSupportedLanguages(std::vector<std::string>* languages); | |
108 | |
109 // Returns the language code that can be used with the Translate method for a | |
110 // specified |chrome_locale|. | |
111 static std::string GetLanguageCode(const std::string& chrome_locale); | |
112 | |
113 // Returns true if |language| is supported by the translation server. | |
114 static bool IsSupportedLanguage(const std::string& language); | |
115 | |
116 // static const values shared with our browser tests. | |
117 static const char kLanguageListCallbackName[]; | |
118 static const char kTargetLanguagesKey[]; | |
119 protected: | 103 protected: |
120 TranslateManager(); | 104 TranslateManager(); |
121 | 105 |
122 private: | 106 private: |
123 friend struct DefaultSingletonTraits<TranslateManager>; | 107 friend struct DefaultSingletonTraits<TranslateManager>; |
124 | 108 |
125 // Structure that describes a translate request. | 109 // Structure that describes a translate request. |
126 // Translation may be deferred while the translate script is being retrieved | 110 // Translation may be deferred while the translate script is being retrieved |
127 // from the translate server. | 111 // from the translate server. |
128 struct PendingRequest { | 112 struct PendingRequest { |
129 int render_process_id; | 113 int render_process_id; |
130 int render_view_id; | 114 int render_view_id; |
131 int page_id; | 115 int page_id; |
132 std::string source_lang; | 116 std::string source_lang; |
133 std::string target_lang; | 117 std::string target_lang; |
134 }; | 118 }; |
135 | 119 |
136 // Fills supported_languages_ with the list of languages that the translate | |
137 // server can translate to and from. | |
138 static void SetSupportedLanguages(const std::string& language_list); | |
139 | |
140 // Initializes the list of supported languages if it wasn't initialized before | |
141 // in case we failed to get them from the server, or didn't get them just yet. | |
142 static void InitSupportedLanguages(); | |
143 | |
144 // Starts the translation process on |tab| containing the page in the | 120 // Starts the translation process on |tab| containing the page in the |
145 // |page_lang| language. | 121 // |page_lang| language. |
146 void InitiateTranslation(content::WebContents* web_contents, | 122 void InitiateTranslation(content::WebContents* web_contents, |
147 const std::string& page_lang); | 123 const std::string& page_lang); |
148 | 124 |
149 // If the tab identified by |process_id| and |render_id| has been closed, this | 125 // If the tab identified by |process_id| and |render_id| has been closed, this |
150 // does nothing, otherwise it calls InitiateTranslation. | 126 // does nothing, otherwise it calls InitiateTranslation. |
151 void InitiateTranslationPosted(int process_id, int render_id, | 127 void InitiateTranslationPosted(int process_id, int render_id, |
152 const std::string& page_lang, int attempt); | 128 const std::string& page_lang, int attempt); |
153 | 129 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 // Delay after which the translate script is fetched again | 182 // Delay after which the translate script is fetched again |
207 // from the translate server. | 183 // from the translate server. |
208 base::TimeDelta translate_script_expiration_delay_; | 184 base::TimeDelta translate_script_expiration_delay_; |
209 | 185 |
210 // Max number of attempts before checking if a page has been reloaded. | 186 // Max number of attempts before checking if a page has been reloaded. |
211 int max_reload_check_attempts_; | 187 int max_reload_check_attempts_; |
212 | 188 |
213 // Set when the translate JS is currently being retrieved. NULL otherwise. | 189 // Set when the translate JS is currently being retrieved. NULL otherwise. |
214 scoped_ptr<net::URLFetcher> translate_script_request_pending_; | 190 scoped_ptr<net::URLFetcher> translate_script_request_pending_; |
215 | 191 |
216 // Set when the list of languages is currently being retrieved. | |
217 // NULL otherwise. | |
218 scoped_ptr<net::URLFetcher> language_list_request_pending_; | |
219 | |
220 // The list of pending translate requests. Translate requests are queued when | 192 // The list of pending translate requests. Translate requests are queued when |
221 // the translate script is not ready and has to be fetched from the translate | 193 // the translate script is not ready and has to be fetched from the translate |
222 // server. | 194 // server. |
223 std::vector<PendingRequest> pending_requests_; | 195 std::vector<PendingRequest> pending_requests_; |
224 | 196 |
225 // The languages supported by the translation server. | |
226 static base::LazyInstance<std::set<std::string> > supported_languages_; | |
227 | |
228 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 197 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
229 }; | 198 }; |
230 | 199 |
231 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 200 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
OLD | NEW |