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

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

Issue 25373009: Translate: New Bubble UX (for the view toolkit) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add 'Try again' button; rename enum names Created 7 years, 2 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
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/memory/singleton.h" 9 #include "base/memory/singleton.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 14 matching lines...) Expand all
25 #include "chrome/browser/translate/translate_event_details.h" 25 #include "chrome/browser/translate/translate_event_details.h"
26 #include "chrome/browser/translate/translate_infobar_delegate.h" 26 #include "chrome/browser/translate/translate_infobar_delegate.h"
27 #include "chrome/browser/translate/translate_language_list.h" 27 #include "chrome/browser/translate/translate_language_list.h"
28 #include "chrome/browser/translate/translate_prefs.h" 28 #include "chrome/browser/translate/translate_prefs.h"
29 #include "chrome/browser/translate/translate_script.h" 29 #include "chrome/browser/translate/translate_script.h"
30 #include "chrome/browser/translate/translate_tab_helper.h" 30 #include "chrome/browser/translate/translate_tab_helper.h"
31 #include "chrome/browser/translate/translate_url_util.h" 31 #include "chrome/browser/translate/translate_url_util.h"
32 #include "chrome/browser/ui/browser.h" 32 #include "chrome/browser/ui/browser.h"
33 #include "chrome/browser/ui/browser_finder.h" 33 #include "chrome/browser/ui/browser_finder.h"
34 #include "chrome/browser/ui/browser_tabstrip.h" 34 #include "chrome/browser/ui/browser_tabstrip.h"
35 #include "chrome/browser/ui/browser_window.h"
35 #include "chrome/browser/ui/tabs/tab_strip_model.h" 36 #include "chrome/browser/ui/tabs/tab_strip_model.h"
37 #include "chrome/browser/ui/translate/translate_bubble.h"
36 #include "chrome/common/chrome_switches.h" 38 #include "chrome/common/chrome_switches.h"
37 #include "chrome/common/pref_names.h" 39 #include "chrome/common/pref_names.h"
38 #include "chrome/common/render_messages.h" 40 #include "chrome/common/render_messages.h"
39 #include "chrome/common/translate/language_detection_details.h" 41 #include "chrome/common/translate/language_detection_details.h"
40 #include "chrome/common/url_constants.h" 42 #include "chrome/common/url_constants.h"
41 #include "components/translate/common/translate_constants.h" 43 #include "components/translate/common/translate_constants.h"
42 #include "content/public/browser/navigation_controller.h" 44 #include "content/public/browser/navigation_controller.h"
43 #include "content/public/browser/navigation_details.h" 45 #include "content/public/browser/navigation_details.h"
44 #include "content/public/browser/navigation_entry.h" 46 #include "content/public/browser/navigation_entry.h"
45 #include "content/public/browser/notification_details.h" 47 #include "content/public/browser/notification_details.h"
46 #include "content/public/browser/notification_service.h" 48 #include "content/public/browser/notification_service.h"
47 #include "content/public/browser/notification_source.h" 49 #include "content/public/browser/notification_source.h"
48 #include "content/public/browser/notification_types.h" 50 #include "content/public/browser/notification_types.h"
49 #include "content/public/browser/render_process_host.h" 51 #include "content/public/browser/render_process_host.h"
50 #include "content/public/browser/render_view_host.h" 52 #include "content/public/browser/render_view_host.h"
51 #include "content/public/browser/web_contents.h" 53 #include "content/public/browser/web_contents.h"
52 #include "net/base/url_util.h" 54 #include "net/base/url_util.h"
53 #include "net/http/http_status_code.h" 55 #include "net/http/http_status_code.h"
54 56
55 #ifdef FILE_MANAGER_EXTENSION 57 #ifdef FILE_MANAGER_EXTENSION
56 #include "chrome/browser/chromeos/file_manager/app_id.h" 58 #include "chrome/browser/chromeos/file_manager/app_id.h"
57 #include "extensions/common/constants.h" 59 #include "extensions/common/constants.h"
58 #endif 60 #endif
59 61
60 using content::NavigationController; 62 using content::NavigationController;
61 using content::NavigationEntry; 63 using content::NavigationEntry;
62 using content::WebContents; 64 using content::WebContents;
63 65
66 namespace {
67
64 const char kReportLanguageDetectionErrorURL[] = 68 const char kReportLanguageDetectionErrorURL[] =
65 "https://translate.google.com/translate_error?client=cr&action=langidc"; 69 "https://translate.google.com/translate_error?client=cr&action=langidc";
66 70
67 // Used in kReportLanguageDetectionErrorURL to specify the original page 71 // Used in kReportLanguageDetectionErrorURL to specify the original page
68 // language. 72 // language.
69 const char kSourceLanguageQueryName[] = "sl"; 73 const char kSourceLanguageQueryName[] = "sl";
70 74
71 // Used in kReportLanguageDetectionErrorURL to specify the page URL. 75 // Used in kReportLanguageDetectionErrorURL to specify the page URL.
72 const char kUrlQueryName[] = "u"; 76 const char kUrlQueryName[] = "u";
73 77
74 // The maximum number of attempts we'll do to see if the page has finshed 78 // The maximum number of attempts we'll do to see if the page has finshed
75 // loading before giving up the translation 79 // loading before giving up the translation
76 const int kMaxTranslateLoadCheckAttempts = 20; 80 const int kMaxTranslateLoadCheckAttempts = 20;
77 81
82 bool IsEnabledTranslateNewUX() {
Takashi Toyoshima 2013/10/08 14:54:09 IsTranslateNewUXEnabled() sounds better. Just an
83 return CommandLine::ForCurrentProcess()->HasSwitch(
84 switches::kEnableTranslateNewUX);
85 }
86
87 void ShowBubble(WebContents* web_contents, TranslateBubble::Type type) {
88 // TODO(hajimehoshi): Show bubble only when needed.
Takashi Toyoshima 2013/10/08 14:54:09 What do you mean by this TODO?
hajimehoshi 2013/10/10 11:07:10 This comment means nothing. I'll remove this.
89 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
90 DCHECK(browser != NULL);
Takashi Toyoshima 2013/10/08 14:54:09 Do you check if these DCHECK are always correct? S
hajimehoshi 2013/10/10 11:07:10 I found these assertion failed on TranslateManager
91 BrowserWindow* window = browser->window();
92 DCHECK(window != NULL);
93 window->ShowTranslateBubble(web_contents, type);
94 }
95
96 } // namespace
97
78 TranslateManager::~TranslateManager() { 98 TranslateManager::~TranslateManager() {
79 weak_method_factory_.InvalidateWeakPtrs(); 99 weak_method_factory_.InvalidateWeakPtrs();
80 } 100 }
81 101
82 // static 102 // static
83 TranslateManager* TranslateManager::GetInstance() { 103 TranslateManager* TranslateManager::GetInstance() {
84 return Singleton<TranslateManager>::get(); 104 return Singleton<TranslateManager>::get();
85 } 105 }
86 106
87 // static 107 // static
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 content::NotificationService::AllSources()); 316 content::NotificationService::AllSources());
297 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, 317 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED,
298 content::NotificationService::AllSources()); 318 content::NotificationService::AllSources());
299 language_list_.reset(new TranslateLanguageList); 319 language_list_.reset(new TranslateLanguageList);
300 accept_languages_.reset(new TranslateAcceptLanguages); 320 accept_languages_.reset(new TranslateAcceptLanguages);
301 script_.reset(new TranslateScript); 321 script_.reset(new TranslateScript);
302 } 322 }
303 323
304 void TranslateManager::InitiateTranslation(WebContents* web_contents, 324 void TranslateManager::InitiateTranslation(WebContents* web_contents,
305 const std::string& page_lang) { 325 const std::string& page_lang) {
326 TranslateTabHelper* translate_tab_helper =
327 TranslateTabHelper::FromWebContents(web_contents);
328 if (!translate_tab_helper)
329 return;
330
306 Profile* profile = 331 Profile* profile =
307 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 332 Profile::FromBrowserContext(web_contents->GetBrowserContext());
308 Profile* original_profile = profile->GetOriginalProfile(); 333 Profile* original_profile = profile->GetOriginalProfile();
309 PrefService* prefs = original_profile->GetPrefs(); 334 PrefService* prefs = original_profile->GetPrefs();
310 if (!prefs->GetBoolean(prefs::kEnableTranslate)) { 335 if (!prefs->GetBoolean(prefs::kEnableTranslate)) {
311 TranslateBrowserMetrics::ReportInitiationStatus( 336 TranslateBrowserMetrics::ReportInitiationStatus(
312 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS); 337 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS);
313 const std::string& locale = g_browser_process->GetApplicationLocale(); 338 const std::string& locale = g_browser_process->GetApplicationLocale();
314 TranslateBrowserMetrics::ReportLocalesOnDisabledByPrefs(locale); 339 TranslateBrowserMetrics::ReportLocalesOnDisabledByPrefs(locale);
315 return; 340 return;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 // Also, GetLanguageCode will take care of removing country code if any. 414 // Also, GetLanguageCode will take care of removing country code if any.
390 auto_target_lang = GetLanguageCode(auto_target_lang); 415 auto_target_lang = GetLanguageCode(auto_target_lang);
391 if (IsSupportedLanguage(auto_target_lang)) { 416 if (IsSupportedLanguage(auto_target_lang)) {
392 TranslateBrowserMetrics::ReportInitiationStatus( 417 TranslateBrowserMetrics::ReportInitiationStatus(
393 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG); 418 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG);
394 TranslatePage(web_contents, language_code, auto_target_lang); 419 TranslatePage(web_contents, language_code, auto_target_lang);
395 return; 420 return;
396 } 421 }
397 } 422 }
398 423
399 TranslateTabHelper* translate_tab_helper = 424 LanguageState& language_state = translate_tab_helper->language_state();
400 TranslateTabHelper::FromWebContents(web_contents); 425 std::string auto_translate_to = language_state.AutoTranslateTo();
401 if (!translate_tab_helper)
402 return;
403 std::string auto_translate_to =
404 translate_tab_helper->language_state().AutoTranslateTo();
405 if (!auto_translate_to.empty()) { 426 if (!auto_translate_to.empty()) {
406 // This page was navigated through a click from a translated page. 427 // This page was navigated through a click from a translated page.
407 TranslateBrowserMetrics::ReportInitiationStatus( 428 TranslateBrowserMetrics::ReportInitiationStatus(
408 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK); 429 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK);
409 TranslatePage(web_contents, language_code, auto_translate_to); 430 TranslatePage(web_contents, language_code, auto_translate_to);
410 return; 431 return;
411 } 432 }
412 433
413 // Prompts the user if he/she wants the page translated.
414 TranslateBrowserMetrics::ReportInitiationStatus( 434 TranslateBrowserMetrics::ReportInitiationStatus(
415 TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR); 435 TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR);
416 TranslateInfoBarDelegate::Create( 436
417 false, InfoBarService::FromWebContents(web_contents), 437 if (IsEnabledTranslateNewUX()) {
418 TranslateInfoBarDelegate::BEFORE_TRANSLATE, language_code, target_lang, 438 language_state.SetIsTranslateSuggested(true);
419 TranslateErrors::NONE, profile->GetPrefs(), ShortcutConfig()); 439 if (language_state.IsLanguageChanged())
440 ShowBubble(web_contents, TranslateBubble::BEFORE_TRANSLATE);
441 } else {
442 // Prompts the user if he/she wants the page translated.
443 TranslateInfoBarDelegate::Create(
444 false, InfoBarService::FromWebContents(web_contents),
445 TranslateInfoBarDelegate::BEFORE_TRANSLATE, language_code, target_lang,
446 TranslateErrors::NONE, profile->GetPrefs(), ShortcutConfig());
447 }
420 } 448 }
421 449
422 void TranslateManager::InitiateTranslationPosted(int process_id, 450 void TranslateManager::InitiateTranslationPosted(int process_id,
423 int render_id, 451 int render_id,
424 const std::string& page_lang, 452 const std::string& page_lang,
425 int attempt) { 453 int attempt) {
426 // The tab might have been closed. 454 // The tab might have been closed.
427 WebContents* web_contents = 455 WebContents* web_contents =
428 tab_util::GetWebContentsByID(process_id, render_id); 456 tab_util::GetWebContentsByID(process_id, render_id);
429 if (!web_contents) 457 if (!web_contents)
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 } 489 }
462 490
463 // Translation can be kicked by context menu against unsupported languages. 491 // Translation can be kicked by context menu against unsupported languages.
464 // Unsupported language strings should be replaced with 492 // Unsupported language strings should be replaced with
465 // kUnknownLanguageCode in order to send a translation request with enabling 493 // kUnknownLanguageCode in order to send a translation request with enabling
466 // server side auto language detection. 494 // server side auto language detection.
467 std::string source_lang(original_source_lang); 495 std::string source_lang(original_source_lang);
468 if (!IsSupportedLanguage(source_lang)) 496 if (!IsSupportedLanguage(source_lang))
469 source_lang = std::string(translate::kUnknownLanguageCode); 497 source_lang = std::string(translate::kUnknownLanguageCode);
470 498
471 Profile* profile = 499 if (IsEnabledTranslateNewUX()) {
472 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 500 ShowBubble(web_contents, TranslateBubble::TRANSLATING);
473 TranslateInfoBarDelegate::Create( 501 } else {
474 true, InfoBarService::FromWebContents(web_contents), 502 Profile* profile =
475 TranslateInfoBarDelegate::TRANSLATING, source_lang, target_lang, 503 Profile::FromBrowserContext(web_contents->GetBrowserContext());
476 TranslateErrors::NONE, profile->GetPrefs(), ShortcutConfig()); 504 TranslateInfoBarDelegate::Create(
505 true, InfoBarService::FromWebContents(web_contents),
506 TranslateInfoBarDelegate::TRANSLATING, source_lang, target_lang,
507 TranslateErrors::NONE, profile->GetPrefs(), ShortcutConfig());
508 }
477 509
478 DCHECK(script_.get() != NULL); 510 DCHECK(script_.get() != NULL);
479 511
480 const std::string& translate_script = script_->data(); 512 const std::string& translate_script = script_->data();
481 if (!translate_script.empty()) { 513 if (!translate_script.empty()) {
482 DoTranslatePage(web_contents, translate_script, source_lang, target_lang); 514 DoTranslatePage(web_contents, translate_script, source_lang, target_lang);
483 return; 515 return;
484 } 516 }
485 517
486 // The script is not available yet. Queue that request and query for the 518 // The script is not available yet. Queue that request and query for the
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 } 610 }
579 611
580 void TranslateManager::PageTranslated(WebContents* web_contents, 612 void TranslateManager::PageTranslated(WebContents* web_contents,
581 PageTranslatedDetails* details) { 613 PageTranslatedDetails* details) {
582 if ((details->error_type == TranslateErrors::NONE) && 614 if ((details->error_type == TranslateErrors::NONE) &&
583 details->source_language != translate::kUnknownLanguageCode && 615 details->source_language != translate::kUnknownLanguageCode &&
584 !IsSupportedLanguage(details->source_language)) { 616 !IsSupportedLanguage(details->source_language)) {
585 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; 617 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE;
586 } 618 }
587 619
588 PrefService* prefs = Profile::FromBrowserContext( 620 if (IsEnabledTranslateNewUX()) {
589 web_contents->GetBrowserContext())->GetPrefs(); 621 TranslateBubble::Type type =
590 TranslateInfoBarDelegate::Create( 622 (details->error_type == TranslateErrors::NONE) ?
591 true, InfoBarService::FromWebContents(web_contents), 623 TranslateBubble::AFTER_TRANSLATE :
592 (details->error_type == TranslateErrors::NONE) ? 624 TranslateBubble::ERROR;
593 TranslateInfoBarDelegate::AFTER_TRANSLATE : 625 ShowBubble(web_contents, type);
594 TranslateInfoBarDelegate::TRANSLATION_ERROR, 626 } else {
595 details->source_language, details->target_language, details->error_type, 627 PrefService* prefs = Profile::FromBrowserContext(
596 prefs, ShortcutConfig()); 628 web_contents->GetBrowserContext())->GetPrefs();
629 TranslateInfoBarDelegate::Create(
630 true, InfoBarService::FromWebContents(web_contents),
631 (details->error_type == TranslateErrors::NONE) ?
632 TranslateInfoBarDelegate::AFTER_TRANSLATE :
633 TranslateInfoBarDelegate::TRANSLATION_ERROR,
634 details->source_language, details->target_language, details->error_type,
635 prefs, ShortcutConfig());
636 }
597 637
598 if (details->error_type != TranslateErrors::NONE && 638 if (details->error_type != TranslateErrors::NONE &&
599 !web_contents->GetBrowserContext()->IsOffTheRecord()) { 639 !web_contents->GetBrowserContext()->IsOffTheRecord()) {
600 TranslateErrorDetails error_details; 640 TranslateErrorDetails error_details;
601 error_details.time = base::Time::Now(); 641 error_details.time = base::Time::Now();
602 error_details.url = web_contents->GetLastCommittedURL(); 642 error_details.url = web_contents->GetLastCommittedURL();
603 error_details.error = details->error_type; 643 error_details.error = details->error_type;
604 NotifyTranslateError(error_details); 644 NotifyTranslateError(error_details);
605 } 645 }
606 } 646 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 // We navigated away from the page the translation was triggered on. 684 // We navigated away from the page the translation was triggered on.
645 continue; 685 continue;
646 } 686 }
647 687
648 if (success) { 688 if (success) {
649 // Translate the page. 689 // Translate the page.
650 const std::string& translate_script = script_->data(); 690 const std::string& translate_script = script_->data();
651 DoTranslatePage(web_contents, translate_script, 691 DoTranslatePage(web_contents, translate_script,
652 request.source_lang, request.target_lang); 692 request.source_lang, request.target_lang);
653 } else { 693 } else {
654 Profile* profile = 694 if (IsEnabledTranslateNewUX()) {
655 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 695 ShowBubble(web_contents, TranslateBubble::ERROR);
656 TranslateInfoBarDelegate::Create( 696 } else {
657 true, InfoBarService::FromWebContents(web_contents), 697 Profile* profile =
658 TranslateInfoBarDelegate::TRANSLATION_ERROR, request.source_lang, 698 Profile::FromBrowserContext(web_contents->GetBrowserContext());
659 request.target_lang, TranslateErrors::NETWORK, profile->GetPrefs(), 699 TranslateInfoBarDelegate::Create(
660 ShortcutConfig()); 700 true, InfoBarService::FromWebContents(web_contents),
701 TranslateInfoBarDelegate::TRANSLATION_ERROR, request.source_lang,
702 request.target_lang, TranslateErrors::NETWORK, profile->GetPrefs(),
703 ShortcutConfig());
704 }
661 705
662 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { 706 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) {
663 TranslateErrorDetails error_details; 707 TranslateErrorDetails error_details;
664 error_details.time = base::Time::Now(); 708 error_details.time = base::Time::Now();
665 error_details.url = entry->GetURL(); 709 error_details.url = entry->GetURL();
666 error_details.error = TranslateErrors::NETWORK; 710 error_details.error = TranslateErrors::NETWORK;
667 NotifyTranslateError(error_details); 711 NotifyTranslateError(error_details);
668 } 712 }
669 } 713 }
670 } 714 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 // so we are more aggressive about showing the shortcut to never translate. 748 // so we are more aggressive about showing the shortcut to never translate.
705 #if defined(OS_ANDROID) 749 #if defined(OS_ANDROID)
706 config.never_translate_min_count = 1; 750 config.never_translate_min_count = 1;
707 #else 751 #else
708 config.never_translate_min_count = 3; 752 config.never_translate_min_count = 3;
709 #endif // defined(OS_ANDROID) 753 #endif // defined(OS_ANDROID)
710 754
711 config.always_translate_min_count = 3; 755 config.always_translate_min_count = 3;
712 return config; 756 return config;
713 } 757 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698