summaryrefslogtreecommitdiffstats
path: root/chrome/browser/translate
diff options
context:
space:
mode:
authorkuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-29 16:41:53 +0000
committerkuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-29 16:41:53 +0000
commit8db841ac0528a7eba71e89260526da9ba33dbb54 (patch)
tree79501d52aa828b2bbaf87e91064f978851d702d2 /chrome/browser/translate
parent89e29d25efa776ed1f564e576d764fd3e9ddecfc (diff)
downloadchromium_src-8db841ac0528a7eba71e89260526da9ba33dbb54.zip
chromium_src-8db841ac0528a7eba71e89260526da9ba33dbb54.tar.gz
chromium_src-8db841ac0528a7eba71e89260526da9ba33dbb54.tar.bz2
implement error state for translate infobar
- this cl implements the UI on Windows and the partial backend of IPC messaging to include error type - implement error state - add translate error types - use a structure as details for IPC messaging between render view and browser so as to include error type (was using std::pair) - translate delegate handles error state and provides mapping to error messages - infobar handles visual error states - modify background painting to handle normal and error backgrounds, and animation of cross-fading between the 2 backgrounds - infobar now stores state (and translation_pending flag) that it's currently displaying to user, instead of just relying on TransateInfoBarDelegate's - if infobar receives PAGE_TRANSLAED notification before delegate does (possible because order is not fixed), delegate's state won't be updated to be used by infobar. - after all the observers have received the notification, both infobar and delegate will end up with matching states, so there's no worries of out-of-sync. - update unittests accordingly - update mac and linux code accordingly to make build pass - jay will implement the remaining backend to pass actual translate error types to the IPC message (tracked by bug 37778) BUG=38548 TEST=none yet, until bug 37778 is also fixed. Review URL: http://codereview.chromium.org/1321003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42953 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/translate')
-rw-r--r--chrome/browser/translate/page_translated_details.h26
-rw-r--r--chrome/browser/translate/translate_infobars_delegates.cc42
-rw-r--r--chrome/browser/translate/translate_infobars_delegates.h24
-rw-r--r--chrome/browser/translate/translate_manager.cc27
-rw-r--r--chrome/browser/translate/translate_manager.h4
-rw-r--r--chrome/browser/translate/translate_manager_unittest.cc15
6 files changed, 109 insertions, 29 deletions
diff --git a/chrome/browser/translate/page_translated_details.h b/chrome/browser/translate/page_translated_details.h
new file mode 100644
index 0000000..61a43d4
--- /dev/null
+++ b/chrome/browser/translate/page_translated_details.h
@@ -0,0 +1,26 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_TRANSLATE_PAGE_TRANSLATED_DETAILS_H_
+#define CHROME_BROWSER_TRANSLATE_PAGE_TRANSLATED_DETAILS_H_
+
+#include <string>
+
+#include "chrome/common/translate_errors.h"
+
+// Used when sending a notification about a page that has been translated.
+struct PageTranslatedDetails {
+ PageTranslatedDetails(const std::string& in_source_language,
+ const std::string& in_target_language,
+ TranslateErrors::Type in_error_type)
+ : source_language(in_source_language),
+ target_language(in_target_language),
+ error_type(in_error_type) { }
+
+ std::string source_language;
+ std::string target_language;
+ TranslateErrors::Type error_type;
+};
+
+#endif // CHROME_BROWSER_TRANSLATE_PAGE_TRANSLATED_DETAILS_H_
diff --git a/chrome/browser/translate/translate_infobars_delegates.cc b/chrome/browser/translate/translate_infobars_delegates.cc
index e13be6a..5caff33 100644
--- a/chrome/browser/translate/translate_infobars_delegates.cc
+++ b/chrome/browser/translate/translate_infobars_delegates.cc
@@ -48,8 +48,10 @@ string16 TranslateInfoBarDelegate::GetDisplayNameForLocale(
language_code, g_browser_process->GetApplicationLocale(), true);
}
-void TranslateInfoBarDelegate::UpdateState(TranslateState new_state) {
+void TranslateInfoBarDelegate::UpdateState(TranslateState new_state,
+ TranslateErrors::Type error_type) {
translation_pending_ = false;
+ error_type_ = error_type;
if (state_ != new_state)
state_ = new_state;
}
@@ -137,14 +139,15 @@ void TranslateInfoBarDelegate::ToggleAlwaysTranslate() {
target_lang_code());
}
-void TranslateInfoBarDelegate::GetMessageText(string16 *message_text,
- std::vector<size_t> *offsets, bool *swapped_language_placeholders) {
+void TranslateInfoBarDelegate::GetMessageText(
+ TranslateInfoBarDelegate::TranslateState state, string16 *message_text,
+ std::vector<size_t> *offsets, bool *swapped_language_placeholders) {
*swapped_language_placeholders = false;
offsets->clear();
std::vector<size_t> offsets_tmp;
int message_resource_id = IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE;
- if (state() == kAfterTranslate)
+ if (state == kAfterTranslate)
message_resource_id = IDS_TRANSLATE_INFOBAR_AFTER_MESSAGE;
*message_text = l10n_util::GetStringFUTF16(message_resource_id,
string16(), string16(), &offsets_tmp);
@@ -165,13 +168,33 @@ void TranslateInfoBarDelegate::GetMessageText(string16 *message_text,
*offsets = offsets_tmp;
}
+string16 TranslateInfoBarDelegate::GetErrorMessage(
+ TranslateErrors::Type error_type) {
+ int message_id = 0;
+ switch (error_type) {
+ case TranslateErrors::NONE:
+ return string16();
+ case TranslateErrors::NETWORK:
+ message_id = IDS_TRANSLATE_INFOBAR_ERROR_CANT_CONNECT;
+ break;
+ case TranslateErrors::SERVER:
+ message_id = IDS_TRANSLATE_INFOBAR_ERROR_CANT_TRANSLATE;
+ break;
+ default:
+ NOTREACHED() << "Invalid translate error type";
+ break;
+ }
+ return l10n_util::GetStringUTF16(message_id);
+}
+
// TranslateInfoBarDelegate: static: -------------------------------------------
TranslateInfoBarDelegate* TranslateInfoBarDelegate::Create(
TabContents* tab_contents, PrefService* user_prefs, TranslateState state,
const GURL& url,
const std::string& original_lang_code,
- const std::string& target_lang_code) {
+ const std::string& target_lang_code,
+ TranslateErrors::Type error_type) {
std::vector<std::string> supported_languages;
TranslationService::GetSupportedLanguages(&supported_languages);
@@ -196,14 +219,16 @@ TranslateInfoBarDelegate* TranslateInfoBarDelegate::Create(
return NULL;
return new TranslateInfoBarDelegate(tab_contents, user_prefs, state, url,
- original_lang_index, target_lang_index);
+ original_lang_index, target_lang_index,
+ error_type);
}
// TranslateInfoBarDelegate: private: ------------------------------------------
TranslateInfoBarDelegate::TranslateInfoBarDelegate(TabContents* tab_contents,
PrefService* user_prefs, TranslateState state, const GURL& url,
- int original_lang_index, int target_lang_index)
+ int original_lang_index, int target_lang_index,
+ TranslateErrors::Type error_type)
: InfoBarDelegate(tab_contents),
tab_contents_(tab_contents),
prefs_(new TranslatePrefs(user_prefs)),
@@ -214,7 +239,8 @@ TranslateInfoBarDelegate::TranslateInfoBarDelegate(TabContents* tab_contents,
target_lang_index_(target_lang_index),
never_translate_language_(false),
never_translate_site_(false),
- always_translate_(false) {
+ always_translate_(false),
+ error_type_(error_type) {
TranslationService::GetSupportedLanguages(&supported_languages_);
DCHECK(original_lang_index_ > -1);
DCHECK(target_lang_index_ > -1);
diff --git a/chrome/browser/translate/translate_infobars_delegates.h b/chrome/browser/translate/translate_infobars_delegates.h
index 2d14724..40262a8 100644
--- a/chrome/browser/translate/translate_infobars_delegates.h
+++ b/chrome/browser/translate/translate_infobars_delegates.h
@@ -7,6 +7,7 @@
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/translate/translate_prefs.h"
+#include "chrome/common/translate_errors.h"
class SkBitmap;
@@ -15,12 +16,13 @@ class SkBitmap;
class TranslateInfoBarDelegate : public InfoBarDelegate {
public:
enum TranslateState {
+ kTranslateNone = 0,
kBeforeTranslate = 1,
+ kAfterTranslate,
+ kTranslateError,
// TODO(playmobil or erg): remove kTranslating state when mac and linux code
// have been updated to use transaction_pending() instead.
kTranslating,
- kAfterTranslate,
- kTranslationFailed,
};
// Instantiates a TranslateInfoBarDelegate. Can return NULL if the passed
@@ -30,9 +32,10 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
TranslateState state,
const GURL& url,
const std::string& original_language,
- const std::string& target_language);
+ const std::string& target_language,
+ TranslateErrors::Type error_type);
- void UpdateState(TranslateState new_state);
+ void UpdateState(TranslateState new_state, TranslateErrors::Type error_type);
void GetAvailableOriginalLanguages(std::vector<std::string>* languages);
void GetAvailableTargetLanguages(std::vector<std::string>* languages);
void ModifyOriginalLanguage(int lang_index);
@@ -71,11 +74,15 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
bool translation_pending() const {
return translation_pending_;
}
+ TranslateErrors::Type error_type() const {
+ return error_type_;
+ }
// Retrieve the text for the toolbar label. The toolbar label is a bit
// strange since we need to place popup menus inside the string in question.
// To do this we use two placeholders.
//
+ // |state| is the state to get message for.
// |message_text| is the text to display for the label.
// |offsets| contains the offsets of the number of placeholders in the text
// + message_text->length() i.e. it can contain 2 or 3 elements.
@@ -83,10 +90,13 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
// displayed in reverse order.
// |swapped_language_placeholders| is true if we need to flip the order
// of the menus in the current locale.
- void GetMessageText(string16 *message_text,
+ void GetMessageText(TranslateState state,
+ string16 *message_text,
std::vector<size_t> *offsets,
bool *swapped_language_placeholders);
+ string16 GetErrorMessage(TranslateErrors::Type error_type);
+
// Overridden from InfoBarDelegate.
virtual Type GetInfoBarType() {
return PAGE_ACTION_TYPE;
@@ -123,7 +133,8 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
TranslateState state,
const GURL& url,
int original_language_index,
- int target_language_index);
+ int target_language_index,
+ TranslateErrors::Type error_type);
TabContents* tab_contents_; // Weak.
scoped_ptr<TranslatePrefs> prefs_;
@@ -137,6 +148,7 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
bool never_translate_language_;
bool never_translate_site_;
bool always_translate_;
+ TranslateErrors::Type error_type_;
DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate);
};
diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc
index 5665c43..214d731 100644
--- a/chrome/browser/translate/translate_manager.cc
+++ b/chrome/browser/translate/translate_manager.cc
@@ -17,6 +17,7 @@
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_util.h"
+#include "chrome/browser/translate/page_translated_details.h"
#include "chrome/browser/translate/translate_prefs.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_service.h"
@@ -86,23 +87,28 @@ void TranslateManager::Observe(NotificationType type,
// just update the state, the actual infobar would have received the same
// notification and update the visual display accordingly.
TabContents* tab = Source<TabContents>(source).ptr();
+ PageTranslatedDetails* page_translated_details =
+ Details<PageTranslatedDetails>(details).ptr();
+ TranslateInfoBarDelegate::TranslateState state =
+ (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(TranslateInfoBarDelegate::kAfterTranslate);
+ info_bar->UpdateState(state, page_translated_details->error_type);
break;
}
}
if (i == tab->infobar_delegate_count()) {
NavigationEntry* entry = tab->controller().GetActiveEntry();
if (entry) {
- std::pair<std::string, std::string>* language_pair =
- (Details<std::pair<std::string, std::string> >(details).ptr());
- AddTranslateInfoBar(tab, TranslateInfoBarDelegate::kAfterTranslate,
- entry->url(),
- language_pair->first, language_pair->second);
+ AddTranslateInfoBar(tab, state, entry->url(),
+ page_translated_details->source_language,
+ page_translated_details->target_language,
+ page_translated_details->error_type);
}
}
break;
@@ -201,7 +207,8 @@ void TranslateManager::InitiateTranslation(TabContents* tab,
// Prompts the user if he/she wants the page translated.
AddTranslateInfoBar(tab, TranslateInfoBarDelegate::kBeforeTranslate,
- entry->url(), page_lang, target_lang);
+ entry->url(), page_lang, target_lang,
+ TranslateErrors::NONE);
}
void TranslateManager::InitiateTranslationPosted(int process_id,
@@ -270,11 +277,13 @@ void TranslateManager::InitAcceptLanguages(PrefService* prefs) {
void TranslateManager::AddTranslateInfoBar(
TabContents* tab, TranslateInfoBarDelegate::TranslateState state,
const GURL& url,
- const std::string& original_language, const std::string& target_language) {
+ const std::string& original_language, const std::string& target_language,
+ TranslateErrors::Type error_type) {
PrefService* prefs = tab->profile()->GetPrefs();
TranslateInfoBarDelegate* infobar =
TranslateInfoBarDelegate::Create(tab, prefs, state, url,
- original_language, target_language);
+ original_language, target_language,
+ error_type);
if (!infobar) {
NOTREACHED() << "Failed to create infobar for language " <<
original_language << " and " << target_language;
diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h
index fed522d..cf0e452 100644
--- a/chrome/browser/translate/translate_manager.h
+++ b/chrome/browser/translate/translate_manager.h
@@ -14,6 +14,7 @@
#include "chrome/browser/translate/translate_infobars_delegates.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"
+#include "chrome/common/translate_errors.h"
class GURL;
class PrefService;
@@ -74,7 +75,8 @@ class TranslateManager : public NotificationObserver {
TranslateInfoBarDelegate::TranslateState state,
const GURL& url,
const std::string& original_language,
- const std::string& target_language);
+ const std::string& target_language,
+ TranslateErrors::Type error_type);
// 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
diff --git a/chrome/browser/translate/translate_manager_unittest.cc b/chrome/browser/translate/translate_manager_unittest.cc
index 30bc4c5..0a982fd 100644
--- a/chrome/browser/translate/translate_manager_unittest.cc
+++ b/chrome/browser/translate/translate_manager_unittest.cc
@@ -268,7 +268,8 @@ TEST_F(TranslateManagerTest, NormalTranslate) {
// EXPECT_EQ(TranslateInfoBarDelegate::kTranslating, infobar->state());
// Simulate the render notifying the translation has been done.
- rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en"));
+ rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
+ TranslateErrors::NONE));
// The infobar should have changed to the after state.
EXPECT_FALSE(InfoBarRemoved());
@@ -375,7 +376,8 @@ TEST_F(TranslateManagerTest, AutoTranslateOnNavigate) {
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
- rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en"));
+ rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
+ TranslateErrors::NONE));
// Now navigate to a new page in the same language.
process()->sink().ClearMessages();
@@ -517,7 +519,8 @@ TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) {
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
- rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en"));
+ rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
+ TranslateErrors::NONE));
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
@@ -544,7 +547,8 @@ TEST_F(TranslateManagerTest, TranslateInPageNavigation) {
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
- rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en"));
+ rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
+ TranslateErrors::NONE));
// Navigate in page, the same infobar should still be shown.
SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr");
@@ -767,7 +771,8 @@ TEST_F(TranslateManagerTest, ContextMenu) {
process()->sink().ClearMessages();
// Let's simulate the page being translated.
- rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en"));
+ rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
+ TranslateErrors::NONE));
// The translate menu should now be disabled.
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));