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/observer_list.h" | 16 #include "base/observer_list.h" |
18 #include "base/prefs/pref_change_registrar.h" | 17 #include "base/prefs/pref_change_registrar.h" |
19 #include "base/time.h" | 18 #include "base/time.h" |
20 #include "chrome/common/translate_errors.h" | 19 #include "chrome/common/translate_errors.h" |
21 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
22 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
23 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
24 | 23 |
25 template <typename T> struct DefaultSingletonTraits; | 24 template <typename T> struct DefaultSingletonTraits; |
26 class GURL; | 25 class GURL; |
27 struct LanguageDetectionDetails; | 26 struct LanguageDetectionDetails; |
28 struct PageTranslatedDetails; | 27 struct PageTranslatedDetails; |
29 class PrefService; | 28 class PrefService; |
30 struct ShortcutConfiguration; | 29 struct ShortcutConfiguration; |
31 class TranslateInfoBarDelegate; | 30 class TranslateInfoBarDelegate; |
| 31 class TranslateLanguageList; |
32 | 32 |
33 namespace content { | 33 namespace content { |
34 class WebContents; | 34 class WebContents; |
35 } | 35 } |
36 | 36 |
37 namespace net { | 37 namespace net { |
38 class URLFetcher; | 38 class URLFetcher; |
39 } | 39 } |
40 | 40 |
41 // 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 |
42 // 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 |
43 // page translation the user requests. | 43 // page translation the user requests. |
44 // It is a singleton. | 44 // It is a singleton. |
45 | 45 |
46 class TranslateManager : public content::NotificationObserver, | 46 class TranslateManager : public content::NotificationObserver, |
47 public net::URLFetcherDelegate { | 47 public net::URLFetcherDelegate { |
48 public: | 48 public: |
49 // Returns the singleton instance. | 49 // Returns the singleton instance. |
50 static TranslateManager* GetInstance(); | 50 static TranslateManager* GetInstance(); |
51 | 51 |
52 virtual ~TranslateManager(); | 52 virtual ~TranslateManager(); |
53 | 53 |
54 // Let the caller decide if and when we should fetch the language list from | 54 // Let the caller decide if and when we should fetch the language list from |
55 // the translate server. This is a NOOP if switches::kDisableTranslate is | 55 // the translate server. This is a NOOP if switches::kDisableTranslate is set |
56 // set or if prefs::kEnableTranslate is set to false. | 56 // or if prefs::kEnableTranslate is set to false. |
57 // It will not retry more than kMaxRetryLanguageListFetch times. | |
58 void FetchLanguageListFromTranslateServer(PrefService* prefs); | 57 void FetchLanguageListFromTranslateServer(PrefService* prefs); |
59 | 58 |
60 // Allows caller to cleanup pending URLFetcher objects to make sure they | 59 // Allows caller to cleanup pending URLFetcher objects to make sure they |
61 // get released in the appropriate thread... Mainly for tests. | 60 // get released in the appropriate thread... Mainly for tests. |
62 void CleanupPendingUlrFetcher(); | 61 void CleanupPendingUlrFetcher(); |
63 | 62 |
64 // Translates the page contents from |source_lang| to |target_lang|. | 63 // Translates the page contents from |source_lang| to |target_lang|. |
65 // The actual translation might be performed asynchronously if the translate | 64 // The actual translation might be performed asynchronously if the translate |
66 // script is not yet available. | 65 // script is not yet available. |
67 void TranslatePage(content::WebContents* web_contents, | 66 void TranslatePage(content::WebContents* web_contents, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // translate to and from. | 107 // translate to and from. |
109 static void GetSupportedLanguages(std::vector<std::string>* languages); | 108 static void GetSupportedLanguages(std::vector<std::string>* languages); |
110 | 109 |
111 // Returns the language code that can be used with the Translate method for a | 110 // Returns the language code that can be used with the Translate method for a |
112 // specified |chrome_locale|. | 111 // specified |chrome_locale|. |
113 static std::string GetLanguageCode(const std::string& chrome_locale); | 112 static std::string GetLanguageCode(const std::string& chrome_locale); |
114 | 113 |
115 // Returns true if |language| is supported by the translation server. | 114 // Returns true if |language| is supported by the translation server. |
116 static bool IsSupportedLanguage(const std::string& language); | 115 static bool IsSupportedLanguage(const std::string& language); |
117 | 116 |
118 // static const values shared with our browser tests. | |
119 static const char kLanguageListCallbackName[]; | |
120 static const char kTargetLanguagesKey[]; | |
121 | |
122 // The observer class for TranslateManager. | 117 // The observer class for TranslateManager. |
123 class Observer { | 118 class Observer { |
124 public: | 119 public: |
125 virtual void OnLanguageDetection( | 120 virtual void OnLanguageDetection( |
126 const LanguageDetectionDetails& details) = 0; | 121 const LanguageDetectionDetails& details) = 0; |
127 }; | 122 }; |
128 | 123 |
129 // Adds/removes observer. | 124 // Adds/removes observer. |
130 void AddObserver(Observer* obs); | 125 void AddObserver(Observer* obs); |
131 void RemoveObserver(Observer* obs); | 126 void RemoveObserver(Observer* obs); |
132 | 127 |
133 protected: | 128 protected: |
134 TranslateManager(); | 129 TranslateManager(); |
135 | 130 |
136 private: | 131 private: |
137 friend struct DefaultSingletonTraits<TranslateManager>; | 132 friend struct DefaultSingletonTraits<TranslateManager>; |
138 | 133 |
139 // Structure that describes a translate request. | 134 // Structure that describes a translate request. |
140 // Translation may be deferred while the translate script is being retrieved | 135 // Translation may be deferred while the translate script is being retrieved |
141 // from the translate server. | 136 // from the translate server. |
142 struct PendingRequest { | 137 struct PendingRequest { |
143 int render_process_id; | 138 int render_process_id; |
144 int render_view_id; | 139 int render_view_id; |
145 int page_id; | 140 int page_id; |
146 std::string source_lang; | 141 std::string source_lang; |
147 std::string target_lang; | 142 std::string target_lang; |
148 }; | 143 }; |
149 | 144 |
150 // Fills supported_languages_ with the list of languages that the translate | |
151 // server can translate to and from. | |
152 static void SetSupportedLanguages(const std::string& language_list); | |
153 | |
154 // Initializes the list of supported languages if it wasn't initialized before | |
155 // in case we failed to get them from the server, or didn't get them just yet. | |
156 static void InitSupportedLanguages(); | |
157 | |
158 // Starts the translation process on |tab| containing the page in the | 145 // Starts the translation process on |tab| containing the page in the |
159 // |page_lang| language. | 146 // |page_lang| language. |
160 void InitiateTranslation(content::WebContents* web_contents, | 147 void InitiateTranslation(content::WebContents* web_contents, |
161 const std::string& page_lang); | 148 const std::string& page_lang); |
162 | 149 |
163 // If the tab identified by |process_id| and |render_id| has been closed, this | 150 // If the tab identified by |process_id| and |render_id| has been closed, this |
164 // does nothing, otherwise it calls InitiateTranslation. | 151 // does nothing, otherwise it calls InitiateTranslation. |
165 void InitiateTranslationPosted(int process_id, int render_id, | 152 void InitiateTranslationPosted(int process_id, int render_id, |
166 const std::string& page_lang, int attempt); | 153 const std::string& page_lang, int attempt); |
167 | 154 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 // Delay after which the translate script is fetched again | 210 // Delay after which the translate script is fetched again |
224 // from the translate server. | 211 // from the translate server. |
225 base::TimeDelta translate_script_expiration_delay_; | 212 base::TimeDelta translate_script_expiration_delay_; |
226 | 213 |
227 // Max number of attempts before checking if a page has been reloaded. | 214 // Max number of attempts before checking if a page has been reloaded. |
228 int max_reload_check_attempts_; | 215 int max_reload_check_attempts_; |
229 | 216 |
230 // Set when the translate JS is currently being retrieved. NULL otherwise. | 217 // Set when the translate JS is currently being retrieved. NULL otherwise. |
231 scoped_ptr<net::URLFetcher> translate_script_request_pending_; | 218 scoped_ptr<net::URLFetcher> translate_script_request_pending_; |
232 | 219 |
233 // Set when the list of languages is currently being retrieved. | |
234 // NULL otherwise. | |
235 scoped_ptr<net::URLFetcher> language_list_request_pending_; | |
236 | |
237 // The list of pending translate requests. Translate requests are queued when | 220 // The list of pending translate requests. Translate requests are queued when |
238 // 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 |
239 // server. | 222 // server. |
240 std::vector<PendingRequest> pending_requests_; | 223 std::vector<PendingRequest> pending_requests_; |
241 | 224 |
242 // The languages supported by the translation server. | |
243 static base::LazyInstance<std::set<std::string> > supported_languages_; | |
244 | |
245 // List of registered observers. | 225 // List of registered observers. |
246 ObserverList<Observer> observer_list_; | 226 ObserverList<Observer> observer_list_; |
247 | 227 |
| 228 // An instance of TranslateLanguageList which manages supported language list. |
| 229 scoped_ptr<TranslateLanguageList> language_list_; |
| 230 |
248 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 231 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
249 }; | 232 }; |
250 | 233 |
251 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 234 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
OLD | NEW |