| 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/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/infobars/infobar_service.h" | 20 #include "chrome/browser/infobars/infobar_service.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/tab_contents/language_state.h" | 22 #include "chrome/browser/tab_contents/language_state.h" |
| 23 #include "chrome/browser/tab_contents/tab_util.h" | 23 #include "chrome/browser/tab_contents/tab_util.h" |
| 24 #include "chrome/browser/translate/page_translated_details.h" | 24 #include "chrome/browser/translate/page_translated_details.h" |
| 25 #include "chrome/browser/translate/translate_accept_languages.h" | 25 #include "chrome/browser/translate/translate_accept_languages.h" |
| 26 #include "chrome/browser/translate/translate_browser_metrics.h" |
| 26 #include "chrome/browser/translate/translate_error_details.h" | 27 #include "chrome/browser/translate/translate_error_details.h" |
| 27 #include "chrome/browser/translate/translate_event_details.h" | 28 #include "chrome/browser/translate/translate_event_details.h" |
| 28 #include "chrome/browser/translate/translate_infobar_delegate.h" | 29 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 29 #include "chrome/browser/translate/translate_language_list.h" | 30 #include "chrome/browser/translate/translate_language_list.h" |
| 30 #include "chrome/browser/translate/translate_manager_metrics.h" | |
| 31 #include "chrome/browser/translate/translate_prefs.h" | 31 #include "chrome/browser/translate/translate_prefs.h" |
| 32 #include "chrome/browser/translate/translate_tab_helper.h" | 32 #include "chrome/browser/translate/translate_tab_helper.h" |
| 33 #include "chrome/browser/translate/translate_url_util.h" | 33 #include "chrome/browser/translate/translate_url_util.h" |
| 34 #include "chrome/browser/ui/browser.h" | 34 #include "chrome/browser/ui/browser.h" |
| 35 #include "chrome/browser/ui/browser_finder.h" | 35 #include "chrome/browser/ui/browser_finder.h" |
| 36 #include "chrome/browser/ui/browser_tabstrip.h" | 36 #include "chrome/browser/ui/browser_tabstrip.h" |
| 37 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 37 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 38 #include "chrome/common/chrome_constants.h" | 38 #include "chrome/common/chrome_constants.h" |
| 39 #include "chrome/common/chrome_notification_types.h" | 39 #include "chrome/common/chrome_notification_types.h" |
| 40 #include "chrome/common/chrome_switches.h" | 40 #include "chrome/common/chrome_switches.h" |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 accept_languages_.reset(new TranslateAcceptLanguages); | 386 accept_languages_.reset(new TranslateAcceptLanguages); |
| 387 } | 387 } |
| 388 | 388 |
| 389 void TranslateManager::InitiateTranslation(WebContents* web_contents, | 389 void TranslateManager::InitiateTranslation(WebContents* web_contents, |
| 390 const std::string& page_lang) { | 390 const std::string& page_lang) { |
| 391 Profile* profile = | 391 Profile* profile = |
| 392 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 392 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 393 Profile* original_profile = profile->GetOriginalProfile(); | 393 Profile* original_profile = profile->GetOriginalProfile(); |
| 394 PrefService* prefs = original_profile->GetPrefs(); | 394 PrefService* prefs = original_profile->GetPrefs(); |
| 395 if (!prefs->GetBoolean(prefs::kEnableTranslate)) { | 395 if (!prefs->GetBoolean(prefs::kEnableTranslate)) { |
| 396 TranslateManagerMetrics::ReportInitiationStatus( | 396 TranslateBrowserMetrics::ReportInitiationStatus( |
| 397 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_PREFS); | 397 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS); |
| 398 const std::string& locale = g_browser_process->GetApplicationLocale(); | 398 const std::string& locale = g_browser_process->GetApplicationLocale(); |
| 399 TranslateManagerMetrics::ReportLocalesOnDisabledByPrefs(locale); | 399 TranslateBrowserMetrics::ReportLocalesOnDisabledByPrefs(locale); |
| 400 return; | 400 return; |
| 401 } | 401 } |
| 402 | 402 |
| 403 // Allow disabling of translate from the command line to assist with | 403 // Allow disabling of translate from the command line to assist with |
| 404 // automated browser testing. | 404 // automated browser testing. |
| 405 if (CommandLine::ForCurrentProcess()->HasSwitch( | 405 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 406 switches::kDisableTranslate)) { | 406 switches::kDisableTranslate)) { |
| 407 TranslateManagerMetrics::ReportInitiationStatus( | 407 TranslateBrowserMetrics::ReportInitiationStatus( |
| 408 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH); | 408 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH); |
| 409 return; | 409 return; |
| 410 } | 410 } |
| 411 | 411 |
| 412 // Don't translate any Chrome specific page, e.g., New Tab Page, Download, | 412 // Don't translate any Chrome specific page, e.g., New Tab Page, Download, |
| 413 // History, and so on. | 413 // History, and so on. |
| 414 GURL page_url = web_contents->GetURL(); | 414 GURL page_url = web_contents->GetURL(); |
| 415 if (!IsTranslatableURL(page_url)) { | 415 if (!IsTranslatableURL(page_url)) { |
| 416 TranslateManagerMetrics::ReportInitiationStatus( | 416 TranslateBrowserMetrics::ReportInitiationStatus( |
| 417 TranslateManagerMetrics::INITIATION_STATUS_URL_IS_NOT_SUPPORTED); | 417 TranslateBrowserMetrics::INITIATION_STATUS_URL_IS_NOT_SUPPORTED); |
| 418 return; | 418 return; |
| 419 } | 419 } |
| 420 | 420 |
| 421 // Don't translate similar languages (ex: en-US to en). | 421 // Don't translate similar languages (ex: en-US to en). |
| 422 std::string target_lang = GetTargetLanguage(prefs); | 422 std::string target_lang = GetTargetLanguage(prefs); |
| 423 std::string language_code = GetLanguageCode(page_lang); | 423 std::string language_code = GetLanguageCode(page_lang); |
| 424 if (language_code == target_lang) { | 424 if (language_code == target_lang) { |
| 425 TranslateManagerMetrics::ReportInitiationStatus( | 425 TranslateBrowserMetrics::ReportInitiationStatus( |
| 426 TranslateManagerMetrics::INITIATION_STATUS_SIMILAR_LANGUAGES); | 426 TranslateBrowserMetrics::INITIATION_STATUS_SIMILAR_LANGUAGES); |
| 427 return; | 427 return; |
| 428 } | 428 } |
| 429 | 429 |
| 430 // Don't translate any language the user configured as accepted languages. | 430 // Don't translate any language the user configured as accepted languages. |
| 431 if (accept_languages_->IsAcceptLanguage(original_profile, language_code)) { | 431 if (accept_languages_->IsAcceptLanguage(original_profile, language_code)) { |
| 432 TranslateManagerMetrics::ReportInitiationStatus( | 432 TranslateBrowserMetrics::ReportInitiationStatus( |
| 433 TranslateManagerMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES); | 433 TranslateBrowserMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES); |
| 434 return; | 434 return; |
| 435 } | 435 } |
| 436 | 436 |
| 437 // Nothing to do if either the language Chrome is in or the language of the | 437 // Nothing to do if either the language Chrome is in or the language of the |
| 438 // page is not supported by the translation server. | 438 // page is not supported by the translation server. |
| 439 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { | 439 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { |
| 440 TranslateManagerMetrics::ReportInitiationStatus( | 440 TranslateBrowserMetrics::ReportInitiationStatus( |
| 441 TranslateManagerMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); | 441 TranslateBrowserMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); |
| 442 TranslateManagerMetrics::ReportUnsupportedLanguageAtInitiation( | 442 TranslateBrowserMetrics::ReportUnsupportedLanguageAtInitiation( |
| 443 language_code); | 443 language_code); |
| 444 | |
| 445 return; | 444 return; |
| 446 } | 445 } |
| 447 | 446 |
| 448 // Don't translate any user black-listed URLs or user selected language | 447 // Don't translate any user black-listed URLs or user selected language |
| 449 // combination. | 448 // combination. |
| 450 if (!TranslatePrefs::CanTranslate(prefs, language_code, page_url)) { | 449 if (!TranslatePrefs::CanTranslate(prefs, language_code, page_url)) { |
| 451 TranslateManagerMetrics::ReportInitiationStatus( | 450 TranslateBrowserMetrics::ReportInitiationStatus( |
| 452 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); | 451 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); |
| 453 return; | 452 return; |
| 454 } | 453 } |
| 455 | 454 |
| 456 // If the user has previously selected "always translate" for this language we | 455 // If the user has previously selected "always translate" for this language we |
| 457 // automatically translate. Note that in incognito mode we disable that | 456 // automatically translate. Note that in incognito mode we disable that |
| 458 // feature; the user will get an infobar, so they can control whether the | 457 // feature; the user will get an infobar, so they can control whether the |
| 459 // page's text is sent to the translate server. | 458 // page's text is sent to the translate server. |
| 460 std::string auto_target_lang; | 459 std::string auto_target_lang; |
| 461 if (!web_contents->GetBrowserContext()->IsOffTheRecord() && | 460 if (!web_contents->GetBrowserContext()->IsOffTheRecord() && |
| 462 TranslatePrefs::ShouldAutoTranslate(prefs, language_code, | 461 TranslatePrefs::ShouldAutoTranslate(prefs, language_code, |
| 463 &auto_target_lang)) { | 462 &auto_target_lang)) { |
| 464 // We need to confirm that the saved target language is still supported. | 463 // We need to confirm that the saved target language is still supported. |
| 465 // Also, GetLanguageCode will take care of removing country code if any. | 464 // Also, GetLanguageCode will take care of removing country code if any. |
| 466 auto_target_lang = GetLanguageCode(auto_target_lang); | 465 auto_target_lang = GetLanguageCode(auto_target_lang); |
| 467 if (IsSupportedLanguage(auto_target_lang)) { | 466 if (IsSupportedLanguage(auto_target_lang)) { |
| 468 TranslateManagerMetrics::ReportInitiationStatus( | 467 TranslateBrowserMetrics::ReportInitiationStatus( |
| 469 TranslateManagerMetrics::INITIATION_STATUS_AUTO_BY_CONFIG); | 468 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG); |
| 470 TranslatePage(web_contents, language_code, auto_target_lang); | 469 TranslatePage(web_contents, language_code, auto_target_lang); |
| 471 return; | 470 return; |
| 472 } | 471 } |
| 473 } | 472 } |
| 474 | 473 |
| 475 TranslateTabHelper* translate_tab_helper = | 474 TranslateTabHelper* translate_tab_helper = |
| 476 TranslateTabHelper::FromWebContents(web_contents); | 475 TranslateTabHelper::FromWebContents(web_contents); |
| 477 if (!translate_tab_helper) | 476 if (!translate_tab_helper) |
| 478 return; | 477 return; |
| 479 | 478 |
| 480 std::string auto_translate_to = | 479 std::string auto_translate_to = |
| 481 translate_tab_helper->language_state().AutoTranslateTo(); | 480 translate_tab_helper->language_state().AutoTranslateTo(); |
| 482 if (!auto_translate_to.empty()) { | 481 if (!auto_translate_to.empty()) { |
| 483 // This page was navigated through a click from a translated page. | 482 // This page was navigated through a click from a translated page. |
| 484 TranslateManagerMetrics::ReportInitiationStatus( | 483 TranslateBrowserMetrics::ReportInitiationStatus( |
| 485 TranslateManagerMetrics::INITIATION_STATUS_AUTO_BY_LINK); | 484 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK); |
| 486 TranslatePage(web_contents, language_code, auto_translate_to); | 485 TranslatePage(web_contents, language_code, auto_translate_to); |
| 487 return; | 486 return; |
| 488 } | 487 } |
| 489 | 488 |
| 490 // Prompts the user if he/she wants the page translated. | 489 // Prompts the user if he/she wants the page translated. |
| 491 TranslateManagerMetrics::ReportInitiationStatus( | 490 TranslateBrowserMetrics::ReportInitiationStatus( |
| 492 TranslateManagerMetrics::INITIATION_STATUS_SHOW_INFOBAR); | 491 TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR); |
| 493 TranslateInfoBarDelegate::Create( | 492 TranslateInfoBarDelegate::Create( |
| 494 InfoBarService::FromWebContents(web_contents), false, | 493 InfoBarService::FromWebContents(web_contents), false, |
| 495 TranslateInfoBarDelegate::BEFORE_TRANSLATE, TranslateErrors::NONE, | 494 TranslateInfoBarDelegate::BEFORE_TRANSLATE, TranslateErrors::NONE, |
| 496 profile->GetPrefs(), ShortcutConfig(), | 495 profile->GetPrefs(), ShortcutConfig(), |
| 497 language_code, target_lang); | 496 language_code, target_lang); |
| 498 } | 497 } |
| 499 | 498 |
| 500 void TranslateManager::InitiateTranslationPosted( | 499 void TranslateManager::InitiateTranslationPosted( |
| 501 int process_id, int render_id, const std::string& page_lang, int attempt) { | 500 int process_id, int render_id, const std::string& page_lang, int attempt) { |
| 502 // The tab might have been closed. | 501 // The tab might have been closed. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( | 579 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( |
| 581 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID())); | 580 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID())); |
| 582 | 581 |
| 583 TranslateTabHelper* translate_tab_helper = | 582 TranslateTabHelper* translate_tab_helper = |
| 584 TranslateTabHelper::FromWebContents(web_contents); | 583 TranslateTabHelper::FromWebContents(web_contents); |
| 585 translate_tab_helper->language_state().set_current_language( | 584 translate_tab_helper->language_state().set_current_language( |
| 586 translate_tab_helper->language_state().original_language()); | 585 translate_tab_helper->language_state().original_language()); |
| 587 } | 586 } |
| 588 | 587 |
| 589 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { | 588 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { |
| 590 TranslateManagerMetrics::ReportLanguageDetectionError(); | 589 TranslateBrowserMetrics::ReportLanguageDetectionError(); |
| 591 // We'll open the URL in a new tab so that the user can tell us more. | 590 // We'll open the URL in a new tab so that the user can tell us more. |
| 592 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 591 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 593 if (!browser) { | 592 if (!browser) { |
| 594 NOTREACHED(); | 593 NOTREACHED(); |
| 595 return; | 594 return; |
| 596 } | 595 } |
| 597 | 596 |
| 598 GURL report_error_url = GURL(kReportLanguageDetectionErrorURL); | 597 GURL report_error_url = GURL(kReportLanguageDetectionErrorURL); |
| 599 | 598 |
| 600 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); | 599 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( | 635 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( |
| 637 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), | 636 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), |
| 638 translate_script, source_lang, target_lang)); | 637 translate_script, source_lang, target_lang)); |
| 639 } | 638 } |
| 640 | 639 |
| 641 void TranslateManager::PageTranslated(WebContents* web_contents, | 640 void TranslateManager::PageTranslated(WebContents* web_contents, |
| 642 PageTranslatedDetails* details) { | 641 PageTranslatedDetails* details) { |
| 643 if ((details->error_type == TranslateErrors::NONE) && | 642 if ((details->error_type == TranslateErrors::NONE) && |
| 644 details->source_language != chrome::kUnknownLanguageCode && | 643 details->source_language != chrome::kUnknownLanguageCode && |
| 645 !IsSupportedLanguage(details->source_language)) { | 644 !IsSupportedLanguage(details->source_language)) { |
| 646 // TODO(toyoshim): http://crbug.com/242142 We should check if | |
| 647 // l10n_util::GetDisplayNameForLocale() support |source_language| here. | |
| 648 // Also, following metrics should be modified to have language code. | |
| 649 TranslateManagerMetrics::ReportUnsupportedLanguage(); | |
| 650 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; | 645 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; |
| 651 } | 646 } |
| 652 | 647 |
| 653 Profile* profile = | 648 Profile* profile = |
| 654 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 649 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 655 PrefService* prefs = profile->GetPrefs(); | 650 PrefService* prefs = profile->GetPrefs(); |
| 656 TranslateInfoBarDelegate::Create( | 651 TranslateInfoBarDelegate::Create( |
| 657 InfoBarService::FromWebContents(web_contents), true, | 652 InfoBarService::FromWebContents(web_contents), true, |
| 658 (details->error_type == TranslateErrors::NONE) ? | 653 (details->error_type == TranslateErrors::NONE) ? |
| 659 TranslateInfoBarDelegate::AFTER_TRANSLATE : | 654 TranslateInfoBarDelegate::AFTER_TRANSLATE : |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 // reason so we are more aggressive showing the shortcuts for never translate. | 764 // reason so we are more aggressive showing the shortcuts for never translate. |
| 770 #if defined(OS_ANDROID) | 765 #if defined(OS_ANDROID) |
| 771 config.never_translate_min_count = 1; | 766 config.never_translate_min_count = 1; |
| 772 #else | 767 #else |
| 773 config.never_translate_min_count = 3; | 768 config.never_translate_min_count = 3; |
| 774 #endif // defined(OS_ANDROID) | 769 #endif // defined(OS_ANDROID) |
| 775 | 770 |
| 776 config.always_translate_min_count = 3; | 771 config.always_translate_min_count = 3; |
| 777 return config; | 772 return config; |
| 778 } | 773 } |
| OLD | NEW |