| 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" |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 // notification. So we need to explictly initiate the translation. | 332 // notification. So we need to explictly initiate the translation. |
| 333 // Note that we delay it as the TranslateManager gets this notification | 333 // Note that we delay it as the TranslateManager gets this notification |
| 334 // before the TabContents and the TabContents processing might remove the | 334 // before the TabContents and the TabContents processing might remove the |
| 335 // current infobars. Since InitTranslation might add an infobar, it must | 335 // current infobars. Since InitTranslation might add an infobar, it must |
| 336 // be done after that. | 336 // be done after that. |
| 337 MessageLoop::current()->PostTask(FROM_HERE, | 337 MessageLoop::current()->PostTask(FROM_HERE, |
| 338 base::Bind( | 338 base::Bind( |
| 339 &TranslateManager::InitiateTranslationPosted, | 339 &TranslateManager::InitiateTranslationPosted, |
| 340 weak_method_factory_.GetWeakPtr(), | 340 weak_method_factory_.GetWeakPtr(), |
| 341 controller->GetWebContents()->GetRenderProcessHost()->GetID(), | 341 controller->GetWebContents()->GetRenderProcessHost()->GetID(), |
| 342 controller->GetWebContents()->GetRenderViewHost()->routing_id(), | 342 controller->GetWebContents()-> |
| 343 GetRenderViewHost()->GetRoutingID(), |
| 343 helper->language_state().original_language())); | 344 helper->language_state().original_language())); |
| 344 break; | 345 break; |
| 345 } | 346 } |
| 346 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { | 347 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { |
| 347 WebContents* tab = content::Source<WebContents>(source).ptr(); | 348 WebContents* tab = content::Source<WebContents>(source).ptr(); |
| 348 // We may get this notifications multiple times. Make sure to translate | 349 // We may get this notifications multiple times. Make sure to translate |
| 349 // only once. | 350 // only once. |
| 350 TabContentsWrapper* wrapper = | 351 TabContentsWrapper* wrapper = |
| 351 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 352 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 352 if (!wrapper || !wrapper->translate_tab_helper()) | 353 if (!wrapper || !wrapper->translate_tab_helper()) |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 | 612 |
| 612 if (!translate_script_.empty()) { | 613 if (!translate_script_.empty()) { |
| 613 DoTranslatePage(web_contents, translate_script_, source_lang, target_lang); | 614 DoTranslatePage(web_contents, translate_script_, source_lang, target_lang); |
| 614 return; | 615 return; |
| 615 } | 616 } |
| 616 | 617 |
| 617 // The script is not available yet. Queue that request and query for the | 618 // The script is not available yet. Queue that request and query for the |
| 618 // script. Once it is downloaded we'll do the translate. | 619 // script. Once it is downloaded we'll do the translate. |
| 619 RenderViewHost* rvh = web_contents->GetRenderViewHost(); | 620 RenderViewHost* rvh = web_contents->GetRenderViewHost(); |
| 620 PendingRequest request; | 621 PendingRequest request; |
| 621 request.render_process_id = rvh->process()->GetID(); | 622 request.render_process_id = rvh->GetProcess()->GetID(); |
| 622 request.render_view_id = rvh->routing_id(); | 623 request.render_view_id = rvh->GetRoutingID(); |
| 623 request.page_id = entry->GetPageID(); | 624 request.page_id = entry->GetPageID(); |
| 624 request.source_lang = source_lang; | 625 request.source_lang = source_lang; |
| 625 request.target_lang = target_lang; | 626 request.target_lang = target_lang; |
| 626 pending_requests_.push_back(request); | 627 pending_requests_.push_back(request); |
| 627 RequestTranslateScript(); | 628 RequestTranslateScript(); |
| 628 } | 629 } |
| 629 | 630 |
| 630 void TranslateManager::RevertTranslation(WebContents* web_contents) { | 631 void TranslateManager::RevertTranslation(WebContents* web_contents) { |
| 631 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 632 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 632 if (!entry) { | 633 if (!entry) { |
| 633 NOTREACHED(); | 634 NOTREACHED(); |
| 634 return; | 635 return; |
| 635 } | 636 } |
| 636 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( | 637 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( |
| 637 web_contents->GetRenderViewHost()->routing_id(), entry->GetPageID())); | 638 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID())); |
| 638 | 639 |
| 639 TranslateTabHelper* helper = TabContentsWrapper::GetCurrentWrapperForContents( | 640 TranslateTabHelper* helper = TabContentsWrapper::GetCurrentWrapperForContents( |
| 640 web_contents)->translate_tab_helper(); | 641 web_contents)->translate_tab_helper(); |
| 641 helper->language_state().set_current_language( | 642 helper->language_state().set_current_language( |
| 642 helper->language_state().original_language()); | 643 helper->language_state().original_language()); |
| 643 } | 644 } |
| 644 | 645 |
| 645 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { | 646 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { |
| 646 UMA_HISTOGRAM_COUNTS("Translate.ReportLanguageDetectionError", 1); | 647 UMA_HISTOGRAM_COUNTS("Translate.ReportLanguageDetectionError", 1); |
| 647 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); | 648 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 } | 682 } |
| 682 | 683 |
| 683 TabContentsWrapper* wrapper = | 684 TabContentsWrapper* wrapper = |
| 684 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 685 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 685 if (!wrapper || !wrapper->translate_tab_helper()) | 686 if (!wrapper || !wrapper->translate_tab_helper()) |
| 686 return; | 687 return; |
| 687 | 688 |
| 688 wrapper->translate_tab_helper()->language_state().set_translation_pending( | 689 wrapper->translate_tab_helper()->language_state().set_translation_pending( |
| 689 true); | 690 true); |
| 690 tab->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( | 691 tab->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( |
| 691 tab->GetRenderViewHost()->routing_id(), entry->GetPageID(), | 692 tab->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), |
| 692 translate_script, source_lang, target_lang)); | 693 translate_script, source_lang, target_lang)); |
| 693 } | 694 } |
| 694 | 695 |
| 695 void TranslateManager::PageTranslated(WebContents* tab, | 696 void TranslateManager::PageTranslated(WebContents* tab, |
| 696 PageTranslatedDetails* details) { | 697 PageTranslatedDetails* details) { |
| 697 TabContentsWrapper* wrapper = | 698 TabContentsWrapper* wrapper = |
| 698 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 699 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 699 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 700 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
| 700 PrefService* prefs = wrapper->profile()->GetPrefs(); | 701 PrefService* prefs = wrapper->profile()->GetPrefs(); |
| 701 | 702 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 875 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
| 875 | 876 |
| 876 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { | 877 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { |
| 877 TranslateInfoBarDelegate* delegate = | 878 TranslateInfoBarDelegate* delegate = |
| 878 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 879 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
| 879 if (delegate) | 880 if (delegate) |
| 880 return delegate; | 881 return delegate; |
| 881 } | 882 } |
| 882 return NULL; | 883 return NULL; |
| 883 } | 884 } |
| OLD | NEW |