OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 MessageLoop::current()->PostTask(FROM_HERE, | 295 MessageLoop::current()->PostTask(FROM_HERE, |
296 base::Bind( | 296 base::Bind( |
297 &TranslateManager::InitiateTranslationPosted, | 297 &TranslateManager::InitiateTranslationPosted, |
298 weak_method_factory_.GetWeakPtr(), | 298 weak_method_factory_.GetWeakPtr(), |
299 controller->tab_contents()->GetRenderProcessHost()->GetID(), | 299 controller->tab_contents()->GetRenderProcessHost()->GetID(), |
300 controller->tab_contents()->GetRenderViewHost()->routing_id(), | 300 controller->tab_contents()->GetRenderViewHost()->routing_id(), |
301 helper->language_state().original_language())); | 301 helper->language_state().original_language())); |
302 break; | 302 break; |
303 } | 303 } |
304 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { | 304 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { |
305 TabContents* tab = content::Source<TabContents>(source).ptr(); | 305 WebContents* tab = content::Source<WebContents>(source).ptr(); |
306 // We may get this notifications multiple times. Make sure to translate | 306 // We may get this notifications multiple times. Make sure to translate |
307 // only once. | 307 // only once. |
308 TabContentsWrapper* wrapper = | 308 TabContentsWrapper* wrapper = |
309 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 309 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
310 if (!wrapper || !wrapper->translate_tab_helper()) | 310 if (!wrapper || !wrapper->translate_tab_helper()) |
311 return; | 311 return; |
312 | 312 |
313 LanguageState& language_state = | 313 LanguageState& language_state = |
314 wrapper->translate_tab_helper()->language_state(); | 314 wrapper->translate_tab_helper()->language_state(); |
315 if (language_state.page_translatable() && | 315 if (language_state.page_translatable() && |
316 !language_state.translation_pending() && | 316 !language_state.translation_pending() && |
317 !language_state.translation_declined() && | 317 !language_state.translation_declined() && |
318 !language_state.IsPageTranslated()) { | 318 !language_state.IsPageTranslated()) { |
319 std::string language = *(content::Details<std::string>(details).ptr()); | 319 std::string language = *(content::Details<std::string>(details).ptr()); |
320 InitiateTranslation(tab, language); | 320 InitiateTranslation(tab, language); |
321 } | 321 } |
322 break; | 322 break; |
323 } | 323 } |
324 case chrome::NOTIFICATION_PAGE_TRANSLATED: { | 324 case chrome::NOTIFICATION_PAGE_TRANSLATED: { |
325 // Only add translate infobar if it doesn't exist; if it already exists, | 325 // Only add translate infobar if it doesn't exist; if it already exists, |
326 // just update the state, the actual infobar would have received the same | 326 // just update the state, the actual infobar would have received the same |
327 // notification and update the visual display accordingly. | 327 // notification and update the visual display accordingly. |
328 TabContents* tab = content::Source<TabContents>(source).ptr(); | 328 WebContents* tab = content::Source<WebContents>(source).ptr(); |
329 PageTranslatedDetails* page_translated_details = | 329 PageTranslatedDetails* page_translated_details = |
330 content::Details<PageTranslatedDetails>(details).ptr(); | 330 content::Details<PageTranslatedDetails>(details).ptr(); |
331 PageTranslated(tab, page_translated_details); | 331 PageTranslated(tab, page_translated_details); |
332 break; | 332 break; |
333 } | 333 } |
334 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 334 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
335 PrefService* pref_service = | 335 PrefService* pref_service = |
336 content::Source<Profile>(source).ptr()->GetPrefs(); | 336 content::Source<Profile>(source).ptr()->GetPrefs(); |
337 notification_registrar_.Remove(this, | 337 notification_registrar_.Remove(this, |
338 chrome::NOTIFICATION_PROFILE_DESTROYED, | 338 chrome::NOTIFICATION_PROFILE_DESTROYED, |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 std::string data; | 433 std::string data; |
434 source->GetResponseAsString(&data); | 434 source->GetResponseAsString(&data); |
435 SetSupportedLanguages(data); | 435 SetSupportedLanguages(data); |
436 } else { | 436 } else { |
437 VLOG(1) << "Failed to Fetch languages from: " << kLanguageListFetchURL; | 437 VLOG(1) << "Failed to Fetch languages from: " << kLanguageListFetchURL; |
438 } | 438 } |
439 } | 439 } |
440 } | 440 } |
441 | 441 |
442 // static | 442 // static |
443 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { | 443 bool TranslateManager::IsShowingTranslateInfobar(WebContents* tab) { |
444 return GetTranslateInfoBarDelegate(tab) != NULL; | 444 return GetTranslateInfoBarDelegate(tab) != NULL; |
445 } | 445 } |
446 | 446 |
447 TranslateManager::TranslateManager() | 447 TranslateManager::TranslateManager() |
448 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_method_factory_(this)), | 448 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_method_factory_(this)), |
449 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS) { | 449 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS) { |
450 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 450 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
451 content::NotificationService::AllSources()); | 451 content::NotificationService::AllSources()); |
452 notification_registrar_.Add(this, | 452 notification_registrar_.Add(this, |
453 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 453 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
454 content::NotificationService::AllSources()); | 454 content::NotificationService::AllSources()); |
455 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, | 455 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, |
456 content::NotificationService::AllSources()); | 456 content::NotificationService::AllSources()); |
457 } | 457 } |
458 | 458 |
459 void TranslateManager::InitiateTranslation(TabContents* tab, | 459 void TranslateManager::InitiateTranslation(WebContents* tab, |
460 const std::string& page_lang) { | 460 const std::string& page_lang) { |
461 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); | 461 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); |
462 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); | 462 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); |
463 if (!prefs->GetBoolean(prefs::kEnableTranslate)) | 463 if (!prefs->GetBoolean(prefs::kEnableTranslate)) |
464 return; | 464 return; |
465 | 465 |
466 // Allow disabling of translate from the command line to assist with | 466 // Allow disabling of translate from the command line to assist with |
467 // automated browser testing. | 467 // automated browser testing. |
468 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) | 468 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) |
469 return; | 469 return; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 if (!wrapper || !wrapper->translate_tab_helper()) | 642 if (!wrapper || !wrapper->translate_tab_helper()) |
643 return; | 643 return; |
644 | 644 |
645 wrapper->translate_tab_helper()->language_state().set_translation_pending( | 645 wrapper->translate_tab_helper()->language_state().set_translation_pending( |
646 true); | 646 true); |
647 tab->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( | 647 tab->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( |
648 tab->GetRenderViewHost()->routing_id(), entry->GetPageID(), | 648 tab->GetRenderViewHost()->routing_id(), entry->GetPageID(), |
649 translate_script, source_lang, target_lang)); | 649 translate_script, source_lang, target_lang)); |
650 } | 650 } |
651 | 651 |
652 void TranslateManager::PageTranslated(TabContents* tab, | 652 void TranslateManager::PageTranslated(WebContents* tab, |
653 PageTranslatedDetails* details) { | 653 PageTranslatedDetails* details) { |
654 TabContentsWrapper* wrapper = | 654 TabContentsWrapper* wrapper = |
655 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 655 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
656 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 656 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
657 PrefService* prefs = wrapper->profile()->GetPrefs(); | 657 PrefService* prefs = wrapper->profile()->GetPrefs(); |
658 | 658 |
659 // Create the new infobar to display. | 659 // Create the new infobar to display. |
660 TranslateInfoBarDelegate* infobar; | 660 TranslateInfoBarDelegate* infobar; |
661 if (details->error_type != TranslateErrors::NONE) { | 661 if (details->error_type != TranslateErrors::NONE) { |
662 infobar = TranslateInfoBarDelegate::CreateErrorDelegate( | 662 infobar = TranslateInfoBarDelegate::CreateErrorDelegate( |
(...skipping 11 matching lines...) Expand all Loading... |
674 TranslateErrors::UNSUPPORTED_LANGUAGE, infobar_helper, | 674 TranslateErrors::UNSUPPORTED_LANGUAGE, infobar_helper, |
675 prefs, details->source_language, details->target_language); | 675 prefs, details->source_language, details->target_language); |
676 } else { | 676 } else { |
677 infobar = TranslateInfoBarDelegate::CreateDelegate( | 677 infobar = TranslateInfoBarDelegate::CreateDelegate( |
678 TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar_helper, | 678 TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar_helper, |
679 prefs, details->source_language, details->target_language); | 679 prefs, details->source_language, details->target_language); |
680 } | 680 } |
681 ShowInfoBar(tab, infobar); | 681 ShowInfoBar(tab, infobar); |
682 } | 682 } |
683 | 683 |
684 bool TranslateManager::IsAcceptLanguage(TabContents* tab, | 684 bool TranslateManager::IsAcceptLanguage(WebContents* tab, |
685 const std::string& language) { | 685 const std::string& language) { |
686 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); | 686 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); |
687 profile = profile->GetOriginalProfile(); | 687 profile = profile->GetOriginalProfile(); |
688 PrefService* pref_service = profile->GetPrefs(); | 688 PrefService* pref_service = profile->GetPrefs(); |
689 PrefServiceLanguagesMap::const_iterator iter = | 689 PrefServiceLanguagesMap::const_iterator iter = |
690 accept_languages_.find(pref_service); | 690 accept_languages_.find(pref_service); |
691 if (iter == accept_languages_.end()) { | 691 if (iter == accept_languages_.end()) { |
692 InitAcceptLanguages(pref_service); | 692 InitAcceptLanguages(pref_service); |
693 // Listen for this profile going away, in which case we would need to clear | 693 // Listen for this profile going away, in which case we would need to clear |
694 // the accepted languages for the profile. | 694 // the accepted languages for the profile. |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 831 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
832 | 832 |
833 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { | 833 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { |
834 TranslateInfoBarDelegate* delegate = | 834 TranslateInfoBarDelegate* delegate = |
835 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 835 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
836 if (delegate) | 836 if (delegate) |
837 return delegate; | 837 return delegate; |
838 } | 838 } |
839 return NULL; | 839 return NULL; |
840 } | 840 } |
OLD | NEW |