| 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" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/prefs/public/pref_change_registrar.h" | 17 #include "base/prefs/public/pref_change_registrar.h" |
| 18 #include "base/prefs/public/pref_observer.h" |
| 18 #include "base/time.h" | 19 #include "base/time.h" |
| 19 #include "chrome/common/translate_errors.h" | 20 #include "chrome/common/translate_errors.h" |
| 20 #include "content/public/browser/notification_observer.h" | 21 #include "content/public/browser/notification_observer.h" |
| 21 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
| 23 | 24 |
| 24 template <typename T> struct DefaultSingletonTraits; | 25 template <typename T> struct DefaultSingletonTraits; |
| 25 class GURL; | 26 class GURL; |
| 26 struct PageTranslatedDetails; | 27 struct PageTranslatedDetails; |
| 27 class PrefService; | 28 class PrefService; |
| 29 class PrefServiceBase; |
| 28 class TranslateInfoBarDelegate; | 30 class TranslateInfoBarDelegate; |
| 29 | 31 |
| 30 namespace content { | 32 namespace content { |
| 31 class WebContents; | 33 class WebContents; |
| 32 } | 34 } |
| 33 | 35 |
| 34 namespace net { | 36 namespace net { |
| 35 class URLFetcher; | 37 class URLFetcher; |
| 36 } | 38 } |
| 37 | 39 |
| 38 // The TranslateManager class is responsible for showing an info-bar when a page | 40 // The TranslateManager class is responsible for showing an info-bar when a page |
| 39 // in a language different than the user language is loaded. It triggers the | 41 // in a language different than the user language is loaded. It triggers the |
| 40 // page translation the user requests. | 42 // page translation the user requests. |
| 41 // It is a singleton. | 43 // It is a singleton. |
| 42 | 44 |
| 43 class TranslateManager : public content::NotificationObserver, | 45 class TranslateManager : public content::NotificationObserver, |
| 46 public PrefObserver, |
| 44 public net::URLFetcherDelegate { | 47 public net::URLFetcherDelegate { |
| 45 public: | 48 public: |
| 46 // Returns the singleton instance. | 49 // Returns the singleton instance. |
| 47 static TranslateManager* GetInstance(); | 50 static TranslateManager* GetInstance(); |
| 48 | 51 |
| 49 virtual ~TranslateManager(); | 52 virtual ~TranslateManager(); |
| 50 | 53 |
| 51 // 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 |
| 52 // the translate server. This is a NOOP if switches::kDisableTranslate is | 55 // the translate server. This is a NOOP if switches::kDisableTranslate is |
| 53 // set or if prefs::kEnableTranslate is set to false. | 56 // set or if prefs::kEnableTranslate is set to false. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 75 void ReportLanguageDetectionError(content::WebContents* web_contents); | 78 void ReportLanguageDetectionError(content::WebContents* web_contents); |
| 76 | 79 |
| 77 // Clears the translate script, so it will be fetched next time we translate. | 80 // Clears the translate script, so it will be fetched next time we translate. |
| 78 void ClearTranslateScript() { translate_script_.clear(); } | 81 void ClearTranslateScript() { translate_script_.clear(); } |
| 79 | 82 |
| 80 // content::NotificationObserver implementation: | 83 // content::NotificationObserver implementation: |
| 81 virtual void Observe(int type, | 84 virtual void Observe(int type, |
| 82 const content::NotificationSource& source, | 85 const content::NotificationSource& source, |
| 83 const content::NotificationDetails& details) OVERRIDE; | 86 const content::NotificationDetails& details) OVERRIDE; |
| 84 | 87 |
| 88 // PrefObserver implementation: |
| 89 virtual void OnPreferenceChanged(PrefServiceBase* service, |
| 90 const std::string& pref_name) OVERRIDE; |
| 91 |
| 85 // net::URLFetcherDelegate implementation: | 92 // net::URLFetcherDelegate implementation: |
| 86 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 93 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 87 | 94 |
| 88 // Used by unit-tests to override the default delay after which the translate | 95 // Used by unit-tests to override the default delay after which the translate |
| 89 // script is fetched again from the translation server. | 96 // script is fetched again from the translation server. |
| 90 void set_translate_script_expiration_delay(int delay_ms) { | 97 void set_translate_script_expiration_delay(int delay_ms) { |
| 91 translate_script_expiration_delay_ = | 98 translate_script_expiration_delay_ = |
| 92 base::TimeDelta::FromMilliseconds(delay_ms); | 99 base::TimeDelta::FromMilliseconds(delay_ms); |
| 93 } | 100 } |
| 94 | 101 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 void PageTranslated(content::WebContents* web_contents, | 167 void PageTranslated(content::WebContents* web_contents, |
| 161 PageTranslatedDetails* details); | 168 PageTranslatedDetails* details); |
| 162 | 169 |
| 163 // Returns true if the passed language has been configured by the user as an | 170 // Returns true if the passed language has been configured by the user as an |
| 164 // accept language. | 171 // accept language. |
| 165 bool IsAcceptLanguage(content::WebContents* web_contents, | 172 bool IsAcceptLanguage(content::WebContents* web_contents, |
| 166 const std::string& language); | 173 const std::string& language); |
| 167 | 174 |
| 168 // Initializes the |accept_languages_| language table based on the associated | 175 // Initializes the |accept_languages_| language table based on the associated |
| 169 // preference in |prefs|. | 176 // preference in |prefs|. |
| 170 void InitAcceptLanguages(PrefService* prefs); | 177 void InitAcceptLanguages(PrefServiceBase* prefs); |
| 171 | 178 |
| 172 // Fetches the JS translate script (the script that is injected in the page | 179 // Fetches the JS translate script (the script that is injected in the page |
| 173 // to translate it). | 180 // to translate it). |
| 174 void RequestTranslateScript(); | 181 void RequestTranslateScript(); |
| 175 | 182 |
| 176 // Shows the specified translate |infobar| in the given |tab|. If a current | 183 // Shows the specified translate |infobar| in the given |tab|. If a current |
| 177 // translate infobar is showing, it just replaces it with the new one. | 184 // translate infobar is showing, it just replaces it with the new one. |
| 178 void ShowInfoBar(content::WebContents* web_contents, | 185 void ShowInfoBar(content::WebContents* web_contents, |
| 179 TranslateInfoBarDelegate* infobar); | 186 TranslateInfoBarDelegate* infobar); |
| 180 | 187 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 192 | 199 |
| 193 content::NotificationRegistrar notification_registrar_; | 200 content::NotificationRegistrar notification_registrar_; |
| 194 | 201 |
| 195 // Each PrefChangeRegistrar only tracks a single PrefService, so a map from | 202 // Each PrefChangeRegistrar only tracks a single PrefService, so a map from |
| 196 // each PrefService used to its registrar is needed. | 203 // each PrefService used to its registrar is needed. |
| 197 typedef std::map<PrefService*, PrefChangeRegistrar*> PrefServiceRegistrarMap; | 204 typedef std::map<PrefService*, PrefChangeRegistrar*> PrefServiceRegistrarMap; |
| 198 PrefServiceRegistrarMap pref_change_registrars_; | 205 PrefServiceRegistrarMap pref_change_registrars_; |
| 199 | 206 |
| 200 // A map that associates a profile with its parsed "accept languages". | 207 // A map that associates a profile with its parsed "accept languages". |
| 201 typedef std::set<std::string> LanguageSet; | 208 typedef std::set<std::string> LanguageSet; |
| 202 typedef std::map<PrefService*, LanguageSet> PrefServiceLanguagesMap; | 209 typedef std::map<PrefServiceBase*, LanguageSet> PrefServiceLanguagesMap; |
| 203 PrefServiceLanguagesMap accept_languages_; | 210 PrefServiceLanguagesMap accept_languages_; |
| 204 | 211 |
| 205 base::WeakPtrFactory<TranslateManager> weak_method_factory_; | 212 base::WeakPtrFactory<TranslateManager> weak_method_factory_; |
| 206 | 213 |
| 207 // The JS injected in the page to do the translation. | 214 // The JS injected in the page to do the translation. |
| 208 std::string translate_script_; | 215 std::string translate_script_; |
| 209 | 216 |
| 210 // Delay after which the translate script is fetched again | 217 // Delay after which the translate script is fetched again |
| 211 // from the translate server. | 218 // from the translate server. |
| 212 base::TimeDelta translate_script_expiration_delay_; | 219 base::TimeDelta translate_script_expiration_delay_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 223 // server. | 230 // server. |
| 224 std::vector<PendingRequest> pending_requests_; | 231 std::vector<PendingRequest> pending_requests_; |
| 225 | 232 |
| 226 // The languages supported by the translation server. | 233 // The languages supported by the translation server. |
| 227 static base::LazyInstance<std::set<std::string> > supported_languages_; | 234 static base::LazyInstance<std::set<std::string> > supported_languages_; |
| 228 | 235 |
| 229 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 236 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 230 }; | 237 }; |
| 231 | 238 |
| 232 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 239 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| OLD | NEW |