| 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/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 | 358 |
| 359 // When doing a page reload, TAB_LANGUAGE_DETERMINED is not sent, | 359 // When doing a page reload, TAB_LANGUAGE_DETERMINED is not sent, |
| 360 // so the translation needs to be explicitly initiated, but only when the | 360 // so the translation needs to be explicitly initiated, but only when the |
| 361 // page needs translation. | 361 // page needs translation. |
| 362 if (!translate_tab_helper->language_state().page_needs_translation()) | 362 if (!translate_tab_helper->language_state().page_needs_translation()) |
| 363 return; | 363 return; |
| 364 // Note that we delay it as the TranslateManager gets this notification | 364 // Note that we delay it as the TranslateManager gets this notification |
| 365 // before the WebContents and the WebContents processing might remove the | 365 // before the WebContents and the WebContents processing might remove the |
| 366 // current infobars. Since InitTranslation might add an infobar, it must | 366 // current infobars. Since InitTranslation might add an infobar, it must |
| 367 // be done after that. | 367 // be done after that. |
| 368 MessageLoop::current()->PostTask(FROM_HERE, | 368 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 369 base::Bind( | 369 base::Bind( |
| 370 &TranslateManager::InitiateTranslationPosted, | 370 &TranslateManager::InitiateTranslationPosted, |
| 371 weak_method_factory_.GetWeakPtr(), | 371 weak_method_factory_.GetWeakPtr(), |
| 372 controller->GetWebContents()->GetRenderProcessHost()->GetID(), | 372 controller->GetWebContents()->GetRenderProcessHost()->GetID(), |
| 373 controller->GetWebContents()->GetRenderViewHost()->GetRoutingID(), | 373 controller->GetWebContents()->GetRenderViewHost()->GetRoutingID(), |
| 374 translate_tab_helper->language_state().original_language(), 0)); | 374 translate_tab_helper->language_state().original_language(), 0)); |
| 375 break; | 375 break; |
| 376 } | 376 } |
| 377 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { | 377 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { |
| 378 const LanguageDetectionDetails* lang_det_details = | 378 const LanguageDetectionDetails* lang_det_details = |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 std::string api_key = google_apis::GetAPIKey(); | 453 std::string api_key = google_apis::GetAPIKey(); |
| 454 argument += net::EscapeQueryParamValue(api_key, true); | 454 argument += net::EscapeQueryParamValue(api_key, true); |
| 455 argument += "');\n"; | 455 argument += "');\n"; |
| 456 std::string data; | 456 std::string data; |
| 457 source->GetResponseAsString(&data); | 457 source->GetResponseAsString(&data); |
| 458 translate_script_ += argument + data; | 458 translate_script_ += argument + data; |
| 459 | 459 |
| 460 // We'll expire the cached script after some time, to make sure long | 460 // We'll expire the cached script after some time, to make sure long |
| 461 // running browsers still get fixes that might get pushed with newer | 461 // running browsers still get fixes that might get pushed with newer |
| 462 // scripts. | 462 // scripts. |
| 463 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 463 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 464 base::Bind(&TranslateManager::ClearTranslateScript, | 464 base::Bind(&TranslateManager::ClearTranslateScript, |
| 465 weak_method_factory_.GetWeakPtr()), | 465 weak_method_factory_.GetWeakPtr()), |
| 466 translate_script_expiration_delay_); | 466 translate_script_expiration_delay_); |
| 467 } | 467 } |
| 468 // Process any pending requests. | 468 // Process any pending requests. |
| 469 std::vector<PendingRequest>::const_iterator iter; | 469 std::vector<PendingRequest>::const_iterator iter; |
| 470 for (iter = pending_requests_.begin(); iter != pending_requests_.end(); | 470 for (iter = pending_requests_.begin(); iter != pending_requests_.end(); |
| 471 ++iter) { | 471 ++iter) { |
| 472 const PendingRequest& request = *iter; | 472 const PendingRequest& request = *iter; |
| 473 WebContents* web_contents = | 473 WebContents* web_contents = |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 TranslateTabHelper::FromWebContents(web_contents); | 660 TranslateTabHelper::FromWebContents(web_contents); |
| 661 if (translate_tab_helper->language_state().translation_pending()) | 661 if (translate_tab_helper->language_state().translation_pending()) |
| 662 return; | 662 return; |
| 663 | 663 |
| 664 // During a reload we need web content to be available before the | 664 // During a reload we need web content to be available before the |
| 665 // translate script is executed. Otherwise we will run the translate script on | 665 // translate script is executed. Otherwise we will run the translate script on |
| 666 // an empty DOM which will fail. Therefore we wait a bit to see if the page | 666 // an empty DOM which will fail. Therefore we wait a bit to see if the page |
| 667 // has finished. | 667 // has finished. |
| 668 if ((web_contents->IsLoading()) && attempt < kMaxTranslateLoadCheckAttempts) { | 668 if ((web_contents->IsLoading()) && attempt < kMaxTranslateLoadCheckAttempts) { |
| 669 int backoff = attempt * max_reload_check_attempts_; | 669 int backoff = attempt * max_reload_check_attempts_; |
| 670 MessageLoop::current()->PostDelayedTask( | 670 base::MessageLoop::current()->PostDelayedTask( |
| 671 FROM_HERE, base::Bind(&TranslateManager::InitiateTranslationPosted, | 671 FROM_HERE, base::Bind(&TranslateManager::InitiateTranslationPosted, |
| 672 weak_method_factory_.GetWeakPtr(), process_id, | 672 weak_method_factory_.GetWeakPtr(), process_id, |
| 673 render_id, page_lang, ++attempt), | 673 render_id, page_lang, ++attempt), |
| 674 base::TimeDelta::FromMilliseconds(backoff)); | 674 base::TimeDelta::FromMilliseconds(backoff)); |
| 675 return; | 675 return; |
| 676 } | 676 } |
| 677 | 677 |
| 678 InitiateTranslation(web_contents, GetLanguageCode(page_lang)); | 678 InitiateTranslation(web_contents, GetLanguageCode(page_lang)); |
| 679 } | 679 } |
| 680 | 680 |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 995 // reason so we are more aggressive showing the shortcuts for never translate. | 995 // reason so we are more aggressive showing the shortcuts for never translate. |
| 996 #if defined(OS_ANDROID) | 996 #if defined(OS_ANDROID) |
| 997 config.never_translate_min_count = 1; | 997 config.never_translate_min_count = 1; |
| 998 #else | 998 #else |
| 999 config.never_translate_min_count = 3; | 999 config.never_translate_min_count = 3; |
| 1000 #endif // defined(OS_ANDROID) | 1000 #endif // defined(OS_ANDROID) |
| 1001 | 1001 |
| 1002 config.always_translate_min_count = 3; | 1002 config.always_translate_min_count = 3; |
| 1003 return config; | 1003 return config; |
| 1004 } | 1004 } |
| OLD | NEW |