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" |
31 #include "components/translate/core/browser/translate_browser_metrics.h" | 30 #include "components/translate/core/browser/translate_browser_metrics.h" |
32 #include "components/translate/core/browser/translate_download_manager.h" | 31 #include "components/translate/core/browser/translate_download_manager.h" |
33 #include "components/translate/core/browser/translate_error_details.h" | 32 #include "components/translate/core/browser/translate_error_details.h" |
34 #include "components/translate/core/browser/translate_language_list.h" | 33 #include "components/translate/core/browser/translate_language_list.h" |
| 34 #include "components/translate/core/browser/translate_prefs.h" |
35 #include "components/translate/core/browser/translate_script.h" | 35 #include "components/translate/core/browser/translate_script.h" |
36 #include "components/translate/core/browser/translate_url_util.h" | 36 #include "components/translate/core/browser/translate_url_util.h" |
37 #include "components/translate/core/common/language_detection_details.h" | 37 #include "components/translate/core/common/language_detection_details.h" |
38 #include "components/translate/core/common/translate_constants.h" | 38 #include "components/translate/core/common/translate_constants.h" |
39 #include "components/translate/core/common/translate_pref_names.h" | 39 #include "components/translate/core/common/translate_pref_names.h" |
40 #include "components/translate/core/common/translate_switches.h" | 40 #include "components/translate/core/common/translate_switches.h" |
41 #include "content/public/browser/navigation_controller.h" | 41 #include "content/public/browser/navigation_controller.h" |
42 #include "content/public/browser/navigation_details.h" | 42 #include "content/public/browser/navigation_details.h" |
43 #include "content/public/browser/navigation_entry.h" | 43 #include "content/public/browser/navigation_entry.h" |
44 #include "content/public/browser/notification_details.h" | 44 #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. | 292 // page is not supported by the translation server. |
293 if (target_lang.empty() || | 293 if (target_lang.empty() || |
294 !TranslateDownloadManager::IsSupportedLanguage(language_code)) { | 294 !TranslateDownloadManager::IsSupportedLanguage(language_code)) { |
295 TranslateBrowserMetrics::ReportInitiationStatus( | 295 TranslateBrowserMetrics::ReportInitiationStatus( |
296 TranslateBrowserMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); | 296 TranslateBrowserMetrics::INITIATION_STATUS_LANGUAGE_IS_NOT_SUPPORTED); |
297 TranslateBrowserMetrics::ReportUnsupportedLanguageAtInitiation( | 297 TranslateBrowserMetrics::ReportUnsupportedLanguageAtInitiation( |
298 language_code); | 298 language_code); |
299 return; | 299 return; |
300 } | 300 } |
301 | 301 |
302 TranslatePrefs translate_prefs(prefs); | 302 scoped_ptr<TranslatePrefs> translate_prefs( |
| 303 TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs())); |
303 | 304 |
| 305 bool is_accept_language = |
| 306 TranslateTabHelper::IsAcceptLanguage(profile, language_code); |
304 // Don't translate any user black-listed languages. | 307 // Don't translate any user black-listed languages. |
305 if (!TranslatePrefs::CanTranslateLanguage(profile, language_code)) { | 308 if (!translate_prefs->CanTranslateLanguage(is_accept_language, |
| 309 language_code)) { |
306 TranslateBrowserMetrics::ReportInitiationStatus( | 310 TranslateBrowserMetrics::ReportInitiationStatus( |
307 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); | 311 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); |
308 return; | 312 return; |
309 } | 313 } |
310 | 314 |
311 // Don't translate any user black-listed URLs. | 315 // Don't translate any user black-listed URLs. |
312 if (translate_prefs.IsSiteBlacklisted(page_url.HostNoBrackets())) { | 316 if (translate_prefs->IsSiteBlacklisted(page_url.HostNoBrackets())) { |
313 TranslateBrowserMetrics::ReportInitiationStatus( | 317 TranslateBrowserMetrics::ReportInitiationStatus( |
314 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); | 318 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_CONFIG); |
315 return; | 319 return; |
316 } | 320 } |
317 | 321 |
318 // If the user has previously selected "always translate" for this language we | 322 // If the user has previously selected "always translate" for this language we |
319 // automatically translate. Note that in incognito mode we disable that | 323 // automatically translate. Note that in incognito mode we disable that |
320 // feature; the user will get an infobar, so they can control whether the | 324 // feature; the user will get an infobar, so they can control whether the |
321 // page's text is sent to the translate server. | 325 // page's text is sent to the translate server. |
322 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { | 326 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 return lang_code; | 608 return lang_code; |
605 } | 609 } |
606 return std::string(); | 610 return std::string(); |
607 } | 611 } |
608 | 612 |
609 // static | 613 // static |
610 std::string TranslateManager::GetAutoTargetLanguage( | 614 std::string TranslateManager::GetAutoTargetLanguage( |
611 const std::string& original_language, | 615 const std::string& original_language, |
612 PrefService* prefs) { | 616 PrefService* prefs) { |
613 std::string auto_target_lang; | 617 std::string auto_target_lang; |
614 if (TranslatePrefs::ShouldAutoTranslate(prefs, original_language, | 618 scoped_ptr<TranslatePrefs> translate_prefs( |
615 &auto_target_lang)) { | 619 TranslateTabHelper::CreateTranslatePrefs(prefs)); |
| 620 if (translate_prefs->ShouldAutoTranslate(original_language, |
| 621 &auto_target_lang)) { |
616 // We need to confirm that the saved target language is still supported. | 622 // We need to confirm that the saved target language is still supported. |
617 // Also, GetLanguageCode will take care of removing country code if any. | 623 // Also, GetLanguageCode will take care of removing country code if any. |
618 auto_target_lang = | 624 auto_target_lang = |
619 TranslateDownloadManager::GetLanguageCode(auto_target_lang); | 625 TranslateDownloadManager::GetLanguageCode(auto_target_lang); |
620 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) | 626 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) |
621 return auto_target_lang; | 627 return auto_target_lang; |
622 } | 628 } |
623 return std::string(); | 629 return std::string(); |
624 } | 630 } |
OLD | NEW |