diff options
author | jcivelli@chromium.org <jcivelli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-25 06:44:41 +0000 |
---|---|---|
committer | jcivelli@chromium.org <jcivelli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-25 06:44:41 +0000 |
commit | 17909c7a380dc1dfda8166e07fc67d73ad330795 (patch) | |
tree | 377ac5c2bd8f697802b9d89b181a0ce5213e6f08 /chrome/browser/translate | |
parent | 0415650844f13b0f13ed25b4bea2b1cdfeafe5c7 (diff) | |
download | chromium_src-17909c7a380dc1dfda8166e07fc67d73ad330795.zip chromium_src-17909c7a380dc1dfda8166e07fc67d73ad330795.tar.gz chromium_src-17909c7a380dc1dfda8166e07fc67d73ad330795.tar.bz2 |
Adding a context menu to bring up or bring back the translate infobar
when it is not showing.
BUG=35480
TEST=Open a page so that the translate infobar is shown. Close the
infobar. Use the page translate context menu, make sure it
works. Do the same thing but make sure you translate the page
first.
Make sure the menu is not enabled on Chrome specific pages (new
tab, history, downloads...) as well as on interstitials.
Review URL: http://codereview.chromium.org/1151003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42582 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/translate')
-rw-r--r-- | chrome/browser/translate/translate_manager.cc | 67 | ||||
-rw-r--r-- | chrome/browser/translate/translate_manager.h | 31 | ||||
-rw-r--r-- | chrome/browser/translate/translate_manager_unittest.cc | 39 |
3 files changed, 121 insertions, 16 deletions
diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc index c494208..2e7dd2f 100644 --- a/chrome/browser/translate/translate_manager.cc +++ b/chrome/browser/translate/translate_manager.cc @@ -30,6 +30,10 @@ bool TranslateManager::test_enabled_ = false; TranslateManager::~TranslateManager() { } +bool TranslateManager::IsTranslatableURL(const GURL& url) { + return !url.SchemeIs("chrome"); +} + void TranslateManager::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { @@ -124,6 +128,39 @@ void TranslateManager::Observe(NotificationType type, } } +// static +bool TranslateManager::ShowInfoBar(TabContents* tab) { + NavigationEntry* nav_entry = tab->controller().GetActiveEntry(); + if (!nav_entry || IsShowingTranslateInfobar(tab)) + return false; + + LanguageState& language_state = tab->language_state(); + std::string target_lang; + TranslateInfoBarDelegate::TranslateState state; + if (language_state.IsPageTranslated()) { + state = TranslateInfoBarDelegate::kAfterTranslate; + target_lang = language_state.current_language(); + } else { + state = TranslateInfoBarDelegate::kBeforeTranslate; + target_lang = GetTargetLanguage(); + if (target_lang.empty()) + return false; // The language Chrome is in is not supported. + } + + AddTranslateInfoBar(tab, state, nav_entry->url(), + language_state.original_language(), target_lang); + return true; +} + +// static +bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { + for (int i = 0; i < tab->infobar_delegate_count(); ++i) { + if (tab->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate()) + return true; + } + return false; +} + TranslateManager::TranslateManager() : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { if (!test_enabled_ && !TranslationService::IsTranslationEnabled()) @@ -145,16 +182,14 @@ void TranslateManager::InitiateTranslation(TabContents* tab, NavigationEntry* entry = tab->controller().GetActiveEntry(); if (!entry) { - NOTREACHED(); + // This can happen for popups created with window.open(""). return; } - std::string ui_lang = TranslationService::GetLanguageCode( - g_browser_process->GetApplicationLocale()); - - // Nothing to do if the language Chrome is in or the language of the page is - // not supported by the translation server. - if (!TranslationService::IsSupportedLanguage(ui_lang) || + std::string target_lang = GetTargetLanguage(); + // Nothing to do if either the language Chrome is in or the language of the + // page is not supported by the translation server. + if (target_lang.empty() || !TranslationService::IsSupportedLanguage(page_lang)) { return; } @@ -164,7 +199,7 @@ void TranslateManager::InitiateTranslation(TabContents* tab, // - similar languages (ex: en-US to en). // - any user black-listed URLs or user selected language combination. // - any language the user configured as accepted languages. - if (entry->url().SchemeIs("chrome") || page_lang == ui_lang || + if (!IsTranslatableURL(entry->url()) || page_lang == target_lang || !TranslatePrefs::CanTranslate(prefs, page_lang, entry->url()) || IsAcceptLanguage(tab, page_lang)) { return; @@ -174,9 +209,9 @@ void TranslateManager::InitiateTranslation(TabContents* tab, // automatically translate. Note that in incognito mode we disable that // feature; the user will get an infobar, so they can control whether the // page's text is sent to the translate server. - if (TranslatePrefs::ShouldAutoTranslate(prefs, page_lang, ui_lang) && + if (TranslatePrefs::ShouldAutoTranslate(prefs, page_lang, target_lang) && !tab->profile()->IsOffTheRecord()) { - tab->TranslatePage(page_lang, ui_lang); + tab->TranslatePage(page_lang, target_lang); return; } @@ -189,7 +224,7 @@ void TranslateManager::InitiateTranslation(TabContents* tab, // Prompts the user if he/she wants the page translated. AddTranslateInfoBar(tab, TranslateInfoBarDelegate::kBeforeTranslate, - entry->url(), page_lang, ui_lang); + entry->url(), page_lang, target_lang); } void TranslateManager::InitiateTranslationPosted(int process_id, @@ -254,6 +289,7 @@ void TranslateManager::InitAcceptLanguages(PrefService* prefs) { accept_languages_[prefs] = accept_langs_set; } +// static void TranslateManager::AddTranslateInfoBar( TabContents* tab, TranslateInfoBarDelegate::TranslateState state, const GURL& url, @@ -269,3 +305,12 @@ void TranslateManager::AddTranslateInfoBar( } tab->AddInfoBar(infobar); } + +// static +std::string TranslateManager::GetTargetLanguage() { + std::string target_lang = TranslationService::GetLanguageCode( + g_browser_process->GetApplicationLocale()); + if (TranslationService::IsSupportedLanguage(target_lang)) + return target_lang; + return std::string(); +} diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h index 7ac1eb8..704d928 100644 --- a/chrome/browser/translate/translate_manager.h +++ b/chrome/browser/translate/translate_manager.h @@ -15,6 +15,7 @@ #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" +class GURL; class PrefService; class TabContents; @@ -27,11 +28,25 @@ class TranslateManager : public NotificationObserver { public: virtual ~TranslateManager(); + // Returns true if the URL can be translated, if it is not an internal URL + // (chrome:// and others). + bool IsTranslatableURL(const GURL& url); + // NotificationObserver implementation: virtual void Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details); + // Shows the translate infobar if it's not already showing. The state and + // languages are determined based on the current state of the page. + // Returns true if a new infobar was shown as a result of this call, false + // otherwise (if there was already a translate infobar or if there is no + // current navigation entry). + static bool ShowInfoBar(TabContents* tab); + + // Convenience method to know if a tab is showing a translate infobar. + static bool IsShowingTranslateInfobar(TabContents* tab); + // Used by unit-test to enable the TranslateManager for testing purpose. static void set_test_enabled(bool enabled) { test_enabled_ = enabled; } @@ -60,11 +75,17 @@ class TranslateManager : public NotificationObserver { void InitAcceptLanguages(PrefService* prefs); // Convenience method that adds a translate infobar to |tab|. - void AddTranslateInfoBar(TabContents* tab, - TranslateInfoBarDelegate::TranslateState state, - const GURL& url, - const std::string& original_language, - const std::string& target_language); + static void AddTranslateInfoBar( + TabContents* tab, + TranslateInfoBarDelegate::TranslateState state, + const GURL& url, + const std::string& original_language, + const std::string& target_language); + + // Returns the language to translate to, which is the language the UI is + // configured in. Returns an empty string if that language is not supported + // by the translation service. + static std::string GetTargetLanguage(); NotificationRegistrar notification_registrar_; diff --git a/chrome/browser/translate/translate_manager_unittest.cc b/chrome/browser/translate/translate_manager_unittest.cc index 03c7be8..13278c5 100644 --- a/chrome/browser/translate/translate_manager_unittest.cc +++ b/chrome/browser/translate/translate_manager_unittest.cc @@ -681,3 +681,42 @@ TEST_F(TranslateManagerTest, AlwaysTranslateLanguagePref) { EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); EXPECT_TRUE(GetTranslateInfoBar() != NULL); } + +// Tests TranslateManager::ShowInfoBar. +TEST_F(TranslateManagerTest, ShowInfoBar) { + // Simulate navigating to a page and getting its language. + SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); + TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); + EXPECT_TRUE(infobar != NULL); + + // ShowInfobar should have no effect since a bar is already showing. + EXPECT_FALSE(TranslateManager::ShowInfoBar(contents())); + // The infobar should still be showing. + EXPECT_EQ(infobar, GetTranslateInfoBar()); + + // Close the infobar. + EXPECT_TRUE(CloseTranslateInfoBar()); + + // ShowInfoBar should bring back the infobar. + EXPECT_TRUE(TranslateManager::ShowInfoBar(contents())); + infobar = GetTranslateInfoBar(); + ASSERT_TRUE(infobar != NULL); + + // Translate. + infobar->Translate(); + rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en")); + + // Test again that ShowInfobar has no effect since a bar is already showing. + EXPECT_FALSE(TranslateManager::ShowInfoBar(contents())); + EXPECT_EQ(infobar, GetTranslateInfoBar()); + + // Close the infobar. + EXPECT_TRUE(CloseTranslateInfoBar()); + + // ShowInfoBar should bring back the infobar, with the right languages. + EXPECT_TRUE(TranslateManager::ShowInfoBar(contents())); + infobar = GetTranslateInfoBar(); + ASSERT_TRUE(infobar != NULL); + EXPECT_EQ("fr", infobar->original_lang_code()); + EXPECT_EQ("en", infobar->target_lang_code()); +} |