| 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 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 } | 458 } |
| 459 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 459 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 460 if (!entry || entry->GetPageID() != request.page_id) { | 460 if (!entry || entry->GetPageID() != request.page_id) { |
| 461 // We navigated away from the page the translation was triggered on. | 461 // We navigated away from the page the translation was triggered on. |
| 462 continue; | 462 continue; |
| 463 } | 463 } |
| 464 | 464 |
| 465 if (error) { | 465 if (error) { |
| 466 Profile* profile = | 466 Profile* profile = |
| 467 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 467 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 468 InfoBarService* infobar_service = | 468 TranslateInfoBarDelegate::Create( |
| 469 InfoBarService::FromWebContents(web_contents); | 469 InfoBarService::FromWebContents(web_contents), |
| 470 ShowInfoBar( | 470 true, |
| 471 web_contents, | 471 TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 472 TranslateInfoBarDelegate::CreateErrorDelegate( | 472 TranslateErrors::NETWORK, |
| 473 TranslateErrors::NETWORK, | 473 profile->GetPrefs(), |
| 474 infobar_service, | 474 request.source_lang, |
| 475 profile->GetPrefs(), | 475 request.target_lang); |
| 476 request.source_lang, | |
| 477 request.target_lang)); | |
| 478 } else { | 476 } else { |
| 479 // Translate the page. | 477 // Translate the page. |
| 480 DoTranslatePage(web_contents, translate_script_, | 478 DoTranslatePage(web_contents, translate_script_, |
| 481 request.source_lang, request.target_lang); | 479 request.source_lang, request.target_lang); |
| 482 } | 480 } |
| 483 } | 481 } |
| 484 pending_requests_.clear(); | 482 pending_requests_.clear(); |
| 485 } else { // if (translate_script_request_pending_.get() == source) | 483 } else { // if (translate_script_request_pending_.get() == source) |
| 486 scoped_ptr<const net::URLFetcher> delete_ptr( | 484 scoped_ptr<const net::URLFetcher> delete_ptr( |
| 487 language_list_request_pending_.release()); | 485 language_list_request_pending_.release()); |
| 488 if (!error) { | 486 if (!error) { |
| 489 std::string data; | 487 std::string data; |
| 490 source->GetResponseAsString(&data); | 488 source->GetResponseAsString(&data); |
| 491 SetSupportedLanguages(data); | 489 SetSupportedLanguages(data); |
| 492 } else { | 490 } else { |
| 493 VLOG(9) << "Failed to Fetch languages from: " << kLanguageListFetchURL; | 491 VLOG(9) << "Failed to Fetch languages from: " << kLanguageListFetchURL; |
| 494 } | 492 } |
| 495 } | 493 } |
| 496 } | 494 } |
| 497 | 495 |
| 498 // static | |
| 499 bool TranslateManager::IsShowingTranslateInfobar(WebContents* web_contents) { | |
| 500 return GetTranslateInfoBarDelegate(web_contents) != NULL; | |
| 501 } | |
| 502 | |
| 503 TranslateManager::TranslateManager() | 496 TranslateManager::TranslateManager() |
| 504 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_method_factory_(this)), | 497 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_method_factory_(this)), |
| 505 translate_script_expiration_delay_( | 498 translate_script_expiration_delay_( |
| 506 base::TimeDelta::FromDays(kTranslateScriptExpirationDelayDays)) { | 499 base::TimeDelta::FromDays(kTranslateScriptExpirationDelayDays)) { |
| 507 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 500 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 508 content::NotificationService::AllSources()); | 501 content::NotificationService::AllSources()); |
| 509 notification_registrar_.Add(this, | 502 notification_registrar_.Add(this, |
| 510 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 503 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 511 content::NotificationService::AllSources()); | 504 content::NotificationService::AllSources()); |
| 512 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, | 505 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 532 // automated browser testing. | 525 // automated browser testing. |
| 533 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) | 526 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) |
| 534 return; | 527 return; |
| 535 | 528 |
| 536 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 529 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 537 if (!entry) { | 530 if (!entry) { |
| 538 // This can happen for popups created with window.open(""). | 531 // This can happen for popups created with window.open(""). |
| 539 return; | 532 return; |
| 540 } | 533 } |
| 541 | 534 |
| 542 // If there is already a translate infobar showing, don't show another one. | |
| 543 if (GetTranslateInfoBarDelegate(web_contents)) | |
| 544 return; | |
| 545 | |
| 546 std::string target_lang = GetTargetLanguage(prefs); | 535 std::string target_lang = GetTargetLanguage(prefs); |
| 547 std::string language_code = GetLanguageCode(page_lang); | 536 std::string language_code = GetLanguageCode(page_lang); |
| 548 // Nothing to do if either the language Chrome is in or the language of the | 537 // Nothing to do if either the language Chrome is in or the language of the |
| 549 // page is not supported by the translation server. | 538 // page is not supported by the translation server. |
| 550 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { | 539 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { |
| 551 return; | 540 return; |
| 552 } | 541 } |
| 553 | 542 |
| 554 // We don't want to translate: | 543 // We don't want to translate: |
| 555 // - any Chrome specific page (New Tab Page, Download, History... pages). | 544 // - any Chrome specific page (New Tab Page, Download, History... pages). |
| (...skipping 29 matching lines...) Expand all Loading... |
| 585 return; | 574 return; |
| 586 | 575 |
| 587 std::string auto_translate_to = | 576 std::string auto_translate_to = |
| 588 translate_tab_helper->language_state().AutoTranslateTo(); | 577 translate_tab_helper->language_state().AutoTranslateTo(); |
| 589 if (!auto_translate_to.empty()) { | 578 if (!auto_translate_to.empty()) { |
| 590 // This page was navigated through a click from a translated page. | 579 // This page was navigated through a click from a translated page. |
| 591 TranslatePage(web_contents, language_code, auto_translate_to); | 580 TranslatePage(web_contents, language_code, auto_translate_to); |
| 592 return; | 581 return; |
| 593 } | 582 } |
| 594 | 583 |
| 595 InfoBarService* infobar_service = | |
| 596 InfoBarService::FromWebContents(web_contents); | |
| 597 // Prompts the user if he/she wants the page translated. | 584 // Prompts the user if he/she wants the page translated. |
| 598 infobar_service->AddInfoBar( | 585 TranslateInfoBarDelegate::Create( |
| 599 TranslateInfoBarDelegate::CreateDelegate( | 586 InfoBarService::FromWebContents(web_contents), false, |
| 600 TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar_service, | 587 TranslateInfoBarDelegate::BEFORE_TRANSLATE, TranslateErrors::NONE, |
| 601 profile->GetPrefs(), language_code, target_lang)); | 588 profile->GetPrefs(), language_code, target_lang); |
| 602 } | 589 } |
| 603 | 590 |
| 604 void TranslateManager::InitiateTranslationPosted( | 591 void TranslateManager::InitiateTranslationPosted( |
| 605 int process_id, int render_id, const std::string& page_lang) { | 592 int process_id, int render_id, const std::string& page_lang) { |
| 606 // The tab might have been closed. | 593 // The tab might have been closed. |
| 607 WebContents* web_contents = | 594 WebContents* web_contents = |
| 608 tab_util::GetWebContentsByID(process_id, render_id); | 595 tab_util::GetWebContentsByID(process_id, render_id); |
| 609 if (!web_contents) | 596 if (!web_contents) |
| 610 return; | 597 return; |
| 611 | 598 |
| 612 TranslateTabHelper* translate_tab_helper = | 599 TranslateTabHelper* translate_tab_helper = |
| 613 TranslateTabHelper::FromWebContents(web_contents); | 600 TranslateTabHelper::FromWebContents(web_contents); |
| 614 if (translate_tab_helper->language_state().translation_pending()) | 601 if (translate_tab_helper->language_state().translation_pending()) |
| 615 return; | 602 return; |
| 616 | 603 |
| 617 InitiateTranslation(web_contents, GetLanguageCode(page_lang)); | 604 InitiateTranslation(web_contents, GetLanguageCode(page_lang)); |
| 618 } | 605 } |
| 619 | 606 |
| 620 void TranslateManager::TranslatePage(WebContents* web_contents, | 607 void TranslateManager::TranslatePage(WebContents* web_contents, |
| 621 const std::string& source_lang, | 608 const std::string& source_lang, |
| 622 const std::string& target_lang) { | 609 const std::string& target_lang) { |
| 623 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 610 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 624 if (!entry) { | 611 if (!entry) { |
| 625 NOTREACHED(); | 612 NOTREACHED(); |
| 626 return; | 613 return; |
| 627 } | 614 } |
| 628 | 615 |
| 629 Profile* profile = | 616 Profile* profile = |
| 630 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 617 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 631 InfoBarService* infobar_service = | 618 TranslateInfoBarDelegate::Create( |
| 632 InfoBarService::FromWebContents(web_contents); | 619 InfoBarService::FromWebContents(web_contents), true, |
| 633 ShowInfoBar(web_contents, TranslateInfoBarDelegate::CreateDelegate( | 620 TranslateInfoBarDelegate::TRANSLATING, TranslateErrors::NONE, |
| 634 TranslateInfoBarDelegate::TRANSLATING, infobar_service, | 621 profile->GetPrefs(), source_lang, target_lang); |
| 635 profile->GetPrefs(), source_lang, target_lang)); | |
| 636 | 622 |
| 637 if (!translate_script_.empty()) { | 623 if (!translate_script_.empty()) { |
| 638 DoTranslatePage(web_contents, translate_script_, source_lang, target_lang); | 624 DoTranslatePage(web_contents, translate_script_, source_lang, target_lang); |
| 639 return; | 625 return; |
| 640 } | 626 } |
| 641 | 627 |
| 642 // The script is not available yet. Queue that request and query for the | 628 // The script is not available yet. Queue that request and query for the |
| 643 // script. Once it is downloaded we'll do the translate. | 629 // script. Once it is downloaded we'll do the translate. |
| 644 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); | 630 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); |
| 645 PendingRequest request; | 631 PendingRequest request; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 return; | 700 return; |
| 715 | 701 |
| 716 translate_tab_helper->language_state().set_translation_pending(true); | 702 translate_tab_helper->language_state().set_translation_pending(true); |
| 717 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( | 703 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( |
| 718 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), | 704 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), |
| 719 translate_script, source_lang, target_lang)); | 705 translate_script, source_lang, target_lang)); |
| 720 } | 706 } |
| 721 | 707 |
| 722 void TranslateManager::PageTranslated(WebContents* web_contents, | 708 void TranslateManager::PageTranslated(WebContents* web_contents, |
| 723 PageTranslatedDetails* details) { | 709 PageTranslatedDetails* details) { |
| 724 InfoBarService* infobar_service = | 710 if ((details->error_type == TranslateErrors::NONE) && |
| 725 InfoBarService::FromWebContents(web_contents); | 711 !IsSupportedLanguage(details->source_language)) { |
| 726 Profile* profile = | |
| 727 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 728 PrefService* prefs = profile->GetPrefs(); | |
| 729 | |
| 730 // Create the new infobar to display. | |
| 731 TranslateInfoBarDelegate* infobar; | |
| 732 if (details->error_type != TranslateErrors::NONE) { | |
| 733 infobar = TranslateInfoBarDelegate::CreateErrorDelegate( | |
| 734 details->error_type, | |
| 735 infobar_service, | |
| 736 prefs, | |
| 737 details->source_language, | |
| 738 details->target_language); | |
| 739 } else if (!IsSupportedLanguage(details->source_language)) { | |
| 740 // TODO(jcivelli): http://crbug.com/9390 We should change the "after | 712 // TODO(jcivelli): http://crbug.com/9390 We should change the "after |
| 741 // translate" infobar to support unknown as the original | 713 // translate" infobar to support unknown as the original |
| 742 // language. | 714 // language. |
| 743 UMA_HISTOGRAM_COUNTS("Translate.ServerReportedUnsupportedLanguage", 1); | 715 UMA_HISTOGRAM_COUNTS("Translate.ServerReportedUnsupportedLanguage", 1); |
| 744 infobar = TranslateInfoBarDelegate::CreateErrorDelegate( | 716 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; |
| 745 TranslateErrors::UNSUPPORTED_LANGUAGE, infobar_service, | |
| 746 prefs, details->source_language, details->target_language); | |
| 747 } else { | |
| 748 infobar = TranslateInfoBarDelegate::CreateDelegate( | |
| 749 TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar_service, | |
| 750 prefs, details->source_language, details->target_language); | |
| 751 } | 717 } |
| 752 ShowInfoBar(web_contents, infobar); | 718 Profile* profile = |
| 719 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 720 PrefService* prefs = profile->GetPrefs(); |
| 721 TranslateInfoBarDelegate::Create( |
| 722 InfoBarService::FromWebContents(web_contents), true, |
| 723 (details->error_type == TranslateErrors::NONE) ? |
| 724 TranslateInfoBarDelegate::AFTER_TRANSLATE : |
| 725 TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 726 details->error_type, prefs, details->source_language, |
| 727 details->target_language); |
| 753 } | 728 } |
| 754 | 729 |
| 755 bool TranslateManager::IsAcceptLanguage(WebContents* web_contents, | 730 bool TranslateManager::IsAcceptLanguage(WebContents* web_contents, |
| 756 const std::string& language) { | 731 const std::string& language) { |
| 757 Profile* profile = | 732 Profile* profile = |
| 758 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 733 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 759 profile = profile->GetOriginalProfile(); | 734 profile = profile->GetOriginalProfile(); |
| 760 PrefService* pref_service = profile->GetPrefs(); | 735 PrefService* pref_service = profile->GetPrefs(); |
| 761 PrefServiceLanguagesMap::const_iterator iter = | 736 PrefServiceLanguagesMap::const_iterator iter = |
| 762 accept_languages_.find(pref_service); | 737 accept_languages_.find(pref_service); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 861 0, translate_script_url, net::URLFetcher::GET, this)); | 836 0, translate_script_url, net::URLFetcher::GET, this)); |
| 862 translate_script_request_pending_->SetLoadFlags( | 837 translate_script_request_pending_->SetLoadFlags( |
| 863 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); | 838 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); |
| 864 translate_script_request_pending_->SetRequestContext( | 839 translate_script_request_pending_->SetRequestContext( |
| 865 g_browser_process->system_request_context()); | 840 g_browser_process->system_request_context()); |
| 866 translate_script_request_pending_->SetExtraRequestHeaders( | 841 translate_script_request_pending_->SetExtraRequestHeaders( |
| 867 kTranslateScriptHeader); | 842 kTranslateScriptHeader); |
| 868 translate_script_request_pending_->Start(); | 843 translate_script_request_pending_->Start(); |
| 869 } | 844 } |
| 870 | 845 |
| 871 void TranslateManager::ShowInfoBar(content::WebContents* web_contents, | |
| 872 TranslateInfoBarDelegate* infobar) { | |
| 873 DCHECK(infobar != NULL); | |
| 874 TranslateInfoBarDelegate* old_infobar = | |
| 875 GetTranslateInfoBarDelegate(web_contents); | |
| 876 infobar->UpdateBackgroundAnimation(old_infobar); | |
| 877 InfoBarService* infobar_service = | |
| 878 InfoBarService::FromWebContents(web_contents); | |
| 879 if (!infobar_service) | |
| 880 return; | |
| 881 if (old_infobar) { | |
| 882 // There already is a translate infobar, simply replace it. | |
| 883 infobar_service->ReplaceInfoBar(old_infobar, infobar); | |
| 884 } else { | |
| 885 infobar_service->AddInfoBar(infobar); | |
| 886 } | |
| 887 } | |
| 888 | |
| 889 // static | 846 // static |
| 890 std::string TranslateManager::GetTargetLanguage(PrefService* prefs) { | 847 std::string TranslateManager::GetTargetLanguage(PrefService* prefs) { |
| 891 std::string ui_lang = | 848 std::string ui_lang = |
| 892 GetLanguageCode(g_browser_process->GetApplicationLocale()); | 849 GetLanguageCode(g_browser_process->GetApplicationLocale()); |
| 893 if (IsSupportedLanguage(ui_lang)) | 850 if (IsSupportedLanguage(ui_lang)) |
| 894 return ui_lang; | 851 return ui_lang; |
| 895 | 852 |
| 896 // Getting the accepted languages list | 853 // Getting the accepted languages list |
| 897 std::string accept_langs_str = prefs->GetString(prefs::kAcceptLanguages); | 854 std::string accept_langs_str = prefs->GetString(prefs::kAcceptLanguages); |
| 898 | 855 |
| 899 std::vector<std::string> accept_langs_list; | 856 std::vector<std::string> accept_langs_list; |
| 900 base::SplitString(accept_langs_str, ',', &accept_langs_list); | 857 base::SplitString(accept_langs_str, ',', &accept_langs_list); |
| 901 | 858 |
| 902 // Will translate to the first supported language on the Accepted Language | 859 // Will translate to the first supported language on the Accepted Language |
| 903 // list or not at all if no such candidate exists | 860 // list or not at all if no such candidate exists |
| 904 std::vector<std::string>::iterator iter; | 861 std::vector<std::string>::iterator iter; |
| 905 for (iter = accept_langs_list.begin(); | 862 for (iter = accept_langs_list.begin(); |
| 906 iter != accept_langs_list.end(); ++iter) { | 863 iter != accept_langs_list.end(); ++iter) { |
| 907 std::string lang_code = GetLanguageCode(*iter); | 864 std::string lang_code = GetLanguageCode(*iter); |
| 908 if (IsSupportedLanguage(lang_code)) | 865 if (IsSupportedLanguage(lang_code)) |
| 909 return lang_code; | 866 return lang_code; |
| 910 } | 867 } |
| 911 return std::string(); | 868 return std::string(); |
| 912 } | 869 } |
| 913 | |
| 914 // static | |
| 915 TranslateInfoBarDelegate* TranslateManager::GetTranslateInfoBarDelegate( | |
| 916 WebContents* web_contents) { | |
| 917 InfoBarService* infobar_service = | |
| 918 InfoBarService::FromWebContents(web_contents); | |
| 919 if (!infobar_service) | |
| 920 return NULL; | |
| 921 | |
| 922 for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { | |
| 923 TranslateInfoBarDelegate* delegate = | |
| 924 infobar_service->GetInfoBarDelegateAt(i)-> | |
| 925 AsTranslateInfoBarDelegate(); | |
| 926 if (delegate) | |
| 927 return delegate; | |
| 928 } | |
| 929 return NULL; | |
| 930 } | |
| OLD | NEW |