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" |
11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
| 18 #include "base/time.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/infobars/infobar_service.h" | 21 #include "chrome/browser/infobars/infobar_service.h" |
21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/tab_contents/language_state.h" | 23 #include "chrome/browser/tab_contents/language_state.h" |
23 #include "chrome/browser/tab_contents/tab_util.h" | 24 #include "chrome/browser/tab_contents/tab_util.h" |
24 #include "chrome/browser/translate/page_translated_details.h" | 25 #include "chrome/browser/translate/page_translated_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_prefs.h" | 27 #include "chrome/browser/translate/translate_prefs.h" |
27 #include "chrome/browser/translate/translate_tab_helper.h" | 28 #include "chrome/browser/translate/translate_tab_helper.h" |
28 #include "chrome/browser/ui/browser.h" | 29 #include "chrome/browser/ui/browser.h" |
29 #include "chrome/browser/ui/browser_finder.h" | 30 #include "chrome/browser/ui/browser_finder.h" |
30 #include "chrome/browser/ui/browser_tabstrip.h" | 31 #include "chrome/browser/ui/browser_tabstrip.h" |
31 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 32 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
32 #include "chrome/common/chrome_notification_types.h" | 33 #include "chrome/common/chrome_notification_types.h" |
33 #include "chrome/common/chrome_switches.h" | 34 #include "chrome/common/chrome_switches.h" |
| 35 #include "chrome/common/language_detection_details.h" |
34 #include "chrome/common/pref_names.h" | 36 #include "chrome/common/pref_names.h" |
35 #include "chrome/common/render_messages.h" | 37 #include "chrome/common/render_messages.h" |
36 #include "chrome/common/translate_errors.h" | 38 #include "chrome/common/translate_errors.h" |
37 #include "chrome/common/url_constants.h" | 39 #include "chrome/common/url_constants.h" |
38 #include "content/public/browser/navigation_controller.h" | 40 #include "content/public/browser/navigation_controller.h" |
39 #include "content/public/browser/navigation_details.h" | 41 #include "content/public/browser/navigation_details.h" |
40 #include "content/public/browser/navigation_entry.h" | 42 #include "content/public/browser/navigation_entry.h" |
41 #include "content/public/browser/notification_details.h" | 43 #include "content/public/browser/notification_details.h" |
42 #include "content/public/browser/notification_service.h" | 44 #include "content/public/browser/notification_service.h" |
43 #include "content/public/browser/notification_source.h" | 45 #include "content/public/browser/notification_source.h" |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 MessageLoop::current()->PostTask(FROM_HERE, | 357 MessageLoop::current()->PostTask(FROM_HERE, |
356 base::Bind( | 358 base::Bind( |
357 &TranslateManager::InitiateTranslationPosted, | 359 &TranslateManager::InitiateTranslationPosted, |
358 weak_method_factory_.GetWeakPtr(), | 360 weak_method_factory_.GetWeakPtr(), |
359 controller->GetWebContents()->GetRenderProcessHost()->GetID(), | 361 controller->GetWebContents()->GetRenderProcessHost()->GetID(), |
360 controller->GetWebContents()->GetRenderViewHost()->GetRoutingID(), | 362 controller->GetWebContents()->GetRenderViewHost()->GetRoutingID(), |
361 translate_tab_helper->language_state().original_language())); | 363 translate_tab_helper->language_state().original_language())); |
362 break; | 364 break; |
363 } | 365 } |
364 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { | 366 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { |
| 367 const LanguageDetectionDetails* lang_det_details = |
| 368 content::Details<const LanguageDetectionDetails>(details).ptr(); |
| 369 NotifyLanguageDetection(*lang_det_details); |
| 370 |
365 WebContents* tab = content::Source<WebContents>(source).ptr(); | 371 WebContents* tab = content::Source<WebContents>(source).ptr(); |
366 // We may get this notifications multiple times. Make sure to translate | 372 // We may get this notifications multiple times. Make sure to translate |
367 // only once. | 373 // only once. |
368 TranslateTabHelper* translate_tab_helper = | 374 TranslateTabHelper* translate_tab_helper = |
369 TranslateTabHelper::FromWebContents(tab); | 375 TranslateTabHelper::FromWebContents(tab); |
370 if (!translate_tab_helper) | 376 if (!translate_tab_helper) |
371 return; | 377 return; |
372 | 378 |
373 LanguageState& language_state = translate_tab_helper->language_state(); | 379 LanguageState& language_state = translate_tab_helper->language_state(); |
374 if (language_state.page_translatable() && | 380 if (language_state.page_translatable() && |
375 !language_state.translation_pending() && | 381 !language_state.translation_pending() && |
376 !language_state.translation_declined() && | 382 !language_state.translation_declined() && |
377 !language_state.IsPageTranslated()) { | 383 !language_state.IsPageTranslated()) { |
378 std::string language = *(content::Details<std::string>(details).ptr()); | 384 std::string language = lang_det_details->adopted_language; |
379 InitiateTranslation(tab, language); | 385 InitiateTranslation(tab, language); |
380 } | 386 } |
381 break; | 387 break; |
382 } | 388 } |
383 case chrome::NOTIFICATION_PAGE_TRANSLATED: { | 389 case chrome::NOTIFICATION_PAGE_TRANSLATED: { |
384 // Only add translate infobar if it doesn't exist; if it already exists, | 390 // Only add translate infobar if it doesn't exist; if it already exists, |
385 // just update the state, the actual infobar would have received the same | 391 // just update the state, the actual infobar would have received the same |
386 // notification and update the visual display accordingly. | 392 // notification and update the visual display accordingly. |
387 WebContents* tab = content::Source<WebContents>(source).ptr(); | 393 WebContents* tab = content::Source<WebContents>(source).ptr(); |
388 PageTranslatedDetails* page_translated_details = | 394 PageTranslatedDetails* page_translated_details = |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 if (!error) { | 496 if (!error) { |
491 std::string data; | 497 std::string data; |
492 source->GetResponseAsString(&data); | 498 source->GetResponseAsString(&data); |
493 SetSupportedLanguages(data); | 499 SetSupportedLanguages(data); |
494 } else { | 500 } else { |
495 VLOG(9) << "Failed to Fetch languages from: " << kLanguageListFetchURL; | 501 VLOG(9) << "Failed to Fetch languages from: " << kLanguageListFetchURL; |
496 } | 502 } |
497 } | 503 } |
498 } | 504 } |
499 | 505 |
| 506 void TranslateManager::AddObserver(Observer* obs) { |
| 507 observer_list_.AddObserver(obs); |
| 508 } |
| 509 |
| 510 void TranslateManager::RemoveObserver(Observer* obs) { |
| 511 observer_list_.RemoveObserver(obs); |
| 512 } |
| 513 |
| 514 void TranslateManager::NotifyLanguageDetection( |
| 515 const LanguageDetectionDetails& details) { |
| 516 FOR_EACH_OBSERVER(Observer, observer_list_, OnLanguageDetection(details)); |
| 517 } |
| 518 |
| 519 |
500 TranslateManager::TranslateManager() | 520 TranslateManager::TranslateManager() |
501 : weak_method_factory_(this), | 521 : weak_method_factory_(this), |
502 translate_script_expiration_delay_( | 522 translate_script_expiration_delay_( |
503 base::TimeDelta::FromDays(kTranslateScriptExpirationDelayDays)) { | 523 base::TimeDelta::FromDays(kTranslateScriptExpirationDelayDays)) { |
504 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 524 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
505 content::NotificationService::AllSources()); | 525 content::NotificationService::AllSources()); |
506 notification_registrar_.Add(this, | 526 notification_registrar_.Add(this, |
507 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 527 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
508 content::NotificationService::AllSources()); | 528 content::NotificationService::AllSources()); |
509 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, | 529 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
703 void TranslateManager::PageTranslated(WebContents* web_contents, | 723 void TranslateManager::PageTranslated(WebContents* web_contents, |
704 PageTranslatedDetails* details) { | 724 PageTranslatedDetails* details) { |
705 if ((details->error_type == TranslateErrors::NONE) && | 725 if ((details->error_type == TranslateErrors::NONE) && |
706 !IsSupportedLanguage(details->source_language)) { | 726 !IsSupportedLanguage(details->source_language)) { |
707 // TODO(jcivelli): http://crbug.com/9390 We should change the "after | 727 // TODO(jcivelli): http://crbug.com/9390 We should change the "after |
708 // translate" infobar to support unknown as the original | 728 // translate" infobar to support unknown as the original |
709 // language. | 729 // language. |
710 UMA_HISTOGRAM_COUNTS("Translate.ServerReportedUnsupportedLanguage", 1); | 730 UMA_HISTOGRAM_COUNTS("Translate.ServerReportedUnsupportedLanguage", 1); |
711 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; | 731 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; |
712 } | 732 } |
| 733 |
713 Profile* profile = | 734 Profile* profile = |
714 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 735 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
715 PrefService* prefs = profile->GetPrefs(); | 736 PrefService* prefs = profile->GetPrefs(); |
716 TranslateInfoBarDelegate::Create( | 737 TranslateInfoBarDelegate::Create( |
717 InfoBarService::FromWebContents(web_contents), true, | 738 InfoBarService::FromWebContents(web_contents), true, |
718 (details->error_type == TranslateErrors::NONE) ? | 739 (details->error_type == TranslateErrors::NONE) ? |
719 TranslateInfoBarDelegate::AFTER_TRANSLATE : | 740 TranslateInfoBarDelegate::AFTER_TRANSLATE : |
720 TranslateInfoBarDelegate::TRANSLATION_ERROR, | 741 TranslateInfoBarDelegate::TRANSLATION_ERROR, |
721 details->error_type, prefs, ShortcutConfig(), details->source_language, | 742 details->error_type, prefs, ShortcutConfig(), details->source_language, |
722 details->target_language); | 743 details->target_language); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 // reason so we are more aggressive showing the shortcuts for never translate. | 924 // reason so we are more aggressive showing the shortcuts for never translate. |
904 #if defined(OS_ANDROID) | 925 #if defined(OS_ANDROID) |
905 config.never_translate_min_count = 1; | 926 config.never_translate_min_count = 1; |
906 #else | 927 #else |
907 config.never_translate_min_count = 3; | 928 config.never_translate_min_count = 3; |
908 #endif // defined(OS_ANDROID) | 929 #endif // defined(OS_ANDROID) |
909 | 930 |
910 config.always_translate_min_count = 3; | 931 config.always_translate_min_count = 3; |
911 return config; | 932 return config; |
912 } | 933 } |
OLD | NEW |