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 #include "chrome/browser/translate/translate_manager.h" | 5 #include "chrome/browser/translate/translate_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/memory/singleton.h" | |
10 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
11 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
12 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
13 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
14 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
15 #include "base/time/time.h" | 14 #include "base/time/time.h" |
16 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/tab_contents/tab_util.h" | 18 #include "chrome/browser/tab_contents/tab_util.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 // Notifies |g_callback_list_| of translate errors. | 82 // Notifies |g_callback_list_| of translate errors. |
84 void NotifyTranslateError(const TranslateErrorDetails& details) { | 83 void NotifyTranslateError(const TranslateErrorDetails& details) { |
85 if (!g_callback_list_) | 84 if (!g_callback_list_) |
86 return; | 85 return; |
87 | 86 |
88 g_callback_list_->Notify(details); | 87 g_callback_list_->Notify(details); |
89 } | 88 } |
90 | 89 |
91 } // namespace | 90 } // namespace |
92 | 91 |
93 TranslateManager::~TranslateManager() { | 92 TranslateManager::~TranslateManager() {} |
94 } | |
95 | |
96 // static | |
97 TranslateManager* TranslateManager::GetInstance() { | |
98 return Singleton<TranslateManager>::get(); | |
99 } | |
100 | 93 |
101 // static | 94 // static |
102 bool TranslateManager::IsTranslatableURL(const GURL& url) { | 95 bool TranslateManager::IsTranslatableURL(const GURL& url) { |
103 // A URLs is translatable unless it is one of the following: | 96 // A URLs is translatable unless it is one of the following: |
104 // - empty (can happen for popups created with window.open("")) | 97 // - empty (can happen for popups created with window.open("")) |
105 // - an internal URL (chrome:// and others) | 98 // - an internal URL (chrome:// and others) |
106 // - the devtools (which is considered UI) | 99 // - the devtools (which is considered UI) |
107 // - Chrome OS file manager extension | 100 // - Chrome OS file manager extension |
108 // - an FTP page (as FTP pages tend to have long lists of filenames that may | 101 // - an FTP page (as FTP pages tend to have long lists of filenames that may |
109 // confuse the CLD) | 102 // confuse the CLD) |
110 return !url.is_empty() && | 103 return !url.is_empty() && |
111 !url.SchemeIs(content::kChromeUIScheme) && | 104 !url.SchemeIs(content::kChromeUIScheme) && |
112 !url.SchemeIs(content::kChromeDevToolsScheme) && | 105 !url.SchemeIs(content::kChromeDevToolsScheme) && |
113 #if defined(OS_CHROMEOS) | 106 #if defined(OS_CHROMEOS) |
114 !(url.SchemeIs(extensions::kExtensionScheme) && | 107 !(url.SchemeIs(extensions::kExtensionScheme) && |
115 url.DomainIs(file_manager::kFileManagerAppId)) && | 108 url.DomainIs(file_manager::kFileManagerAppId)) && |
116 #endif | 109 #endif |
117 !url.SchemeIs(content::kFtpScheme); | 110 !url.SchemeIs(content::kFtpScheme); |
118 } | 111 } |
119 | 112 |
120 void TranslateManager::Observe(int type, | 113 void TranslateManager::Observe(int type, |
121 const content::NotificationSource& source, | 114 const content::NotificationSource& source, |
122 const content::NotificationDetails& details) { | 115 const content::NotificationDetails& details) { |
123 switch (type) { | 116 switch (type) { |
124 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { | 117 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { |
125 NavigationController* controller = | 118 NavigationController* controller = |
126 content::Source<NavigationController>(source).ptr(); | 119 content::Source<NavigationController>(source).ptr(); |
120 DCHECK_EQ(&translate_tab_helper_->GetWebContents()->GetController(), | |
121 controller); | |
127 content::LoadCommittedDetails* load_details = | 122 content::LoadCommittedDetails* load_details = |
128 content::Details<content::LoadCommittedDetails>(details).ptr(); | 123 content::Details<content::LoadCommittedDetails>(details).ptr(); |
129 NavigationEntry* entry = controller->GetActiveEntry(); | 124 NavigationEntry* entry = controller->GetActiveEntry(); |
130 if (!entry) { | 125 if (!entry) { |
131 NOTREACHED(); | 126 NOTREACHED(); |
132 return; | 127 return; |
133 } | 128 } |
134 | 129 |
135 TranslateTabHelper* translate_tab_helper = | 130 if (!translate_tab_helper_->GetWebContents()) |
136 TranslateTabHelper::FromWebContents(controller->GetWebContents()); | |
137 if (!translate_tab_helper) | |
138 return; | 131 return; |
139 | 132 |
140 // If the navigation happened while offline don't show the translate | 133 // If the navigation happened while offline don't show the translate |
141 // bar since there will be nothing to translate. | 134 // bar since there will be nothing to translate. |
142 if (load_details->http_status_code == 0 || | 135 if (load_details->http_status_code == 0 || |
143 load_details->http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { | 136 load_details->http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { |
144 return; | 137 return; |
145 } | 138 } |
146 | 139 |
147 if (!load_details->is_main_frame && | 140 if (!load_details->is_main_frame && |
148 translate_tab_helper->GetLanguageState().translation_declined()) { | 141 translate_tab_helper_->GetLanguageState().translation_declined()) { |
149 // Some sites (such as Google map) may trigger sub-frame navigations | 142 // Some sites (such as Google map) may trigger sub-frame navigations |
150 // when the user interacts with the page. We don't want to show a new | 143 // when the user interacts with the page. We don't want to show a new |
151 // infobar if the user already dismissed one in that case. | 144 // infobar if the user already dismissed one in that case. |
152 return; | 145 return; |
153 } | 146 } |
154 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && | 147 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && |
155 load_details->type != content::NAVIGATION_TYPE_SAME_PAGE) { | 148 load_details->type != content::NAVIGATION_TYPE_SAME_PAGE) { |
156 return; | 149 return; |
157 } | 150 } |
158 | 151 |
159 // When doing a page reload, TAB_LANGUAGE_DETERMINED is not sent, | 152 // When doing a page reload, TAB_LANGUAGE_DETERMINED is not sent, |
160 // so the translation needs to be explicitly initiated, but only when the | 153 // so the translation needs to be explicitly initiated, but only when the |
161 // page needs translation. | 154 // page needs translation. |
162 if (!translate_tab_helper->GetLanguageState().page_needs_translation()) | 155 if (!translate_tab_helper_->GetLanguageState().page_needs_translation()) |
163 return; | 156 return; |
164 // Note that we delay it as the TranslateManager gets this notification | 157 // Note that we delay it as the TranslateManager gets this notification |
165 // before the WebContents and the WebContents processing might remove the | 158 // before the WebContents and the WebContents processing might remove the |
166 // current infobars. Since InitTranslation might add an infobar, it must | 159 // current infobars. Since InitTranslation might add an infobar, it must |
167 // be done after that. | 160 // be done after that. |
168 base::MessageLoop::current()->PostTask(FROM_HERE, | 161 base::MessageLoop::current()->PostTask( |
162 FROM_HERE, | |
169 base::Bind( | 163 base::Bind( |
170 &TranslateManager::InitiateTranslationPosted, | 164 &TranslateManager::InitiateTranslationPosted, |
171 weak_method_factory_.GetWeakPtr(), | 165 weak_method_factory_.GetWeakPtr(), |
172 controller->GetWebContents()->GetRenderProcessHost()->GetID(), | 166 translate_tab_helper_->GetLanguageState().original_language(), |
173 controller->GetWebContents()->GetRenderViewHost()->GetRoutingID(), | 167 0)); |
174 translate_tab_helper->GetLanguageState().original_language(), 0)); | |
175 break; | 168 break; |
176 } | 169 } |
177 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { | 170 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { |
178 const LanguageDetectionDetails* lang_det_details = | 171 const LanguageDetectionDetails* lang_det_details = |
179 content::Details<const LanguageDetectionDetails>(details).ptr(); | 172 content::Details<const LanguageDetectionDetails>(details).ptr(); |
180 | 173 |
181 WebContents* tab = content::Source<WebContents>(source).ptr(); | 174 WebContents* tab = content::Source<WebContents>(source).ptr(); |
175 DCHECK_EQ(translate_tab_helper_->GetWebContents(), tab); | |
182 | 176 |
183 // We may get this notifications multiple times. Make sure to translate | 177 // We may get this notifications multiple times. Make sure to translate |
blundell
2014/02/18 09:21:46
Orthogonal to your CL, but this comment seems misp
droger
2014/02/18 10:16:51
Done.
| |
184 // only once. | 178 // only once. |
185 TranslateTabHelper* translate_tab_helper = | 179 if (!translate_tab_helper_->GetWebContents()) |
186 TranslateTabHelper::FromWebContents(tab); | |
187 if (!translate_tab_helper) | |
188 return; | 180 return; |
189 | 181 |
190 LanguageState& language_state = translate_tab_helper->GetLanguageState(); | 182 LanguageState& language_state = translate_tab_helper_->GetLanguageState(); |
191 if (language_state.page_needs_translation() && | 183 if (language_state.page_needs_translation() && |
192 !language_state.translation_pending() && | 184 !language_state.translation_pending() && |
193 !language_state.translation_declined() && | 185 !language_state.translation_declined() && |
194 !language_state.IsPageTranslated()) { | 186 !language_state.IsPageTranslated()) { |
195 std::string language = lang_det_details->adopted_language; | 187 std::string language = lang_det_details->adopted_language; |
196 InitiateTranslation(tab, language); | 188 InitiateTranslation(language); |
197 } | 189 } |
198 break; | 190 break; |
199 } | 191 } |
200 case chrome::NOTIFICATION_PAGE_TRANSLATED: { | 192 case chrome::NOTIFICATION_PAGE_TRANSLATED: { |
201 // Only add translate infobar if it doesn't exist; if it already exists, | 193 // Only add translate infobar if it doesn't exist; if it already exists, |
202 // just update the state, the actual infobar would have received the same | 194 // just update the state, the actual infobar would have received the same |
203 // notification and update the visual display accordingly. | 195 // notification and update the visual display accordingly. |
204 WebContents* tab = content::Source<WebContents>(source).ptr(); | |
205 PageTranslatedDetails* page_translated_details = | 196 PageTranslatedDetails* page_translated_details = |
206 content::Details<PageTranslatedDetails>(details).ptr(); | 197 content::Details<PageTranslatedDetails>(details).ptr(); |
207 PageTranslated(tab, page_translated_details); | 198 PageTranslated(page_translated_details); |
208 break; | 199 break; |
209 } | 200 } |
210 default: | 201 default: |
211 NOTREACHED(); | 202 NOTREACHED(); |
212 } | 203 } |
213 } | 204 } |
214 | 205 |
215 // static | 206 // static |
216 scoped_ptr<TranslateManager::TranslateErrorCallbackList::Subscription> | 207 scoped_ptr<TranslateManager::TranslateErrorCallbackList::Subscription> |
217 TranslateManager::RegisterTranslateErrorCallback( | 208 TranslateManager::RegisterTranslateErrorCallback( |
218 const TranslateManager::TranslateErrorCallback& callback) { | 209 const TranslateManager::TranslateErrorCallback& callback) { |
219 if (!g_callback_list_) | 210 if (!g_callback_list_) |
220 g_callback_list_ = new TranslateErrorCallbackList; | 211 g_callback_list_ = new TranslateErrorCallbackList; |
221 return g_callback_list_->Add(callback); | 212 return g_callback_list_->Add(callback); |
222 } | 213 } |
223 | 214 |
224 TranslateManager::TranslateManager() | 215 TranslateManager::TranslateManager(TranslateTabHelper* helper) |
225 : max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), | 216 : max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), |
226 weak_method_factory_(this) { | 217 translate_tab_helper_(helper), |
227 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 218 weak_method_factory_(this) { |
228 content::NotificationService::AllSources()); | 219 |
220 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
221 | |
222 notification_registrar_.Add( | |
223 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
224 content::Source<NavigationController>(&web_contents->GetController())); | |
229 notification_registrar_.Add(this, | 225 notification_registrar_.Add(this, |
230 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 226 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
231 content::NotificationService::AllSources()); | 227 content::Source<WebContents>(web_contents)); |
232 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, | 228 notification_registrar_.Add(this, |
233 content::NotificationService::AllSources()); | 229 chrome::NOTIFICATION_PAGE_TRANSLATED, |
230 content::Source<WebContents>(web_contents)); | |
234 } | 231 } |
235 | 232 |
236 void TranslateManager::InitiateTranslation(WebContents* web_contents, | 233 void TranslateManager::InitiateTranslation(const std::string& page_lang) { |
237 const std::string& page_lang) { | 234 WebContents* web_contents = translate_tab_helper_->GetWebContents(); |
238 TranslateTabHelper* translate_tab_helper = | 235 if (!web_contents) |
239 TranslateTabHelper::FromWebContents(web_contents); | |
240 if (!translate_tab_helper) | |
241 return; | 236 return; |
242 | 237 |
243 Profile* profile = | 238 Profile* profile = |
244 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 239 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
245 Profile* original_profile = profile->GetOriginalProfile(); | 240 Profile* original_profile = profile->GetOriginalProfile(); |
246 PrefService* prefs = original_profile->GetPrefs(); | 241 PrefService* prefs = original_profile->GetPrefs(); |
247 if (!prefs->GetBoolean(prefs::kEnableTranslate)) { | 242 if (!prefs->GetBoolean(prefs::kEnableTranslate)) { |
248 TranslateBrowserMetrics::ReportInitiationStatus( | 243 TranslateBrowserMetrics::ReportInitiationStatus( |
249 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS); | 244 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS); |
250 const std::string& locale = g_browser_process->GetApplicationLocale(); | 245 const std::string& locale = g_browser_process->GetApplicationLocale(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 | 317 |
323 // If the user has previously selected "always translate" for this language we | 318 // If the user has previously selected "always translate" for this language we |
324 // automatically translate. Note that in incognito mode we disable that | 319 // automatically translate. Note that in incognito mode we disable that |
325 // feature; the user will get an infobar, so they can control whether the | 320 // feature; the user will get an infobar, so they can control whether the |
326 // page's text is sent to the translate server. | 321 // page's text is sent to the translate server. |
327 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { | 322 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { |
328 std::string auto_target_lang = GetAutoTargetLanguage(language_code, prefs); | 323 std::string auto_target_lang = GetAutoTargetLanguage(language_code, prefs); |
329 if (!auto_target_lang.empty()) { | 324 if (!auto_target_lang.empty()) { |
330 TranslateBrowserMetrics::ReportInitiationStatus( | 325 TranslateBrowserMetrics::ReportInitiationStatus( |
331 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG); | 326 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG); |
332 TranslatePage(web_contents, language_code, auto_target_lang); | 327 TranslatePage(language_code, auto_target_lang); |
333 return; | 328 return; |
334 } | 329 } |
335 } | 330 } |
336 | 331 |
337 LanguageState& language_state = translate_tab_helper->GetLanguageState(); | 332 LanguageState& language_state = translate_tab_helper_->GetLanguageState(); |
338 std::string auto_translate_to = language_state.AutoTranslateTo(); | 333 std::string auto_translate_to = language_state.AutoTranslateTo(); |
339 if (!auto_translate_to.empty()) { | 334 if (!auto_translate_to.empty()) { |
340 // This page was navigated through a click from a translated page. | 335 // This page was navigated through a click from a translated page. |
341 TranslateBrowserMetrics::ReportInitiationStatus( | 336 TranslateBrowserMetrics::ReportInitiationStatus( |
342 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK); | 337 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK); |
343 TranslatePage(web_contents, language_code, auto_translate_to); | 338 TranslatePage(language_code, auto_translate_to); |
344 return; | 339 return; |
345 } | 340 } |
346 | 341 |
347 TranslateBrowserMetrics::ReportInitiationStatus( | 342 TranslateBrowserMetrics::ReportInitiationStatus( |
348 TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR); | 343 TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR); |
349 | 344 |
350 // Prompts the user if he/she wants the page translated. | 345 // Prompts the user if he/she wants the page translated. |
351 translate_tab_helper->ShowTranslateUI(TranslateTabHelper::BEFORE_TRANSLATE, | 346 translate_tab_helper_->ShowTranslateUI(TranslateTabHelper::BEFORE_TRANSLATE, |
352 web_contents, | 347 language_code, |
353 language_code, | 348 target_lang, |
354 target_lang, | 349 TranslateErrors::NONE); |
355 TranslateErrors::NONE); | |
356 } | 350 } |
357 | 351 |
358 void TranslateManager::InitiateTranslationPosted(int process_id, | 352 void TranslateManager::InitiateTranslationPosted(const std::string& page_lang, |
359 int render_id, | |
360 const std::string& page_lang, | |
361 int attempt) { | 353 int attempt) { |
362 // The tab might have been closed. | 354 WebContents* web_contents = translate_tab_helper_->GetWebContents(); |
363 WebContents* web_contents = | |
364 tab_util::GetWebContentsByID(process_id, render_id); | |
365 if (!web_contents) | 355 if (!web_contents) |
366 return; | 356 return; |
367 | 357 |
368 TranslateTabHelper* translate_tab_helper = | 358 if (translate_tab_helper_->GetLanguageState().translation_pending()) |
369 TranslateTabHelper::FromWebContents(web_contents); | |
370 if (translate_tab_helper->GetLanguageState().translation_pending()) | |
371 return; | 359 return; |
372 | 360 |
373 // During a reload we need web content to be available before the | 361 // During a reload we need web content to be available before the |
374 // translate script is executed. Otherwise we will run the translate script on | 362 // translate script is executed. Otherwise we will run the translate script on |
375 // an empty DOM which will fail. Therefore we wait a bit to see if the page | 363 // an empty DOM which will fail. Therefore we wait a bit to see if the page |
376 // has finished. | 364 // has finished. |
377 if ((web_contents->IsLoading()) && attempt < kMaxTranslateLoadCheckAttempts) { | 365 if ((web_contents->IsLoading()) && attempt < kMaxTranslateLoadCheckAttempts) { |
blundell
2014/02/18 09:21:46
I see, this is why you changed the comment on the
droger
2014/02/18 10:16:51
Yes, Done.
| |
378 int backoff = attempt * max_reload_check_attempts_; | 366 int backoff = attempt * max_reload_check_attempts_; |
379 base::MessageLoop::current()->PostDelayedTask( | 367 base::MessageLoop::current()->PostDelayedTask( |
380 FROM_HERE, base::Bind(&TranslateManager::InitiateTranslationPosted, | 368 FROM_HERE, base::Bind(&TranslateManager::InitiateTranslationPosted, |
381 weak_method_factory_.GetWeakPtr(), process_id, | 369 weak_method_factory_.GetWeakPtr(), |
382 render_id, page_lang, ++attempt), | 370 page_lang, ++attempt), |
383 base::TimeDelta::FromMilliseconds(backoff)); | 371 base::TimeDelta::FromMilliseconds(backoff)); |
384 return; | 372 return; |
385 } | 373 } |
386 | 374 |
387 InitiateTranslation(web_contents, | 375 InitiateTranslation(TranslateDownloadManager::GetLanguageCode(page_lang)); |
388 TranslateDownloadManager::GetLanguageCode(page_lang)); | |
389 } | 376 } |
390 | 377 |
391 void TranslateManager::TranslatePage(WebContents* web_contents, | 378 void TranslateManager::TranslatePage(const std::string& original_source_lang, |
392 const std::string& original_source_lang, | |
393 const std::string& target_lang) { | 379 const std::string& target_lang) { |
380 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
381 DCHECK(web_contents); | |
394 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 382 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
395 if (!entry) { | 383 if (!entry) { |
396 NOTREACHED(); | 384 NOTREACHED(); |
397 return; | 385 return; |
398 } | 386 } |
399 | 387 |
400 // Translation can be kicked by context menu against unsupported languages. | 388 // Translation can be kicked by context menu against unsupported languages. |
401 // Unsupported language strings should be replaced with | 389 // Unsupported language strings should be replaced with |
402 // kUnknownLanguageCode in order to send a translation request with enabling | 390 // kUnknownLanguageCode in order to send a translation request with enabling |
403 // server side auto language detection. | 391 // server side auto language detection. |
404 std::string source_lang(original_source_lang); | 392 std::string source_lang(original_source_lang); |
405 if (!TranslateDownloadManager::IsSupportedLanguage(source_lang)) | 393 if (!TranslateDownloadManager::IsSupportedLanguage(source_lang)) |
406 source_lang = std::string(translate::kUnknownLanguageCode); | 394 source_lang = std::string(translate::kUnknownLanguageCode); |
407 | 395 |
408 TranslateTabHelper* translate_tab_helper = | 396 translate_tab_helper_->ShowTranslateUI(TranslateTabHelper::TRANSLATING, |
409 TranslateTabHelper::FromWebContents(web_contents); | 397 source_lang, |
410 DCHECK(translate_tab_helper); | 398 target_lang, |
411 translate_tab_helper->ShowTranslateUI(TranslateTabHelper::TRANSLATING, | 399 TranslateErrors::NONE); |
412 web_contents, | |
413 source_lang, | |
414 target_lang, | |
415 TranslateErrors::NONE); | |
416 | 400 |
417 TranslateScript* script = TranslateDownloadManager::GetInstance()->script(); | 401 TranslateScript* script = TranslateDownloadManager::GetInstance()->script(); |
418 DCHECK(script != NULL); | 402 DCHECK(script != NULL); |
419 | 403 |
420 const std::string& script_data = script->data(); | 404 const std::string& script_data = script->data(); |
421 if (!script_data.empty()) { | 405 if (!script_data.empty()) { |
422 DoTranslatePage(web_contents, script_data, source_lang, target_lang); | 406 DoTranslatePage(script_data, source_lang, target_lang); |
423 return; | 407 return; |
424 } | 408 } |
425 | 409 |
426 // The script is not available yet. Queue that request and query for the | 410 // The script is not available yet. Queue that request and query for the |
427 // script. Once it is downloaded we'll do the translate. | 411 // script. Once it is downloaded we'll do the translate. |
428 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); | 412 TranslateScript::RequestCallback callback = |
429 PendingRequest request; | 413 base::Bind(&TranslateManager::OnTranslateScriptFetchComplete, |
430 request.render_process_id = rvh->GetProcess()->GetID(); | 414 weak_method_factory_.GetWeakPtr(), |
431 request.render_view_id = rvh->GetRoutingID(); | 415 entry->GetPageID(), |
432 request.page_id = entry->GetPageID(); | 416 source_lang, |
433 request.source_lang = source_lang; | 417 target_lang); |
434 request.target_lang = target_lang; | |
435 | 418 |
436 script->Request(base::Bind(&TranslateManager::OnTranslateScriptFetchComplete, | 419 script->Request(callback); |
437 weak_method_factory_.GetWeakPtr(), request)); | |
438 } | 420 } |
439 | 421 |
440 void TranslateManager::RevertTranslation(WebContents* web_contents) { | 422 void TranslateManager::RevertTranslation() { |
423 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
441 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 424 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
442 if (!entry) { | 425 if (!entry) { |
443 NOTREACHED(); | 426 NOTREACHED(); |
444 return; | 427 return; |
445 } | 428 } |
446 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( | 429 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( |
447 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID())); | 430 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID())); |
448 | 431 |
449 TranslateTabHelper* translate_tab_helper = | 432 translate_tab_helper_->GetLanguageState().SetCurrentLanguage( |
450 TranslateTabHelper::FromWebContents(web_contents); | 433 translate_tab_helper_->GetLanguageState().original_language()); |
451 translate_tab_helper->GetLanguageState().SetCurrentLanguage( | |
452 translate_tab_helper->GetLanguageState().original_language()); | |
453 } | 434 } |
454 | 435 |
455 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { | 436 void TranslateManager::ReportLanguageDetectionError() { |
456 TranslateBrowserMetrics::ReportLanguageDetectionError(); | 437 TranslateBrowserMetrics::ReportLanguageDetectionError(); |
457 // We'll open the URL in a new tab so that the user can tell us more. | 438 // We'll open the URL in a new tab so that the user can tell us more. |
439 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
458 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 440 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
459 if (!browser) { | 441 if (!browser) { |
460 NOTREACHED(); | 442 NOTREACHED(); |
461 return; | 443 return; |
462 } | 444 } |
463 | 445 |
464 GURL report_error_url = GURL(kReportLanguageDetectionErrorURL); | 446 GURL report_error_url = GURL(kReportLanguageDetectionErrorURL); |
465 | 447 |
466 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); | 448 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); |
467 report_error_url = net::AppendQueryParameter( | 449 report_error_url = net::AppendQueryParameter( |
468 report_error_url, | 450 report_error_url, |
469 kUrlQueryName, | 451 kUrlQueryName, |
470 page_url.spec()); | 452 page_url.spec()); |
471 | 453 |
472 TranslateTabHelper* translate_tab_helper = | |
473 TranslateTabHelper::FromWebContents(web_contents); | |
474 report_error_url = net::AppendQueryParameter( | 454 report_error_url = net::AppendQueryParameter( |
475 report_error_url, | 455 report_error_url, |
476 kSourceLanguageQueryName, | 456 kSourceLanguageQueryName, |
477 translate_tab_helper->GetLanguageState().original_language()); | 457 translate_tab_helper_->GetLanguageState().original_language()); |
478 | 458 |
479 report_error_url = TranslateURLUtil::AddHostLocaleToUrl(report_error_url); | 459 report_error_url = TranslateURLUtil::AddHostLocaleToUrl(report_error_url); |
480 report_error_url = TranslateURLUtil::AddApiKeyToUrl(report_error_url); | 460 report_error_url = TranslateURLUtil::AddApiKeyToUrl(report_error_url); |
481 | 461 |
482 chrome::AddSelectedTabWithURL(browser, report_error_url, | 462 chrome::AddSelectedTabWithURL(browser, report_error_url, |
483 content::PAGE_TRANSITION_AUTO_BOOKMARK); | 463 content::PAGE_TRANSITION_AUTO_BOOKMARK); |
484 } | 464 } |
485 | 465 |
486 void TranslateManager::DoTranslatePage(WebContents* web_contents, | 466 void TranslateManager::DoTranslatePage(const std::string& translate_script, |
487 const std::string& translate_script, | |
488 const std::string& source_lang, | 467 const std::string& source_lang, |
489 const std::string& target_lang) { | 468 const std::string& target_lang) { |
469 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
490 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 470 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
491 if (!entry) { | 471 if (!entry) { |
492 NOTREACHED(); | 472 NOTREACHED(); |
493 return; | 473 return; |
494 } | 474 } |
495 | 475 |
496 TranslateTabHelper* translate_tab_helper = | 476 if (!web_contents) |
blundell
2014/02/18 09:21:46
This should be above the check that starts on 470.
| |
497 TranslateTabHelper::FromWebContents(web_contents); | |
498 if (!translate_tab_helper) | |
499 return; | 477 return; |
500 | 478 |
501 translate_tab_helper->GetLanguageState().set_translation_pending(true); | 479 translate_tab_helper_->GetLanguageState().set_translation_pending(true); |
502 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( | 480 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( |
503 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), | 481 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), |
504 translate_script, source_lang, target_lang)); | 482 translate_script, source_lang, target_lang)); |
505 } | 483 } |
506 | 484 |
507 void TranslateManager::PageTranslated(WebContents* web_contents, | 485 void TranslateManager::PageTranslated(PageTranslatedDetails* details) { |
508 PageTranslatedDetails* details) { | |
509 if ((details->error_type == TranslateErrors::NONE) && | 486 if ((details->error_type == TranslateErrors::NONE) && |
510 details->source_language != translate::kUnknownLanguageCode && | 487 details->source_language != translate::kUnknownLanguageCode && |
511 !TranslateDownloadManager::IsSupportedLanguage( | 488 !TranslateDownloadManager::IsSupportedLanguage( |
512 details->source_language)) { | 489 details->source_language)) { |
513 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; | 490 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; |
514 } | 491 } |
515 | 492 |
516 TranslateTabHelper* translate_tab_helper = | 493 DCHECK(translate_tab_helper_->GetWebContents()); |
517 TranslateTabHelper::FromWebContents(web_contents); | |
518 DCHECK(translate_tab_helper); | |
519 translate_tab_helper->ShowTranslateUI(TranslateTabHelper::AFTER_TRANSLATE, | |
520 web_contents, | |
521 details->source_language, | |
522 details->target_language, | |
523 details->error_type); | |
524 | 494 |
495 translate_tab_helper_->ShowTranslateUI(TranslateTabHelper::AFTER_TRANSLATE, | |
496 details->source_language, | |
497 details->target_language, | |
498 details->error_type); | |
499 | |
500 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
525 if (details->error_type != TranslateErrors::NONE && | 501 if (details->error_type != TranslateErrors::NONE && |
526 !web_contents->GetBrowserContext()->IsOffTheRecord()) { | 502 !web_contents->GetBrowserContext()->IsOffTheRecord()) { |
527 TranslateErrorDetails error_details; | 503 TranslateErrorDetails error_details; |
528 error_details.time = base::Time::Now(); | 504 error_details.time = base::Time::Now(); |
529 error_details.url = web_contents->GetLastCommittedURL(); | 505 error_details.url = web_contents->GetLastCommittedURL(); |
530 error_details.error = details->error_type; | 506 error_details.error = details->error_type; |
531 NotifyTranslateError(error_details); | 507 NotifyTranslateError(error_details); |
532 } | 508 } |
533 } | 509 } |
534 | 510 |
535 void TranslateManager::OnTranslateScriptFetchComplete(PendingRequest request, | 511 void TranslateManager::OnTranslateScriptFetchComplete( |
536 bool success, | 512 int page_id, |
537 const std::string& data) { | 513 const std::string& source_lang, |
538 WebContents* web_contents = tab_util::GetWebContentsByID( | 514 const std::string& target_lang, |
539 request.render_process_id, request.render_view_id); | 515 bool success, |
516 const std::string& data) { | |
517 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
540 if (!web_contents) { | 518 if (!web_contents) { |
541 // The tab went away while we were retrieving the script. | 519 // The tab went away while we were retrieving the script. |
542 return; | 520 return; |
543 } | 521 } |
544 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 522 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
545 if (!entry || entry->GetPageID() != request.page_id) { | 523 if (!entry || entry->GetPageID() != page_id) { |
546 // We navigated away from the page the translation was triggered on. | 524 // We navigated away from the page the translation was triggered on. |
547 return; | 525 return; |
548 } | 526 } |
549 | 527 |
550 if (success) { | 528 if (success) { |
551 // Translate the page. | 529 // Translate the page. |
552 TranslateScript* translate_script = | 530 TranslateScript* translate_script = |
553 TranslateDownloadManager::GetInstance()->script(); | 531 TranslateDownloadManager::GetInstance()->script(); |
554 DCHECK(translate_script); | 532 DCHECK(translate_script); |
555 DoTranslatePage( | 533 DoTranslatePage(data, source_lang, target_lang); |
556 web_contents, data, request.source_lang, request.target_lang); | |
557 } else { | 534 } else { |
558 TranslateTabHelper* translate_tab_helper = | 535 translate_tab_helper_->ShowTranslateUI(TranslateTabHelper::TRANSLATE_ERROR, |
559 TranslateTabHelper::FromWebContents(web_contents); | 536 source_lang, |
560 DCHECK(translate_tab_helper); | 537 target_lang, |
561 translate_tab_helper->ShowTranslateUI(TranslateTabHelper::TRANSLATE_ERROR, | 538 TranslateErrors::NETWORK); |
562 web_contents, | |
563 request.source_lang, | |
564 request.target_lang, | |
565 TranslateErrors::NETWORK); | |
566 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { | 539 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { |
567 TranslateErrorDetails error_details; | 540 TranslateErrorDetails error_details; |
568 error_details.time = base::Time::Now(); | 541 error_details.time = base::Time::Now(); |
569 error_details.url = entry->GetURL(); | 542 error_details.url = entry->GetURL(); |
570 error_details.error = TranslateErrors::NETWORK; | 543 error_details.error = TranslateErrors::NETWORK; |
571 NotifyTranslateError(error_details); | 544 NotifyTranslateError(error_details); |
572 } | 545 } |
573 } | 546 } |
574 } | 547 } |
575 | 548 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
611 &auto_target_lang)) { | 584 &auto_target_lang)) { |
612 // We need to confirm that the saved target language is still supported. | 585 // We need to confirm that the saved target language is still supported. |
613 // Also, GetLanguageCode will take care of removing country code if any. | 586 // Also, GetLanguageCode will take care of removing country code if any. |
614 auto_target_lang = | 587 auto_target_lang = |
615 TranslateDownloadManager::GetLanguageCode(auto_target_lang); | 588 TranslateDownloadManager::GetLanguageCode(auto_target_lang); |
616 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) | 589 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) |
617 return auto_target_lang; | 590 return auto_target_lang; |
618 } | 591 } |
619 return std::string(); | 592 return std::string(); |
620 } | 593 } |
OLD | NEW |