Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: chrome/browser/translate/translate_manager.h

Issue 17390018: Refactoring: Create TranslateScript from TranslateManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactoring-translate-url-fetcher
Patch Set: Removed the nits Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/gtest_prod_util.h" 12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h" 15 #include "base/observer_list.h"
16 #include "base/time.h" 16 #include "base/time.h"
17 #include "chrome/common/translate/translate_errors.h" 17 #include "chrome/common/translate/translate_errors.h"
18 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/browser/notification_registrar.h"
20 #include "net/url_request/url_fetcher_delegate.h"
21 20
22 template <typename T> struct DefaultSingletonTraits; 21 template <typename T> struct DefaultSingletonTraits;
23 class GURL; 22 class GURL;
24 struct LanguageDetectionDetails; 23 struct LanguageDetectionDetails;
25 struct PageTranslatedDetails; 24 struct PageTranslatedDetails;
26 class PrefService; 25 class PrefService;
27 class Profile; 26 class Profile;
28 struct ShortcutConfiguration; 27 struct ShortcutConfiguration;
29 class TranslateAcceptLanguages; 28 class TranslateAcceptLanguages;
30 struct TranslateErrorDetails; 29 struct TranslateErrorDetails;
31 struct TranslateEventDetails; 30 struct TranslateEventDetails;
32 class TranslateInfoBarDelegate; 31 class TranslateInfoBarDelegate;
33 class TranslateLanguageList; 32 class TranslateLanguageList;
33 class TranslateScript;
34 34
35 namespace content { 35 namespace content {
36 class WebContents; 36 class WebContents;
37 } 37 }
38 38
39 namespace net { 39 namespace net {
40 class URLFetcher; 40 class URLFetcher;
41 } 41 }
42 42
43 // The TranslateManager class is responsible for showing an info-bar when a page 43 // The TranslateManager class is responsible for showing an info-bar when a page
44 // in a language different than the user language is loaded. It triggers the 44 // in a language different than the user language is loaded. It triggers the
45 // page translation the user requests. 45 // page translation the user requests.
46 // It is a singleton. 46 // It is a singleton.
47 47
48 class TranslateManager : public content::NotificationObserver, 48 class TranslateManager : public content::NotificationObserver {
49 public net::URLFetcherDelegate {
50 public: 49 public:
51 // Returns the singleton instance. 50 // Returns the singleton instance.
52 static TranslateManager* GetInstance(); 51 static TranslateManager* GetInstance();
53 52
54 virtual ~TranslateManager(); 53 virtual ~TranslateManager();
55 54
56 // Returns true if the URL can be translated. 55 // Returns true if the URL can be translated.
57 static bool IsTranslatableURL(const GURL& url); 56 static bool IsTranslatableURL(const GURL& url);
58 57
59 // Fills |languages| with the list of languages that the translate server can 58 // Fills |languages| with the list of languages that the translate server can
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 // Reverts the contents of the page in |web_contents| to its original 96 // Reverts the contents of the page in |web_contents| to its original
98 // language. 97 // language.
99 void RevertTranslation(content::WebContents* web_contents); 98 void RevertTranslation(content::WebContents* web_contents);
100 99
101 // Reports to the Google translate server that a page language was incorrectly 100 // Reports to the Google translate server that a page language was incorrectly
102 // detected. This call is initiated by the user selecting the "report" menu 101 // detected. This call is initiated by the user selecting the "report" menu
103 // under options in the translate infobar. 102 // under options in the translate infobar.
104 void ReportLanguageDetectionError(content::WebContents* web_contents); 103 void ReportLanguageDetectionError(content::WebContents* web_contents);
105 104
106 // Clears the translate script, so it will be fetched next time we translate. 105 // Clears the translate script, so it will be fetched next time we translate.
107 void ClearTranslateScript() { translate_script_.clear(); } 106 void ClearTranslateScript();
108 107
109 // content::NotificationObserver implementation: 108 // content::NotificationObserver implementation:
110 virtual void Observe(int type, 109 virtual void Observe(int type,
111 const content::NotificationSource& source, 110 const content::NotificationSource& source,
112 const content::NotificationDetails& details) OVERRIDE; 111 const content::NotificationDetails& details) OVERRIDE;
113 112
114 // net::URLFetcherDelegate implementation:
115 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
116
117 // Used by unit-tests to override some defaults: 113 // Used by unit-tests to override some defaults:
118 // Delay after which the translate script is fetched again from the 114 // Delay after which the translate script is fetched again from the
119 // translation server. 115 // translation server.
120 void set_translate_script_expiration_delay(int delay_ms) { 116 void SetTranslateScriptExpirationDelay(int delay_ms);
121 translate_script_expiration_delay_ =
122 base::TimeDelta::FromMilliseconds(delay_ms);
123 }
124 117
125 // Number of attempts before waiting for a page to be fully reloaded. 118 // Number of attempts before waiting for a page to be fully reloaded.
126 void set_translate_max_reload_attemps(int attempts) { 119 void set_translate_max_reload_attemps(int attempts) {
127 max_reload_check_attempts_ = attempts; 120 max_reload_check_attempts_ = attempts;
128 } 121 }
129 122
130 // The observer class for TranslateManager. 123 // The observer class for TranslateManager.
131 class Observer { 124 class Observer {
132 public: 125 public:
133 virtual void OnLanguageDetection( 126 virtual void OnLanguageDetection(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 // Sends a translation request to the RenderView of |web_contents|. 168 // Sends a translation request to the RenderView of |web_contents|.
176 void DoTranslatePage(content::WebContents* web_contents, 169 void DoTranslatePage(content::WebContents* web_contents,
177 const std::string& translate_script, 170 const std::string& translate_script,
178 const std::string& source_lang, 171 const std::string& source_lang,
179 const std::string& target_lang); 172 const std::string& target_lang);
180 173
181 // Shows the after translate or error infobar depending on the details. 174 // Shows the after translate or error infobar depending on the details.
182 void PageTranslated(content::WebContents* web_contents, 175 void PageTranslated(content::WebContents* web_contents,
183 PageTranslatedDetails* details); 176 PageTranslatedDetails* details);
184 177
185 // Fetches the JS translate script (the script that is injected in the page 178 void OnTranslateScriptFetchComplete(bool success, const std::string& data);
186 // to translate it).
187 void RequestTranslateScript();
188 179
189 // Notifies to the observers when a language is detected. 180 // Notifies to the observers when a language is detected.
190 void NotifyLanguageDetection(const LanguageDetectionDetails& details); 181 void NotifyLanguageDetection(const LanguageDetectionDetails& details);
191 182
192 // Notifies to the observers when translate failed. 183 // Notifies to the observers when translate failed.
193 void NotifyTranslateError(const TranslateErrorDetails& details); 184 void NotifyTranslateError(const TranslateErrorDetails& details);
194 185
195 // Returns the language to translate to. The language returned is the 186 // Returns the language to translate to. The language returned is the
196 // first language found in the following list that is supported by the 187 // first language found in the following list that is supported by the
197 // translation service: 188 // translation service:
198 // the UI language 189 // the UI language
199 // the accept-language list 190 // the accept-language list
200 // If no language is found then an empty string is returned. 191 // If no language is found then an empty string is returned.
201 static std::string GetTargetLanguage(PrefService* prefs); 192 static std::string GetTargetLanguage(PrefService* prefs);
202 193
203 // Returns the different parameters used to decide whether extra shortcuts 194 // Returns the different parameters used to decide whether extra shortcuts
204 // are needed. 195 // are needed.
205 static ShortcutConfiguration ShortcutConfig(); 196 static ShortcutConfiguration ShortcutConfig();
206 197
207 content::NotificationRegistrar notification_registrar_; 198 content::NotificationRegistrar notification_registrar_;
208 199
209 base::WeakPtrFactory<TranslateManager> weak_method_factory_; 200 base::WeakPtrFactory<TranslateManager> weak_method_factory_;
210 201
211 // The JS injected in the page to do the translation.
212 std::string translate_script_;
213
214 // Delay after which the translate script is fetched again
215 // from the translate server.
216 base::TimeDelta translate_script_expiration_delay_;
217
218 // Max number of attempts before checking if a page has been reloaded. 202 // Max number of attempts before checking if a page has been reloaded.
219 int max_reload_check_attempts_; 203 int max_reload_check_attempts_;
220 204
221 // Set when the translate JS is currently being retrieved. NULL otherwise.
222 scoped_ptr<net::URLFetcher> translate_script_request_pending_;
223
224 // The list of pending translate requests. Translate requests are queued when 205 // The list of pending translate requests. Translate requests are queued when
225 // the translate script is not ready and has to be fetched from the translate 206 // the translate script is not ready and has to be fetched from the translate
226 // server. 207 // server.
227 std::vector<PendingRequest> pending_requests_; 208 std::vector<PendingRequest> pending_requests_;
228 209
229 // List of registered observers. 210 // List of registered observers.
230 ObserverList<Observer> observer_list_; 211 ObserverList<Observer> observer_list_;
231 212
232 // An instance of TranslateLanguageList which manages supported language list. 213 // An instance of TranslateLanguageList which manages supported language list.
233 scoped_ptr<TranslateLanguageList> language_list_; 214 scoped_ptr<TranslateLanguageList> language_list_;
234 215
216 // An instance of TranslateScript which manages JavaScript source for
217 // Translate.
218 scoped_ptr<TranslateScript> script_;
219
235 // An instance of TranslateAcceptLanguages which manages Accept languages of 220 // An instance of TranslateAcceptLanguages which manages Accept languages of
236 // each profiles. 221 // each profiles.
237 scoped_ptr<TranslateAcceptLanguages> accept_languages_; 222 scoped_ptr<TranslateAcceptLanguages> accept_languages_;
238 223
239 DISALLOW_COPY_AND_ASSIGN(TranslateManager); 224 DISALLOW_COPY_AND_ASSIGN(TranslateManager);
240 }; 225 };
241 226
242 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ 227 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
OLDNEW
« no previous file with comments | « chrome/browser/translate/translate_language_list.cc ('k') | chrome/browser/translate/translate_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698