diff options
author | jcivelli@google.com <jcivelli@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-14 18:13:40 +0000 |
---|---|---|
committer | jcivelli@google.com <jcivelli@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-14 18:13:40 +0000 |
commit | 74a5fec1709b6c1621ced52ddecf54004d8d09c0 (patch) | |
tree | 262109ba5f6f9a3fc863a2dae3fcdca2d3eb14e1 /chrome/browser/translate | |
parent | 969e76b80b5f7cded1980935dab6882a0fd45e92 (diff) | |
download | chromium_src-74a5fec1709b6c1621ced52ddecf54004d8d09c0.zip chromium_src-74a5fec1709b6c1621ced52ddecf54004d8d09c0.tar.gz chromium_src-74a5fec1709b6c1621ced52ddecf54004d8d09c0.tar.bz2 |
Show an error infobar when we fail to retrieve the translate script.
BUG=41349
TEST=Visit a page in a language different than the Chrome UI language.
Disconnect the network cable. Click Translate in the infobar.
After some time an error infobar should be shown.
Review URL: http://codereview.chromium.org/1602024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44501 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/translate')
-rw-r--r-- | chrome/browser/translate/translate_manager.cc | 71 | ||||
-rw-r--r-- | chrome/browser/translate/translate_manager.h | 4 | ||||
-rw-r--r-- | chrome/browser/translate/translate_manager_unittest.cc | 5 |
3 files changed, 53 insertions, 27 deletions
diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc index e58cc9d..e1d2b82 100644 --- a/chrome/browser/translate/translate_manager.cc +++ b/chrome/browser/translate/translate_manager.cc @@ -24,6 +24,7 @@ #include "chrome/common/notification_source.h" #include "chrome/common/notification_type.h" #include "chrome/common/pref_names.h" +#include "chrome/common/translate_errors.h" #include "grit/browser_resources.h" #include "net/url_request/url_request_status.h" @@ -214,16 +215,10 @@ void TranslateManager::Observe(NotificationType type, (page_translated_details->error_type == TranslateErrors::NONE ? TranslateInfoBarDelegate::kAfterTranslate : TranslateInfoBarDelegate::kTranslateError); - int i; - for (i = 0; i < tab->infobar_delegate_count(); ++i) { - TranslateInfoBarDelegate* info_bar = - tab->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); - if (info_bar) { - info_bar->UpdateState(state, page_translated_details->error_type); - break; - } - } - if (i == tab->infobar_delegate_count()) { + TranslateInfoBarDelegate* info_bar = GetTranslateInfoBarDelegate(tab); + if (info_bar) { + info_bar->UpdateState(state, page_translated_details->error_type); + } else { NavigationEntry* entry = tab->controller().GetActiveEntry(); if (entry) { AddTranslateInfoBar(tab, state, entry->url(), @@ -265,16 +260,18 @@ void TranslateManager::OnURLFetchComplete(const URLFetcher* source, scoped_ptr<const URLFetcher> delete_ptr(source); DCHECK(translate_script_request_pending_); translate_script_request_pending_ = false; - if (status.status() != URLRequestStatus::SUCCESS || response_code != 200) - return; // We could not retrieve the translate script. - - base::StringPiece str = ResourceBundle::GetSharedInstance(). - GetRawDataResource(IDR_TRANSLATE_JS); - DCHECK(translate_script_.empty()); - str.CopyToString(&translate_script_); - translate_script_ += "\n" + data; + bool error = + (status.status() != URLRequestStatus::SUCCESS || response_code != 200); + + if (!error) { + base::StringPiece str = ResourceBundle::GetSharedInstance(). + GetRawDataResource(IDR_TRANSLATE_JS); + DCHECK(translate_script_.empty()); + str.CopyToString(&translate_script_); + translate_script_ += "\n" + data; + } - // Execute any pending requests. + // Process any pending requests. std::vector<PendingRequest>::const_iterator iter; for (iter = pending_requests_.begin(); iter != pending_requests_.end(); ++iter) { @@ -291,20 +288,28 @@ void TranslateManager::OnURLFetchComplete(const URLFetcher* source, continue; } - // Translate the page. - DoTranslatePage(tab, translate_script_, - request.source_lang, request.target_lang); + if (error) { + // TODO(jcivelli): http://crbug.com/40828 The translate infobar delegate + // should notify the info-bar so it updates its + // appearance so we don't have to send a notification. + PageTranslatedDetails details(request.source_lang, request.target_lang, + TranslateErrors::NETWORK); + NotificationService::current()->Notify( + NotificationType::PAGE_TRANSLATED, + Source<TabContents>(tab), + Details<PageTranslatedDetails>(&details)); + } else { + // Translate the page. + DoTranslatePage(tab, translate_script_, + request.source_lang, request.target_lang); + } } pending_requests_.clear(); } // 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; + return GetTranslateInfoBarDelegate(tab) != NULL; } TranslateManager::TranslateManager() @@ -525,3 +530,15 @@ std::string TranslateManager::GetTargetLanguage() { return target_lang; return std::string(); } + +// static +TranslateInfoBarDelegate* TranslateManager::GetTranslateInfoBarDelegate( + TabContents* tab) { + for (int i = 0; i < tab->infobar_delegate_count(); ++i) { + TranslateInfoBarDelegate* delegate = + tab->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); + if (delegate) + return delegate; + } + return NULL; +} diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h index 5c7bbb2..14db089 100644 --- a/chrome/browser/translate/translate_manager.h +++ b/chrome/browser/translate/translate_manager.h @@ -138,6 +138,10 @@ class TranslateManager : public NotificationObserver, // by the translation service. static std::string GetTargetLanguage(); + // Returns the translate info bar showing in |tab| or NULL if none is showing. + static TranslateInfoBarDelegate* GetTranslateInfoBarDelegate( + TabContents* tab); + NotificationRegistrar notification_registrar_; // A map that associates a profile with its parsed "accept languages". diff --git a/chrome/browser/translate/translate_manager_unittest.cc b/chrome/browser/translate/translate_manager_unittest.cc index 45432c5..33852b6 100644 --- a/chrome/browser/translate/translate_manager_unittest.cc +++ b/chrome/browser/translate/translate_manager_unittest.cc @@ -354,6 +354,11 @@ TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) { // We should not have sent any message to translate to the renderer. EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); + + // And we should have an error infobar showing. + infobar = GetTranslateInfoBar(); + ASSERT_TRUE(infobar != NULL); + EXPECT_EQ(TranslateInfoBarDelegate::kTranslateError, infobar->state()); } // Tests that we show/don't show an info-bar for all languages the CLD can |