Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / translate / translate_prefs.h
blob6b2f680f86510817f10a6237c4e938f76c17bf7c
1 // Copyright (c) 2010 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 CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_
8 #include <string>
9 #include <vector>
11 #include "base/gtest_prod_util.h"
12 #include "url/gurl.h"
14 class PrefService;
15 class Profile;
17 namespace base {
18 class DictionaryValue;
19 class ListValue;
22 namespace user_prefs {
23 class PrefRegistrySyncable;
26 // The wrapper of PrefService object for Translate.
28 // It is assumed that |prefs_| is alive while this instance is alive.
29 class TranslatePrefs {
30 public:
31 static const char kPrefTranslateLanguageBlacklist[];
32 static const char kPrefTranslateSiteBlacklist[];
33 static const char kPrefTranslateWhitelists[];
34 static const char kPrefTranslateDeniedCount[];
35 static const char kPrefTranslateAcceptedCount[];
36 static const char kPrefTranslateBlockedLanguages[];
38 explicit TranslatePrefs(PrefService* user_prefs);
40 // Resets the blocked languages list, the sites blacklist, the languages
41 // whitelist, and the accepted/denied counts.
42 void ResetToDefaults();
44 bool IsBlockedLanguage(const std::string& original_language) const;
45 void BlockLanguage(const std::string& original_language);
46 void UnblockLanguage(const std::string& original_language);
48 // Removes a language from the old blacklist. This method is for
49 // chrome://translate-internals/. Don't use this if there is no special
50 // reason.
51 void RemoveLanguageFromLegacyBlacklist(const std::string& original_language);
53 bool IsSiteBlacklisted(const std::string& site) const;
54 void BlacklistSite(const std::string& site);
55 void RemoveSiteFromBlacklist(const std::string& site);
57 bool HasWhitelistedLanguagePairs() const;
59 bool IsLanguagePairWhitelisted(const std::string& original_language,
60 const std::string& target_language);
61 void WhitelistLanguagePair(const std::string& original_language,
62 const std::string& target_language);
63 void RemoveLanguagePairFromWhitelist(const std::string& original_language,
64 const std::string& target_language);
66 // Will return true if at least one language has been blacklisted.
67 bool HasBlockedLanguages() const;
69 // Will return true if at least one site has been blacklisted.
70 bool HasBlacklistedSites() const;
72 // These methods are used to track how many times the user has denied the
73 // translation for a specific language. (So we can present a UI to black-list
74 // that language if the user keeps denying translations).
75 int GetTranslationDeniedCount(const std::string& language) const;
76 void IncrementTranslationDeniedCount(const std::string& language);
77 void ResetTranslationDeniedCount(const std::string& language);
79 // These methods are used to track how many times the user has accepted the
80 // translation for a specific language. (So we can present a UI to white-list
81 // that language if the user keeps accepting translations).
82 int GetTranslationAcceptedCount(const std::string& language);
83 void IncrementTranslationAcceptedCount(const std::string& language);
84 void ResetTranslationAcceptedCount(const std::string& language);
86 // Sets the language list of chrome://settings/languages.
87 void GetLanguageList(std::vector<std::string>* languages);
89 // Updates the language list of chrome://settings/languages.
90 void UpdateLanguageList(const std::vector<std::string>& languages);
92 static bool CanTranslateLanguage(
93 Profile* profile, const std::string& language);
94 static bool ShouldAutoTranslate(PrefService* user_prefs,
95 const std::string& original_language, std::string* target_language);
96 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
97 static void MigrateUserPrefs(PrefService* user_prefs);
99 // Converts the language code for Translate. This removes the sub code (like
100 // -US) except for Chinese, and converts the synonyms.
101 // The same logic exists at language_options.js, and please keep consistency
102 // with the JavaScript file.
103 static std::string ConvertLangCodeForTranslation(const std::string &lang);
105 private:
106 friend class TranslatePrefsTest;
107 FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, CreateBlockedLanguages);
108 FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest,
109 CreateBlockedLanguagesNonEnglishUI);
111 // Merges two language sets to migrate to the language setting UI.
112 static void CreateBlockedLanguages(
113 std::vector<std::string>* blocked_languages,
114 const std::vector<std::string>& blacklisted_languages,
115 const std::vector<std::string>& accept_languages);
117 void ClearBlockedLanguages();
118 void ClearBlacklistedSites();
119 void ClearWhitelistedLanguagePairs();
120 bool IsValueBlacklisted(const char* pref_id, const std::string& value) const;
121 void BlacklistValue(const char* pref_id, const std::string& value);
122 void RemoveValueFromBlacklist(const char* pref_id, const std::string& value);
123 bool IsValueInList(
124 const base::ListValue* list, const std::string& value) const;
125 bool IsLanguageWhitelisted(const std::string& original_language,
126 std::string* target_language) const;
127 bool IsListEmpty(const char* pref_id) const;
128 bool IsDictionaryEmpty(const char* pref_id) const;
130 // Retrieves the dictionary mapping the number of times translation has been
131 // denied for a language, creating it if necessary.
132 base::DictionaryValue* GetTranslationDeniedCountDictionary();
134 // Retrieves the dictionary mapping the number of times translation has been
135 // accepted for a language, creating it if necessary.
136 base::DictionaryValue* GetTranslationAcceptedCountDictionary() const;
138 PrefService* prefs_; // Weak.
140 DISALLOW_COPY_AND_ASSIGN(TranslatePrefs);
143 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_