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/renderer/translate/translate_helper.h" | 5 #include "chrome/renderer/translate/translate_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/common/chrome_constants.h" | |
15 #include "chrome/common/render_messages.h" | 14 #include "chrome/common/render_messages.h" |
16 #include "chrome/common/translate/language_detection_util.h" | |
17 #include "chrome/common/translate/translate_common_metrics.h" | |
18 #include "chrome/common/translate/translate_util.h" | |
19 #include "chrome/renderer/extensions/extension_groups.h" | 15 #include "chrome/renderer/extensions/extension_groups.h" |
20 #include "chrome/renderer/isolated_world_ids.h" | 16 #include "chrome/renderer/isolated_world_ids.h" |
| 17 #include "components/translate/common/translate_constants.h" |
| 18 #include "components/translate/common/translate_metrics.h" |
| 19 #include "components/translate/common/translate_util.h" |
| 20 #include "components/translate/language_detection/language_detection_util.h" |
21 #include "content/public/renderer/render_view.h" | 21 #include "content/public/renderer/render_view.h" |
22 #include "third_party/WebKit/public/web/WebDocument.h" | 22 #include "third_party/WebKit/public/web/WebDocument.h" |
23 #include "third_party/WebKit/public/web/WebElement.h" | 23 #include "third_party/WebKit/public/web/WebElement.h" |
24 #include "third_party/WebKit/public/web/WebFrame.h" | 24 #include "third_party/WebKit/public/web/WebFrame.h" |
25 #include "third_party/WebKit/public/web/WebNode.h" | 25 #include "third_party/WebKit/public/web/WebNode.h" |
26 #include "third_party/WebKit/public/web/WebNodeList.h" | 26 #include "third_party/WebKit/public/web/WebNodeList.h" |
27 #include "third_party/WebKit/public/web/WebScriptSource.h" | 27 #include "third_party/WebKit/public/web/WebScriptSource.h" |
28 #include "third_party/WebKit/public/web/WebView.h" | 28 #include "third_party/WebKit/public/web/WebView.h" |
29 #include "third_party/WebKit/public/web/WebWidget.h" | 29 #include "third_party/WebKit/public/web/WebWidget.h" |
30 #include "url/gurl.h" | 30 #include "url/gurl.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 WebDocument document = main_frame->document(); | 94 WebDocument document = main_frame->document(); |
95 std::string content_language = document.contentLanguage().utf8(); | 95 std::string content_language = document.contentLanguage().utf8(); |
96 WebElement html_element = document.documentElement(); | 96 WebElement html_element = document.documentElement(); |
97 std::string html_lang; | 97 std::string html_lang; |
98 // |html_element| can be null element, e.g. in | 98 // |html_element| can be null element, e.g. in |
99 // BrowserTest.WindowOpenClose. | 99 // BrowserTest.WindowOpenClose. |
100 if (!html_element.isNull()) | 100 if (!html_element.isNull()) |
101 html_lang = html_element.getAttribute("lang").utf8(); | 101 html_lang = html_element.getAttribute("lang").utf8(); |
102 std::string cld_language; | 102 std::string cld_language; |
103 bool is_cld_reliable; | 103 bool is_cld_reliable; |
104 std::string language = LanguageDetectionUtil::DeterminePageLanguage( | 104 std::string language = translate::DeterminePageLanguage( |
105 content_language, html_lang, contents, &cld_language, &is_cld_reliable); | 105 content_language, html_lang, contents, &cld_language, &is_cld_reliable); |
106 | 106 |
107 if (language.empty()) | 107 if (language.empty()) |
108 return; | 108 return; |
109 | 109 |
110 language_determined_time_ = base::TimeTicks::Now(); | 110 language_determined_time_ = base::TimeTicks::Now(); |
111 | 111 |
112 GURL url(document.url()); | 112 GURL url(document.url()); |
113 LanguageDetectionDetails details; | 113 LanguageDetectionDetails details; |
114 details.time = base::Time::Now(); | 114 details.time = base::Time::Now(); |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 return; | 328 return; |
329 | 329 |
330 // Any pending translation is now irrelevant. | 330 // Any pending translation is now irrelevant. |
331 CancelPendingTranslation(); | 331 CancelPendingTranslation(); |
332 | 332 |
333 // Set our states. | 333 // Set our states. |
334 translation_pending_ = true; | 334 translation_pending_ = true; |
335 | 335 |
336 // If the source language is undetermined, we'll let the translate element | 336 // If the source language is undetermined, we'll let the translate element |
337 // detect it. | 337 // detect it. |
338 source_lang_ = (source_lang != chrome::kUnknownLanguageCode) ? | 338 source_lang_ = (source_lang != translate::kUnknownLanguageCode) ? |
339 source_lang : kAutoDetectionLanguage; | 339 source_lang : kAutoDetectionLanguage; |
340 target_lang_ = target_lang; | 340 target_lang_ = target_lang; |
341 | 341 |
342 TranslateCommonMetrics::ReportUserActionDuration(language_determined_time_, | 342 translate::ReportUserActionDuration(language_determined_time_, |
343 base::TimeTicks::Now()); | 343 base::TimeTicks::Now()); |
344 | 344 |
345 GURL url(main_frame->document().url()); | 345 GURL url(main_frame->document().url()); |
346 TranslateCommonMetrics::ReportPageScheme(url.scheme()); | 346 translate::ReportPageScheme(url.scheme()); |
347 | 347 |
348 // Set up v8 isolated world with proper content-security-policy and | 348 // Set up v8 isolated world with proper content-security-policy and |
349 // security-origin. | 349 // security-origin. |
350 WebFrame* frame = GetMainFrame(); | 350 WebFrame* frame = GetMainFrame(); |
351 if (frame) { | 351 if (frame) { |
352 frame->setIsolatedWorldContentSecurityPolicy( | 352 frame->setIsolatedWorldContentSecurityPolicy( |
353 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 353 chrome::ISOLATED_WORLD_ID_TRANSLATE, |
354 WebString::fromUTF8(kContentSecurityPolicy)); | 354 WebString::fromUTF8(kContentSecurityPolicy)); |
355 | 355 |
356 GURL security_origin = TranslateUtil::GetTranslateSecurityOrigin(); | 356 GURL security_origin = translate::GetTranslateSecurityOrigin(); |
357 frame->setIsolatedWorldSecurityOrigin( | 357 frame->setIsolatedWorldSecurityOrigin( |
358 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 358 chrome::ISOLATED_WORLD_ID_TRANSLATE, |
359 WebSecurityOrigin::create(security_origin)); | 359 WebSecurityOrigin::create(security_origin)); |
360 } | 360 } |
361 | 361 |
362 if (!IsTranslateLibAvailable()) { | 362 if (!IsTranslateLibAvailable()) { |
363 // Evaluate the script to add the translation related method to the global | 363 // Evaluate the script to add the translation related method to the global |
364 // context of the page. | 364 // context of the page. |
365 ExecuteScript(translate_script); | 365 ExecuteScript(translate_script); |
366 DCHECK(IsTranslateLibAvailable()); | 366 DCHECK(IsTranslateLibAvailable()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 } | 414 } |
415 | 415 |
416 if (!translation_pending_) { | 416 if (!translation_pending_) { |
417 NOTREACHED(); | 417 NOTREACHED(); |
418 return; | 418 return; |
419 } | 419 } |
420 | 420 |
421 translation_pending_ = false; | 421 translation_pending_ = false; |
422 | 422 |
423 // Check JavaScript performance counters for UMA reports. | 423 // Check JavaScript performance counters for UMA reports. |
424 TranslateCommonMetrics::ReportTimeToTranslate( | 424 translate::ReportTimeToTranslate( |
425 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); | 425 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); |
426 | 426 |
427 // Notify the browser we are done. | 427 // Notify the browser we are done. |
428 render_view()->Send(new ChromeViewHostMsg_PageTranslated( | 428 render_view()->Send(new ChromeViewHostMsg_PageTranslated( |
429 render_view()->GetRoutingID(), render_view()->GetPageId(), | 429 render_view()->GetRoutingID(), render_view()->GetPageId(), |
430 actual_source_lang, target_lang_, TranslateErrors::NONE)); | 430 actual_source_lang, target_lang_, TranslateErrors::NONE)); |
431 return; | 431 return; |
432 } | 432 } |
433 | 433 |
434 // The translation is still pending, check again later. | 434 // The translation is still pending, check again later. |
(...skipping 20 matching lines...) Expand all Loading... |
455 FROM_HERE, | 455 FROM_HERE, |
456 base::Bind(&TranslateHelper::TranslatePageImpl, | 456 base::Bind(&TranslateHelper::TranslatePageImpl, |
457 weak_method_factory_.GetWeakPtr(), | 457 weak_method_factory_.GetWeakPtr(), |
458 count), | 458 count), |
459 AdjustDelay(count * kTranslateInitCheckDelayMs)); | 459 AdjustDelay(count * kTranslateInitCheckDelayMs)); |
460 return; | 460 return; |
461 } | 461 } |
462 | 462 |
463 // The library is loaded, and ready for translation now. | 463 // The library is loaded, and ready for translation now. |
464 // Check JavaScript performance counters for UMA reports. | 464 // Check JavaScript performance counters for UMA reports. |
465 TranslateCommonMetrics::ReportTimeToBeReady( | 465 translate::ReportTimeToBeReady( |
466 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); | 466 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); |
467 TranslateCommonMetrics::ReportTimeToLoad( | 467 translate::ReportTimeToLoad( |
468 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); | 468 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); |
469 | 469 |
470 if (!StartTranslation()) { | 470 if (!StartTranslation()) { |
471 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); | 471 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); |
472 return; | 472 return; |
473 } | 473 } |
474 // Check the status of the translation. | 474 // Check the status of the translation. |
475 base::MessageLoop::current()->PostDelayedTask( | 475 base::MessageLoop::current()->PostDelayedTask( |
476 FROM_HERE, | 476 FROM_HERE, |
477 base::Bind(&TranslateHelper::CheckTranslateStatus, | 477 base::Bind(&TranslateHelper::CheckTranslateStatus, |
(...skipping 12 matching lines...) Expand all Loading... |
490 | 490 |
491 WebFrame* TranslateHelper::GetMainFrame() { | 491 WebFrame* TranslateHelper::GetMainFrame() { |
492 WebView* web_view = render_view()->GetWebView(); | 492 WebView* web_view = render_view()->GetWebView(); |
493 | 493 |
494 // When the tab is going to be closed, the web_view can be NULL. | 494 // When the tab is going to be closed, the web_view can be NULL. |
495 if (!web_view) | 495 if (!web_view) |
496 return NULL; | 496 return NULL; |
497 | 497 |
498 return web_view->mainFrame(); | 498 return web_view->mainFrame(); |
499 } | 499 } |
OLD | NEW |