From 7cfc45880ce2b181c318fff2a336f655e294644f Mon Sep 17 00:00:00 2001 From: mcrouse Date: Fri, 20 Aug 2021 17:58:16 -0700 Subject: [PATCH] [Translate] Fix migration of deprecated pref. This migrates old prefs to use a default time rather than a value. Bug: 1241228 Change-Id: I07efec68aa0c06b0bbb13b51657a1c60b2bfcf7e --- diff --git a/components/translate/core/browser/translate_prefs.cc b/components/translate/core/browser/translate_prefs.cc index f599ec6..375f805 100644 --- a/components/translate/core/browser/translate_prefs.cc +++ b/components/translate/core/browser/translate_prefs.cc @@ -944,8 +944,7 @@ // Migration should only be necessary once but there could still be old // Chrome instances that sync the old preference, so do it once per // startup. - static bool migrated = false; - if (migrated) + if (migrated_) return; DictionaryPrefUpdate never_prompt_list_update(prefs_, kPrefNeverPromptSitesWithTime); @@ -954,11 +953,16 @@ const base::Value* list = prefs_->GetList(kPrefNeverPromptSitesDeprecated); for (auto& site : list->GetList()) { if (!never_prompt_list->FindKey(site.GetString())) { - never_prompt_list->SetKey(site.GetString(), base::Value(0)); + never_prompt_list->SetKey(site.GetString(), + base::TimeToValue(base::Time::Now())); } } } - migrated = true; + migrated_ = true; +} + +void TranslatePrefs::ResetMigrationForTesting() { + migrated_ = false; } bool TranslatePrefs::IsValueOnNeverPromptList(const char* pref_id, diff --git a/components/translate/core/browser/translate_prefs.h b/components/translate/core/browser/translate_prefs.h index b822bed..6d58fd7 100644 --- a/components/translate/core/browser/translate_prefs.h +++ b/components/translate/core/browser/translate_prefs.h @@ -339,6 +339,9 @@ static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); + // Reset |migrated_| to enable testing of pref migrations. + void ResetMigrationForTesting(); + private: FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, UpdateLanguageListFeatureEnabled); @@ -356,6 +359,7 @@ FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, MoveLanguageUp); FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, MoveLanguageDown); FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, ResetBlockedLanguagesToDefault); + FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, MigrateNeverPromptSites); friend class TranslatePrefsTest; void ClearNeverPromptSiteList(); @@ -385,6 +389,9 @@ std::unique_ptr language_prefs_; + // Whether any deprecated prefs have been successfully migrated. + bool migrated_ = false; + DISALLOW_COPY_AND_ASSIGN(TranslatePrefs); }; diff --git a/components/translate/core/browser/translate_prefs_unittest.cc b/components/translate/core/browser/translate_prefs_unittest.cc index 601b834..3feb232c 100644 --- a/components/translate/core/browser/translate_prefs_unittest.cc +++ b/components/translate/core/browser/translate_prefs_unittest.cc @@ -931,6 +931,17 @@ accept_languages_tester_->ExpectAcceptLanguagePrefs("en,it,es,zh,fr"); } +TEST_F(TranslatePrefsTest, MigrateNeverPromptSites) { + translate_prefs_->AddValueToNeverPromptList( + TranslatePrefs::kPrefNeverPromptSitesDeprecated, "unmigrated.com"); + translate_prefs_->ResetMigrationForTesting(); + translate_prefs_->MigrateNeverPromptSites(); + EXPECT_EQ( + std::vector({"unmigrated.com"}), + translate_prefs_->GetNeverPromptSitesBetween( + base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Max())); +} + TEST_F(TranslatePrefsTest, SiteNeverPromptList) { translate_prefs_->AddSiteToNeverPromptList("a.com"); base::Time t = base::Time::Now();