1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// Copyright 2014 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 COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_
#define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_
#include <string>
#include <vector>
#include "base/gtest_prod_util.h"
#include "url/gurl.h"
class PrefService;
class Profile;
namespace base {
class DictionaryValue;
class ListValue;
}
namespace user_prefs {
class PrefRegistrySyncable;
}
namespace translate {
class TranslateAcceptLanguages;
// The wrapper of PrefService object for Translate.
//
// It is assumed that |prefs_| is alive while this instance is alive.
class TranslatePrefs {
public:
static const char kPrefTranslateLanguageBlacklist[];
static const char kPrefTranslateSiteBlacklist[];
static const char kPrefTranslateWhitelists[];
static const char kPrefTranslateDeniedCount[];
static const char kPrefTranslateAcceptedCount[];
static const char kPrefTranslateBlockedLanguages[];
static const char kPrefTranslateLastDeniedTime[];
static const char kPrefTranslateTooOftenDenied[];
// |preferred_languages_pref| is only used on Chrome OS, other platforms must
// pass NULL.
TranslatePrefs(PrefService* user_prefs,
const char* accept_languages_pref,
const char* preferred_languages_pref);
// Resets the blocked languages list, the sites blacklist, the languages
// whitelist, and the accepted/denied counts.
void ResetToDefaults();
bool IsBlockedLanguage(const std::string& original_language) const;
void BlockLanguage(const std::string& original_language);
void UnblockLanguage(const std::string& original_language);
// Removes a language from the old blacklist. Only used internally for
// diagnostics. Don't use this if there is no special reason.
void RemoveLanguageFromLegacyBlacklist(const std::string& original_language);
bool IsSiteBlacklisted(const std::string& site) const;
void BlacklistSite(const std::string& site);
void RemoveSiteFromBlacklist(const std::string& site);
bool HasWhitelistedLanguagePairs() const;
bool IsLanguagePairWhitelisted(const std::string& original_language,
const std::string& target_language);
void WhitelistLanguagePair(const std::string& original_language,
const std::string& target_language);
void RemoveLanguagePairFromWhitelist(const std::string& original_language,
const std::string& target_language);
// Will return true if at least one language has been blacklisted.
bool HasBlockedLanguages() const;
// Will return true if at least one site has been blacklisted.
bool HasBlacklistedSites() const;
// These methods are used to track how many times the user has denied the
// translation for a specific language. (So we can present a UI to black-list
// that language if the user keeps denying translations).
int GetTranslationDeniedCount(const std::string& language) const;
void IncrementTranslationDeniedCount(const std::string& language);
void ResetTranslationDeniedCount(const std::string& language);
// These methods are used to track how many times the user has accepted the
// translation for a specific language. (So we can present a UI to white-list
// that language if the user keeps accepting translations).
int GetTranslationAcceptedCount(const std::string& language);
void IncrementTranslationAcceptedCount(const std::string& language);
void ResetTranslationAcceptedCount(const std::string& language);
// Update the last time on closing the Translate UI without translation.
void UpdateLastDeniedTime();
// Returns true if translation is denied too often.
bool IsTooOftenDenied() const;
// Resets the prefs of denial state. Only used internally for diagnostics.
void ResetDenialState();
// Gets the language list of the language settings.
void GetLanguageList(std::vector<std::string>* languages);
// Updates the language list of the language settings.
void UpdateLanguageList(const std::vector<std::string>& languages);
bool CanTranslateLanguage(TranslateAcceptLanguages* accept_languages,
const std::string& language);
bool ShouldAutoTranslate(const std::string& original_language,
std::string* target_language);
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
static void MigrateUserPrefs(PrefService* user_prefs,
const char* accept_languages_pref);
private:
friend class TranslatePrefsTest;
FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, CreateBlockedLanguages);
FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest,
CreateBlockedLanguagesNonEnglishUI);
// Merges two language sets to migrate to the language setting UI.
static void CreateBlockedLanguages(
std::vector<std::string>* blocked_languages,
const std::vector<std::string>& blacklisted_languages,
const std::vector<std::string>& accept_languages);
void ClearBlockedLanguages();
void ClearBlacklistedSites();
void ClearWhitelistedLanguagePairs();
bool IsValueBlacklisted(const char* pref_id, const std::string& value) const;
void BlacklistValue(const char* pref_id, const std::string& value);
void RemoveValueFromBlacklist(const char* pref_id, const std::string& value);
bool IsValueInList(const base::ListValue* list,
const std::string& value) const;
bool IsListEmpty(const char* pref_id) const;
bool IsDictionaryEmpty(const char* pref_id) const;
// Path to the preference storing the accept languages.
const std::string accept_languages_pref_;
#if defined(OS_CHROMEOS)
// Path to the preference storing the preferred languages.
// Only used on ChromeOS.
std::string preferred_languages_pref_;
#endif
// Retrieves the dictionary mapping the number of times translation has been
// denied for a language, creating it if necessary.
base::DictionaryValue* GetTranslationDeniedCountDictionary();
// Retrieves the dictionary mapping the number of times translation has been
// accepted for a language, creating it if necessary.
base::DictionaryValue* GetTranslationAcceptedCountDictionary() const;
PrefService* prefs_; // Weak.
DISALLOW_COPY_AND_ASSIGN(TranslatePrefs);
};
} // namespace translate
#endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_
|