| 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/observer_list.h" |
| 17 #include "base/prefs/pref_change_registrar.h" | 18 #include "base/prefs/pref_change_registrar.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; |
| 27 struct LanguageDetectionDetails; |
| 26 struct PageTranslatedDetails; | 28 struct PageTranslatedDetails; |
| 27 class PrefService; | 29 class PrefService; |
| 28 struct ShortcutConfiguration; | 30 struct ShortcutConfiguration; |
| 29 class TranslateInfoBarDelegate; | 31 class TranslateInfoBarDelegate; |
| 30 | 32 |
| 31 namespace content { | 33 namespace content { |
| 32 class WebContents; | 34 class WebContents; |
| 33 } | 35 } |
| 34 | 36 |
| 35 namespace net { | 37 namespace net { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // Returns the language code that can be used with the Translate method for a | 111 // Returns the language code that can be used with the Translate method for a |
| 110 // specified |chrome_locale|. | 112 // specified |chrome_locale|. |
| 111 static std::string GetLanguageCode(const std::string& chrome_locale); | 113 static std::string GetLanguageCode(const std::string& chrome_locale); |
| 112 | 114 |
| 113 // Returns true if |language| is supported by the translation server. | 115 // Returns true if |language| is supported by the translation server. |
| 114 static bool IsSupportedLanguage(const std::string& language); | 116 static bool IsSupportedLanguage(const std::string& language); |
| 115 | 117 |
| 116 // static const values shared with our browser tests. | 118 // static const values shared with our browser tests. |
| 117 static const char kLanguageListCallbackName[]; | 119 static const char kLanguageListCallbackName[]; |
| 118 static const char kTargetLanguagesKey[]; | 120 static const char kTargetLanguagesKey[]; |
| 121 |
| 122 // The observer class for TranslateManager. |
| 123 class Observer { |
| 124 public: |
| 125 virtual void OnLanguageDetection( |
| 126 const LanguageDetectionDetails& details) = 0; |
| 127 }; |
| 128 |
| 129 // Adds/removes observer. |
| 130 void AddObserver(Observer* obs); |
| 131 void RemoveObserver(Observer* obs); |
| 132 |
| 119 protected: | 133 protected: |
| 120 TranslateManager(); | 134 TranslateManager(); |
| 121 | 135 |
| 122 private: | 136 private: |
| 123 friend struct DefaultSingletonTraits<TranslateManager>; | 137 friend struct DefaultSingletonTraits<TranslateManager>; |
| 124 | 138 |
| 125 // Structure that describes a translate request. | 139 // Structure that describes a translate request. |
| 126 // Translation may be deferred while the translate script is being retrieved | 140 // Translation may be deferred while the translate script is being retrieved |
| 127 // from the translate server. | 141 // from the translate server. |
| 128 struct PendingRequest { | 142 struct PendingRequest { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 const std::string& language); | 181 const std::string& language); |
| 168 | 182 |
| 169 // Initializes the |accept_languages_| language table based on the associated | 183 // Initializes the |accept_languages_| language table based on the associated |
| 170 // preference in |prefs|. | 184 // preference in |prefs|. |
| 171 void InitAcceptLanguages(PrefService* prefs); | 185 void InitAcceptLanguages(PrefService* prefs); |
| 172 | 186 |
| 173 // Fetches the JS translate script (the script that is injected in the page | 187 // Fetches the JS translate script (the script that is injected in the page |
| 174 // to translate it). | 188 // to translate it). |
| 175 void RequestTranslateScript(); | 189 void RequestTranslateScript(); |
| 176 | 190 |
| 191 // Notifies to the observers when a language is detected. |
| 192 void NotifyLanguageDetection(const LanguageDetectionDetails& details); |
| 193 |
| 177 // Returns the language to translate to. The language returned is the | 194 // Returns the language to translate to. The language returned is the |
| 178 // first language found in the following list that is supported by the | 195 // first language found in the following list that is supported by the |
| 179 // translation service: | 196 // translation service: |
| 180 // the UI language | 197 // the UI language |
| 181 // the accept-language list | 198 // the accept-language list |
| 182 // If no language is found then an empty string is returned. | 199 // If no language is found then an empty string is returned. |
| 183 static std::string GetTargetLanguage(PrefService* prefs); | 200 static std::string GetTargetLanguage(PrefService* prefs); |
| 184 | 201 |
| 185 // Returns the different parameters used to decide whether extra shortcuts | 202 // Returns the different parameters used to decide whether extra shortcuts |
| 186 // are needed. | 203 // are needed. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 scoped_ptr<net::URLFetcher> language_list_request_pending_; | 235 scoped_ptr<net::URLFetcher> language_list_request_pending_; |
| 219 | 236 |
| 220 // The list of pending translate requests. Translate requests are queued when | 237 // 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 | 238 // the translate script is not ready and has to be fetched from the translate |
| 222 // server. | 239 // server. |
| 223 std::vector<PendingRequest> pending_requests_; | 240 std::vector<PendingRequest> pending_requests_; |
| 224 | 241 |
| 225 // The languages supported by the translation server. | 242 // The languages supported by the translation server. |
| 226 static base::LazyInstance<std::set<std::string> > supported_languages_; | 243 static base::LazyInstance<std::set<std::string> > supported_languages_; |
| 227 | 244 |
| 245 // List of registered observers. |
| 246 ObserverList<Observer> observer_list_; |
| 247 |
| 228 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 248 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 229 }; | 249 }; |
| 230 | 250 |
| 231 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 251 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| OLD | NEW |