| 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" |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 bool is_cld_reliable; | 163 bool is_cld_reliable; |
| 164 std::string language = translate::DeterminePageLanguage( | 164 std::string language = translate::DeterminePageLanguage( |
| 165 content_language, html_lang, contents, &cld_language, &is_cld_reliable); | 165 content_language, html_lang, contents, &cld_language, &is_cld_reliable); |
| 166 | 166 |
| 167 if (language.empty()) | 167 if (language.empty()) |
| 168 return; | 168 return; |
| 169 | 169 |
| 170 language_determined_time_ = base::TimeTicks::Now(); | 170 language_determined_time_ = base::TimeTicks::Now(); |
| 171 | 171 |
| 172 GURL url(document.url()); | 172 GURL url(document.url()); |
| 173 LanguageDetectionDetails details; | 173 translate::LanguageDetectionDetails details; |
| 174 details.time = base::Time::Now(); | 174 details.time = base::Time::Now(); |
| 175 details.url = url; | 175 details.url = url; |
| 176 details.content_language = content_language; | 176 details.content_language = content_language; |
| 177 details.cld_language = cld_language; | 177 details.cld_language = cld_language; |
| 178 details.is_cld_reliable = is_cld_reliable; | 178 details.is_cld_reliable = is_cld_reliable; |
| 179 details.html_root_language = html_lang; | 179 details.html_root_language = html_lang; |
| 180 details.adopted_language = language; | 180 details.adopted_language = language; |
| 181 | 181 |
| 182 // TODO(hajimehoshi): If this affects performance, it should be set only if | 182 // TODO(hajimehoshi): If this affects performance, it should be set only if |
| 183 // translate-internals tab exists. | 183 // translate-internals tab exists. |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 | 447 |
| 448 void TranslateHelper::CheckTranslateStatus(int page_seq_no) { | 448 void TranslateHelper::CheckTranslateStatus(int page_seq_no) { |
| 449 // If this is not the same page, the translation has been canceled. If the | 449 // If this is not the same page, the translation has been canceled. If the |
| 450 // view is gone, the page is closing. | 450 // view is gone, the page is closing. |
| 451 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) | 451 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) |
| 452 return; | 452 return; |
| 453 | 453 |
| 454 // First check if there was an error. | 454 // First check if there was an error. |
| 455 if (HasTranslationFailed()) { | 455 if (HasTranslationFailed()) { |
| 456 // TODO(toyoshim): Check |errorCode| of translate.js and notify it here. | 456 // TODO(toyoshim): Check |errorCode| of translate.js and notify it here. |
| 457 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); | 457 NotifyBrowserTranslationFailed( |
| 458 translate::TranslateErrors::TRANSLATION_ERROR); |
| 458 return; // There was an error. | 459 return; // There was an error. |
| 459 } | 460 } |
| 460 | 461 |
| 461 if (HasTranslationFinished()) { | 462 if (HasTranslationFinished()) { |
| 462 std::string actual_source_lang; | 463 std::string actual_source_lang; |
| 463 // Translation was successfull, if it was auto, retrieve the source | 464 // Translation was successfull, if it was auto, retrieve the source |
| 464 // language the Translate Element detected. | 465 // language the Translate Element detected. |
| 465 if (source_lang_ == kAutoDetectionLanguage) { | 466 if (source_lang_ == kAutoDetectionLanguage) { |
| 466 actual_source_lang = GetOriginalPageLanguage(); | 467 actual_source_lang = GetOriginalPageLanguage(); |
| 467 if (actual_source_lang.empty()) { | 468 if (actual_source_lang.empty()) { |
| 468 NotifyBrowserTranslationFailed(TranslateErrors::UNKNOWN_LANGUAGE); | 469 NotifyBrowserTranslationFailed( |
| 470 translate::TranslateErrors::UNKNOWN_LANGUAGE); |
| 469 return; | 471 return; |
| 470 } else if (actual_source_lang == target_lang_) { | 472 } else if (actual_source_lang == target_lang_) { |
| 471 NotifyBrowserTranslationFailed(TranslateErrors::IDENTICAL_LANGUAGES); | 473 NotifyBrowserTranslationFailed( |
| 474 translate::TranslateErrors::IDENTICAL_LANGUAGES); |
| 472 return; | 475 return; |
| 473 } | 476 } |
| 474 } else { | 477 } else { |
| 475 actual_source_lang = source_lang_; | 478 actual_source_lang = source_lang_; |
| 476 } | 479 } |
| 477 | 480 |
| 478 if (!translation_pending_) { | 481 if (!translation_pending_) { |
| 479 NOTREACHED(); | 482 NOTREACHED(); |
| 480 return; | 483 return; |
| 481 } | 484 } |
| 482 | 485 |
| 483 translation_pending_ = false; | 486 translation_pending_ = false; |
| 484 | 487 |
| 485 // Check JavaScript performance counters for UMA reports. | 488 // Check JavaScript performance counters for UMA reports. |
| 486 translate::ReportTimeToTranslate( | 489 translate::ReportTimeToTranslate( |
| 487 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); | 490 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); |
| 488 | 491 |
| 489 // Notify the browser we are done. | 492 // Notify the browser we are done. |
| 490 render_view()->Send(new ChromeViewHostMsg_PageTranslated( | 493 render_view()->Send( |
| 491 render_view()->GetRoutingID(), actual_source_lang, target_lang_, | 494 new ChromeViewHostMsg_PageTranslated(render_view()->GetRoutingID(), |
| 492 TranslateErrors::NONE)); | 495 actual_source_lang, |
| 496 target_lang_, |
| 497 translate::TranslateErrors::NONE)); |
| 493 return; | 498 return; |
| 494 } | 499 } |
| 495 | 500 |
| 496 // The translation is still pending, check again later. | 501 // The translation is still pending, check again later. |
| 497 base::MessageLoop::current()->PostDelayedTask( | 502 base::MessageLoop::current()->PostDelayedTask( |
| 498 FROM_HERE, | 503 FROM_HERE, |
| 499 base::Bind(&TranslateHelper::CheckTranslateStatus, | 504 base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 500 weak_method_factory_.GetWeakPtr(), page_seq_no), | 505 weak_method_factory_.GetWeakPtr(), page_seq_no), |
| 501 AdjustDelay(kTranslateStatusCheckDelayMs)); | 506 AdjustDelay(kTranslateStatusCheckDelayMs)); |
| 502 } | 507 } |
| 503 | 508 |
| 504 void TranslateHelper::TranslatePageImpl(int page_seq_no, int count) { | 509 void TranslateHelper::TranslatePageImpl(int page_seq_no, int count) { |
| 505 DCHECK_LT(count, kMaxTranslateInitCheckAttempts); | 510 DCHECK_LT(count, kMaxTranslateInitCheckAttempts); |
| 506 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) | 511 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) |
| 507 return; | 512 return; |
| 508 | 513 |
| 509 if (!IsTranslateLibReady()) { | 514 if (!IsTranslateLibReady()) { |
| 510 // The library is not ready, try again later, unless we have tried several | 515 // The library is not ready, try again later, unless we have tried several |
| 511 // times unsucessfully already. | 516 // times unsucessfully already. |
| 512 if (++count >= kMaxTranslateInitCheckAttempts) { | 517 if (++count >= kMaxTranslateInitCheckAttempts) { |
| 513 NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR); | 518 NotifyBrowserTranslationFailed( |
| 519 translate::TranslateErrors::INITIALIZATION_ERROR); |
| 514 return; | 520 return; |
| 515 } | 521 } |
| 516 base::MessageLoop::current()->PostDelayedTask( | 522 base::MessageLoop::current()->PostDelayedTask( |
| 517 FROM_HERE, | 523 FROM_HERE, |
| 518 base::Bind(&TranslateHelper::TranslatePageImpl, | 524 base::Bind(&TranslateHelper::TranslatePageImpl, |
| 519 weak_method_factory_.GetWeakPtr(), | 525 weak_method_factory_.GetWeakPtr(), |
| 520 page_seq_no, count), | 526 page_seq_no, count), |
| 521 AdjustDelay(count * kTranslateInitCheckDelayMs)); | 527 AdjustDelay(count * kTranslateInitCheckDelayMs)); |
| 522 return; | 528 return; |
| 523 } | 529 } |
| 524 | 530 |
| 525 // The library is loaded, and ready for translation now. | 531 // The library is loaded, and ready for translation now. |
| 526 // Check JavaScript performance counters for UMA reports. | 532 // Check JavaScript performance counters for UMA reports. |
| 527 translate::ReportTimeToBeReady( | 533 translate::ReportTimeToBeReady( |
| 528 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); | 534 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); |
| 529 translate::ReportTimeToLoad( | 535 translate::ReportTimeToLoad( |
| 530 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); | 536 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); |
| 531 | 537 |
| 532 if (!StartTranslation()) { | 538 if (!StartTranslation()) { |
| 533 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); | 539 NotifyBrowserTranslationFailed( |
| 540 translate::TranslateErrors::TRANSLATION_ERROR); |
| 534 return; | 541 return; |
| 535 } | 542 } |
| 536 // Check the status of the translation. | 543 // Check the status of the translation. |
| 537 base::MessageLoop::current()->PostDelayedTask( | 544 base::MessageLoop::current()->PostDelayedTask( |
| 538 FROM_HERE, | 545 FROM_HERE, |
| 539 base::Bind(&TranslateHelper::CheckTranslateStatus, | 546 base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 540 weak_method_factory_.GetWeakPtr(), page_seq_no), | 547 weak_method_factory_.GetWeakPtr(), page_seq_no), |
| 541 AdjustDelay(kTranslateStatusCheckDelayMs)); | 548 AdjustDelay(kTranslateStatusCheckDelayMs)); |
| 542 } | 549 } |
| 543 | 550 |
| 544 void TranslateHelper::NotifyBrowserTranslationFailed( | 551 void TranslateHelper::NotifyBrowserTranslationFailed( |
| 545 TranslateErrors::Type error) { | 552 translate::TranslateErrors::Type error) { |
| 546 translation_pending_ = false; | 553 translation_pending_ = false; |
| 547 // Notify the browser there was an error. | 554 // Notify the browser there was an error. |
| 548 render_view()->Send(new ChromeViewHostMsg_PageTranslated( | 555 render_view()->Send(new ChromeViewHostMsg_PageTranslated( |
| 549 render_view()->GetRoutingID(), source_lang_, target_lang_, error)); | 556 render_view()->GetRoutingID(), source_lang_, target_lang_, error)); |
| 550 } | 557 } |
| 551 | 558 |
| 552 WebFrame* TranslateHelper::GetMainFrame() { | 559 WebFrame* TranslateHelper::GetMainFrame() { |
| 553 WebView* web_view = render_view()->GetWebView(); | 560 WebView* web_view = render_view()->GetWebView(); |
| 554 | 561 |
| 555 // When the tab is going to be closed, the web_view can be NULL. | 562 // When the tab is going to be closed, the web_view can be NULL. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 } | 608 } |
| 602 | 609 |
| 603 void TranslateHelper::OnCldDataAvailable() { | 610 void TranslateHelper::OnCldDataAvailable() { |
| 604 if (deferred_page_capture_) { | 611 if (deferred_page_capture_) { |
| 605 deferred_page_capture_ = false; // Don't do this a second time. | 612 deferred_page_capture_ = false; // Don't do this a second time. |
| 606 PageCapturedImpl(deferred_page_seq_no_, deferred_contents_); | 613 PageCapturedImpl(deferred_page_seq_no_, deferred_contents_); |
| 607 deferred_page_seq_no_ = -1; // Clean up for sanity | 614 deferred_page_seq_no_ = -1; // Clean up for sanity |
| 608 deferred_contents_.clear(); // Clean up for sanity | 615 deferred_contents_.clear(); // Clean up for sanity |
| 609 } | 616 } |
| 610 } | 617 } |
| OLD | NEW |