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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 #include "base/time.h" | 18 #include "base/time.h" |
19 #include "chrome/browser/prefs/pref_change_registrar.h" | 19 #include "chrome/browser/prefs/pref_change_registrar.h" |
20 #include "chrome/common/translate_errors.h" | 20 #include "chrome/common/translate_errors.h" |
21 #include "content/public/browser/notification_observer.h" | 21 #include "content/public/browser/notification_observer.h" |
22 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
23 #include "content/public/common/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
24 | 24 |
25 template <typename T> struct DefaultSingletonTraits; | 25 template <typename T> struct DefaultSingletonTraits; |
26 class GURL; | 26 class GURL; |
27 struct PageTranslatedDetails; | 27 struct PageTranslatedDetails; |
28 class PrefService; | 28 class PrefService; |
29 class TranslateInfoBarDelegate; | 29 class TranslateInfoBarDelegate; |
30 | 30 |
31 namespace content { | 31 namespace content { |
32 class WebContents; | 32 class WebContents; |
33 } | 33 } |
34 | 34 |
| 35 namespace net { |
| 36 class URLFetcher; |
| 37 } |
| 38 |
35 // The TranslateManager class is responsible for showing an info-bar when a page | 39 // The TranslateManager class is responsible for showing an info-bar when a page |
36 // in a language different than the user language is loaded. It triggers the | 40 // in a language different than the user language is loaded. It triggers the |
37 // page translation the user requests. | 41 // page translation the user requests. |
38 // It is a singleton. | 42 // It is a singleton. |
39 | 43 |
40 class TranslateManager : public content::NotificationObserver, | 44 class TranslateManager : public content::NotificationObserver, |
41 public content::URLFetcherDelegate { | 45 public net::URLFetcherDelegate { |
42 public: | 46 public: |
43 // Returns the singleton instance. | 47 // Returns the singleton instance. |
44 static TranslateManager* GetInstance(); | 48 static TranslateManager* GetInstance(); |
45 | 49 |
46 virtual ~TranslateManager(); | 50 virtual ~TranslateManager(); |
47 | 51 |
48 // Let the caller decide if and when we should fetch the language list from | 52 // Let the caller decide if and when we should fetch the language list from |
49 // the translate server. This is a NOOP if switches::kDisableTranslate is | 53 // the translate server. This is a NOOP if switches::kDisableTranslate is |
50 // set or if prefs::kEnableTranslate is set to false. | 54 // set or if prefs::kEnableTranslate is set to false. |
51 // It will not retry more than kMaxRetryLanguageListFetch times. | 55 // It will not retry more than kMaxRetryLanguageListFetch times. |
(...skipping 20 matching lines...) Expand all Loading... |
72 void ReportLanguageDetectionError(content::WebContents* web_contents); | 76 void ReportLanguageDetectionError(content::WebContents* web_contents); |
73 | 77 |
74 // Clears the translate script, so it will be fetched next time we translate. | 78 // Clears the translate script, so it will be fetched next time we translate. |
75 void ClearTranslateScript() { translate_script_.clear(); } | 79 void ClearTranslateScript() { translate_script_.clear(); } |
76 | 80 |
77 // content::NotificationObserver implementation: | 81 // content::NotificationObserver implementation: |
78 virtual void Observe(int type, | 82 virtual void Observe(int type, |
79 const content::NotificationSource& source, | 83 const content::NotificationSource& source, |
80 const content::NotificationDetails& details) OVERRIDE; | 84 const content::NotificationDetails& details) OVERRIDE; |
81 | 85 |
82 // content::URLFetcherDelegate implementation: | 86 // net::URLFetcherDelegate implementation: |
83 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 87 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
84 | 88 |
85 // Used by unit-tests to override the default delay after which the translate | 89 // Used by unit-tests to override the default delay after which the translate |
86 // script is fetched again from the translation server. | 90 // script is fetched again from the translation server. |
87 void set_translate_script_expiration_delay(int delay_ms) { | 91 void set_translate_script_expiration_delay(int delay_ms) { |
88 translate_script_expiration_delay_ = | 92 translate_script_expiration_delay_ = |
89 base::TimeDelta::FromMilliseconds(delay_ms); | 93 base::TimeDelta::FromMilliseconds(delay_ms); |
90 } | 94 } |
91 | 95 |
92 // Convenience method to know if a tab is showing a translate infobar. | 96 // Convenience method to know if a tab is showing a translate infobar. |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 base::WeakPtrFactory<TranslateManager> weak_method_factory_; | 205 base::WeakPtrFactory<TranslateManager> weak_method_factory_; |
202 | 206 |
203 // The JS injected in the page to do the translation. | 207 // The JS injected in the page to do the translation. |
204 std::string translate_script_; | 208 std::string translate_script_; |
205 | 209 |
206 // Delay after which the translate script is fetched again | 210 // Delay after which the translate script is fetched again |
207 // from the translate server. | 211 // from the translate server. |
208 base::TimeDelta translate_script_expiration_delay_; | 212 base::TimeDelta translate_script_expiration_delay_; |
209 | 213 |
210 // Set when the translate JS is currently being retrieved. NULL otherwise. | 214 // Set when the translate JS is currently being retrieved. NULL otherwise. |
211 scoped_ptr<content::URLFetcher> translate_script_request_pending_; | 215 scoped_ptr<net::URLFetcher> translate_script_request_pending_; |
212 | 216 |
213 // Set when the list of languages is currently being retrieved. | 217 // Set when the list of languages is currently being retrieved. |
214 // NULL otherwise. | 218 // NULL otherwise. |
215 scoped_ptr<content::URLFetcher> language_list_request_pending_; | 219 scoped_ptr<net::URLFetcher> language_list_request_pending_; |
216 | 220 |
217 // The list of pending translate requests. Translate requests are queued when | 221 // The list of pending translate requests. Translate requests are queued when |
218 // the translate script is not ready and has to be fetched from the translate | 222 // the translate script is not ready and has to be fetched from the translate |
219 // server. | 223 // server. |
220 std::vector<PendingRequest> pending_requests_; | 224 std::vector<PendingRequest> pending_requests_; |
221 | 225 |
222 // The languages supported by the translation server. | 226 // The languages supported by the translation server. |
223 static base::LazyInstance<std::set<std::string> > supported_languages_; | 227 static base::LazyInstance<std::set<std::string> > supported_languages_; |
224 | 228 |
225 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 229 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
226 }; | 230 }; |
227 | 231 |
228 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 232 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
OLD | NEW |