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"
12 #include "base/prefs/scoped_user_pref_update.h"
13 #include "base/time/time.h"
20 class DictionaryValue
;
24 namespace user_prefs
{
25 class PrefRegistrySyncable
;
30 class TranslateAcceptLanguages
;
32 // Allows updating denial times for a specific language while maintaining the
33 // maximum list size and ensuring PrefObservers are notified of change values.
34 class DenialTimeUpdate
{
36 DenialTimeUpdate(PrefService
* prefs
,
37 const std::string
& language
,
38 size_t max_denial_count
);
41 // Gets the list of timestamps when translation was denied. Guaranteed to
42 // be non-null, potentially inserts a new listvalue into the dictionary if
44 base::ListValue
* GetDenialTimes();
46 // Gets the oldest denial time on record. Will return base::Time::max() if
47 // no denial time has been recorded yet.
48 base::Time
GetOldestDenialTime();
50 // Records a new denial time. Does not ensure ordering of denial times - it is
51 // up to the user to ensure times are in monotonic order.
52 void AddDenialTime(base::Time denial_time
);
55 DictionaryPrefUpdate denial_time_dict_update_
;
56 std::string language_
;
57 size_t max_denial_count_
;
58 base::ListValue
* time_list_
; // Weak, owned by the containing prefs service.
61 // The wrapper of PrefService object for Translate.
63 // It is assumed that |prefs_| is alive while this instance is alive.
64 class TranslatePrefs
{
66 static const char kPrefTranslateSiteBlacklist
[];
67 static const char kPrefTranslateWhitelists
[];
68 static const char kPrefTranslateDeniedCount
[];
69 static const char kPrefTranslateAcceptedCount
[];
70 static const char kPrefTranslateBlockedLanguages
[];
71 static const char kPrefTranslateLastDeniedTimeForLanguage
[];
72 static const char kPrefTranslateTooOftenDeniedForLanguage
[];
74 // |preferred_languages_pref| is only used on Chrome OS, other platforms must
76 TranslatePrefs(PrefService
* user_prefs
,
77 const char* accept_languages_pref
,
78 const char* preferred_languages_pref
);
80 // Resets the blocked languages list, the sites blacklist, the languages
81 // whitelist, and the accepted/denied counts.
82 void ResetToDefaults();
84 bool IsBlockedLanguage(const std::string
& original_language
) const;
85 void BlockLanguage(const std::string
& original_language
);
86 void UnblockLanguage(const std::string
& original_language
);
88 // Removes a language from the old blacklist. Only used internally for
89 // diagnostics. Don't use this if there is no special reason.
90 void RemoveLanguageFromLegacyBlacklist(const std::string
& original_language
);
92 bool IsSiteBlacklisted(const std::string
& site
) const;
93 void BlacklistSite(const std::string
& site
);
94 void RemoveSiteFromBlacklist(const std::string
& site
);
96 bool HasWhitelistedLanguagePairs() const;
98 bool IsLanguagePairWhitelisted(const std::string
& original_language
,
99 const std::string
& target_language
);
100 void WhitelistLanguagePair(const std::string
& original_language
,
101 const std::string
& target_language
);
102 void RemoveLanguagePairFromWhitelist(const std::string
& original_language
,
103 const std::string
& target_language
);
105 // Will return true if at least one language has been blacklisted.
106 bool HasBlockedLanguages() const;
108 // Will return true if at least one site has been blacklisted.
109 bool HasBlacklistedSites() const;
111 // These methods are used to track how many times the user has denied the
112 // translation for a specific language. (So we can present a UI to black-list
113 // that language if the user keeps denying translations).
114 int GetTranslationDeniedCount(const std::string
& language
) const;
115 void IncrementTranslationDeniedCount(const std::string
& language
);
116 void ResetTranslationDeniedCount(const std::string
& language
);
118 // These methods are used to track how many times the user has accepted the
119 // translation for a specific language. (So we can present a UI to white-list
120 // that language if the user keeps accepting translations).
121 int GetTranslationAcceptedCount(const std::string
& language
);
122 void IncrementTranslationAcceptedCount(const std::string
& language
);
123 void ResetTranslationAcceptedCount(const std::string
& language
);
125 // Update the last time on closing the Translate UI without translation.
126 void UpdateLastDeniedTime(const std::string
& language
);
128 // Returns true if translation is denied too often.
129 bool IsTooOftenDenied(const std::string
& language
) const;
131 // Resets the prefs of denial state. Only used internally for diagnostics.
132 void ResetDenialState();
134 // Gets the language list of the language settings.
135 void GetLanguageList(std::vector
<std::string
>* languages
);
137 // Updates the language list of the language settings.
138 void UpdateLanguageList(const std::vector
<std::string
>& languages
);
140 bool CanTranslateLanguage(TranslateAcceptLanguages
* accept_languages
,
141 const std::string
& language
);
142 bool ShouldAutoTranslate(const std::string
& original_language
,
143 std::string
* target_language
);
144 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
145 static void MigrateUserPrefs(PrefService
* user_prefs
,
146 const char* accept_languages_pref
);
149 friend class TranslatePrefsTest
;
150 FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest
, CreateBlockedLanguages
);
151 FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest
,
152 CreateBlockedLanguagesNonEnglishUI
);
154 // Merges two language sets to migrate to the language setting UI.
155 static void CreateBlockedLanguages(
156 std::vector
<std::string
>* blocked_languages
,
157 const std::vector
<std::string
>& blacklisted_languages
,
158 const std::vector
<std::string
>& accept_languages
);
160 void ClearBlockedLanguages();
161 void ClearBlacklistedSites();
162 void ClearWhitelistedLanguagePairs();
163 bool IsValueBlacklisted(const char* pref_id
, const std::string
& value
) const;
164 void BlacklistValue(const char* pref_id
, const std::string
& value
);
165 void RemoveValueFromBlacklist(const char* pref_id
, const std::string
& value
);
166 bool IsValueInList(const base::ListValue
* list
,
167 const std::string
& value
) const;
168 bool IsListEmpty(const char* pref_id
) const;
169 bool IsDictionaryEmpty(const char* pref_id
) const;
171 // Path to the preference storing the accept languages.
172 const std::string accept_languages_pref_
;
173 #if defined(OS_CHROMEOS)
174 // Path to the preference storing the preferred languages.
175 // Only used on ChromeOS.
176 std::string preferred_languages_pref_
;
179 // Retrieves the dictionary mapping the number of times translation has been
180 // denied for a language, creating it if necessary.
181 base::DictionaryValue
* GetTranslationDeniedCountDictionary();
183 // Retrieves the dictionary mapping the number of times translation has been
184 // accepted for a language, creating it if necessary.
185 base::DictionaryValue
* GetTranslationAcceptedCountDictionary() const;
187 PrefService
* prefs_
; // Weak.
189 DISALLOW_COPY_AND_ASSIGN(TranslatePrefs
);
192 } // namespace translate
194 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_