summaryrefslogtreecommitdiffstats
path: root/chrome/browser/translate
diff options
context:
space:
mode:
authorjcivelli@chromium.org <jcivelli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-21 18:30:05 +0000
committerjcivelli@chromium.org <jcivelli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-21 18:30:05 +0000
commit59efee1132427f4610125f2933992b5952783b26 (patch)
tree0dc78d7c15a9dff63597422d5d230c935f394185 /chrome/browser/translate
parent6b77976f7ebe04fd91ccbaf5bfd77c5a9065c518 (diff)
downloadchromium_src-59efee1132427f4610125f2933992b5952783b26.zip
chromium_src-59efee1132427f4610125f2933992b5952783b26.tar.gz
chromium_src-59efee1132427f4610125f2933992b5952783b26.tar.bz2
Makes the browser fetch the translate script regularly.
New versions of that script may get pushed from time to time. Fetching the script again ensures people that don't restart their browser for long periods of time still get the new versions. Also renamed the enum from constant style to macro-style, as it is the Chromium way to go from the Chromium style guide (which is in that regard different than the Google style guide). BUG=None TEST=Run the unit-tests. Review URL: http://codereview.chromium.org/3034013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53220 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/translate')
-rw-r--r--chrome/browser/translate/translate_infobar_delegate.cc36
-rw-r--r--chrome/browser/translate/translate_infobar_delegate.h14
-rw-r--r--chrome/browser/translate/translate_manager.cc15
-rw-r--r--chrome/browser/translate/translate_manager.h11
-rw-r--r--chrome/browser/translate/translate_manager_unittest.cc73
5 files changed, 107 insertions, 42 deletions
diff --git a/chrome/browser/translate/translate_infobar_delegate.cc b/chrome/browser/translate/translate_infobar_delegate.cc
index 2233991..e1a801b 100644
--- a/chrome/browser/translate/translate_infobar_delegate.cc
+++ b/chrome/browser/translate/translate_infobar_delegate.cc
@@ -23,7 +23,7 @@ TranslateInfoBarDelegate* TranslateInfoBarDelegate::CreateDelegate(
TabContents* tab_contents,
const std::string& original_language,
const std::string& target_language) {
- DCHECK(type != kTranslationError);
+ DCHECK(type != TRANSLATION_ERROR);
if (!TranslateManager::IsSupportedLanguage(original_language) ||
!TranslateManager::IsSupportedLanguage(target_language)) {
return NULL;
@@ -42,7 +42,7 @@ TranslateInfoBarDelegate* TranslateInfoBarDelegate::CreateErrorDelegate(
TabContents* tab_contents,
const std::string& original_language,
const std::string& target_language) {
- return new TranslateInfoBarDelegate(kTranslationError, error, tab_contents,
+ return new TranslateInfoBarDelegate(TRANSLATION_ERROR, error, tab_contents,
original_language, target_language);
}
@@ -54,7 +54,7 @@ TranslateInfoBarDelegate::TranslateInfoBarDelegate(
const std::string& target_language)
: InfoBarDelegate(tab_contents),
type_(type),
- background_animation_(kNone),
+ background_animation_(NONE),
tab_contents_(tab_contents),
original_language_index_(-1),
initial_original_language_index_(-1),
@@ -62,8 +62,8 @@ TranslateInfoBarDelegate::TranslateInfoBarDelegate(
error_(error),
infobar_view_(NULL),
prefs_(tab_contents_->profile()->GetPrefs()) {
- DCHECK((type_ != kTranslationError && error == TranslateErrors::NONE) ||
- (type_ == kTranslationError && error != TranslateErrors::NONE));
+ DCHECK((type_ != TRANSLATION_ERROR && error == TranslateErrors::NONE) ||
+ (type_ == TRANSLATION_ERROR && error != TranslateErrors::NONE));
std::vector<std::string> language_codes;
TranslateManager::GetSupportedLanguages(&language_codes);
@@ -123,7 +123,7 @@ void TranslateInfoBarDelegate::SetOriginalLanguage(int language_index) {
original_language_index_ = language_index;
if (infobar_view_)
infobar_view_->OriginalLanguageChanged();
- if (type_ == kAfterTranslate)
+ if (type_ == AFTER_TRANSLATE)
Translate();
}
@@ -132,12 +132,12 @@ void TranslateInfoBarDelegate::SetTargetLanguage(int language_index) {
target_language_index_ = language_index;
if (infobar_view_)
infobar_view_->TargetLanguageChanged();
- if (type_ == kAfterTranslate)
+ if (type_ == AFTER_TRANSLATE)
Translate();
}
bool TranslateInfoBarDelegate::IsError() {
- return type_ == kTranslationError;
+ return type_ == TRANSLATION_ERROR;
}
void TranslateInfoBarDelegate::Translate() {
@@ -175,7 +175,7 @@ void TranslateInfoBarDelegate::TranslationDeclined() {
}
void TranslateInfoBarDelegate::InfoBarDismissed() {
- if (type_ != kBeforeTranslate)
+ if (type_ != BEFORE_TRANSLATE)
return;
// The user closed the infobar without clicking the translate button.
@@ -259,11 +259,11 @@ void TranslateInfoBarDelegate::NeverTranslatePageLanguage() {
string16 TranslateInfoBarDelegate::GetMessageInfoBarText() {
switch (type_) {
- case kTranslating:
+ case TRANSLATING:
return l10n_util::GetStringFUTF16(
IDS_TRANSLATE_INFOBAR_TRANSLATING_TO,
GetLanguageDisplayableNameAt(target_language_index_));
- case kTranslationError:
+ case TRANSLATION_ERROR:
switch (error_) {
case TranslateErrors::NETWORK:
return l10n_util::GetStringUTF16(
@@ -295,9 +295,9 @@ string16 TranslateInfoBarDelegate::GetMessageInfoBarText() {
string16 TranslateInfoBarDelegate::GetMessageInfoBarButtonText() {
switch (type_) {
- case kTranslating:
+ case TRANSLATING:
return string16();
- case kTranslationError:
+ case TRANSLATION_ERROR:
if (error_ == TranslateErrors::IDENTICAL_LANGUAGES ||
error_ == TranslateErrors::UNKNOWN_LANGUAGE) {
// No retry button, we would fail again with the same error.
@@ -313,7 +313,7 @@ string16 TranslateInfoBarDelegate::GetMessageInfoBarButtonText() {
}
void TranslateInfoBarDelegate::MessageInfoBarButtonPressed() {
- DCHECK(type_ == kTranslationError);
+ DCHECK(type_ == TRANSLATION_ERROR);
if (error_ == TranslateErrors::UNSUPPORTED_LANGUAGE) {
RevertTranslation();
return;
@@ -324,22 +324,22 @@ void TranslateInfoBarDelegate::MessageInfoBarButtonPressed() {
}
bool TranslateInfoBarDelegate::ShouldShowNeverTranslateButton() {
- DCHECK(type_ == kBeforeTranslate);
+ DCHECK(type_ == BEFORE_TRANSLATE);
return prefs_.GetTranslationDeniedCount(GetOriginalLanguageCode()) >= 3;
}
bool TranslateInfoBarDelegate::ShouldShowAlwaysTranslateButton() {
- DCHECK(type_ == kBeforeTranslate);
+ DCHECK(type_ == BEFORE_TRANSLATE);
return prefs_.GetTranslationAcceptedCount(GetOriginalLanguageCode()) >= 3;
}
void TranslateInfoBarDelegate::UpdateBackgroundAnimation(
TranslateInfoBarDelegate* previous_infobar) {
if (!previous_infobar || previous_infobar->IsError() == IsError()) {
- background_animation_ = kNone;
+ background_animation_ = NONE;
return;
}
- background_animation_ = IsError() ? kNormalToError: kErrorToNormal;
+ background_animation_ = IsError() ? NORMAL_TO_ERROR: ERROR_TO_NORMAL;
}
std::string TranslateInfoBarDelegate::GetPageHost() {
diff --git a/chrome/browser/translate/translate_infobar_delegate.h b/chrome/browser/translate/translate_infobar_delegate.h
index c82cde7..4b1b1e9 100644
--- a/chrome/browser/translate/translate_infobar_delegate.h
+++ b/chrome/browser/translate/translate_infobar_delegate.h
@@ -19,17 +19,17 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
public:
// The different types of infobars that can be shown for translation.
enum Type {
- kBeforeTranslate,
- kTranslating,
- kAfterTranslate,
- kTranslationError
+ BEFORE_TRANSLATE,
+ TRANSLATING,
+ AFTER_TRANSLATE,
+ TRANSLATION_ERROR
};
// The types of background color animations.
enum BackgroundAnimationType {
- kNone,
- kNormalToError,
- kErrorToNormal
+ NONE,
+ NORMAL_TO_ERROR,
+ ERROR_TO_NORMAL
};
// Factory method to create a non-error translate infobar.
diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc
index 8a8e645..48b3079 100644
--- a/chrome/browser/translate/translate_manager.cc
+++ b/chrome/browser/translate/translate_manager.cc
@@ -129,6 +129,8 @@ const char* const kTranslateScriptHeader =
const char* const kReportLanguageDetectionErrorURL =
"http://translate.google.com/translate_error";
+const int kTranslateScriptExpirationDelayMS = 24 * 60 * 60 * 1000; // 1 day.
+
} // namespace
// static
@@ -268,6 +270,12 @@ void TranslateManager::OnURLFetchComplete(const URLFetcher* source,
DCHECK(translate_script_.empty());
str.CopyToString(&translate_script_);
translate_script_ += "\n" + data;
+ // We'll expire the cached script after some time, to make sure long running
+ // browsers still get fixes that might get pushed with newer scripts.
+ MessageLoop::current()->PostDelayedTask(FROM_HERE,
+ method_factory_.NewRunnableMethod(
+ &TranslateManager::ClearTranslateScript),
+ translate_script_expiration_delay_);
}
// Process any pending requests.
@@ -307,6 +315,7 @@ bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) {
TranslateManager::TranslateManager()
: ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
+ translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS),
translate_script_request_pending_(false) {
notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
NotificationService::AllSources());
@@ -371,7 +380,7 @@ void TranslateManager::InitiateTranslation(TabContents* tab,
// Prompts the user if he/she wants the page translated.
tab->AddInfoBar(TranslateInfoBarDelegate::CreateDelegate(
- TranslateInfoBarDelegate::kBeforeTranslate, tab,
+ TranslateInfoBarDelegate::BEFORE_TRANSLATE, tab,
page_lang, target_lang));
}
@@ -400,7 +409,7 @@ void TranslateManager::TranslatePage(TabContents* tab_contents,
// showing (that is the case when the translation was triggered by the
// "always translate" for example).
infobar = TranslateInfoBarDelegate::CreateDelegate(
- TranslateInfoBarDelegate::kTranslating, tab_contents,
+ TranslateInfoBarDelegate::TRANSLATING, tab_contents,
source_lang, target_lang);
ShowInfoBar(tab_contents, infobar);
}
@@ -489,7 +498,7 @@ void TranslateManager::PageTranslated(TabContents* tab,
details->source_language, details->target_language);
} else {
infobar = TranslateInfoBarDelegate::CreateDelegate(
- TranslateInfoBarDelegate::kAfterTranslate, tab,
+ TranslateInfoBarDelegate::AFTER_TRANSLATE, tab,
details->source_language, details->target_language);
}
ShowInfoBar(tab, infobar);
diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h
index 260f345..54debf4 100644
--- a/chrome/browser/translate/translate_manager.h
+++ b/chrome/browser/translate/translate_manager.h
@@ -51,7 +51,6 @@ class TranslateManager : public NotificationObserver,
void ReportLanguageDetectionError(TabContents* tab_contents);
// Clears the translate script, so it will be fetched next time we translate.
- // Currently used by unit-tests.
void ClearTranslateScript() { translate_script_.clear(); }
// NotificationObserver implementation:
@@ -67,6 +66,12 @@ class TranslateManager : public NotificationObserver,
const ResponseCookies& cookies,
const std::string& data);
+ // Used by unit-tests to override the default delay after which the translate
+ // script is fetched again from the translation server.
+ void set_translate_script_expiration_delay(int delay_ms) {
+ translate_script_expiration_delay_ = delay_ms;
+ }
+
// Convenience method to know if a tab is showing a translate infobar.
static bool IsShowingTranslateInfobar(TabContents* tab);
@@ -158,6 +163,10 @@ class TranslateManager : public NotificationObserver,
// The JS injected in the page to do the translation.
std::string translate_script_;
+ // Delay in milli-seconds after which the translate script is fetched again
+ // from the translate server.
+ int translate_script_expiration_delay_;
+
// Whether the translate JS is currently being retrieved.
bool translate_script_request_pending_;
diff --git a/chrome/browser/translate/translate_manager_unittest.cc b/chrome/browser/translate/translate_manager_unittest.cc
index 41d8c3d..1b1598d 100644
--- a/chrome/browser/translate/translate_manager_unittest.cc
+++ b/chrome/browser/translate/translate_manager_unittest.cc
@@ -113,6 +113,11 @@ class TranslateManagerTest : public RenderViewHostTestHarness,
removed_infobars_.clear();
}
+ void ExpireTranslateScriptImmediately() {
+ Singleton<TranslateManager>::get()->
+ set_translate_script_expiration_delay(0);
+ }
+
// If there is 1 infobar and it is a translate infobar, deny translation and
// returns true. Returns false otherwise.
bool DenyTranslation() {
@@ -140,8 +145,12 @@ class TranslateManagerTest : public RenderViewHostTestHarness,
// the TranslateManager is created before the TabContents. This matters as
// they both register for similar events and we want the notifications to
// happen in the same sequence (TranslateManager first, TabContents second).
- // Also clears the translate script so it is fetched everytime.
+ // Also clears the translate script so it is fetched everytime and sets the
+ // expiration delay to a large value by default (in case it was zeroed in
+ // a previous test).
Singleton<TranslateManager>::get()->ClearTranslateScript();
+ Singleton<TranslateManager>::get()->
+ set_translate_script_expiration_delay(60 * 60 * 1000);
RenderViewHostTestHarness::SetUp();
@@ -272,7 +281,7 @@ TEST_F(TranslateManagerTest, NormalTranslate) {
// We should have an infobar.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
// Simulate clicking translate.
process()->sink().ClearMessages();
@@ -281,7 +290,7 @@ TEST_F(TranslateManagerTest, NormalTranslate) {
// The "Translating..." infobar should be showing.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kTranslating, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type());
// Simulate the translate script being retrieved (it only needs to be done
// once in the test as it is cached).
@@ -302,7 +311,7 @@ TEST_F(TranslateManagerTest, NormalTranslate) {
// The after translate infobar should be showing.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kAfterTranslate, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type());
// Simulate changing the original language, this should trigger a translation.
process()->sink().ClearMessages();
@@ -343,7 +352,7 @@ TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) {
// We should have an infobar.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
// Simulate clicking translate.
process()->sink().ClearMessages();
@@ -357,7 +366,7 @@ TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) {
// And we should have an error infobar showing.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kTranslationError, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type());
}
// Ensures we deal correctly with pages for which the browser does not recognize
@@ -375,7 +384,7 @@ TEST_F(TranslateManagerTest, TranslateUnknownLanguage) {
TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE);
- SimulateURLFetch(true); // Simulare receiving the translate script.
+ SimulateURLFetch(true); // Simulate receiving the translate script.
// Simulate the render notifying the translation has been done, the server
// having detected the page was in a known and supported language.
@@ -385,7 +394,7 @@ TEST_F(TranslateManagerTest, TranslateUnknownLanguage) {
// The after translate infobar should be showing.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kAfterTranslate, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type());
EXPECT_EQ("fr", infobar->GetOriginalLanguageCode());
EXPECT_EQ("en", infobar->GetTargetLanguageCode());
@@ -400,7 +409,7 @@ TEST_F(TranslateManagerTest, TranslateUnknownLanguage) {
TranslateErrors::IDENTICAL_LANGUAGES));
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kTranslationError, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type());
EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error());
// Let's run the same steps again but this time the server fails to detect the
@@ -414,7 +423,7 @@ TEST_F(TranslateManagerTest, TranslateUnknownLanguage) {
TranslateErrors::UNKNOWN_LANGUAGE));
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kTranslationError, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type());
EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error());
}
@@ -734,7 +743,7 @@ TEST_F(TranslateManagerTest, ServerReportsUnsupportedLanguage) {
// language.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kTranslationError, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type());
// This infobar should have a button (so the string should not be empty).
ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty());
@@ -1056,7 +1065,7 @@ TEST_F(TranslateManagerTest, BeforeTranslateExtraButtons) {
true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
if (i < 3) {
EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateButton());
infobar->Translate();
@@ -1084,7 +1093,7 @@ TEST_F(TranslateManagerTest, BeforeTranslateExtraButtons) {
true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
- EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->type());
+ EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
if (i < 3) {
EXPECT_FALSE(infobar->ShouldShowNeverTranslateButton());
infobar->TranslationDeclined();
@@ -1119,3 +1128,41 @@ TEST_F(TranslateManagerTest, NonTranslatablePage) {
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
}
+// Tests that the script is expired and refetched as expected.
+TEST_F(TranslateManagerTest, ScriptExpires) {
+ ExpireTranslateScriptImmediately();
+
+ // Simulate navigating to a page and translating it.
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr", true);
+ TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
+ ASSERT_TRUE(infobar != NULL);
+ process()->sink().ClearMessages();
+ infobar->Translate();
+ SimulateURLFetch(true);
+ rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
+ TranslateErrors::NONE));
+
+ // A task should have been posted to clear the script, run it.
+ MessageLoop::current()->RunAllPending();
+
+ // Do another navigation and translation.
+ SimulateNavigation(GURL("http://www.google.es"), 1, "El Google", "es", true);
+ infobar = GetTranslateInfoBar();
+ ASSERT_TRUE(infobar != NULL);
+ process()->sink().ClearMessages();
+ infobar->Translate();
+ // If we don't simulate the URL fetch, the TranslateManager should be waiting
+ // for the script and no message should have been sent to the renderer.
+ EXPECT_TRUE(
+ process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID) ==
+ NULL);
+ // Now simulate the URL fetch.
+ SimulateURLFetch(true);
+ // Now the message should have been sent.
+ int page_id = 0;
+ std::string original_lang, target_lang;
+ EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
+ EXPECT_EQ(1, page_id);
+ EXPECT_EQ("es", original_lang);
+ EXPECT_EQ("en", target_lang);
+}