Commit dd81a7cf authored by mad@chromium.org's avatar mad@chromium.org

Fix a crash in the translate info bar.

A language from the accepted list was converted before being validated, but the unconverted version was returned.

BUG=90106

Review URL: http://codereview.chromium.org/7589002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96125 0039d316-1c4b-4281-b951-d872f2087c98
parent 4fe827a4
...@@ -30,15 +30,14 @@ TranslateInfoBarDelegate* TranslateInfoBarDelegate::CreateDelegate( ...@@ -30,15 +30,14 @@ TranslateInfoBarDelegate* TranslateInfoBarDelegate::CreateDelegate(
const std::string& original_language, const std::string& original_language,
const std::string& target_language) { const std::string& target_language) {
DCHECK_NE(TRANSLATION_ERROR, type); DCHECK_NE(TRANSLATION_ERROR, type);
// These must be validated by our callers.
DCHECK(TranslateManager::IsSupportedLanguage(target_language));
// The original language can only be "unknown" for the "translating" // The original language can only be "unknown" for the "translating"
// infobar, which is the case when the user started a translation from the // infobar, which is the case when the user started a translation from the
// context menu. // context menu.
DCHECK(type == TRANSLATING || DCHECK(TranslateManager::IsSupportedLanguage(original_language) ||
original_language != chrome::kUnknownLanguageCode); ((type == TRANSLATING) &&
if ((original_language != chrome::kUnknownLanguageCode && (original_language == chrome::kUnknownLanguageCode)));
!TranslateManager::IsSupportedLanguage(original_language)) ||
!TranslateManager::IsSupportedLanguage(target_language))
return NULL;
TranslateInfoBarDelegate* delegate = TranslateInfoBarDelegate* delegate =
new TranslateInfoBarDelegate(type, TranslateErrors::NONE, tab_contents, new TranslateInfoBarDelegate(type, TranslateErrors::NONE, tab_contents,
original_language, target_language); original_language, target_language);
......
...@@ -36,11 +36,12 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { ...@@ -36,11 +36,12 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
static const size_t kNoIndex; static const size_t kNoIndex;
// Factory method to create a non-error translate infobar. // Factory method to create a non-error translate infobar. |original_language|
// The original and target language specified are the ASCII language codes // and |target_language| must be ASCII language codes (e.g. "en", "fr", etc.)
// (ex: en, fr...). // for languages the TranslateManager supports translating. The lone exception
// Returns NULL if it failed, typically if |original_language| or // is when the user initiates translation from the context menu, in which case
// |target_language| is not a supported language. // it's legal to call this with |type| == TRANSLATING and
// |originalLanguage| == kUnknownLanguageCode.
static TranslateInfoBarDelegate* CreateDelegate( static TranslateInfoBarDelegate* CreateDelegate(
Type infobar_type, Type infobar_type,
TabContents* tab_contents, TabContents* tab_contents,
......
...@@ -507,7 +507,7 @@ void TranslateManager::InitiateTranslation(TabContents* tab, ...@@ -507,7 +507,7 @@ void TranslateManager::InitiateTranslation(TabContents* tab,
// Prompts the user if he/she wants the page translated. // Prompts the user if he/she wants the page translated.
wrapper->AddInfoBar(TranslateInfoBarDelegate::CreateDelegate( wrapper->AddInfoBar(TranslateInfoBarDelegate::CreateDelegate(
TranslateInfoBarDelegate::BEFORE_TRANSLATE, tab, language_code, TranslateInfoBarDelegate::BEFORE_TRANSLATE, tab, language_code,
target_lang)); target_lang));
} }
void TranslateManager::InitiateTranslationPosted( void TranslateManager::InitiateTranslationPosted(
...@@ -534,17 +534,9 @@ void TranslateManager::TranslatePage(TabContents* tab_contents, ...@@ -534,17 +534,9 @@ void TranslateManager::TranslatePage(TabContents* tab_contents,
return; return;
} }
TranslateInfoBarDelegate* infobar = TranslateInfoBarDelegate::CreateDelegate( ShowInfoBar(tab_contents, TranslateInfoBarDelegate::CreateDelegate(
TranslateInfoBarDelegate::TRANSLATING, tab_contents, TranslateInfoBarDelegate::TRANSLATING, tab_contents,
source_lang, target_lang); source_lang, target_lang));
if (!infobar) {
// This means the source or target languages are not supported, which should
// not happen as we won't show a translate infobar or have the translate
// context menu activated in such cases.
NOTREACHED();
return;
}
ShowInfoBar(tab_contents, infobar);
if (!translate_script_.empty()) { if (!translate_script_.empty()) {
DoTranslatePage(tab_contents, translate_script_, source_lang, target_lang); DoTranslatePage(tab_contents, translate_script_, source_lang, target_lang);
...@@ -749,6 +741,7 @@ void TranslateManager::RequestTranslateScript() { ...@@ -749,6 +741,7 @@ void TranslateManager::RequestTranslateScript() {
void TranslateManager::ShowInfoBar(TabContents* tab, void TranslateManager::ShowInfoBar(TabContents* tab,
TranslateInfoBarDelegate* infobar) { TranslateInfoBarDelegate* infobar) {
DCHECK(infobar != NULL);
TranslateInfoBarDelegate* old_infobar = GetTranslateInfoBarDelegate(tab); TranslateInfoBarDelegate* old_infobar = GetTranslateInfoBarDelegate(tab);
infobar->UpdateBackgroundAnimation(old_infobar); infobar->UpdateBackgroundAnimation(old_infobar);
TabContentsWrapper* wrapper = TabContentsWrapper* wrapper =
...@@ -781,8 +774,9 @@ std::string TranslateManager::GetTargetLanguage(PrefService* prefs) { ...@@ -781,8 +774,9 @@ std::string TranslateManager::GetTargetLanguage(PrefService* prefs) {
std::vector<std::string>::iterator iter; std::vector<std::string>::iterator iter;
for (iter = accept_langs_list.begin(); for (iter = accept_langs_list.begin();
iter != accept_langs_list.end(); ++iter) { iter != accept_langs_list.end(); ++iter) {
if (IsSupportedLanguage(GetLanguageCode(*iter))) std::string lang_code = GetLanguageCode(*iter);
return *iter; if (IsSupportedLanguage(lang_code))
return lang_code;
} }
return std::string(); return std::string();
} }
......
...@@ -903,6 +903,17 @@ TEST_F(TranslateManagerTest, TranslateAcceptLanguage) { ...@@ -903,6 +903,17 @@ TEST_F(TranslateManagerTest, TranslateAcceptLanguage) {
// Expect the infobar to pop up // Expect the infobar to pop up
EXPECT_TRUE(GetTranslateInfoBar() != NULL); EXPECT_TRUE(GetTranslateInfoBar() != NULL);
// Set Qbz and English-US as the only accepted languages to test the country
// code removal code which was causing a crash as filed in Issue 90106,
// a crash caused by a language with a country code that wasn't recognized.
prefs->SetString(prefs::kAcceptLanguages, "qbz,en-us");
// Go to a German page
SimulateNavigation(GURL("http://google.de"), "de", true);
// Expect the infobar to pop up
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
} }
// Tests that the translate enabled preference is honored. // Tests that the translate enabled preference is honored.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment