| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 #include "content/public/browser/render_view_host.h" | 49 #include "content/public/browser/render_view_host.h" |
| 50 #include "content/public/browser/web_contents.h" | 50 #include "content/public/browser/web_contents.h" |
| 51 #include "google_apis/google_api_keys.h" | 51 #include "google_apis/google_api_keys.h" |
| 52 #include "grit/browser_resources.h" | 52 #include "grit/browser_resources.h" |
| 53 #include "net/base/escape.h" | 53 #include "net/base/escape.h" |
| 54 #include "net/base/load_flags.h" | 54 #include "net/base/load_flags.h" |
| 55 #include "net/base/url_util.h" | 55 #include "net/base/url_util.h" |
| 56 #include "net/http/http_status_code.h" | 56 #include "net/http/http_status_code.h" |
| 57 #include "net/url_request/url_fetcher.h" | 57 #include "net/url_request/url_fetcher.h" |
| 58 #include "net/url_request/url_request_status.h" | 58 #include "net/url_request/url_request_status.h" |
| 59 #include "ui/base/l10n/l10n_util.h" |
| 59 #include "ui/base/resource/resource_bundle.h" | 60 #include "ui/base/resource/resource_bundle.h" |
| 60 | 61 |
| 61 #ifdef FILE_MANAGER_EXTENSION | 62 #ifdef FILE_MANAGER_EXTENSION |
| 62 #include "chrome/browser/chromeos/extensions/file_manager/file_manager_util.h" | 63 #include "chrome/browser/chromeos/extensions/file_manager/file_manager_util.h" |
| 63 #include "extensions/common/constants.h" | 64 #include "extensions/common/constants.h" |
| 64 #endif | 65 #endif |
| 65 | 66 |
| 66 using content::NavigationController; | 67 using content::NavigationController; |
| 67 using content::NavigationEntry; | 68 using content::NavigationEntry; |
| 68 using content::WebContents; | 69 using content::WebContents; |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 return chrome_locale; | 310 return chrome_locale; |
| 310 return chrome_locale.substr(0, hypen_index); | 311 return chrome_locale.substr(0, hypen_index); |
| 311 } | 312 } |
| 312 | 313 |
| 313 // static | 314 // static |
| 314 bool TranslateManager::IsSupportedLanguage(const std::string& page_language) { | 315 bool TranslateManager::IsSupportedLanguage(const std::string& page_language) { |
| 315 InitSupportedLanguages(); | 316 InitSupportedLanguages(); |
| 316 return supported_languages_.Pointer()->count(page_language) != 0; | 317 return supported_languages_.Pointer()->count(page_language) != 0; |
| 317 } | 318 } |
| 318 | 319 |
| 320 // static |
| 321 bool TranslateManager::IsAvailableLanguage(const std::string& language) { |
| 322 const std::string& locale = g_browser_process->GetApplicationLocale(); |
| 323 return l10n_util::IsLocaleNameTranslated(language, locale);; |
| 324 } |
| 325 |
| 319 void TranslateManager::Observe(int type, | 326 void TranslateManager::Observe(int type, |
| 320 const content::NotificationSource& source, | 327 const content::NotificationSource& source, |
| 321 const content::NotificationDetails& details) { | 328 const content::NotificationDetails& details) { |
| 322 switch (type) { | 329 switch (type) { |
| 323 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { | 330 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { |
| 324 NavigationController* controller = | 331 NavigationController* controller = |
| 325 content::Source<NavigationController>(source).ptr(); | 332 content::Source<NavigationController>(source).ptr(); |
| 326 content::LoadCommittedDetails* load_details = | 333 content::LoadCommittedDetails* load_details = |
| 327 content::Details<content::LoadCommittedDetails>(details).ptr(); | 334 content::Details<content::LoadCommittedDetails>(details).ptr(); |
| 328 NavigationEntry* entry = controller->GetActiveEntry(); | 335 NavigationEntry* entry = controller->GetActiveEntry(); |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 TranslateManagerMetrics::ReportInitiationStatus( | 592 TranslateManagerMetrics::ReportInitiationStatus( |
| 586 TranslateManagerMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES); | 593 TranslateManagerMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES); |
| 587 return; | 594 return; |
| 588 } | 595 } |
| 589 | 596 |
| 590 // Nothing to do if either the language Chrome is in or the language of the | 597 // Nothing to do if either the language Chrome is in or the language of the |
| 591 // page is not supported by the translation server. | 598 // page is not supported by the translation server. |
| 592 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { | 599 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { |
| 593 TranslateManagerMetrics::ReportInitiationStatus( | 600 TranslateManagerMetrics::ReportInitiationStatus( |
| 594 TranslateManagerMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); | 601 TranslateManagerMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); |
| 602 TranslateManagerMetrics::ReportUnsupportedLanguageAtInitiation( |
| 603 language_code); |
| 604 |
| 595 return; | 605 return; |
| 596 } | 606 } |
| 597 | 607 |
| 598 // Don't translate any user black-listed URLs or user selected language | 608 // Don't translate any user black-listed URLs or user selected language |
| 599 // combination. | 609 // combination. |
| 600 if (!TranslatePrefs::CanTranslate(prefs, language_code, page_url)) { | 610 if (!TranslatePrefs::CanTranslate(prefs, language_code, page_url)) { |
| 601 TranslateManagerMetrics::ReportInitiationStatus( | 611 TranslateManagerMetrics::ReportInitiationStatus( |
| 602 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); | 612 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); |
| 603 return; | 613 return; |
| 604 } | 614 } |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 774 return; | 784 return; |
| 775 | 785 |
| 776 translate_tab_helper->language_state().set_translation_pending(true); | 786 translate_tab_helper->language_state().set_translation_pending(true); |
| 777 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( | 787 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( |
| 778 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), | 788 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), |
| 779 translate_script, source_lang, target_lang)); | 789 translate_script, source_lang, target_lang)); |
| 780 } | 790 } |
| 781 | 791 |
| 782 void TranslateManager::PageTranslated(WebContents* web_contents, | 792 void TranslateManager::PageTranslated(WebContents* web_contents, |
| 783 PageTranslatedDetails* details) { | 793 PageTranslatedDetails* details) { |
| 784 if ((details->error_type == TranslateErrors::NONE) && | 794 if (details->error_type == TranslateErrors::NONE) { |
| 785 !IsSupportedLanguage(details->source_language)) { | 795 const std::string& language = details->source_language; |
| 786 // TODO(jcivelli): http://crbug.com/9390 We should change the "after | 796 if (!IsAvailableLanguage(language)) { |
| 787 // translate" infobar to support unknown as the original | 797 TranslateManagerMetrics::ReportUnavailableLanguage(language); |
| 788 // language. | 798 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; |
| 789 TranslateManagerMetrics::ReportUnsupportedLanguage(); | 799 } |
| 790 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; | |
| 791 } | 800 } |
| 792 | 801 |
| 793 Profile* profile = | 802 Profile* profile = |
| 794 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 803 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 795 PrefService* prefs = profile->GetPrefs(); | 804 PrefService* prefs = profile->GetPrefs(); |
| 796 TranslateInfoBarDelegate::Create( | 805 TranslateInfoBarDelegate::Create( |
| 797 InfoBarService::FromWebContents(web_contents), true, | 806 InfoBarService::FromWebContents(web_contents), true, |
| 798 (details->error_type == TranslateErrors::NONE) ? | 807 (details->error_type == TranslateErrors::NONE) ? |
| 799 TranslateInfoBarDelegate::AFTER_TRANSLATE : | 808 TranslateInfoBarDelegate::AFTER_TRANSLATE : |
| 800 TranslateInfoBarDelegate::TRANSLATION_ERROR, | 809 TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 984 // reason so we are more aggressive showing the shortcuts for never translate. | 993 // reason so we are more aggressive showing the shortcuts for never translate. |
| 985 #if defined(OS_ANDROID) | 994 #if defined(OS_ANDROID) |
| 986 config.never_translate_min_count = 1; | 995 config.never_translate_min_count = 1; |
| 987 #else | 996 #else |
| 988 config.never_translate_min_count = 3; | 997 config.never_translate_min_count = 3; |
| 989 #endif // defined(OS_ANDROID) | 998 #endif // defined(OS_ANDROID) |
| 990 | 999 |
| 991 config.always_translate_min_count = 3; | 1000 config.always_translate_min_count = 3; |
| 992 return config; | 1001 return config; |
| 993 } | 1002 } |
| OLD | NEW |