| 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/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/tab_contents/tab_util.h" | 19 #include "chrome/browser/tab_contents/tab_util.h" |
| 20 #include "chrome/browser/translate/translate_prefs.h" | |
| 21 #include "chrome/browser/translate/translate_tab_helper.h" | 20 #include "chrome/browser/translate/translate_tab_helper.h" |
| 22 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/browser_finder.h" | 22 #include "chrome/browser/ui/browser_finder.h" |
| 24 #include "chrome/browser/ui/browser_tabstrip.h" | 23 #include "chrome/browser/ui/browser_tabstrip.h" |
| 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 26 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/common/render_messages.h" | 26 #include "chrome/common/render_messages.h" |
| 28 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
| 29 #include "components/translate/core/browser/language_state.h" | 28 #include "components/translate/core/browser/language_state.h" |
| 30 #include "components/translate/core/browser/page_translated_details.h" | 29 #include "components/translate/core/browser/page_translated_details.h" |
| 30 #include "components/translate/core/browser/translate_accept_languages.h" |
| 31 #include "components/translate/core/browser/translate_browser_metrics.h" | 31 #include "components/translate/core/browser/translate_browser_metrics.h" |
| 32 #include "components/translate/core/browser/translate_download_manager.h" | 32 #include "components/translate/core/browser/translate_download_manager.h" |
| 33 #include "components/translate/core/browser/translate_error_details.h" | 33 #include "components/translate/core/browser/translate_error_details.h" |
| 34 #include "components/translate/core/browser/translate_language_list.h" | 34 #include "components/translate/core/browser/translate_language_list.h" |
| 35 #include "components/translate/core/browser/translate_prefs.h" |
| 35 #include "components/translate/core/browser/translate_script.h" | 36 #include "components/translate/core/browser/translate_script.h" |
| 36 #include "components/translate/core/browser/translate_url_util.h" | 37 #include "components/translate/core/browser/translate_url_util.h" |
| 37 #include "components/translate/core/common/language_detection_details.h" | 38 #include "components/translate/core/common/language_detection_details.h" |
| 38 #include "components/translate/core/common/translate_constants.h" | 39 #include "components/translate/core/common/translate_constants.h" |
| 39 #include "components/translate/core/common/translate_pref_names.h" | 40 #include "components/translate/core/common/translate_pref_names.h" |
| 40 #include "components/translate/core/common/translate_switches.h" | 41 #include "components/translate/core/common/translate_switches.h" |
| 41 #include "content/public/browser/navigation_controller.h" | 42 #include "content/public/browser/navigation_controller.h" |
| 42 #include "content/public/browser/navigation_details.h" | 43 #include "content/public/browser/navigation_details.h" |
| 43 #include "content/public/browser/navigation_entry.h" | 44 #include "content/public/browser/navigation_entry.h" |
| 44 #include "content/public/browser/notification_details.h" | 45 #include "content/public/browser/notification_details.h" |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 // page is not supported by the translation server. | 293 // page is not supported by the translation server. |
| 293 if (target_lang.empty() || | 294 if (target_lang.empty() || |
| 294 !TranslateDownloadManager::IsSupportedLanguage(language_code)) { | 295 !TranslateDownloadManager::IsSupportedLanguage(language_code)) { |
| 295 TranslateBrowserMetrics::ReportInitiationStatus( | 296 TranslateBrowserMetrics::ReportInitiationStatus( |
| 296 TranslateBrowserMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); | 297 TranslateBrowserMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); |
| 297 TranslateBrowserMetrics::ReportUnsupportedLanguageAtInitiation( | 298 TranslateBrowserMetrics::ReportUnsupportedLanguageAtInitiation( |
| 298 language_code); | 299 language_code); |
| 299 return; | 300 return; |
| 300 } | 301 } |
| 301 | 302 |
| 302 TranslatePrefs translate_prefs(prefs); | 303 scoped_ptr<TranslatePrefs> translate_prefs( |
| 304 TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs())); |
| 303 | 305 |
| 306 TranslateAcceptLanguages* accept_languages = |
| 307 TranslateTabHelper::GetTranslateAcceptLanguages(profile); |
| 304 // Don't translate any user black-listed languages. | 308 // Don't translate any user black-listed languages. |
| 305 if (!TranslatePrefs::CanTranslateLanguage(profile, language_code)) { | 309 if (!translate_prefs->CanTranslateLanguage(accept_languages, |
| 310 language_code)) { |
| 306 TranslateBrowserMetrics::ReportInitiationStatus( | 311 TranslateBrowserMetrics::ReportInitiationStatus( |
| 307 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); | 312 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); |
| 308 return; | 313 return; |
| 309 } | 314 } |
| 310 | 315 |
| 311 // Don't translate any user black-listed URLs. | 316 // Don't translate any user black-listed URLs. |
| 312 if (translate_prefs.IsSiteBlacklisted(page_url.HostNoBrackets())) { | 317 if (translate_prefs->IsSiteBlacklisted(page_url.HostNoBrackets())) { |
| 313 TranslateBrowserMetrics::ReportInitiationStatus( | 318 TranslateBrowserMetrics::ReportInitiationStatus( |
| 314 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); | 319 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); |
| 315 return; | 320 return; |
| 316 } | 321 } |
| 317 | 322 |
| 318 // If the user has previously selected "always translate" for this language we | 323 // If the user has previously selected "always translate" for this language we |
| 319 // automatically translate. Note that in incognito mode we disable that | 324 // automatically translate. Note that in incognito mode we disable that |
| 320 // feature; the user will get an infobar, so they can control whether the | 325 // feature; the user will get an infobar, so they can control whether the |
| 321 // page's text is sent to the translate server. | 326 // page's text is sent to the translate server. |
| 322 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { | 327 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 return lang_code; | 609 return lang_code; |
| 605 } | 610 } |
| 606 return std::string(); | 611 return std::string(); |
| 607 } | 612 } |
| 608 | 613 |
| 609 // static | 614 // static |
| 610 std::string TranslateManager::GetAutoTargetLanguage( | 615 std::string TranslateManager::GetAutoTargetLanguage( |
| 611 const std::string& original_language, | 616 const std::string& original_language, |
| 612 PrefService* prefs) { | 617 PrefService* prefs) { |
| 613 std::string auto_target_lang; | 618 std::string auto_target_lang; |
| 614 if (TranslatePrefs::ShouldAutoTranslate(prefs, original_language, | 619 scoped_ptr<TranslatePrefs> translate_prefs( |
| 615 &auto_target_lang)) { | 620 TranslateTabHelper::CreateTranslatePrefs(prefs)); |
| 621 if (translate_prefs->ShouldAutoTranslate(original_language, |
| 622 &auto_target_lang)) { |
| 616 // We need to confirm that the saved target language is still supported. | 623 // We need to confirm that the saved target language is still supported. |
| 617 // Also, GetLanguageCode will take care of removing country code if any. | 624 // Also, GetLanguageCode will take care of removing country code if any. |
| 618 auto_target_lang = | 625 auto_target_lang = |
| 619 TranslateDownloadManager::GetLanguageCode(auto_target_lang); | 626 TranslateDownloadManager::GetLanguageCode(auto_target_lang); |
| 620 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) | 627 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) |
| 621 return auto_target_lang; | 628 return auto_target_lang; |
| 622 } | 629 } |
| 623 return std::string(); | 630 return std::string(); |
| 624 } | 631 } |
| OLD | NEW |