Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: chrome/browser/translate/translate_manager.cc

Issue 10537090: TabContentsWrapper -> TabContents, part 32. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/string_split.h" 14 #include "base/string_split.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/values.h" 17 #include "base/values.h"
18 #include "chrome/browser/browser_process.h" 18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/infobars/infobar_tab_helper.h" 19 #include "chrome/browser/infobars/infobar_tab_helper.h"
20 #include "chrome/browser/prefs/pref_service.h" 20 #include "chrome/browser/prefs/pref_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_infobar_delegate.h" 25 #include "chrome/browser/translate/translate_infobar_delegate.h"
26 #include "chrome/browser/translate/translate_prefs.h" 26 #include "chrome/browser/translate/translate_prefs.h"
27 #include "chrome/browser/translate/translate_tab_helper.h" 27 #include "chrome/browser/translate/translate_tab_helper.h"
28 #include "chrome/browser/ui/browser.h" 28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/browser_finder.h" 29 #include "chrome/browser/ui/browser_finder.h"
30 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 30 #include "chrome/browser/ui/tab_contents/tab_contents.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h" 31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/common/chrome_notification_types.h" 32 #include "chrome/common/chrome_notification_types.h"
33 #include "chrome/common/chrome_switches.h" 33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/pref_names.h" 34 #include "chrome/common/pref_names.h"
35 #include "chrome/common/render_messages.h" 35 #include "chrome/common/render_messages.h"
36 #include "chrome/common/translate_errors.h" 36 #include "chrome/common/translate_errors.h"
37 #include "chrome/common/url_constants.h" 37 #include "chrome/common/url_constants.h"
38 #include "content/public/browser/navigation_controller.h" 38 #include "content/public/browser/navigation_controller.h"
39 #include "content/public/browser/navigation_details.h" 39 #include "content/public/browser/navigation_details.h"
40 #include "content/public/browser/navigation_entry.h" 40 #include "content/public/browser/navigation_entry.h"
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 NavigationController* controller = 310 NavigationController* controller =
311 content::Source<NavigationController>(source).ptr(); 311 content::Source<NavigationController>(source).ptr();
312 content::LoadCommittedDetails* load_details = 312 content::LoadCommittedDetails* load_details =
313 content::Details<content::LoadCommittedDetails>(details).ptr(); 313 content::Details<content::LoadCommittedDetails>(details).ptr();
314 NavigationEntry* entry = controller->GetActiveEntry(); 314 NavigationEntry* entry = controller->GetActiveEntry();
315 if (!entry) { 315 if (!entry) {
316 NOTREACHED(); 316 NOTREACHED();
317 return; 317 return;
318 } 318 }
319 319
320 TabContentsWrapper* wrapper = 320 TabContents* tab_contents =
321 TabContentsWrapper::GetCurrentWrapperForContents( 321 TabContents::FromWebContents(controller->GetWebContents());
322 controller->GetWebContents()); 322 if (!tab_contents || !tab_contents->translate_tab_helper())
323 if (!wrapper || !wrapper->translate_tab_helper())
324 return; 323 return;
325 324
326 TranslateTabHelper* helper = wrapper->translate_tab_helper(); 325 TranslateTabHelper* helper = tab_contents->translate_tab_helper();
327 if (!load_details->is_main_frame && 326 if (!load_details->is_main_frame &&
328 helper->language_state().translation_declined()) { 327 helper->language_state().translation_declined()) {
329 // Some sites (such as Google map) may trigger sub-frame navigations 328 // Some sites (such as Google map) may trigger sub-frame navigations
330 // when the user interacts with the page. We don't want to show a new 329 // when the user interacts with the page. We don't want to show a new
331 // infobar if the user already dismissed one in that case. 330 // infobar if the user already dismissed one in that case.
332 return; 331 return;
333 } 332 }
334 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && 333 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD &&
335 load_details->type != content::NAVIGATION_TYPE_SAME_PAGE) { 334 load_details->type != content::NAVIGATION_TYPE_SAME_PAGE) {
336 return; 335 return;
(...skipping 11 matching lines...) Expand all
348 controller->GetWebContents()->GetRenderProcessHost()->GetID(), 347 controller->GetWebContents()->GetRenderProcessHost()->GetID(),
349 controller->GetWebContents()-> 348 controller->GetWebContents()->
350 GetRenderViewHost()->GetRoutingID(), 349 GetRenderViewHost()->GetRoutingID(),
351 helper->language_state().original_language())); 350 helper->language_state().original_language()));
352 break; 351 break;
353 } 352 }
354 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { 353 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: {
355 WebContents* tab = content::Source<WebContents>(source).ptr(); 354 WebContents* tab = content::Source<WebContents>(source).ptr();
356 // We may get this notifications multiple times. Make sure to translate 355 // We may get this notifications multiple times. Make sure to translate
357 // only once. 356 // only once.
358 TabContentsWrapper* wrapper = 357 TabContents* tab_contents = TabContents::FromWebContents(tab);
359 TabContentsWrapper::GetCurrentWrapperForContents(tab); 358 if (!tab_contents || !tab_contents->translate_tab_helper())
360 if (!wrapper || !wrapper->translate_tab_helper())
361 return; 359 return;
362 360
363 LanguageState& language_state = 361 LanguageState& language_state =
364 wrapper->translate_tab_helper()->language_state(); 362 tab_contents->translate_tab_helper()->language_state();
365 if (language_state.page_translatable() && 363 if (language_state.page_translatable() &&
366 !language_state.translation_pending() && 364 !language_state.translation_pending() &&
367 !language_state.translation_declined() && 365 !language_state.translation_declined() &&
368 !language_state.IsPageTranslated()) { 366 !language_state.IsPageTranslated()) {
369 std::string language = *(content::Details<std::string>(details).ptr()); 367 std::string language = *(content::Details<std::string>(details).ptr());
370 InitiateTranslation(tab, language); 368 InitiateTranslation(tab, language);
371 } 369 }
372 break; 370 break;
373 } 371 }
374 case chrome::NOTIFICATION_PAGE_TRANSLATED: { 372 case chrome::NOTIFICATION_PAGE_TRANSLATED: {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 MessageLoop::current()->PostDelayedTask(FROM_HERE, 438 MessageLoop::current()->PostDelayedTask(FROM_HERE,
441 base::Bind(&TranslateManager::ClearTranslateScript, 439 base::Bind(&TranslateManager::ClearTranslateScript,
442 weak_method_factory_.GetWeakPtr()), 440 weak_method_factory_.GetWeakPtr()),
443 translate_script_expiration_delay_); 441 translate_script_expiration_delay_);
444 } 442 }
445 // Process any pending requests. 443 // Process any pending requests.
446 std::vector<PendingRequest>::const_iterator iter; 444 std::vector<PendingRequest>::const_iterator iter;
447 for (iter = pending_requests_.begin(); iter != pending_requests_.end(); 445 for (iter = pending_requests_.begin(); iter != pending_requests_.end();
448 ++iter) { 446 ++iter) {
449 const PendingRequest& request = *iter; 447 const PendingRequest& request = *iter;
450 WebContents* tab = tab_util::GetWebContentsByID(request.render_process_id, 448 WebContents* web_contents =
451 request.render_view_id); 449 tab_util::GetWebContentsByID(request.render_process_id,
452 if (!tab) { 450 request.render_view_id);
451 if (!web_contents) {
453 // The tab went away while we were retrieving the script. 452 // The tab went away while we were retrieving the script.
454 continue; 453 continue;
455 } 454 }
456 NavigationEntry* entry = tab->GetController().GetActiveEntry(); 455 NavigationEntry* entry = web_contents->GetController().GetActiveEntry();
457 if (!entry || entry->GetPageID() != request.page_id) { 456 if (!entry || entry->GetPageID() != request.page_id) {
458 // We navigated away from the page the translation was triggered on. 457 // We navigated away from the page the translation was triggered on.
459 continue; 458 continue;
460 } 459 }
461 460
462 if (error) { 461 if (error) {
463 TabContentsWrapper* wrapper = 462 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
464 TabContentsWrapper::GetCurrentWrapperForContents(tab); 463 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
465 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper();
466 ShowInfoBar( 464 ShowInfoBar(
467 tab, TranslateInfoBarDelegate::CreateErrorDelegate( 465 web_contents,
466 TranslateInfoBarDelegate::CreateErrorDelegate(
468 TranslateErrors::NETWORK, 467 TranslateErrors::NETWORK,
469 infobar_helper, 468 infobar_helper,
470 wrapper->profile()->GetPrefs(), 469 tab_contents->profile()->GetPrefs(),
471 request.source_lang, 470 request.source_lang,
472 request.target_lang)); 471 request.target_lang));
473 } else { 472 } else {
474 // Translate the page. 473 // Translate the page.
475 DoTranslatePage(tab, translate_script_, 474 DoTranslatePage(web_contents, translate_script_,
476 request.source_lang, request.target_lang); 475 request.source_lang, request.target_lang);
477 } 476 }
478 } 477 }
479 pending_requests_.clear(); 478 pending_requests_.clear();
480 } else { // if (translate_script_request_pending_.get() == source) 479 } else { // if (translate_script_request_pending_.get() == source)
481 scoped_ptr<const net::URLFetcher> delete_ptr( 480 scoped_ptr<const net::URLFetcher> delete_ptr(
482 language_list_request_pending_.release()); 481 language_list_request_pending_.release());
483 if (!error) { 482 if (!error) {
484 std::string data; 483 std::string data;
485 source->GetResponseAsString(&data); 484 source->GetResponseAsString(&data);
486 SetSupportedLanguages(data); 485 SetSupportedLanguages(data);
487 } else { 486 } else {
488 VLOG(1) << "Failed to Fetch languages from: " << kLanguageListFetchURL; 487 VLOG(1) << "Failed to Fetch languages from: " << kLanguageListFetchURL;
489 } 488 }
490 } 489 }
491 } 490 }
492 491
493 // static 492 // static
494 bool TranslateManager::IsShowingTranslateInfobar(WebContents* tab) { 493 bool TranslateManager::IsShowingTranslateInfobar(WebContents* web_contents) {
495 return GetTranslateInfoBarDelegate(tab) != NULL; 494 return GetTranslateInfoBarDelegate(web_contents) != NULL;
496 } 495 }
497 496
498 TranslateManager::TranslateManager() 497 TranslateManager::TranslateManager()
499 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_method_factory_(this)), 498 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_method_factory_(this)),
500 translate_script_expiration_delay_( 499 translate_script_expiration_delay_(
501 base::TimeDelta::FromDays(kTranslateScriptExpirationDelayDays)) { 500 base::TimeDelta::FromDays(kTranslateScriptExpirationDelayDays)) {
502 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 501 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
503 content::NotificationService::AllSources()); 502 content::NotificationService::AllSources());
504 notification_registrar_.Add(this, 503 notification_registrar_.Add(this,
505 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, 504 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
506 content::NotificationService::AllSources()); 505 content::NotificationService::AllSources());
507 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, 506 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED,
508 content::NotificationService::AllSources()); 507 content::NotificationService::AllSources());
509 } 508 }
510 509
511 void TranslateManager::InitiateTranslation(WebContents* tab, 510 void TranslateManager::InitiateTranslation(WebContents* web_contents,
512 const std::string& page_lang) { 511 const std::string& page_lang) {
513 #ifdef FILE_MANAGER_EXTENSION 512 #ifdef FILE_MANAGER_EXTENSION
514 const GURL& page_url = tab->GetURL(); 513 const GURL& page_url = web_contents->GetURL();
515 if (page_url.SchemeIs("chrome-extension") && 514 if (page_url.SchemeIs("chrome-extension") &&
516 page_url.DomainIs(kFileBrowserDomain)) 515 page_url.DomainIs(kFileBrowserDomain))
517 return; 516 return;
518 #endif 517 #endif
519 518
520 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); 519 Profile* profile =
520 Profile::FromBrowserContext(web_contents->GetBrowserContext());
521 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); 521 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs();
522 if (!prefs->GetBoolean(prefs::kEnableTranslate)) 522 if (!prefs->GetBoolean(prefs::kEnableTranslate))
523 return; 523 return;
524 524
525 // Allow disabling of translate from the command line to assist with 525 // Allow disabling of translate from the command line to assist with
526 // automated browser testing. 526 // automated browser testing.
527 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) 527 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate))
528 return; 528 return;
529 529
530 NavigationEntry* entry = tab->GetController().GetActiveEntry(); 530 NavigationEntry* entry = web_contents->GetController().GetActiveEntry();
531 if (!entry) { 531 if (!entry) {
532 // This can happen for popups created with window.open(""). 532 // This can happen for popups created with window.open("").
533 return; 533 return;
534 } 534 }
535 535
536 // If there is already a translate infobar showing, don't show another one. 536 // If there is already a translate infobar showing, don't show another one.
537 if (GetTranslateInfoBarDelegate(tab)) 537 if (GetTranslateInfoBarDelegate(web_contents))
538 return; 538 return;
539 539
540 std::string target_lang = GetTargetLanguage(prefs); 540 std::string target_lang = GetTargetLanguage(prefs);
541 std::string language_code = GetLanguageCode(page_lang); 541 std::string language_code = GetLanguageCode(page_lang);
542 // Nothing to do if either the language Chrome is in or the language of the 542 // Nothing to do if either the language Chrome is in or the language of the
543 // page is not supported by the translation server. 543 // page is not supported by the translation server.
544 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { 544 if (target_lang.empty() || !IsSupportedLanguage(language_code)) {
545 return; 545 return;
546 } 546 }
547 547
548 // We don't want to translate: 548 // We don't want to translate:
549 // - any Chrome specific page (New Tab Page, Download, History... pages). 549 // - any Chrome specific page (New Tab Page, Download, History... pages).
550 // - similar languages (ex: en-US to en). 550 // - similar languages (ex: en-US to en).
551 // - any user black-listed URLs or user selected language combination. 551 // - any user black-listed URLs or user selected language combination.
552 // - any language the user configured as accepted languages. 552 // - any language the user configured as accepted languages.
553 if (!IsTranslatableURL(entry->GetURL()) || language_code == target_lang || 553 if (!IsTranslatableURL(entry->GetURL()) || language_code == target_lang ||
554 !TranslatePrefs::CanTranslate(prefs, language_code, entry->GetURL()) || 554 !TranslatePrefs::CanTranslate(prefs, language_code, entry->GetURL()) ||
555 IsAcceptLanguage(tab, language_code)) { 555 IsAcceptLanguage(web_contents, language_code)) {
556 return; 556 return;
557 } 557 }
558 558
559 // If the user has previously selected "always translate" for this language we 559 // If the user has previously selected "always translate" for this language we
560 // automatically translate. Note that in incognito mode we disable that 560 // automatically translate. Note that in incognito mode we disable that
561 // feature; the user will get an infobar, so they can control whether the 561 // feature; the user will get an infobar, so they can control whether the
562 // page's text is sent to the translate server. 562 // page's text is sent to the translate server.
563 std::string auto_target_lang; 563 std::string auto_target_lang;
564 if (!tab->GetBrowserContext()->IsOffTheRecord() && 564 if (!web_contents->GetBrowserContext()->IsOffTheRecord() &&
565 TranslatePrefs::ShouldAutoTranslate(prefs, language_code, 565 TranslatePrefs::ShouldAutoTranslate(prefs, language_code,
566 &auto_target_lang)) { 566 &auto_target_lang)) {
567 // We need to confirm that the saved target language is still supported. 567 // We need to confirm that the saved target language is still supported.
568 // Also, GetLanguageCode will take care of removing country code if any. 568 // Also, GetLanguageCode will take care of removing country code if any.
569 auto_target_lang = GetLanguageCode(auto_target_lang); 569 auto_target_lang = GetLanguageCode(auto_target_lang);
570 if (IsSupportedLanguage(auto_target_lang)) { 570 if (IsSupportedLanguage(auto_target_lang)) {
571 TranslatePage(tab, language_code, auto_target_lang); 571 TranslatePage(web_contents, language_code, auto_target_lang);
572 return; 572 return;
573 } 573 }
574 } 574 }
575 575
576 TabContentsWrapper* wrapper = 576 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
577 TabContentsWrapper::GetCurrentWrapperForContents(tab); 577 if (!tab_contents || !tab_contents->translate_tab_helper())
578 if (!wrapper || !wrapper->translate_tab_helper())
579 return; 578 return;
580 579
581 TranslateTabHelper* helper = wrapper->translate_tab_helper(); 580 TranslateTabHelper* helper = tab_contents->translate_tab_helper();
582 std::string auto_translate_to = helper->language_state().AutoTranslateTo(); 581 std::string auto_translate_to = helper->language_state().AutoTranslateTo();
583 if (!auto_translate_to.empty()) { 582 if (!auto_translate_to.empty()) {
584 // This page was navigated through a click from a translated page. 583 // This page was navigated through a click from a translated page.
585 TranslatePage(tab, language_code, auto_translate_to); 584 TranslatePage(web_contents, language_code, auto_translate_to);
586 return; 585 return;
587 } 586 }
588 587
589 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); 588 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
590 // Prompts the user if he/she wants the page translated. 589 // Prompts the user if he/she wants the page translated.
591 infobar_helper->AddInfoBar( 590 infobar_helper->AddInfoBar(
592 TranslateInfoBarDelegate::CreateDelegate( 591 TranslateInfoBarDelegate::CreateDelegate(
593 TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar_helper, 592 TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar_helper,
594 wrapper->profile()->GetPrefs(), language_code, target_lang)); 593 tab_contents->profile()->GetPrefs(), language_code, target_lang));
595 } 594 }
596 595
597 void TranslateManager::InitiateTranslationPosted( 596 void TranslateManager::InitiateTranslationPosted(
598 int process_id, int render_id, const std::string& page_lang) { 597 int process_id, int render_id, const std::string& page_lang) {
599 // The tab might have been closed. 598 // The tab might have been closed.
600 WebContents* tab = tab_util::GetWebContentsByID(process_id, render_id); 599 WebContents* web_contents =
601 if (!tab) 600 tab_util::GetWebContentsByID(process_id, render_id);
601 if (!web_contents)
602 return; 602 return;
603 603
604 TranslateTabHelper* helper = TabContentsWrapper::GetCurrentWrapperForContents( 604 TranslateTabHelper* helper =
605 tab)->translate_tab_helper(); 605 TabContents::FromWebContents(web_contents)->translate_tab_helper();
606 if (helper->language_state().translation_pending()) 606 if (helper->language_state().translation_pending())
607 return; 607 return;
608 608
609 InitiateTranslation(tab, GetLanguageCode(page_lang)); 609 InitiateTranslation(web_contents, GetLanguageCode(page_lang));
610 } 610 }
611 611
612 void TranslateManager::TranslatePage(WebContents* web_contents, 612 void TranslateManager::TranslatePage(WebContents* web_contents,
613 const std::string& source_lang, 613 const std::string& source_lang,
614 const std::string& target_lang) { 614 const std::string& target_lang) {
615 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); 615 NavigationEntry* entry = web_contents->GetController().GetActiveEntry();
616 if (!entry) { 616 if (!entry) {
617 NOTREACHED(); 617 NOTREACHED();
618 return; 618 return;
619 } 619 }
620 620
621 TabContentsWrapper* wrapper = 621 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
622 TabContentsWrapper::GetCurrentWrapperForContents(web_contents); 622 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
623 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper();
624 ShowInfoBar(web_contents, TranslateInfoBarDelegate::CreateDelegate( 623 ShowInfoBar(web_contents, TranslateInfoBarDelegate::CreateDelegate(
625 TranslateInfoBarDelegate::TRANSLATING, infobar_helper, 624 TranslateInfoBarDelegate::TRANSLATING, infobar_helper,
626 wrapper->profile()->GetPrefs(), source_lang, target_lang)); 625 tab_contents->profile()->GetPrefs(), source_lang, target_lang));
627 626
628 if (!translate_script_.empty()) { 627 if (!translate_script_.empty()) {
629 DoTranslatePage(web_contents, translate_script_, source_lang, target_lang); 628 DoTranslatePage(web_contents, translate_script_, source_lang, target_lang);
630 return; 629 return;
631 } 630 }
632 631
633 // The script is not available yet. Queue that request and query for the 632 // The script is not available yet. Queue that request and query for the
634 // script. Once it is downloaded we'll do the translate. 633 // script. Once it is downloaded we'll do the translate.
635 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); 634 content::RenderViewHost* rvh = web_contents->GetRenderViewHost();
636 PendingRequest request; 635 PendingRequest request;
637 request.render_process_id = rvh->GetProcess()->GetID(); 636 request.render_process_id = rvh->GetProcess()->GetID();
638 request.render_view_id = rvh->GetRoutingID(); 637 request.render_view_id = rvh->GetRoutingID();
639 request.page_id = entry->GetPageID(); 638 request.page_id = entry->GetPageID();
640 request.source_lang = source_lang; 639 request.source_lang = source_lang;
641 request.target_lang = target_lang; 640 request.target_lang = target_lang;
642 pending_requests_.push_back(request); 641 pending_requests_.push_back(request);
643 RequestTranslateScript(); 642 RequestTranslateScript();
644 } 643 }
645 644
646 void TranslateManager::RevertTranslation(WebContents* web_contents) { 645 void TranslateManager::RevertTranslation(WebContents* web_contents) {
647 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); 646 NavigationEntry* entry = web_contents->GetController().GetActiveEntry();
648 if (!entry) { 647 if (!entry) {
649 NOTREACHED(); 648 NOTREACHED();
650 return; 649 return;
651 } 650 }
652 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( 651 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation(
653 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID())); 652 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID()));
654 653
655 TranslateTabHelper* helper = TabContentsWrapper::GetCurrentWrapperForContents( 654 TranslateTabHelper* helper =
656 web_contents)->translate_tab_helper(); 655 TabContents::FromWebContents(web_contents)->translate_tab_helper();
657 helper->language_state().set_current_language( 656 helper->language_state().set_current_language(
658 helper->language_state().original_language()); 657 helper->language_state().original_language());
659 } 658 }
660 659
661 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { 660 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) {
662 UMA_HISTOGRAM_COUNTS("Translate.ReportLanguageDetectionError", 1); 661 UMA_HISTOGRAM_COUNTS("Translate.ReportLanguageDetectionError", 1);
663 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); 662 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL();
664 // Report option should be disabled for secure URLs. 663 // Report option should be disabled for secure URLs.
665 DCHECK(!page_url.SchemeIsSecure()); 664 DCHECK(!page_url.SchemeIsSecure());
666 std::string report_error_url(kReportLanguageDetectionErrorURL); 665 std::string report_error_url(kReportLanguageDetectionErrorURL);
667 report_error_url += "?client=cr&action=langidc&u="; 666 report_error_url += "?client=cr&action=langidc&u=";
668 report_error_url += net::EscapeUrlEncodedData(page_url.spec(), true); 667 report_error_url += net::EscapeUrlEncodedData(page_url.spec(), true);
669 report_error_url += "&sl="; 668 report_error_url += "&sl=";
670 669
671 TranslateTabHelper* helper = TabContentsWrapper::GetCurrentWrapperForContents( 670 TranslateTabHelper* helper =
672 web_contents)->translate_tab_helper(); 671 TabContents::FromWebContents(web_contents)->translate_tab_helper();
673 report_error_url += helper->language_state().original_language(); 672 report_error_url += helper->language_state().original_language();
674 report_error_url += "&hl="; 673 report_error_url += "&hl=";
675 report_error_url += 674 report_error_url +=
676 GetLanguageCode(g_browser_process->GetApplicationLocale()); 675 GetLanguageCode(g_browser_process->GetApplicationLocale());
677 // Open that URL in a new tab so that the user can tell us more. 676 // Open that URL in a new tab so that the user can tell us more.
678 Profile* profile = 677 Profile* profile =
679 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 678 Profile::FromBrowserContext(web_contents->GetBrowserContext());
680 Browser* browser = browser::FindLastActiveWithProfile(profile); 679 Browser* browser = browser::FindLastActiveWithProfile(profile);
681 if (!browser) { 680 if (!browser) {
682 NOTREACHED(); 681 NOTREACHED();
683 return; 682 return;
684 } 683 }
685 browser->AddSelectedTabWithURL(GURL(report_error_url), 684 browser->AddSelectedTabWithURL(GURL(report_error_url),
686 content::PAGE_TRANSITION_AUTO_BOOKMARK); 685 content::PAGE_TRANSITION_AUTO_BOOKMARK);
687 } 686 }
688 687
689 void TranslateManager::DoTranslatePage(WebContents* tab, 688 void TranslateManager::DoTranslatePage(WebContents* web_contents,
690 const std::string& translate_script, 689 const std::string& translate_script,
691 const std::string& source_lang, 690 const std::string& source_lang,
692 const std::string& target_lang) { 691 const std::string& target_lang) {
693 NavigationEntry* entry = tab->GetController().GetActiveEntry(); 692 NavigationEntry* entry = web_contents->GetController().GetActiveEntry();
694 if (!entry) { 693 if (!entry) {
695 NOTREACHED(); 694 NOTREACHED();
696 return; 695 return;
697 } 696 }
698 697
699 TabContentsWrapper* wrapper = 698 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
700 TabContentsWrapper::GetCurrentWrapperForContents(tab); 699 if (!tab_contents || !tab_contents->translate_tab_helper())
701 if (!wrapper || !wrapper->translate_tab_helper())
702 return; 700 return;
703 701
704 wrapper->translate_tab_helper()->language_state().set_translation_pending( 702 tab_contents->translate_tab_helper()->
705 true); 703 language_state().set_translation_pending(true);
706 tab->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( 704 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage(
707 tab->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), 705 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(),
708 translate_script, source_lang, target_lang)); 706 translate_script, source_lang, target_lang));
709 } 707 }
710 708
711 void TranslateManager::PageTranslated(WebContents* tab, 709 void TranslateManager::PageTranslated(WebContents* web_contents,
712 PageTranslatedDetails* details) { 710 PageTranslatedDetails* details) {
713 TabContentsWrapper* wrapper = 711 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
714 TabContentsWrapper::GetCurrentWrapperForContents(tab); 712 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
715 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); 713 PrefService* prefs = tab_contents->profile()->GetPrefs();
716 PrefService* prefs = wrapper->profile()->GetPrefs();
717 714
718 // Create the new infobar to display. 715 // Create the new infobar to display.
719 TranslateInfoBarDelegate* infobar; 716 TranslateInfoBarDelegate* infobar;
720 if (details->error_type != TranslateErrors::NONE) { 717 if (details->error_type != TranslateErrors::NONE) {
721 infobar = TranslateInfoBarDelegate::CreateErrorDelegate( 718 infobar = TranslateInfoBarDelegate::CreateErrorDelegate(
722 details->error_type, 719 details->error_type,
723 infobar_helper, 720 infobar_helper,
724 prefs, 721 prefs,
725 details->source_language, 722 details->source_language,
726 details->target_language); 723 details->target_language);
727 } else if (!IsSupportedLanguage(details->source_language)) { 724 } else if (!IsSupportedLanguage(details->source_language)) {
728 // TODO(jcivelli): http://crbug.com/9390 We should change the "after 725 // TODO(jcivelli): http://crbug.com/9390 We should change the "after
729 // translate" infobar to support unknown as the original 726 // translate" infobar to support unknown as the original
730 // language. 727 // language.
731 UMA_HISTOGRAM_COUNTS("Translate.ServerReportedUnsupportedLanguage", 1); 728 UMA_HISTOGRAM_COUNTS("Translate.ServerReportedUnsupportedLanguage", 1);
732 infobar = TranslateInfoBarDelegate::CreateErrorDelegate( 729 infobar = TranslateInfoBarDelegate::CreateErrorDelegate(
733 TranslateErrors::UNSUPPORTED_LANGUAGE, infobar_helper, 730 TranslateErrors::UNSUPPORTED_LANGUAGE, infobar_helper,
734 prefs, details->source_language, details->target_language); 731 prefs, details->source_language, details->target_language);
735 } else { 732 } else {
736 infobar = TranslateInfoBarDelegate::CreateDelegate( 733 infobar = TranslateInfoBarDelegate::CreateDelegate(
737 TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar_helper, 734 TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar_helper,
738 prefs, details->source_language, details->target_language); 735 prefs, details->source_language, details->target_language);
739 } 736 }
740 ShowInfoBar(tab, infobar); 737 ShowInfoBar(web_contents, infobar);
741 } 738 }
742 739
743 bool TranslateManager::IsAcceptLanguage(WebContents* tab, 740 bool TranslateManager::IsAcceptLanguage(WebContents* web_contents,
744 const std::string& language) { 741 const std::string& language) {
745 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); 742 Profile* profile =
743 Profile::FromBrowserContext(web_contents->GetBrowserContext());
746 profile = profile->GetOriginalProfile(); 744 profile = profile->GetOriginalProfile();
747 PrefService* pref_service = profile->GetPrefs(); 745 PrefService* pref_service = profile->GetPrefs();
748 PrefServiceLanguagesMap::const_iterator iter = 746 PrefServiceLanguagesMap::const_iterator iter =
749 accept_languages_.find(pref_service); 747 accept_languages_.find(pref_service);
750 if (iter == accept_languages_.end()) { 748 if (iter == accept_languages_.end()) {
751 InitAcceptLanguages(pref_service); 749 InitAcceptLanguages(pref_service);
752 // Listen for this profile going away, in which case we would need to clear 750 // Listen for this profile going away, in which case we would need to clear
753 // the accepted languages for the profile. 751 // the accepted languages for the profile.
754 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, 752 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
755 content::Source<Profile>(profile)); 753 content::Source<Profile>(profile));
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
840 0, GURL(translate_script_url), net::URLFetcher::GET, this)); 838 0, GURL(translate_script_url), net::URLFetcher::GET, this));
841 translate_script_request_pending_->SetLoadFlags( 839 translate_script_request_pending_->SetLoadFlags(
842 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); 840 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES);
843 translate_script_request_pending_->SetRequestContext( 841 translate_script_request_pending_->SetRequestContext(
844 g_browser_process->system_request_context()); 842 g_browser_process->system_request_context());
845 translate_script_request_pending_->SetExtraRequestHeaders( 843 translate_script_request_pending_->SetExtraRequestHeaders(
846 kTranslateScriptHeader); 844 kTranslateScriptHeader);
847 translate_script_request_pending_->Start(); 845 translate_script_request_pending_->Start();
848 } 846 }
849 847
850 void TranslateManager::ShowInfoBar(content::WebContents* tab, 848 void TranslateManager::ShowInfoBar(content::WebContents* web_contents,
851 TranslateInfoBarDelegate* infobar) { 849 TranslateInfoBarDelegate* infobar) {
852 DCHECK(infobar != NULL); 850 DCHECK(infobar != NULL);
853 TranslateInfoBarDelegate* old_infobar = GetTranslateInfoBarDelegate(tab); 851 TranslateInfoBarDelegate* old_infobar =
852 GetTranslateInfoBarDelegate(web_contents);
854 infobar->UpdateBackgroundAnimation(old_infobar); 853 infobar->UpdateBackgroundAnimation(old_infobar);
855 TabContentsWrapper* wrapper = 854 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
856 TabContentsWrapper::GetCurrentWrapperForContents(tab); 855 if (!tab_contents)
857 if (!wrapper)
858 return; 856 return;
859 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); 857 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
860 if (old_infobar) { 858 if (old_infobar) {
861 // There already is a translate infobar, simply replace it. 859 // There already is a translate infobar, simply replace it.
862 infobar_helper->ReplaceInfoBar(old_infobar, infobar); 860 infobar_helper->ReplaceInfoBar(old_infobar, infobar);
863 } else { 861 } else {
864 infobar_helper->AddInfoBar(infobar); 862 infobar_helper->AddInfoBar(infobar);
865 } 863 }
866 } 864 }
867 865
868 // static 866 // static
869 std::string TranslateManager::GetTargetLanguage(PrefService* prefs) { 867 std::string TranslateManager::GetTargetLanguage(PrefService* prefs) {
(...skipping 15 matching lines...) Expand all
885 iter != accept_langs_list.end(); ++iter) { 883 iter != accept_langs_list.end(); ++iter) {
886 std::string lang_code = GetLanguageCode(*iter); 884 std::string lang_code = GetLanguageCode(*iter);
887 if (IsSupportedLanguage(lang_code)) 885 if (IsSupportedLanguage(lang_code))
888 return lang_code; 886 return lang_code;
889 } 887 }
890 return std::string(); 888 return std::string();
891 } 889 }
892 890
893 // static 891 // static
894 TranslateInfoBarDelegate* TranslateManager::GetTranslateInfoBarDelegate( 892 TranslateInfoBarDelegate* TranslateManager::GetTranslateInfoBarDelegate(
895 WebContents* tab) { 893 WebContents* web_contents) {
896 TabContentsWrapper* wrapper = 894 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
897 TabContentsWrapper::GetCurrentWrapperForContents(tab); 895 if (!tab_contents)
898 if (!wrapper)
899 return NULL; 896 return NULL;
900 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); 897 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
901 898
902 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { 899 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) {
903 TranslateInfoBarDelegate* delegate = 900 TranslateInfoBarDelegate* delegate =
904 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); 901 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate();
905 if (delegate) 902 if (delegate)
906 return delegate; 903 return delegate;
907 } 904 }
908 return NULL; 905 return NULL;
909 } 906 }
OLDNEW
« no previous file with comments | « chrome/browser/translate/translate_manager.h ('k') | chrome/browser/translate/translate_manager_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698