| 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 30 matching lines...) Expand all Loading... |
| 41 #include "content/public/browser/notification_details.h" | 41 #include "content/public/browser/notification_details.h" |
| 42 #include "content/public/browser/notification_service.h" | 42 #include "content/public/browser/notification_service.h" |
| 43 #include "content/public/browser/notification_source.h" | 43 #include "content/public/browser/notification_source.h" |
| 44 #include "content/public/browser/notification_types.h" | 44 #include "content/public/browser/notification_types.h" |
| 45 #include "content/public/common/url_fetcher.h" | 45 #include "content/public/common/url_fetcher.h" |
| 46 #include "grit/browser_resources.h" | 46 #include "grit/browser_resources.h" |
| 47 #include "net/base/escape.h" | 47 #include "net/base/escape.h" |
| 48 #include "net/url_request/url_request_status.h" | 48 #include "net/url_request/url_request_status.h" |
| 49 #include "ui/base/resource/resource_bundle.h" | 49 #include "ui/base/resource/resource_bundle.h" |
| 50 | 50 |
| 51 using content::NavigationEntry; |
| 51 using content::WebContents; | 52 using content::WebContents; |
| 52 | 53 |
| 53 namespace { | 54 namespace { |
| 54 | 55 |
| 55 // The list of languages the Google translation server supports. | 56 // The list of languages the Google translation server supports. |
| 56 // For information, here is the list of languages that Chrome can be run in | 57 // For information, here is the list of languages that Chrome can be run in |
| 57 // but that the translation server does not support: | 58 // but that the translation server does not support: |
| 58 // am Amharic | 59 // am Amharic |
| 59 // bn Bengali | 60 // bn Bengali |
| 60 // gu Gujarati | 61 // gu Gujarati |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 256 |
| 256 void TranslateManager::Observe(int type, | 257 void TranslateManager::Observe(int type, |
| 257 const content::NotificationSource& source, | 258 const content::NotificationSource& source, |
| 258 const content::NotificationDetails& details) { | 259 const content::NotificationDetails& details) { |
| 259 switch (type) { | 260 switch (type) { |
| 260 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { | 261 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { |
| 261 NavigationController* controller = | 262 NavigationController* controller = |
| 262 content::Source<NavigationController>(source).ptr(); | 263 content::Source<NavigationController>(source).ptr(); |
| 263 content::LoadCommittedDetails* load_details = | 264 content::LoadCommittedDetails* load_details = |
| 264 content::Details<content::LoadCommittedDetails>(details).ptr(); | 265 content::Details<content::LoadCommittedDetails>(details).ptr(); |
| 265 content::NavigationEntry* entry = controller->GetActiveEntry(); | 266 NavigationEntry* entry = controller->GetActiveEntry(); |
| 266 if (!entry) { | 267 if (!entry) { |
| 267 NOTREACHED(); | 268 NOTREACHED(); |
| 268 return; | 269 return; |
| 269 } | 270 } |
| 270 | 271 |
| 271 TabContentsWrapper* wrapper = | 272 TabContentsWrapper* wrapper = |
| 272 TabContentsWrapper::GetCurrentWrapperForContents( | 273 TabContentsWrapper::GetCurrentWrapperForContents( |
| 273 controller->tab_contents()); | 274 controller->tab_contents()); |
| 274 if (!wrapper || !wrapper->translate_tab_helper()) | 275 if (!wrapper || !wrapper->translate_tab_helper()) |
| 275 return; | 276 return; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 std::vector<PendingRequest>::const_iterator iter; | 396 std::vector<PendingRequest>::const_iterator iter; |
| 396 for (iter = pending_requests_.begin(); iter != pending_requests_.end(); | 397 for (iter = pending_requests_.begin(); iter != pending_requests_.end(); |
| 397 ++iter) { | 398 ++iter) { |
| 398 const PendingRequest& request = *iter; | 399 const PendingRequest& request = *iter; |
| 399 TabContents* tab = tab_util::GetTabContentsByID(request.render_process_id, | 400 TabContents* tab = tab_util::GetTabContentsByID(request.render_process_id, |
| 400 request.render_view_id); | 401 request.render_view_id); |
| 401 if (!tab) { | 402 if (!tab) { |
| 402 // The tab went away while we were retrieving the script. | 403 // The tab went away while we were retrieving the script. |
| 403 continue; | 404 continue; |
| 404 } | 405 } |
| 405 content::NavigationEntry* entry = tab->GetController().GetActiveEntry(); | 406 NavigationEntry* entry = tab->GetController().GetActiveEntry(); |
| 406 if (!entry || entry->GetPageID() != request.page_id) { | 407 if (!entry || entry->GetPageID() != request.page_id) { |
| 407 // We navigated away from the page the translation was triggered on. | 408 // We navigated away from the page the translation was triggered on. |
| 408 continue; | 409 continue; |
| 409 } | 410 } |
| 410 | 411 |
| 411 if (error) { | 412 if (error) { |
| 412 TabContentsWrapper* wrapper = | 413 TabContentsWrapper* wrapper = |
| 413 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 414 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 414 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 415 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
| 415 ShowInfoBar( | 416 ShowInfoBar( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); | 462 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); |
| 462 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); | 463 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); |
| 463 if (!prefs->GetBoolean(prefs::kEnableTranslate)) | 464 if (!prefs->GetBoolean(prefs::kEnableTranslate)) |
| 464 return; | 465 return; |
| 465 | 466 |
| 466 // Allow disabling of translate from the command line to assist with | 467 // Allow disabling of translate from the command line to assist with |
| 467 // automated browser testing. | 468 // automated browser testing. |
| 468 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) | 469 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) |
| 469 return; | 470 return; |
| 470 | 471 |
| 471 content::NavigationEntry* entry = tab->GetController().GetActiveEntry(); | 472 NavigationEntry* entry = tab->GetController().GetActiveEntry(); |
| 472 if (!entry) { | 473 if (!entry) { |
| 473 // This can happen for popups created with window.open(""). | 474 // This can happen for popups created with window.open(""). |
| 474 return; | 475 return; |
| 475 } | 476 } |
| 476 | 477 |
| 477 // If there is already a translate infobar showing, don't show another one. | 478 // If there is already a translate infobar showing, don't show another one. |
| 478 if (GetTranslateInfoBarDelegate(tab)) | 479 if (GetTranslateInfoBarDelegate(tab)) |
| 479 return; | 480 return; |
| 480 | 481 |
| 481 std::string target_lang = GetTargetLanguage(prefs); | 482 std::string target_lang = GetTargetLanguage(prefs); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 tab)->translate_tab_helper(); | 547 tab)->translate_tab_helper(); |
| 547 if (helper->language_state().translation_pending()) | 548 if (helper->language_state().translation_pending()) |
| 548 return; | 549 return; |
| 549 | 550 |
| 550 InitiateTranslation(tab, GetLanguageCode(page_lang)); | 551 InitiateTranslation(tab, GetLanguageCode(page_lang)); |
| 551 } | 552 } |
| 552 | 553 |
| 553 void TranslateManager::TranslatePage(WebContents* web_contents, | 554 void TranslateManager::TranslatePage(WebContents* web_contents, |
| 554 const std::string& source_lang, | 555 const std::string& source_lang, |
| 555 const std::string& target_lang) { | 556 const std::string& target_lang) { |
| 556 content::NavigationEntry* entry = | 557 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 557 web_contents->GetController().GetActiveEntry(); | |
| 558 if (!entry) { | 558 if (!entry) { |
| 559 NOTREACHED(); | 559 NOTREACHED(); |
| 560 return; | 560 return; |
| 561 } | 561 } |
| 562 | 562 |
| 563 TabContentsWrapper* wrapper = | 563 TabContentsWrapper* wrapper = |
| 564 TabContentsWrapper::GetCurrentWrapperForContents(web_contents); | 564 TabContentsWrapper::GetCurrentWrapperForContents(web_contents); |
| 565 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 565 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
| 566 ShowInfoBar(web_contents, TranslateInfoBarDelegate::CreateDelegate( | 566 ShowInfoBar(web_contents, TranslateInfoBarDelegate::CreateDelegate( |
| 567 TranslateInfoBarDelegate::TRANSLATING, infobar_helper, | 567 TranslateInfoBarDelegate::TRANSLATING, infobar_helper, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 579 request.render_process_id = rvh->process()->GetID(); | 579 request.render_process_id = rvh->process()->GetID(); |
| 580 request.render_view_id = rvh->routing_id(); | 580 request.render_view_id = rvh->routing_id(); |
| 581 request.page_id = entry->GetPageID(); | 581 request.page_id = entry->GetPageID(); |
| 582 request.source_lang = source_lang; | 582 request.source_lang = source_lang; |
| 583 request.target_lang = target_lang; | 583 request.target_lang = target_lang; |
| 584 pending_requests_.push_back(request); | 584 pending_requests_.push_back(request); |
| 585 RequestTranslateScript(); | 585 RequestTranslateScript(); |
| 586 } | 586 } |
| 587 | 587 |
| 588 void TranslateManager::RevertTranslation(WebContents* web_contents) { | 588 void TranslateManager::RevertTranslation(WebContents* web_contents) { |
| 589 content::NavigationEntry* entry = | 589 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 590 web_contents->GetController().GetActiveEntry(); | |
| 591 if (!entry) { | 590 if (!entry) { |
| 592 NOTREACHED(); | 591 NOTREACHED(); |
| 593 return; | 592 return; |
| 594 } | 593 } |
| 595 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( | 594 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( |
| 596 web_contents->GetRenderViewHost()->routing_id(), entry->GetPageID())); | 595 web_contents->GetRenderViewHost()->routing_id(), entry->GetPageID())); |
| 597 | 596 |
| 598 TranslateTabHelper* helper = TabContentsWrapper::GetCurrentWrapperForContents( | 597 TranslateTabHelper* helper = TabContentsWrapper::GetCurrentWrapperForContents( |
| 599 web_contents)->translate_tab_helper(); | 598 web_contents)->translate_tab_helper(); |
| 600 helper->language_state().set_current_language( | 599 helper->language_state().set_current_language( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 626 return; | 625 return; |
| 627 } | 626 } |
| 628 browser->AddSelectedTabWithURL(GURL(report_error_url), | 627 browser->AddSelectedTabWithURL(GURL(report_error_url), |
| 629 content::PAGE_TRANSITION_AUTO_BOOKMARK); | 628 content::PAGE_TRANSITION_AUTO_BOOKMARK); |
| 630 } | 629 } |
| 631 | 630 |
| 632 void TranslateManager::DoTranslatePage(WebContents* tab, | 631 void TranslateManager::DoTranslatePage(WebContents* tab, |
| 633 const std::string& translate_script, | 632 const std::string& translate_script, |
| 634 const std::string& source_lang, | 633 const std::string& source_lang, |
| 635 const std::string& target_lang) { | 634 const std::string& target_lang) { |
| 636 content::NavigationEntry* entry = tab->GetController().GetActiveEntry(); | 635 NavigationEntry* entry = tab->GetController().GetActiveEntry(); |
| 637 if (!entry) { | 636 if (!entry) { |
| 638 NOTREACHED(); | 637 NOTREACHED(); |
| 639 return; | 638 return; |
| 640 } | 639 } |
| 641 | 640 |
| 642 TabContentsWrapper* wrapper = | 641 TabContentsWrapper* wrapper = |
| 643 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 642 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 644 if (!wrapper || !wrapper->translate_tab_helper()) | 643 if (!wrapper || !wrapper->translate_tab_helper()) |
| 645 return; | 644 return; |
| 646 | 645 |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 832 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
| 834 | 833 |
| 835 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { | 834 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { |
| 836 TranslateInfoBarDelegate* delegate = | 835 TranslateInfoBarDelegate* delegate = |
| 837 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 836 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
| 838 if (delegate) | 837 if (delegate) |
| 839 return delegate; | 838 return delegate; |
| 840 } | 839 } |
| 841 return NULL; | 840 return NULL; |
| 842 } | 841 } |
| OLD | NEW |