diff options
author | kuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-09 13:32:12 +0000 |
---|---|---|
committer | kuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-09 13:32:12 +0000 |
commit | fcd0c3cb084ffbcc36ed8c8dd1245be02c24aec3 (patch) | |
tree | ff07916dbb6d2169f51e24a87fc129f8797a9294 /chrome/browser/translate | |
parent | 035545f333d5f508bee18782784b17c3d6889924 (diff) | |
download | chromium_src-fcd0c3cb084ffbcc36ed8c8dd1245be02c24aec3.zip chromium_src-fcd0c3cb084ffbcc36ed8c8dd1245be02c24aec3.tar.gz chromium_src-fcd0c3cb084ffbcc36ed8c8dd1245be02c24aec3.tar.bz2 |
redesign and reimplement auto-translate preferences
- old prefs
- one source lang could be auto-translated to several target langs
- storage: list of target langs
- new prefs
- one source lang can only be auto-translated to one target lang, the latest option overwriting previous one
- storage: a single string value of target lang
- besides using new format, also migrate old to new format when registering user prefs
BUG=37313
TEST=verify per bug report.
Review URL: http://codereview.chromium.org/1551021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44080 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/translate')
-rw-r--r-- | chrome/browser/translate/translate_manager.cc | 8 | ||||
-rw-r--r-- | chrome/browser/translate/translate_prefs.cc | 95 | ||||
-rw-r--r-- | chrome/browser/translate/translate_prefs.h | 6 |
3 files changed, 75 insertions, 34 deletions
diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc index 3968de9..e58cc9d 100644 --- a/chrome/browser/translate/translate_manager.cc +++ b/chrome/browser/translate/translate_manager.cc @@ -352,9 +352,11 @@ void TranslateManager::InitiateTranslation(TabContents* tab, // automatically translate. Note that in incognito mode we disable that // feature; the user will get an infobar, so they can control whether the // page's text is sent to the translate server. - if (TranslatePrefs::ShouldAutoTranslate(prefs, page_lang, target_lang) && - !tab->profile()->IsOffTheRecord()) { - TranslatePage(tab, page_lang, target_lang); + std::string auto_target_lang; + if (!tab->profile()->IsOffTheRecord() && + TranslatePrefs::ShouldAutoTranslate(prefs, page_lang, + &auto_target_lang)) { + TranslatePage(tab, page_lang, auto_target_lang); return; } diff --git a/chrome/browser/translate/translate_prefs.cc b/chrome/browser/translate/translate_prefs.cc index bfbf23c..c1036b9 100644 --- a/chrome/browser/translate/translate_prefs.cc +++ b/chrome/browser/translate/translate_prefs.cc @@ -56,10 +56,9 @@ bool TranslatePrefs::IsLanguagePairWhitelisted( const std::string& target_language) { const DictionaryValue* dict = prefs_->GetDictionary(kPrefTranslateWhitelists); if (dict && !dict->empty()) { - ListValue* whitelist = NULL; - if (dict->GetList(ASCIIToWide(original_language), &whitelist) && - whitelist && !whitelist->empty() && - IsValueInList(whitelist, target_language)) + std::string auto_target_lang; + if (dict->GetString(ASCIIToWide(original_language), &auto_target_lang) && + auto_target_lang == target_language) return true; } return false; @@ -75,16 +74,7 @@ void TranslatePrefs::WhitelistLanguagePair( return; } ScopedPrefUpdate update(prefs_, kPrefTranslateWhitelists); - std::wstring wide_original(ASCIIToWide(original_language)); - StringValue* language = new StringValue(target_language); - ListValue* whitelist = NULL; - if (dict->GetList(wide_original, &whitelist) && whitelist) { - whitelist->Append(language); - } else { - ListValue new_whitelist; - new_whitelist.Append(language); - dict->Set(wide_original, new_whitelist.DeepCopy()); - } + dict->SetString(ASCIIToWide(original_language), target_language); prefs_->ScheduleSavePersistentPrefs(); } @@ -98,19 +88,11 @@ void TranslatePrefs::RemoveLanguagePairFromWhitelist( return; } ScopedPrefUpdate update(prefs_, kPrefTranslateWhitelists); - ListValue* whitelist = NULL; - std::wstring wide_original(ASCIIToWide(original_language)); - if (dict->GetList(wide_original, &whitelist) && whitelist) { - StringValue language(target_language); - if (whitelist->Remove(language) != -1) { - if (whitelist->empty()) // If list is empty, remove key from dict. - dict->Remove(wide_original, NULL); - prefs_->ScheduleSavePersistentPrefs(); - } - } + if (dict->Remove(ASCIIToWide(original_language), NULL)) + prefs_->ScheduleSavePersistentPrefs(); } -// TranslatePrefs: static: ----------------------------------------------------- +// TranslatePrefs: public, static: --------------------------------------------- bool TranslatePrefs::CanTranslate(PrefService* user_prefs, const std::string& original_language, const GURL& url) { @@ -121,10 +103,9 @@ bool TranslatePrefs::CanTranslate(PrefService* user_prefs, } bool TranslatePrefs::ShouldAutoTranslate(PrefService* user_prefs, - const std::string& original_language, - const std::string& target_language) { + const std::string& original_language, std::string* target_language) { TranslatePrefs prefs(user_prefs); - return prefs.IsLanguagePairWhitelisted(original_language, target_language); + return prefs.IsLanguageWhitelisted(original_language, target_language); } void TranslatePrefs::RegisterUserPrefs(PrefService* user_prefs) { @@ -132,8 +113,53 @@ void TranslatePrefs::RegisterUserPrefs(PrefService* user_prefs) { user_prefs->RegisterListPref(kPrefTranslateLanguageBlacklist); if (!user_prefs->FindPreference(kPrefTranslateSiteBlacklist)) user_prefs->RegisterListPref(kPrefTranslateSiteBlacklist); - if (!user_prefs->FindPreference(kPrefTranslateWhitelists)) + if (!user_prefs->FindPreference(kPrefTranslateWhitelists)) { user_prefs->RegisterDictionaryPref(kPrefTranslateWhitelists); + MigrateTranslateWhitelists(user_prefs); + } +} + +// TranslatePrefs: private, static: -------------------------------------------- + +void TranslatePrefs::MigrateTranslateWhitelists(PrefService* user_prefs) { + // Old format of kPrefTranslateWhitelists + // - original language -> list of target langs to auto-translate + // - list of langs is in order of being enabled i.e. last in list is the + // most recent language that user enabled via + // Always translate |source_lang| to |target_lang|" + // - this results in a one-to-n relationship between source lang and target + // langs. + // New format: + // - original language -> one target language to auto-translate + // - each time that the user enables the "Always translate..." option, that + // target lang overwrites the previous one. + // - this results in a one-to-one relationship between source lang and target + // lang + // - we replace old list of target langs with the last target lang in list, + // assuming the last (i.e. most recent) target lang is what user wants to + // keep auto-translated. + DictionaryValue* dict = user_prefs->GetMutableDictionary( + kPrefTranslateWhitelists); + if (!dict || dict->empty()) + return; + bool save_prefs = false; + for (DictionaryValue::key_iterator iter(dict->begin_keys()); + iter != dict->end_keys(); ++iter) { + ListValue* list = NULL; + if (!dict->GetList(*iter, &list) || !list) + break; // Dictionary has either been migrated or new format. + save_prefs = true; + std::string target_lang; + if (list->empty() || !list->GetString(list->GetSize() - 1, &target_lang) || + target_lang.empty()) + dict->Remove(*iter, NULL); + else + dict->SetString(*iter, target_lang); + } + if (!save_prefs) + return; + ScopedPrefUpdate update(user_prefs, kPrefTranslateWhitelists); + user_prefs->ScheduleSavePersistentPrefs(); } // TranslatePrefs: private: ---------------------------------------------------- @@ -176,3 +202,14 @@ void TranslatePrefs::RemoveValueFromBlacklist(const wchar_t* pref_id, if (blacklist->Remove(string_value) != -1) prefs_->ScheduleSavePersistentPrefs(); } + +bool TranslatePrefs::IsLanguageWhitelisted( + const std::string& original_language, std::string* target_language) { + const DictionaryValue* dict = prefs_->GetDictionary(kPrefTranslateWhitelists); + if (dict && + dict->GetString(ASCIIToWide(original_language), target_language)) { + DCHECK(!target_language->empty()); + return !target_language->empty(); + } + return false; +} diff --git a/chrome/browser/translate/translate_prefs.h b/chrome/browser/translate/translate_prefs.h index 5cd334d..fa8dc6d 100644 --- a/chrome/browser/translate/translate_prefs.h +++ b/chrome/browser/translate/translate_prefs.h @@ -39,16 +39,18 @@ class TranslatePrefs { static bool CanTranslate(PrefService* user_prefs, const std::string& original_language, const GURL& url); static bool ShouldAutoTranslate(PrefService* user_prefs, - const std::string& original_language, - const std::string& target_language); + const std::string& original_language, std::string* target_language); static void RegisterUserPrefs(PrefService* user_prefs); private: + static void MigrateTranslateWhitelists(PrefService* user_prefs); bool IsValueBlacklisted(const wchar_t* pref_id, const std::string& value); void BlacklistValue(const wchar_t* pref_id, const std::string& value); void RemoveValueFromBlacklist(const wchar_t* pref_id, const std::string& value); bool IsValueInList(const ListValue* list, const std::string& value); + bool IsLanguageWhitelisted(const std::string& original_language, + std::string* target_language); PrefService* prefs_; // Weak. }; |