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/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.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_error_details.h" |
25 #include "chrome/browser/translate/translate_infobar_delegate.h" | 26 #include "chrome/browser/translate/translate_infobar_delegate.h" |
26 #include "chrome/browser/translate/translate_language_list.h" | 27 #include "chrome/browser/translate/translate_language_list.h" |
27 #include "chrome/browser/translate/translate_manager_metrics.h" | 28 #include "chrome/browser/translate/translate_manager_metrics.h" |
28 #include "chrome/browser/translate/translate_prefs.h" | 29 #include "chrome/browser/translate/translate_prefs.h" |
29 #include "chrome/browser/translate/translate_tab_helper.h" | 30 #include "chrome/browser/translate/translate_tab_helper.h" |
30 #include "chrome/browser/translate/translate_url_util.h" | 31 #include "chrome/browser/translate/translate_url_util.h" |
31 #include "chrome/browser/ui/browser.h" | 32 #include "chrome/browser/ui/browser.h" |
32 #include "chrome/browser/ui/browser_finder.h" | 33 #include "chrome/browser/ui/browser_finder.h" |
33 #include "chrome/browser/ui/browser_tabstrip.h" | 34 #include "chrome/browser/ui/browser_tabstrip.h" |
34 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 35 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 327 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
327 TranslateInfoBarDelegate::Create( | 328 TranslateInfoBarDelegate::Create( |
328 InfoBarService::FromWebContents(web_contents), | 329 InfoBarService::FromWebContents(web_contents), |
329 true, | 330 true, |
330 TranslateInfoBarDelegate::TRANSLATION_ERROR, | 331 TranslateInfoBarDelegate::TRANSLATION_ERROR, |
331 TranslateErrors::NETWORK, | 332 TranslateErrors::NETWORK, |
332 profile->GetPrefs(), | 333 profile->GetPrefs(), |
333 ShortcutConfig(), | 334 ShortcutConfig(), |
334 request.source_lang, | 335 request.source_lang, |
335 request.target_lang); | 336 request.target_lang); |
| 337 |
| 338 TranslateErrorDetails error_details; |
| 339 error_details.time = base::Time::Now(); |
| 340 error_details.url = entry->GetURL(); |
| 341 error_details.error = TranslateErrors::NETWORK; |
| 342 NotifyTranslateError(error_details); |
336 } else { | 343 } else { |
337 // Translate the page. | 344 // Translate the page. |
338 DoTranslatePage(web_contents, translate_script_, | 345 DoTranslatePage(web_contents, translate_script_, |
339 request.source_lang, request.target_lang); | 346 request.source_lang, request.target_lang); |
340 } | 347 } |
341 } | 348 } |
342 pending_requests_.clear(); | 349 pending_requests_.clear(); |
343 } | 350 } |
344 } | 351 } |
345 | 352 |
346 void TranslateManager::AddObserver(Observer* obs) { | 353 void TranslateManager::AddObserver(Observer* obs) { |
347 observer_list_.AddObserver(obs); | 354 observer_list_.AddObserver(obs); |
348 } | 355 } |
349 | 356 |
350 void TranslateManager::RemoveObserver(Observer* obs) { | 357 void TranslateManager::RemoveObserver(Observer* obs) { |
351 observer_list_.RemoveObserver(obs); | 358 observer_list_.RemoveObserver(obs); |
352 } | 359 } |
353 | 360 |
354 void TranslateManager::NotifyLanguageDetection( | 361 void TranslateManager::NotifyLanguageDetection( |
355 const LanguageDetectionDetails& details) { | 362 const LanguageDetectionDetails& details) { |
356 FOR_EACH_OBSERVER(Observer, observer_list_, OnLanguageDetection(details)); | 363 FOR_EACH_OBSERVER(Observer, observer_list_, OnLanguageDetection(details)); |
357 } | 364 } |
358 | 365 |
| 366 void TranslateManager::NotifyTranslateError( |
| 367 const TranslateErrorDetails& details) { |
| 368 FOR_EACH_OBSERVER(Observer, observer_list_, OnTranslateError(details)); |
| 369 } |
359 | 370 |
360 TranslateManager::TranslateManager() | 371 TranslateManager::TranslateManager() |
361 : weak_method_factory_(this), | 372 : weak_method_factory_(this), |
362 translate_script_expiration_delay_(base::TimeDelta::FromDays( | 373 translate_script_expiration_delay_(base::TimeDelta::FromDays( |
363 kTranslateScriptExpirationDelayDays)), | 374 kTranslateScriptExpirationDelayDays)), |
364 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts) { | 375 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts) { |
365 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 376 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
366 content::NotificationService::AllSources()); | 377 content::NotificationService::AllSources()); |
367 notification_registrar_.Add(this, | 378 notification_registrar_.Add(this, |
368 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 379 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 TranslateManagerMetrics::ReportUnsupportedLanguage(); | 640 TranslateManagerMetrics::ReportUnsupportedLanguage(); |
630 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; | 641 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; |
631 } | 642 } |
632 | 643 |
633 Profile* profile = | 644 Profile* profile = |
634 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 645 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
635 PrefService* prefs = profile->GetPrefs(); | 646 PrefService* prefs = profile->GetPrefs(); |
636 TranslateInfoBarDelegate::Create( | 647 TranslateInfoBarDelegate::Create( |
637 InfoBarService::FromWebContents(web_contents), true, | 648 InfoBarService::FromWebContents(web_contents), true, |
638 (details->error_type == TranslateErrors::NONE) ? | 649 (details->error_type == TranslateErrors::NONE) ? |
639 TranslateInfoBarDelegate::AFTER_TRANSLATE : | 650 TranslateInfoBarDelegate::AFTER_TRANSLATE : |
640 TranslateInfoBarDelegate::TRANSLATION_ERROR, | 651 TranslateInfoBarDelegate::TRANSLATION_ERROR, |
641 details->error_type, prefs, ShortcutConfig(), details->source_language, | 652 details->error_type, prefs, ShortcutConfig(), details->source_language, |
642 details->target_language); | 653 details->target_language); |
| 654 |
| 655 if (details->error_type != TranslateErrors::NONE) { |
| 656 TranslateErrorDetails error_details; |
| 657 error_details.time = base::Time::Now(); |
| 658 error_details.url = web_contents->GetActiveURL(); |
| 659 error_details.error = details->error_type; |
| 660 NotifyTranslateError(error_details); |
| 661 } |
643 } | 662 } |
644 | 663 |
645 bool TranslateManager::IsAcceptLanguage(WebContents* web_contents, | 664 bool TranslateManager::IsAcceptLanguage(WebContents* web_contents, |
646 const std::string& language) { | 665 const std::string& language) { |
647 Profile* profile = | 666 Profile* profile = |
648 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 667 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
649 profile = profile->GetOriginalProfile(); | 668 profile = profile->GetOriginalProfile(); |
650 PrefService* pref_service = profile->GetPrefs(); | 669 PrefService* pref_service = profile->GetPrefs(); |
651 PrefServiceLanguagesMap::const_iterator iter = | 670 PrefServiceLanguagesMap::const_iterator iter = |
652 accept_languages_.find(pref_service); | 671 accept_languages_.find(pref_service); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 // reason so we are more aggressive showing the shortcuts for never translate. | 822 // reason so we are more aggressive showing the shortcuts for never translate. |
804 #if defined(OS_ANDROID) | 823 #if defined(OS_ANDROID) |
805 config.never_translate_min_count = 1; | 824 config.never_translate_min_count = 1; |
806 #else | 825 #else |
807 config.never_translate_min_count = 3; | 826 config.never_translate_min_count = 3; |
808 #endif // defined(OS_ANDROID) | 827 #endif // defined(OS_ANDROID) |
809 | 828 |
810 config.always_translate_min_count = 3; | 829 config.always_translate_min_count = 3; |
811 return config; | 830 return config; |
812 } | 831 } |
OLD | NEW |