Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(470)

Side by Side Diff: components/translate/core/browser/translate_prefs.h

Issue 159883002: Move TranslatePrefs to the Translate component (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@acceptLanguages
Patch Set: Fix ChromeOS browsertests Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_ 5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_ 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/gtest_prod_util.h" 11 #include "base/gtest_prod_util.h"
12 #include "url/gurl.h" 12 #include "url/gurl.h"
13 13
14 class PrefService; 14 class PrefService;
15 class Profile; 15 class Profile;
16 class TranslateAcceptLanguages;
16 17
17 namespace base { 18 namespace base {
18 class DictionaryValue; 19 class DictionaryValue;
19 class ListValue; 20 class ListValue;
20 } 21 }
21 22
22 namespace user_prefs { 23 namespace user_prefs {
23 class PrefRegistrySyncable; 24 class PrefRegistrySyncable;
24 } 25 }
25 26
26 // The wrapper of PrefService object for Translate. 27 // The wrapper of PrefService object for Translate.
27 // 28 //
28 // It is assumed that |prefs_| is alive while this instance is alive. 29 // It is assumed that |prefs_| is alive while this instance is alive.
29 class TranslatePrefs { 30 class TranslatePrefs {
30 public: 31 public:
31 static const char kPrefTranslateLanguageBlacklist[]; 32 static const char kPrefTranslateLanguageBlacklist[];
32 static const char kPrefTranslateSiteBlacklist[]; 33 static const char kPrefTranslateSiteBlacklist[];
33 static const char kPrefTranslateWhitelists[]; 34 static const char kPrefTranslateWhitelists[];
34 static const char kPrefTranslateDeniedCount[]; 35 static const char kPrefTranslateDeniedCount[];
35 static const char kPrefTranslateAcceptedCount[]; 36 static const char kPrefTranslateAcceptedCount[];
36 static const char kPrefTranslateBlockedLanguages[]; 37 static const char kPrefTranslateBlockedLanguages[];
37 38
38 explicit TranslatePrefs(PrefService* user_prefs); 39 // |preferred_languages_pref| is only used on Chrome OS, other platforms must
40 // pass NULL.
41 TranslatePrefs(PrefService* user_prefs,
42 const char* accept_languages_pref,
43 const char* preferred_languages_pref);
39 44
40 // Resets the blocked languages list, the sites blacklist, the languages 45 // Resets the blocked languages list, the sites blacklist, the languages
41 // whitelist, and the accepted/denied counts. 46 // whitelist, and the accepted/denied counts.
42 void ResetToDefaults(); 47 void ResetToDefaults();
43 48
44 bool IsBlockedLanguage(const std::string& original_language) const; 49 bool IsBlockedLanguage(const std::string& original_language) const;
45 void BlockLanguage(const std::string& original_language); 50 void BlockLanguage(const std::string& original_language);
46 void UnblockLanguage(const std::string& original_language); 51 void UnblockLanguage(const std::string& original_language);
47 52
48 // Removes a language from the old blacklist. This method is for 53 // Removes a language from the old blacklist. Only used internally for
49 // chrome://translate-internals/. Don't use this if there is no special 54 // diagnostics. Don't use this if there is no special reason.
50 // reason.
51 void RemoveLanguageFromLegacyBlacklist(const std::string& original_language); 55 void RemoveLanguageFromLegacyBlacklist(const std::string& original_language);
52 56
53 bool IsSiteBlacklisted(const std::string& site) const; 57 bool IsSiteBlacklisted(const std::string& site) const;
54 void BlacklistSite(const std::string& site); 58 void BlacklistSite(const std::string& site);
55 void RemoveSiteFromBlacklist(const std::string& site); 59 void RemoveSiteFromBlacklist(const std::string& site);
56 60
57 bool HasWhitelistedLanguagePairs() const; 61 bool HasWhitelistedLanguagePairs() const;
58 62
59 bool IsLanguagePairWhitelisted(const std::string& original_language, 63 bool IsLanguagePairWhitelisted(const std::string& original_language,
60 const std::string& target_language); 64 const std::string& target_language);
61 void WhitelistLanguagePair(const std::string& original_language, 65 void WhitelistLanguagePair(const std::string& original_language,
62 const std::string& target_language); 66 const std::string& target_language);
63 void RemoveLanguagePairFromWhitelist(const std::string& original_language, 67 void RemoveLanguagePairFromWhitelist(const std::string& original_language,
64 const std::string& target_language); 68 const std::string& target_language);
65 69
66 // Will return true if at least one language has been blacklisted. 70 // Will return true if at least one language has been blacklisted.
67 bool HasBlockedLanguages() const; 71 bool HasBlockedLanguages() const;
68 72
69 // Will return true if at least one site has been blacklisted. 73 // Will return true if at least one site has been blacklisted.
70 bool HasBlacklistedSites() const; 74 bool HasBlacklistedSites() const;
71 75
72 // These methods are used to track how many times the user has denied the 76 // 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 77 // translation for a specific language. (So we can present a UI to black-list
74 // that language if the user keeps denying translations). 78 // that language if the user keeps denying translations).
75 int GetTranslationDeniedCount(const std::string& language) const; 79 int GetTranslationDeniedCount(const std::string& language) const;
76 void IncrementTranslationDeniedCount(const std::string& language); 80 void IncrementTranslationDeniedCount(const std::string& language);
77 void ResetTranslationDeniedCount(const std::string& language); 81 void ResetTranslationDeniedCount(const std::string& language);
78 82
79 // These methods are used to track how many times the user has accepted the 83 // 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 84 // translation for a specific language. (So we can present a UI to white-list
81 // that language if the user keeps accepting translations). 85 // that language if the user keeps accepting translations).
82 int GetTranslationAcceptedCount(const std::string& language); 86 int GetTranslationAcceptedCount(const std::string& language);
83 void IncrementTranslationAcceptedCount(const std::string& language); 87 void IncrementTranslationAcceptedCount(const std::string& language);
84 void ResetTranslationAcceptedCount(const std::string& language); 88 void ResetTranslationAcceptedCount(const std::string& language);
85 89
86 // Sets the language list of chrome://settings/languages. 90 // Gets the language list of the language settings.
87 void GetLanguageList(std::vector<std::string>* languages); 91 void GetLanguageList(std::vector<std::string>* languages);
88 92
89 // Updates the language list of chrome://settings/languages. 93 // Updates the language list of the language settings.
90 void UpdateLanguageList(const std::vector<std::string>& languages); 94 void UpdateLanguageList(const std::vector<std::string>& languages);
91 95
92 static bool CanTranslateLanguage( 96 bool CanTranslateLanguage(TranslateAcceptLanguages* accept_languages,
93 Profile* profile, const std::string& language); 97 const std::string& language);
94 static bool ShouldAutoTranslate(PrefService* user_prefs, 98 bool ShouldAutoTranslate(const std::string& original_language,
95 const std::string& original_language, std::string* target_language); 99 std::string* target_language);
96 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 100 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
97 static void MigrateUserPrefs(PrefService* user_prefs); 101 static void MigrateUserPrefs(PrefService* user_prefs,
102 const char* accept_languages_pref);
98 103
99 // Converts the language code for Translate. This removes the sub code (like 104 // Converts the language code for Translate. This removes the sub code (like
100 // -US) except for Chinese, and converts the synonyms. 105 // -US) except for Chinese, and converts the synonyms.
101 // The same logic exists at language_options.js, and please keep consistency 106 // The same logic exists at language_options.js, and please keep consistency
102 // with the JavaScript file. 107 // with the JavaScript file.
103 static std::string ConvertLangCodeForTranslation(const std::string &lang); 108 static std::string ConvertLangCodeForTranslation(const std::string& lang);
104 109
105 private: 110 private:
106 friend class TranslatePrefsTest; 111 friend class TranslatePrefsTest;
107 FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, CreateBlockedLanguages); 112 FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, CreateBlockedLanguages);
108 FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, 113 FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest,
109 CreateBlockedLanguagesNonEnglishUI); 114 CreateBlockedLanguagesNonEnglishUI);
110 115
111 // Merges two language sets to migrate to the language setting UI. 116 // Merges two language sets to migrate to the language setting UI.
112 static void CreateBlockedLanguages( 117 static void CreateBlockedLanguages(
113 std::vector<std::string>* blocked_languages, 118 std::vector<std::string>* blocked_languages,
114 const std::vector<std::string>& blacklisted_languages, 119 const std::vector<std::string>& blacklisted_languages,
115 const std::vector<std::string>& accept_languages); 120 const std::vector<std::string>& accept_languages);
116 121
117 void ClearBlockedLanguages(); 122 void ClearBlockedLanguages();
118 void ClearBlacklistedSites(); 123 void ClearBlacklistedSites();
119 void ClearWhitelistedLanguagePairs(); 124 void ClearWhitelistedLanguagePairs();
120 bool IsValueBlacklisted(const char* pref_id, const std::string& value) const; 125 bool IsValueBlacklisted(const char* pref_id, const std::string& value) const;
121 void BlacklistValue(const char* pref_id, const std::string& value); 126 void BlacklistValue(const char* pref_id, const std::string& value);
122 void RemoveValueFromBlacklist(const char* pref_id, const std::string& value); 127 void RemoveValueFromBlacklist(const char* pref_id, const std::string& value);
123 bool IsValueInList( 128 bool IsValueInList(const base::ListValue* list,
124 const base::ListValue* list, const std::string& value) const; 129 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; 130 bool IsListEmpty(const char* pref_id) const;
128 bool IsDictionaryEmpty(const char* pref_id) const; 131 bool IsDictionaryEmpty(const char* pref_id) const;
129 132
133 // Path to the preference storing the accept languages.
134 const std::string accept_languages_pref_;
135 #if defined(OS_CHROMEOS)
136 // Path to the preference storing the preferred languages.
137 // Only used on ChromeOS.
138 std::string preferred_languages_pref_;
139 #endif
140
130 // Retrieves the dictionary mapping the number of times translation has been 141 // Retrieves the dictionary mapping the number of times translation has been
131 // denied for a language, creating it if necessary. 142 // denied for a language, creating it if necessary.
132 base::DictionaryValue* GetTranslationDeniedCountDictionary(); 143 base::DictionaryValue* GetTranslationDeniedCountDictionary();
133 144
134 // Retrieves the dictionary mapping the number of times translation has been 145 // Retrieves the dictionary mapping the number of times translation has been
135 // accepted for a language, creating it if necessary. 146 // accepted for a language, creating it if necessary.
136 base::DictionaryValue* GetTranslationAcceptedCountDictionary() const; 147 base::DictionaryValue* GetTranslationAcceptedCountDictionary() const;
137 148
138 PrefService* prefs_; // Weak. 149 PrefService* prefs_; // Weak.
139 150
140 DISALLOW_COPY_AND_ASSIGN(TranslatePrefs); 151 DISALLOW_COPY_AND_ASSIGN(TranslatePrefs);
141 }; 152 };
142 153
143 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_ 154 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698