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

Unified 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 the browser test 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/translate/translate_manager.cc
diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc
index 7c841d2cbfb87a96498ba3e9487ddfdf1e3dfd81..d6b565ccba25fc86c1d2e768231f6df6f823e484 100644
--- a/chrome/browser/translate/translate_manager.cc
+++ b/chrome/browser/translate/translate_manager.cc
@@ -32,7 +32,9 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_tabstrip.h"
+#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
+#include "chrome/browser/ui/translate/translate_bubble_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
@@ -61,6 +63,8 @@ using content::NavigationController;
using content::NavigationEntry;
using content::WebContents;
+namespace {
+
const char kReportLanguageDetectionErrorURL[] =
"https://translate.google.com/translate_error?client=cr&action=langidc";
@@ -75,6 +79,26 @@ const char kUrlQueryName[] = "u";
// loading before giving up the translation
const int kMaxTranslateLoadCheckAttempts = 20;
+bool IsEnabledTranslateNewUX() {
+ return CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kEnableTranslateNewUX);
+}
+
+void ShowBubble(WebContents* web_contents,
+ TranslateBubbleModel::ViewState view_state) {
+ Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
+ if (!browser)
+ return;
+
+ BrowserWindow* window = browser->window();
+ if (!window)
+ return;
+
+ window->ShowTranslateBubble(web_contents, view_state);
+}
+
+} // namespace
+
TranslateManager::~TranslateManager() {
}
@@ -302,6 +326,11 @@ TranslateManager::TranslateManager()
void TranslateManager::InitiateTranslation(WebContents* web_contents,
const std::string& page_lang) {
+ TranslateTabHelper* translate_tab_helper =
+ TranslateTabHelper::FromWebContents(web_contents);
+ if (!translate_tab_helper)
+ return;
+
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
Profile* original_profile = profile->GetOriginalProfile();
@@ -395,12 +424,8 @@ void TranslateManager::InitiateTranslation(WebContents* web_contents,
}
}
- TranslateTabHelper* translate_tab_helper =
- TranslateTabHelper::FromWebContents(web_contents);
- if (!translate_tab_helper)
- return;
- std::string auto_translate_to =
- translate_tab_helper->language_state().AutoTranslateTo();
+ LanguageState& language_state = translate_tab_helper->language_state();
+ std::string auto_translate_to = language_state.AutoTranslateTo();
if (!auto_translate_to.empty()) {
// This page was navigated through a click from a translated page.
TranslateBrowserMetrics::ReportInitiationStatus(
@@ -409,13 +434,21 @@ void TranslateManager::InitiateTranslation(WebContents* web_contents,
return;
}
- // Prompts the user if he/she wants the page translated.
TranslateBrowserMetrics::ReportInitiationStatus(
TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR);
- TranslateInfoBarDelegate::Create(
- false, InfoBarService::FromWebContents(web_contents),
- TranslateInfoBarDelegate::BEFORE_TRANSLATE, language_code, target_lang,
- TranslateErrors::NONE, profile->GetPrefs(), ShortcutConfig());
+
+ if (IsEnabledTranslateNewUX()) {
+ language_state.SetTranslateEnabled(true);
+ if (language_state.IsLanguageChanged())
+ ShowBubble(web_contents,
+ TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE);
+ } else {
+ // Prompts the user if he/she wants the page translated.
+ TranslateInfoBarDelegate::Create(
+ false, InfoBarService::FromWebContents(web_contents),
+ TranslateInfoBarDelegate::BEFORE_TRANSLATE, language_code, target_lang,
+ TranslateErrors::NONE, profile->GetPrefs(), ShortcutConfig());
+ }
}
void TranslateManager::InitiateTranslationPosted(int process_id,
@@ -467,12 +500,16 @@ void TranslateManager::TranslatePage(WebContents* web_contents,
if (!IsSupportedLanguage(source_lang))
source_lang = std::string(translate::kUnknownLanguageCode);
- Profile* profile =
- Profile::FromBrowserContext(web_contents->GetBrowserContext());
- TranslateInfoBarDelegate::Create(
- true, InfoBarService::FromWebContents(web_contents),
- TranslateInfoBarDelegate::TRANSLATING, source_lang, target_lang,
- TranslateErrors::NONE, profile->GetPrefs(), ShortcutConfig());
+ if (IsEnabledTranslateNewUX()) {
+ ShowBubble(web_contents, TranslateBubbleModel::VIEW_STATE_TRANSLATING);
+ } else {
+ Profile* profile =
+ Profile::FromBrowserContext(web_contents->GetBrowserContext());
+ TranslateInfoBarDelegate::Create(
+ true, InfoBarService::FromWebContents(web_contents),
+ TranslateInfoBarDelegate::TRANSLATING, source_lang, target_lang,
+ TranslateErrors::NONE, profile->GetPrefs(), ShortcutConfig());
+ }
DCHECK(script_.get() != NULL);
@@ -584,15 +621,23 @@ void TranslateManager::PageTranslated(WebContents* web_contents,
details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE;
}
- PrefService* prefs = Profile::FromBrowserContext(
- web_contents->GetBrowserContext())->GetPrefs();
- TranslateInfoBarDelegate::Create(
- true, InfoBarService::FromWebContents(web_contents),
- (details->error_type == TranslateErrors::NONE) ?
- TranslateInfoBarDelegate::AFTER_TRANSLATE :
- TranslateInfoBarDelegate::TRANSLATION_ERROR,
- details->source_language, details->target_language, details->error_type,
- prefs, ShortcutConfig());
+ if (IsEnabledTranslateNewUX()) {
+ TranslateBubbleModel::ViewState view_state =
+ (details->error_type == TranslateErrors::NONE) ?
+ TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE :
+ TranslateBubbleModel::VIEW_STATE_ERROR;
+ ShowBubble(web_contents, view_state);
+ } else {
+ PrefService* prefs = Profile::FromBrowserContext(
+ web_contents->GetBrowserContext())->GetPrefs();
+ TranslateInfoBarDelegate::Create(
+ true, InfoBarService::FromWebContents(web_contents),
+ (details->error_type == TranslateErrors::NONE) ?
+ TranslateInfoBarDelegate::AFTER_TRANSLATE :
+ TranslateInfoBarDelegate::TRANSLATION_ERROR,
+ details->source_language, details->target_language, details->error_type,
+ prefs, ShortcutConfig());
+ }
if (details->error_type != TranslateErrors::NONE &&
!web_contents->GetBrowserContext()->IsOffTheRecord()) {
@@ -650,13 +695,17 @@ void TranslateManager::OnTranslateScriptFetchComplete(
DoTranslatePage(web_contents, translate_script,
request.source_lang, request.target_lang);
} else {
- Profile* profile =
- Profile::FromBrowserContext(web_contents->GetBrowserContext());
- TranslateInfoBarDelegate::Create(
- true, InfoBarService::FromWebContents(web_contents),
- TranslateInfoBarDelegate::TRANSLATION_ERROR, request.source_lang,
- request.target_lang, TranslateErrors::NETWORK, profile->GetPrefs(),
- ShortcutConfig());
+ if (IsEnabledTranslateNewUX()) {
+ ShowBubble(web_contents, TranslateBubbleModel::VIEW_STATE_ERROR);
+ } else {
+ Profile* profile =
+ Profile::FromBrowserContext(web_contents->GetBrowserContext());
+ TranslateInfoBarDelegate::Create(
+ true, InfoBarService::FromWebContents(web_contents),
+ TranslateInfoBarDelegate::TRANSLATION_ERROR, request.source_lang,
+ request.target_lang, TranslateErrors::NETWORK, profile->GetPrefs(),
+ ShortcutConfig());
+ }
if (!web_contents->GetBrowserContext()->IsOffTheRecord()) {
TranslateErrorDetails error_details;

Powered by Google App Engine
This is Rietveld 408576698