Chromium Code Reviews| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 #include "content/public/browser/render_view_host.h" | 47 #include "content/public/browser/render_view_host.h" |
| 48 #include "content/public/browser/web_contents.h" | 48 #include "content/public/browser/web_contents.h" |
| 49 #include "google_apis/google_api_keys.h" | 49 #include "google_apis/google_api_keys.h" |
| 50 #include "grit/browser_resources.h" | 50 #include "grit/browser_resources.h" |
| 51 #include "net/base/escape.h" | 51 #include "net/base/escape.h" |
| 52 #include "net/base/load_flags.h" | 52 #include "net/base/load_flags.h" |
| 53 #include "net/base/url_util.h" | 53 #include "net/base/url_util.h" |
| 54 #include "net/http/http_status_code.h" | 54 #include "net/http/http_status_code.h" |
| 55 #include "net/url_request/url_fetcher.h" | 55 #include "net/url_request/url_fetcher.h" |
| 56 #include "net/url_request/url_request_status.h" | 56 #include "net/url_request/url_request_status.h" |
| 57 #include "ui/base/l10n/l10n_util.h" | |
| 57 #include "ui/base/resource/resource_bundle.h" | 58 #include "ui/base/resource/resource_bundle.h" |
| 58 | 59 |
| 59 #ifdef FILE_MANAGER_EXTENSION | 60 #ifdef FILE_MANAGER_EXTENSION |
| 60 #include "chrome/browser/chromeos/extensions/file_manager/file_manager_util.h" | 61 #include "chrome/browser/chromeos/extensions/file_manager/file_manager_util.h" |
| 61 #include "extensions/common/constants.h" | 62 #include "extensions/common/constants.h" |
| 62 #endif | 63 #endif |
| 63 | 64 |
| 64 using content::NavigationController; | 65 using content::NavigationController; |
| 65 using content::NavigationEntry; | 66 using content::NavigationEntry; |
| 66 using content::WebContents; | 67 using content::WebContents; |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 return chrome_locale; | 308 return chrome_locale; |
| 308 return chrome_locale.substr(0, hypen_index); | 309 return chrome_locale.substr(0, hypen_index); |
| 309 } | 310 } |
| 310 | 311 |
| 311 // static | 312 // static |
| 312 bool TranslateManager::IsSupportedLanguage(const std::string& page_language) { | 313 bool TranslateManager::IsSupportedLanguage(const std::string& page_language) { |
| 313 InitSupportedLanguages(); | 314 InitSupportedLanguages(); |
| 314 return supported_languages_.Pointer()->count(page_language) != 0; | 315 return supported_languages_.Pointer()->count(page_language) != 0; |
| 315 } | 316 } |
| 316 | 317 |
| 318 // static | |
| 319 bool TranslateManager::IsAvailableLanguage(const std::string& language) { | |
| 320 std::string locale = g_browser_process->GetApplicationLocale(); | |
|
Takashi Toyoshima
2013/05/21 11:11:43
const std::string&
hajimehoshi
2013/05/22 04:19:27
Done.
| |
| 321 return l10n_util::IsLocaleNameTranslated(language, locale);; | |
| 322 } | |
| 323 | |
| 317 void TranslateManager::Observe(int type, | 324 void TranslateManager::Observe(int type, |
| 318 const content::NotificationSource& source, | 325 const content::NotificationSource& source, |
| 319 const content::NotificationDetails& details) { | 326 const content::NotificationDetails& details) { |
| 320 switch (type) { | 327 switch (type) { |
| 321 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { | 328 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { |
| 322 NavigationController* controller = | 329 NavigationController* controller = |
| 323 content::Source<NavigationController>(source).ptr(); | 330 content::Source<NavigationController>(source).ptr(); |
| 324 content::LoadCommittedDetails* load_details = | 331 content::LoadCommittedDetails* load_details = |
| 325 content::Details<content::LoadCommittedDetails>(details).ptr(); | 332 content::Details<content::LoadCommittedDetails>(details).ptr(); |
| 326 NavigationEntry* entry = controller->GetActiveEntry(); | 333 NavigationEntry* entry = controller->GetActiveEntry(); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 565 TranslateManagerMetrics::ReportInitiationStatus( | 572 TranslateManagerMetrics::ReportInitiationStatus( |
| 566 TranslateManagerMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES); | 573 TranslateManagerMetrics::INITIATION_STATUS_ACCEPT_LANGUAGES); |
| 567 return; | 574 return; |
| 568 } | 575 } |
| 569 | 576 |
| 570 // Nothing to do if either the language Chrome is in or the language of the | 577 // Nothing to do if either the language Chrome is in or the language of the |
| 571 // page is not supported by the translation server. | 578 // page is not supported by the translation server. |
| 572 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { | 579 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { |
| 573 TranslateManagerMetrics::ReportInitiationStatus( | 580 TranslateManagerMetrics::ReportInitiationStatus( |
| 574 TranslateManagerMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); | 581 TranslateManagerMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); |
| 582 TranslateManagerMetrics::ReportUnsupportedLanguageAtInitiation( | |
| 583 language_code); | |
| 584 | |
| 575 return; | 585 return; |
| 576 } | 586 } |
| 577 | 587 |
| 578 // Don't translate any user black-listed URLs or user selected language | 588 // Don't translate any user black-listed URLs or user selected language |
| 579 // combination. | 589 // combination. |
| 580 if (!TranslatePrefs::CanTranslate(prefs, language_code, page_url)) { | 590 if (!TranslatePrefs::CanTranslate(prefs, language_code, page_url)) { |
| 581 TranslateManagerMetrics::ReportInitiationStatus( | 591 TranslateManagerMetrics::ReportInitiationStatus( |
| 582 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); | 592 TranslateManagerMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); |
| 583 return; | 593 return; |
| 584 } | 594 } |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 754 return; | 764 return; |
| 755 | 765 |
| 756 translate_tab_helper->language_state().set_translation_pending(true); | 766 translate_tab_helper->language_state().set_translation_pending(true); |
| 757 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( | 767 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( |
| 758 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), | 768 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), |
| 759 translate_script, source_lang, target_lang)); | 769 translate_script, source_lang, target_lang)); |
| 760 } | 770 } |
| 761 | 771 |
| 762 void TranslateManager::PageTranslated(WebContents* web_contents, | 772 void TranslateManager::PageTranslated(WebContents* web_contents, |
| 763 PageTranslatedDetails* details) { | 773 PageTranslatedDetails* details) { |
| 764 if ((details->error_type == TranslateErrors::NONE) && | 774 if (details->error_type == TranslateErrors::NONE) { |
| 765 !IsSupportedLanguage(details->source_language)) { | |
| 766 // TODO(jcivelli): http://crbug.com/9390 We should change the "after | 775 // TODO(jcivelli): http://crbug.com/9390 We should change the "after |
|
Takashi Toyoshima
2013/05/21 11:11:43
remove this TODO.
hajimehoshi
2013/05/22 04:19:27
Done.
| |
| 767 // translate" infobar to support unknown as the original | 776 // translate" infobar to support unknown as the original |
| 768 // language. | 777 // language. |
| 769 TranslateManagerMetrics::ReportUnsupportedLanguage(); | 778 std::string language = details->source_language; |
|
Takashi Toyoshima
2013/05/21 11:11:43
const std::string&
hajimehoshi
2013/05/22 04:19:27
Done.
| |
| 770 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; | 779 if (!IsAvailableLanguage(language)) { |
| 780 TranslateManagerMetrics::ReportUnavailableLanguage(language); | |
| 781 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; | |
| 782 } | |
| 771 } | 783 } |
| 784 | |
| 772 Profile* profile = | 785 Profile* profile = |
| 773 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 786 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 774 PrefService* prefs = profile->GetPrefs(); | 787 PrefService* prefs = profile->GetPrefs(); |
| 775 TranslateInfoBarDelegate::Create( | 788 TranslateInfoBarDelegate::Create( |
| 776 InfoBarService::FromWebContents(web_contents), true, | 789 InfoBarService::FromWebContents(web_contents), true, |
| 777 (details->error_type == TranslateErrors::NONE) ? | 790 (details->error_type == TranslateErrors::NONE) ? |
| 778 TranslateInfoBarDelegate::AFTER_TRANSLATE : | 791 TranslateInfoBarDelegate::AFTER_TRANSLATE : |
| 779 TranslateInfoBarDelegate::TRANSLATION_ERROR, | 792 TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 780 details->error_type, prefs, ShortcutConfig(), details->source_language, | 793 details->error_type, prefs, ShortcutConfig(), details->source_language, |
| 781 details->target_language); | 794 details->target_language); |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 963 // reason so we are more aggressive showing the shortcuts for never translate. | 976 // reason so we are more aggressive showing the shortcuts for never translate. |
| 964 #if defined(OS_ANDROID) | 977 #if defined(OS_ANDROID) |
| 965 config.never_translate_min_count = 1; | 978 config.never_translate_min_count = 1; |
| 966 #else | 979 #else |
| 967 config.never_translate_min_count = 3; | 980 config.never_translate_min_count = 3; |
| 968 #endif // defined(OS_ANDROID) | 981 #endif // defined(OS_ANDROID) |
| 969 | 982 |
| 970 config.always_translate_min_count = 3; | 983 config.always_translate_min_count = 3; |
| 971 return config; | 984 return config; |
| 972 } | 985 } |
| OLD | NEW |