Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / translate / core / browser / translate_prefs.h
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.
4
5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/gtest_prod_util.h"
12 #include "url/gurl.h"
13
14 class PrefService;
15 class Profile;
16
17 namespace base {
18 class DictionaryValue;
19 class ListValue;
20 }
21
22 namespace user_prefs {
23 class PrefRegistrySyncable;
24 }
25
26 namespace translate {
27
28 class TranslateAcceptLanguages;
29
30 // The wrapper of PrefService object for Translate.
31 //
32 // It is assumed that |prefs_| is alive while this instance is alive.
33 class TranslatePrefs {
34  public:
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[];
43
44   // |preferred_languages_pref| is only used on Chrome OS, other platforms must
45   // pass NULL.
46   TranslatePrefs(PrefService* user_prefs,
47                  const char* accept_languages_pref,
48                  const char* preferred_languages_pref);
49
50   // Resets the blocked languages list, the sites blacklist, the languages
51   // whitelist, and the accepted/denied counts.
52   void ResetToDefaults();
53
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);
57
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);
61
62   bool IsSiteBlacklisted(const std::string& site) const;
63   void BlacklistSite(const std::string& site);
64   void RemoveSiteFromBlacklist(const std::string& site);
65
66   bool HasWhitelistedLanguagePairs() const;
67
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);
74
75   // Will return true if at least one language has been blacklisted.
76   bool HasBlockedLanguages() const;
77
78   // Will return true if at least one site has been blacklisted.
79   bool HasBlacklistedSites() const;
80
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);
87
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);
94
95   // Update the last time on closing the Translate UI without translation.
96   void UpdateLastDeniedTime();
97
98   // Returns true if translation is denied too often.
99   bool IsTooOftenDenied() const;
100
101   // Resets the prefs of denial state. Only used internally for diagnostics.
102   void ResetDenialState();
103
104   // Gets the language list of the language settings.
105   void GetLanguageList(std::vector<std::string>* languages);
106
107   // Updates the language list of the language settings.
108   void UpdateLanguageList(const std::vector<std::string>& languages);
109
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);
117
118   // Converts the language code for Translate. This removes the sub code (like
119   // -US) except for Chinese, and converts the synonyms.
120   // The same logic exists at language_options.js, and please keep consistency
121   // with the JavaScript file.
122   static std::string ConvertLangCodeForTranslation(const std::string& lang);
123
124  private:
125   friend class TranslatePrefsTest;
126   FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, CreateBlockedLanguages);
127   FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest,
128                            CreateBlockedLanguagesNonEnglishUI);
129
130   // Merges two language sets to migrate to the language setting UI.
131   static void CreateBlockedLanguages(
132       std::vector<std::string>* blocked_languages,
133       const std::vector<std::string>& blacklisted_languages,
134       const std::vector<std::string>& accept_languages);
135
136   void ClearBlockedLanguages();
137   void ClearBlacklistedSites();
138   void ClearWhitelistedLanguagePairs();
139   bool IsValueBlacklisted(const char* pref_id, const std::string& value) const;
140   void BlacklistValue(const char* pref_id, const std::string& value);
141   void RemoveValueFromBlacklist(const char* pref_id, const std::string& value);
142   bool IsValueInList(const base::ListValue* list,
143                      const std::string& value) const;
144   bool IsListEmpty(const char* pref_id) const;
145   bool IsDictionaryEmpty(const char* pref_id) const;
146
147   // Path to the preference storing the accept languages.
148   const std::string accept_languages_pref_;
149 #if defined(OS_CHROMEOS)
150   // Path to the preference storing the preferred languages.
151   // Only used on ChromeOS.
152   std::string preferred_languages_pref_;
153 #endif
154
155   // Retrieves the dictionary mapping the number of times translation has been
156   // denied for a language, creating it if necessary.
157   base::DictionaryValue* GetTranslationDeniedCountDictionary();
158
159   // Retrieves the dictionary mapping the number of times translation has been
160   // accepted for a language, creating it if necessary.
161   base::DictionaryValue* GetTranslationAcceptedCountDictionary() const;
162
163   PrefService* prefs_;  // Weak.
164
165   DISALLOW_COPY_AND_ASSIGN(TranslatePrefs);
166 };
167
168 }  // namespace translate
169
170 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_