diff options
author | miguelg@chromium.org <miguelg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-19 09:59:52 +0000 |
---|---|---|
committer | miguelg@chromium.org <miguelg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-19 09:59:52 +0000 |
commit | d4bab9e8bdf49e57edadb7837ed5971964e15c75 (patch) | |
tree | e4bd43dd845113d80f84cdc3d04673cce9b19b9b | |
parent | 6e3e85e9d76d7b1817279d0f3641a73410f53751 (diff) | |
download | chromium_src-d4bab9e8bdf49e57edadb7837ed5971964e15c75.zip chromium_src-d4bab9e8bdf49e57edadb7837ed5971964e15c75.tar.gz chromium_src-d4bab9e8bdf49e57edadb7837ed5971964e15c75.tar.bz2 |
Manual Merge: Expose manually triggered translations on the translate delegate
On Android: Do not show Always Translate when the translation is triggered
manually.
Also set the target language if needed on the Translating... infobar
since for manually translated pages the first infobar does not show.
BUG=353528
TBR=rohitrao,avi
Review URL: https://codereview.chromium.org/204393002
git-svn-id: svn://svn.chromium.org/chrome/branches/1750/src@257895 0039d316-1c4b-4281-b951-d872f2087c98
13 files changed, 80 insertions, 41 deletions
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateAlwaysPanel.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateAlwaysPanel.java index 4a68bf3..e0d413b 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateAlwaysPanel.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateAlwaysPanel.java @@ -27,8 +27,10 @@ public class TranslateAlwaysPanel implements TranslateSubPanel { panelMessage.setText(context.getString( R.string.translate_infobar_translation_done, mOptions.targetLanguage())); - TranslateCheckBox checkBox = new TranslateCheckBox(mOptions, mListener); - checkBox.createContent(context, layout); + if (!mOptions.triggeredFromMenu()) { + TranslateCheckBox checkBox = new TranslateCheckBox(mOptions, mListener); + checkBox.createContent(context, layout); + } layout.addButtons(context.getString(R.string.translate_button_done), context.getString(R.string.translate_show_original)); diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateInfoBar.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateInfoBar.java index 0232105..dc99bef 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateInfoBar.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateInfoBar.java @@ -43,12 +43,13 @@ public class TranslateInfoBar extends TwoButtonInfoBar implements SubPanelListen public TranslateInfoBar(long nativeInfoBarPtr, TranslateInfoBarDelegate delegate, int infoBarType, int sourceLanguageIndex, int targetLanguageIndex, - boolean autoTranslatePair, boolean shouldShowNeverBar, String[] languages) { + boolean autoTranslatePair, boolean shouldShowNeverBar, + boolean triggeredFromMenu, String[] languages) { super(null, BACKGROUND_TYPE_INFO, R.drawable.infobar_translate); mTranslateDelegate = delegate; mOptions = new TranslateOptions(sourceLanguageIndex, targetLanguageIndex, languages, - autoTranslatePair); + autoTranslatePair, triggeredFromMenu); mInfoBarType = infoBarType; mShouldShowNeverBar = shouldShowNeverBar; mOptionsPanelViewType = NO_PANEL; @@ -180,10 +181,12 @@ public class TranslateInfoBar extends TwoButtonInfoBar implements SubPanelListen return; } - if (getInfoBarType() == AFTER_TRANSLATE_INFOBAR && !needsAlwaysPanel()) { + if (getInfoBarType() == AFTER_TRANSLATE_INFOBAR && + !needsAlwaysPanel() && + !mOptions.triggeredFromMenu()) { // Long always translate version - TranslateCheckBox checkBox = new TranslateCheckBox(mOptions, this); - checkBox.createContent(getContext(), layout); + TranslateCheckBox checkBox = new TranslateCheckBox(mOptions, this); + checkBox.createContent(getContext(), layout); } super.createContent(layout); @@ -330,10 +333,12 @@ public class TranslateInfoBar extends TwoButtonInfoBar implements SubPanelListen return mInfoBarType; } - void changeInfoBarTypeAndNativePointer(int infoBarType, long newNativePointer) { + void changeInfoBarTypeAndNativePointer( + int infoBarType,int newTargetLanguage, long newNativePointer) { if (infoBarType >= 0 && infoBarType < MAX_INFOBAR_INDEX) { mInfoBarType = infoBarType; replaceNativePointer(newNativePointer); + mOptions.setTargetLanguage(newTargetLanguage); updateViewForCurrentState(createView()); } else { assert false : "Trying to change the InfoBar to a type that is invalid."; diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateInfoBarDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateInfoBarDelegate.java index f0a5d9a..4d63b58 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateInfoBarDelegate.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateInfoBarDelegate.java @@ -21,8 +21,9 @@ public class TranslateInfoBarDelegate { @CalledByNative boolean changeTranslateInfoBarTypeAndPointer( - long newNativeInfoBar, int translateBarType) { - mInfoBar.changeInfoBarTypeAndNativePointer(translateBarType, newNativeInfoBar); + long newNativeInfoBar, int translateBarType, int new_target_language) { + mInfoBar.changeInfoBarTypeAndNativePointer( + translateBarType, new_target_language, newNativeInfoBar); return true; } @@ -30,10 +31,11 @@ public class TranslateInfoBarDelegate { InfoBar showTranslateInfoBar( long nativeInfoBar, int translateBarType, int sourceLanguageIndex, int targetLanguageIndex, boolean autoTranslatePair, - boolean showNeverInfobar, String[] languages) { + boolean showNeverInfobar, boolean triggeredFromMenu, + String[] languages) { mInfoBar = new TranslateInfoBar(nativeInfoBar, this, translateBarType, sourceLanguageIndex, targetLanguageIndex, autoTranslatePair, showNeverInfobar, - languages); + triggeredFromMenu, languages); return mInfoBar; } diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateOptions.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateOptions.java index 4992640..59b2c6a 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateOptions.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateOptions.java @@ -31,13 +31,15 @@ public class TranslateOptions { private final boolean[] mOptions; private int mSourceLanguageIndex; private int mTargetLanguageIndex; + private final boolean mTriggeredFromMenu; private TranslateOptions(int sourceLanguageCode, int targetLanguageCode, String[] allLanguages, boolean neverLanguage, boolean neverDomain, boolean alwaysLanguage, - boolean[] originalOptions) { + boolean triggeredFromMenu, boolean[] originalOptions) { mAllLanguages = allLanguages; mSourceLanguageIndex = sourceLanguageCode; mTargetLanguageIndex = targetLanguageCode; + mTriggeredFromMenu = triggeredFromMenu; mOptions = new boolean[3]; mOptions[NEVER_LANGUAGE] = neverLanguage; @@ -56,9 +58,9 @@ public class TranslateOptions { } public TranslateOptions(int sourceLanguageCode, int targetLanguageCode, String[] allLanguages, - boolean alwaysTranslate) { + boolean alwaysTranslate, boolean triggeredFromMenu) { this(sourceLanguageCode, targetLanguageCode, allLanguages, false, false, alwaysTranslate, - null); + triggeredFromMenu, null); } /** @@ -67,7 +69,8 @@ public class TranslateOptions { public TranslateOptions(TranslateOptions other) { this(other.mSourceLanguageIndex, other.mTargetLanguageIndex, other.mAllLanguages, other.mOptions[NEVER_LANGUAGE], other.mOptions[NEVER_DOMAIN], - other.mOptions[ALWAYS_LANGUAGE], other.mOriginalOptions); + other.mOptions[ALWAYS_LANGUAGE], other.mTriggeredFromMenu, + other.mOriginalOptions); } public String sourceLanguage() { @@ -90,6 +93,10 @@ public class TranslateOptions { return checkLanguageBoundaries(mTargetLanguageIndex) ? mTargetLanguageIndex : 0; } + public boolean triggeredFromMenu() { + return mTriggeredFromMenu; + } + public boolean optionsChanged() { return (mSourceLanguageIndex != mOriginalSourceLanguageIndex) || (mTargetLanguageIndex != mOriginalTargetLanguageIndex) || diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/translate/TranslateOptionsTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/translate/TranslateOptionsTest.java index 494e0bb..7447810 100644 --- a/chrome/android/javatests/src/org/chromium/chrome/browser/translate/TranslateOptionsTest.java +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/translate/TranslateOptionsTest.java @@ -20,7 +20,7 @@ public class TranslateOptionsTest extends AndroidTestCase { @SmallTest @Feature({"Translate"}) public void testNoChanges() { - TranslateOptions options = new TranslateOptions(0, 1, languages, ALWAYS_TRANSLATE); + TranslateOptions options = new TranslateOptions(0, 1, languages, ALWAYS_TRANSLATE, false); assertEquals("English", options.sourceLanguage()); assertEquals("Spanish", options.targetLanguage()); assertEquals(0, options.sourceLanguageIndex()); @@ -34,13 +34,14 @@ public class TranslateOptionsTest extends AndroidTestCase { @SmallTest @Feature({"Translate"}) public void testBasicLanguageChanges() { - TranslateOptions options = new TranslateOptions(0, 1, languages, !ALWAYS_TRANSLATE); + TranslateOptions options = new TranslateOptions(0, 1, languages, !ALWAYS_TRANSLATE, true); options.setTargetLanguage(2); options.setSourceLanguage(1); assertEquals("Spanish", options.sourceLanguage()); assertEquals("French", options.targetLanguage()); assertEquals(1, options.sourceLanguageIndex()); assertEquals(2, options.targetLanguageIndex()); + assertTrue(options.triggeredFromMenu()); assertTrue(options.optionsChanged()); // Switch back to the original @@ -52,7 +53,7 @@ public class TranslateOptionsTest extends AndroidTestCase { @SmallTest @Feature({"Translate"}) public void testInvalidLanguageChanges() { - TranslateOptions options = new TranslateOptions(0, 1, languages, ALWAYS_TRANSLATE); + TranslateOptions options = new TranslateOptions(0, 1, languages, ALWAYS_TRANSLATE, false); // Same target language as source assertFalse(options.setTargetLanguage(0)); @@ -74,7 +75,7 @@ public class TranslateOptionsTest extends AndroidTestCase { @SmallTest @Feature({"Translate"}) public void testBasicOptionsChanges() { - TranslateOptions options = new TranslateOptions(0, 1, languages, !ALWAYS_TRANSLATE); + TranslateOptions options = new TranslateOptions(0, 1, languages, !ALWAYS_TRANSLATE, false); assertFalse(options.optionsChanged()); options.toggleNeverTranslateDomainState(true); assertTrue(options.neverTranslateDomainState()); @@ -98,7 +99,7 @@ public class TranslateOptionsTest extends AndroidTestCase { @SmallTest @Feature({"Translate"}) public void testInvalidOptionsChanges() { - TranslateOptions options = new TranslateOptions(0, 1, languages, ALWAYS_TRANSLATE); + TranslateOptions options = new TranslateOptions(0, 1, languages, ALWAYS_TRANSLATE, false); // Never translate language should not work, but never translate domain should assertFalse(options.toggleNeverTranslateLanguageState(true)); diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc index b1fd407..1710b74 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.cc +++ b/chrome/browser/tab_contents/render_view_context_menu.cc @@ -1835,7 +1835,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { prefs.UnblockLanguage(original_lang); prefs.RemoveSiteFromBlacklist(params_.page_url.HostNoBrackets()); TranslateManager::GetInstance()->TranslatePage( - source_web_contents_, original_lang, target_lang); + source_web_contents_, original_lang, target_lang, true); break; } diff --git a/chrome/browser/translate/translate_infobar_delegate.cc b/chrome/browser/translate/translate_infobar_delegate.cc index c578a50..01a64b6 100644 --- a/chrome/browser/translate/translate_infobar_delegate.cc +++ b/chrome/browser/translate/translate_infobar_delegate.cc @@ -40,7 +40,8 @@ void TranslateInfoBarDelegate::Create( const std::string& target_language, TranslateErrors::Type error_type, PrefService* prefs, - const ShortcutConfiguration& shortcut_config) { + const ShortcutConfiguration& shortcut_config, + bool triggered_from_menu) { // Check preconditions. if (infobar_type != TRANSLATION_ERROR) { DCHECK(TranslateManager::IsSupportedLanguage(target_language)); @@ -82,7 +83,8 @@ void TranslateInfoBarDelegate::Create( scoped_ptr<InfoBar> infobar(CreateInfoBar( scoped_ptr<TranslateInfoBarDelegate>(new TranslateInfoBarDelegate( web_contents, infobar_type, old_delegate, original_language, - target_language, error_type, prefs, shortcut_config)))); + target_language, error_type, prefs, shortcut_config, + triggered_from_menu)))); if (old_delegate) infobar_service->ReplaceInfoBar(old_infobar, infobar.Pass()); else @@ -226,7 +228,8 @@ void TranslateInfoBarDelegate::MessageInfoBarButtonPressed() { } // This is the "Try again..." case. TranslateManager::GetInstance()->TranslatePage( - web_contents(), original_language_code(), target_language_code()); + web_contents(), original_language_code(), + target_language_code(), false); } bool TranslateInfoBarDelegate::ShouldShowMessageInfoBarButton() { @@ -297,14 +300,16 @@ TranslateInfoBarDelegate::TranslateInfoBarDelegate( const std::string& target_language, TranslateErrors::Type error_type, PrefService* prefs, - ShortcutConfiguration shortcut_config) + ShortcutConfiguration shortcut_config, + bool triggered_from_menu) : InfoBarDelegate(), infobar_type_(infobar_type), background_animation_(NONE), ui_delegate_(web_contents, original_language, target_language), error_type_(error_type), prefs_(prefs), - shortcut_config_(shortcut_config) { + shortcut_config_(shortcut_config), + triggered_from_menu_(triggered_from_menu) { DCHECK_NE((infobar_type_ == TRANSLATION_ERROR), (error_type_ == TranslateErrors::NONE)); diff --git a/chrome/browser/translate/translate_infobar_delegate.h b/chrome/browser/translate/translate_infobar_delegate.h index 01f0722..630f718 100644 --- a/chrome/browser/translate/translate_infobar_delegate.h +++ b/chrome/browser/translate/translate_infobar_delegate.h @@ -67,7 +67,8 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { const std::string& target_language, TranslateErrors::Type error_type, PrefService* prefs, - const ShortcutConfiguration& shortcut_config); + const ShortcutConfiguration& shortcut_config, + bool triggered_from_menu); // Returns the number of languages supported. size_t num_languages() const { return ui_delegate_.GetNumberOfLanguages(); } @@ -108,6 +109,12 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { // should get a yellow background instead of a blue one). bool is_error() const { return infobar_type_ == TRANSLATION_ERROR; } + // Return true if the translation was triggered by a menu entry instead of + // via an infobar/bubble or preference. + bool triggered_from_menu() const { + return triggered_from_menu_; + } + // Returns what kind of background fading effect the infobar should use when // its is shown. BackgroundAnimationType background_animation_type() const { @@ -180,7 +187,8 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { const std::string& target_language, TranslateErrors::Type error_type, PrefService* prefs, - ShortcutConfiguration shortcut_config); + ShortcutConfiguration shortcut_config, + bool triggered_from_menu); private: friend class TranslationInfoBarTest; @@ -214,6 +222,10 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { // Translation shortcut configuration ShortcutConfiguration shortcut_config_; + + // Whether the translation was triggered via a menu click vs automatically + // (due to language detection, preferences...) + bool triggered_from_menu_; DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate); }; diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc index 7ea6093..b8bb2bb 100644 --- a/chrome/browser/translate/translate_manager.cc +++ b/chrome/browser/translate/translate_manager.cc @@ -399,7 +399,7 @@ void TranslateManager::InitiateTranslation(WebContents* web_contents, if (!auto_target_lang.empty()) { TranslateBrowserMetrics::ReportInitiationStatus( TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG); - TranslatePage(web_contents, language_code, auto_target_lang); + TranslatePage(web_contents, language_code, auto_target_lang, false); return; } } @@ -410,7 +410,7 @@ void TranslateManager::InitiateTranslation(WebContents* web_contents, // This page was navigated through a click from a translated page. TranslateBrowserMetrics::ReportInitiationStatus( TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK); - TranslatePage(web_contents, language_code, auto_translate_to); + TranslatePage(web_contents, language_code, auto_translate_to, false); return; } @@ -429,7 +429,7 @@ void TranslateManager::InitiateTranslation(WebContents* web_contents, TranslateInfoBarDelegate::Create( false, web_contents, TranslateInfoBarDelegate::BEFORE_TRANSLATE, language_code, target_lang, TranslateErrors::NONE, profile->GetPrefs(), - ShortcutConfig()); + ShortcutConfig(), false); } } @@ -467,7 +467,8 @@ void TranslateManager::InitiateTranslationPosted(int process_id, void TranslateManager::TranslatePage(WebContents* web_contents, const std::string& original_source_lang, - const std::string& target_lang) { + const std::string& target_lang, + bool triggered_from_menu) { NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); if (!entry) { NOTREACHED(); @@ -491,7 +492,7 @@ void TranslateManager::TranslatePage(WebContents* web_contents, TranslateInfoBarDelegate::Create( true, web_contents, TranslateInfoBarDelegate::TRANSLATING, source_lang, target_lang, TranslateErrors::NONE, profile->GetPrefs(), - ShortcutConfig()); + ShortcutConfig(), triggered_from_menu); } DCHECK(script_.get() != NULL); @@ -619,7 +620,7 @@ void TranslateManager::PageTranslated(WebContents* web_contents, TranslateInfoBarDelegate::AFTER_TRANSLATE : TranslateInfoBarDelegate::TRANSLATION_ERROR, details->source_language, details->target_language, details->error_type, - prefs, ShortcutConfig()); + prefs, ShortcutConfig(), false); } if (details->error_type != TranslateErrors::NONE && @@ -687,7 +688,7 @@ void TranslateManager::OnTranslateScriptFetchComplete( TranslateInfoBarDelegate::Create( true, web_contents, TranslateInfoBarDelegate::TRANSLATION_ERROR, request.source_lang, request.target_lang, TranslateErrors::NETWORK, - profile->GetPrefs(), ShortcutConfig()); + profile->GetPrefs(), ShortcutConfig(), false); } if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h index 4476bae..07050ad 100644 --- a/chrome/browser/translate/translate_manager.h +++ b/chrome/browser/translate/translate_manager.h @@ -108,7 +108,8 @@ class TranslateManager : public content::NotificationObserver { // script is not yet available. void TranslatePage(content::WebContents* web_contents, const std::string& source_lang, - const std::string& target_lang); + const std::string& target_lang, + bool triggered_from_menu); // Reverts the contents of the page in |web_contents| to its original // language. diff --git a/chrome/browser/translate/translate_ui_delegate.cc b/chrome/browser/translate/translate_ui_delegate.cc index a5348bc..d7bb048c 100644 --- a/chrome/browser/translate/translate_ui_delegate.cc +++ b/chrome/browser/translate/translate_ui_delegate.cc @@ -158,7 +158,8 @@ void TranslateUIDelegate::Translate() { } TranslateManager::GetInstance()->TranslatePage(web_contents(), GetOriginalLanguageCode(), - GetTargetLanguageCode()); + GetTargetLanguageCode(), + false); UMA_HISTOGRAM_BOOLEAN(kPerformTranslate, true); } diff --git a/chrome/browser/ui/android/infobars/translate_infobar.cc b/chrome/browser/ui/android/infobars/translate_infobar.cc index bf73d80..0784a7a 100644 --- a/chrome/browser/ui/android/infobars/translate_infobar.cc +++ b/chrome/browser/ui/android/infobars/translate_infobar.cc @@ -47,7 +47,8 @@ ScopedJavaLocalRef<jobject> TranslateInfoBar::CreateRenderInfoBar(JNIEnv* env) { env, java_translate_delegate_.obj(), reinterpret_cast<intptr_t>(this), delegate->infobar_type(), delegate->original_language_index(), delegate->target_language_index(), delegate->ShouldAlwaysTranslate(), - ShouldDisplayNeverTranslateInfoBarOnCancel(), java_languages.obj()); + ShouldDisplayNeverTranslateInfoBarOnCancel(), + delegate->triggered_from_menu(), java_languages.obj()); } void TranslateInfoBar::ProcessButton(int action, @@ -106,10 +107,11 @@ void TranslateInfoBar::ApplyTranslateOptions(JNIEnv* env, void TranslateInfoBar::TransferOwnership( TranslateInfoBar* destination, TranslateInfoBarDelegate::Type new_type) { + int new_target_language = destination->GetDelegate()->target_language_index(); JNIEnv* env = base::android::AttachCurrentThread(); if (Java_TranslateInfoBarDelegate_changeTranslateInfoBarTypeAndPointer( env, java_translate_delegate_.obj(), - reinterpret_cast<intptr_t>(destination), new_type)) { + reinterpret_cast<intptr_t>(destination), new_type, new_target_language)) { ReassignJavaInfoBar(destination); destination->SetJavaDelegate(java_translate_delegate_.Release()); } diff --git a/chrome/browser/ui/cocoa/infobars/translate_infobar_unittest.mm b/chrome/browser/ui/cocoa/infobars/translate_infobar_unittest.mm index b6e460e..9a6a82a 100644 --- a/chrome/browser/ui/cocoa/infobars/translate_infobar_unittest.mm +++ b/chrome/browser/ui/cocoa/infobars/translate_infobar_unittest.mm @@ -42,7 +42,7 @@ class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { PrefService* prefs, ShortcutConfiguration config) : TranslateInfoBarDelegate(web_contents, type, NULL, "en", "es", error, - prefs, config) { + prefs, config, false) { } MOCK_METHOD0(Translate, void()); |