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