| 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 "components/translate/content/renderer/translate_helper.h" | 5 #include "components/translate/content/renderer/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/location.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 12 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/thread_task_runner_handle.h" |
| 15 #include "components/translate/content/common/translate_messages.h" | 17 #include "components/translate/content/common/translate_messages.h" |
| 16 #include "components/translate/content/renderer/renderer_cld_data_provider.h" | 18 #include "components/translate/content/renderer/renderer_cld_data_provider.h" |
| 17 #include "components/translate/content/renderer/renderer_cld_data_provider_facto
ry.h" | 19 #include "components/translate/content/renderer/renderer_cld_data_provider_facto
ry.h" |
| 18 #include "components/translate/content/renderer/renderer_cld_utils.h" | 20 #include "components/translate/content/renderer/renderer_cld_utils.h" |
| 19 #include "components/translate/core/common/translate_constants.h" | 21 #include "components/translate/core/common/translate_constants.h" |
| 20 #include "components/translate/core/common/translate_metrics.h" | 22 #include "components/translate/core/common/translate_metrics.h" |
| 21 #include "components/translate/core/common/translate_util.h" | 23 #include "components/translate/core/common/translate_util.h" |
| 22 #include "components/translate/core/language_detection/language_detection_util.h
" | 24 #include "components/translate/core/language_detection/language_detection_util.h
" |
| 23 #include "content/public/common/content_constants.h" | 25 #include "content/public/common/content_constants.h" |
| 24 #include "content/public/common/url_constants.h" | 26 #include "content/public/common/url_constants.h" |
| (...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 // Notify the browser we are done. | 503 // Notify the browser we are done. |
| 502 render_view()->Send( | 504 render_view()->Send( |
| 503 new ChromeViewHostMsg_PageTranslated(render_view()->GetRoutingID(), | 505 new ChromeViewHostMsg_PageTranslated(render_view()->GetRoutingID(), |
| 504 actual_source_lang, | 506 actual_source_lang, |
| 505 target_lang_, | 507 target_lang_, |
| 506 TranslateErrors::NONE)); | 508 TranslateErrors::NONE)); |
| 507 return; | 509 return; |
| 508 } | 510 } |
| 509 | 511 |
| 510 // The translation is still pending, check again later. | 512 // The translation is still pending, check again later. |
| 511 base::MessageLoop::current()->PostDelayedTask( | 513 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 512 FROM_HERE, | 514 FROM_HERE, base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 513 base::Bind(&TranslateHelper::CheckTranslateStatus, | 515 weak_method_factory_.GetWeakPtr(), page_seq_no), |
| 514 weak_method_factory_.GetWeakPtr(), page_seq_no), | |
| 515 AdjustDelay(kTranslateStatusCheckDelayMs)); | 516 AdjustDelay(kTranslateStatusCheckDelayMs)); |
| 516 } | 517 } |
| 517 | 518 |
| 518 void TranslateHelper::TranslatePageImpl(int page_seq_no, int count) { | 519 void TranslateHelper::TranslatePageImpl(int page_seq_no, int count) { |
| 519 DCHECK_LT(count, kMaxTranslateInitCheckAttempts); | 520 DCHECK_LT(count, kMaxTranslateInitCheckAttempts); |
| 520 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) | 521 if (page_seq_no_ != page_seq_no || !render_view()->GetWebView()) |
| 521 return; | 522 return; |
| 522 | 523 |
| 523 if (!IsTranslateLibReady()) { | 524 if (!IsTranslateLibReady()) { |
| 524 // The library is not ready, try again later, unless we have tried several | 525 // The library is not ready, try again later, unless we have tried several |
| 525 // times unsucessfully already. | 526 // times unsucessfully already. |
| 526 if (++count >= kMaxTranslateInitCheckAttempts) { | 527 if (++count >= kMaxTranslateInitCheckAttempts) { |
| 527 NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR); | 528 NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR); |
| 528 return; | 529 return; |
| 529 } | 530 } |
| 530 base::MessageLoop::current()->PostDelayedTask( | 531 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 531 FROM_HERE, | 532 FROM_HERE, |
| 532 base::Bind(&TranslateHelper::TranslatePageImpl, | 533 base::Bind(&TranslateHelper::TranslatePageImpl, |
| 533 weak_method_factory_.GetWeakPtr(), | 534 weak_method_factory_.GetWeakPtr(), page_seq_no, count), |
| 534 page_seq_no, count), | |
| 535 AdjustDelay(count * kTranslateInitCheckDelayMs)); | 535 AdjustDelay(count * kTranslateInitCheckDelayMs)); |
| 536 return; | 536 return; |
| 537 } | 537 } |
| 538 | 538 |
| 539 // The library is loaded, and ready for translation now. | 539 // The library is loaded, and ready for translation now. |
| 540 // Check JavaScript performance counters for UMA reports. | 540 // Check JavaScript performance counters for UMA reports. |
| 541 ReportTimeToBeReady( | 541 ReportTimeToBeReady( |
| 542 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); | 542 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); |
| 543 ReportTimeToLoad( | 543 ReportTimeToLoad( |
| 544 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); | 544 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); |
| 545 | 545 |
| 546 if (!StartTranslation()) { | 546 if (!StartTranslation()) { |
| 547 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); | 547 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); |
| 548 return; | 548 return; |
| 549 } | 549 } |
| 550 // Check the status of the translation. | 550 // Check the status of the translation. |
| 551 base::MessageLoop::current()->PostDelayedTask( | 551 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 552 FROM_HERE, | 552 FROM_HERE, base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 553 base::Bind(&TranslateHelper::CheckTranslateStatus, | 553 weak_method_factory_.GetWeakPtr(), page_seq_no), |
| 554 weak_method_factory_.GetWeakPtr(), page_seq_no), | |
| 555 AdjustDelay(kTranslateStatusCheckDelayMs)); | 554 AdjustDelay(kTranslateStatusCheckDelayMs)); |
| 556 } | 555 } |
| 557 | 556 |
| 558 void TranslateHelper::NotifyBrowserTranslationFailed( | 557 void TranslateHelper::NotifyBrowserTranslationFailed( |
| 559 TranslateErrors::Type error) { | 558 TranslateErrors::Type error) { |
| 560 translation_pending_ = false; | 559 translation_pending_ = false; |
| 561 // Notify the browser there was an error. | 560 // Notify the browser there was an error. |
| 562 render_view()->Send(new ChromeViewHostMsg_PageTranslated( | 561 render_view()->Send(new ChromeViewHostMsg_PageTranslated( |
| 563 render_view()->GetRoutingID(), source_lang_, target_lang_, error)); | 562 render_view()->GetRoutingID(), source_lang_, target_lang_, error)); |
| 564 } | 563 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 cld_data_provider_->SendCldDataRequest(); | 612 cld_data_provider_->SendCldDataRequest(); |
| 614 | 613 |
| 615 // ... and enqueue another delayed task to call again. This will start a | 614 // ... and enqueue another delayed task to call again. This will start a |
| 616 // chain of polling that will last until the pointer stops being NULL, | 615 // chain of polling that will last until the pointer stops being NULL, |
| 617 // which is the right thing to do. | 616 // which is the right thing to do. |
| 618 // NB: In the great majority of cases, the data file will be available and | 617 // NB: In the great majority of cases, the data file will be available and |
| 619 // the very first delayed task will be a no-op that terminates the chain. | 618 // the very first delayed task will be a no-op that terminates the chain. |
| 620 // It's only while downloading the file that this will chain for a | 619 // It's only while downloading the file that this will chain for a |
| 621 // nontrivial amount of time. | 620 // nontrivial amount of time. |
| 622 // Use a weak pointer to avoid keeping this helper object around forever. | 621 // Use a weak pointer to avoid keeping this helper object around forever. |
| 623 base::MessageLoop::current()->PostDelayedTask( | 622 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 624 FROM_HERE, | 623 FROM_HERE, base::Bind(&TranslateHelper::SendCldDataRequest, |
| 625 base::Bind(&TranslateHelper::SendCldDataRequest, | 624 weak_method_factory_.GetWeakPtr(), |
| 626 weak_method_factory_.GetWeakPtr(), | 625 next_delay_millis, next_delay_millis * 2), |
| 627 next_delay_millis, | |
| 628 next_delay_millis * 2), | |
| 629 base::TimeDelta::FromMilliseconds(delay_millis)); | 626 base::TimeDelta::FromMilliseconds(delay_millis)); |
| 630 } | 627 } |
| 631 | 628 |
| 632 void TranslateHelper::OnCldDataAvailable() { | 629 void TranslateHelper::OnCldDataAvailable() { |
| 633 if (deferred_page_capture_) { | 630 if (deferred_page_capture_) { |
| 634 deferred_page_capture_ = false; // Don't do this a second time. | 631 deferred_page_capture_ = false; // Don't do this a second time. |
| 635 PageCapturedImpl(deferred_page_seq_no_, deferred_contents_); | 632 PageCapturedImpl(deferred_page_seq_no_, deferred_contents_); |
| 636 deferred_page_seq_no_ = -1; // Clean up for sanity | 633 deferred_page_seq_no_ = -1; // Clean up for sanity |
| 637 deferred_contents_.clear(); // Clean up for sanity | 634 deferred_contents_.clear(); // Clean up for sanity |
| 638 } | 635 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 659 // this, everything should start being ON_TIME. This should never run more | 656 // this, everything should start being ON_TIME. This should never run more |
| 660 // than twice in a page load, under any conditions. | 657 // than twice in a page load, under any conditions. |
| 661 // Also note that language detection is triggered off of a delay AFTER the | 658 // Also note that language detection is triggered off of a delay AFTER the |
| 662 // page load completed event has fired, making this very much off the critical | 659 // page load completed event has fired, making this very much off the critical |
| 663 // path. | 660 // path. |
| 664 content::RenderThread::Get()->UpdateHistograms( | 661 content::RenderThread::Get()->UpdateHistograms( |
| 665 content::kHistogramSynchronizerReservedSequenceNumber); | 662 content::kHistogramSynchronizerReservedSequenceNumber); |
| 666 } | 663 } |
| 667 | 664 |
| 668 } // namespace translate | 665 } // namespace translate |
| OLD | NEW |