Upstream version 5.34.104.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 class TranslateAcceptLanguages;
17
18 namespace base {
19 class DictionaryValue;
20 class ListValue;
21 }
22
23 namespace user_prefs {
24 class PrefRegistrySyncable;
25 }
26
27 // The wrapper of PrefService object for Translate.
28 //
29 // It is assumed that |prefs_| is alive while this instance is alive.
30 class TranslatePrefs {
31  public:
32   static const char kPrefTranslateLanguageBlacklist[];
33   static const char kPrefTranslateSiteBlacklist[];
34   static const char kPrefTranslateWhitelists[];
35   static const char kPrefTranslateDeniedCount[];
36   static const char kPrefTranslateAcceptedCount[];
37   static const char kPrefTranslateBlockedLanguages[];
38
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);
44
45   // Resets the blocked languages list, the sites blacklist, the languages
46   // whitelist, and the accepted/denied counts.
47   void ResetToDefaults();
48
49   bool IsBlockedLanguage(const std::string& original_language) const;
50   void BlockLanguage(const std::string& original_language);
51   void UnblockLanguage(const std::string& original_language);
52
53   // Removes a language from the old blacklist. Only used internally for
54   // diagnostics. Don't use this if there is no special reason.
55   void RemoveLanguageFromLegacyBlacklist(const std::string& original_language);
56
57   bool IsSiteBlacklisted(const std::string& site) const;
58   void BlacklistSite(const std::string& site);
59   void RemoveSiteFromBlacklist(const std::string& site);
60
61   bool HasWhitelistedLanguagePairs() const;
62
63   bool IsLanguagePairWhitelisted(const std::string& original_language,
64                                  const std::string& target_language);
65   void WhitelistLanguagePair(const std::string& original_language,
66                              const std::string& target_language);
67   void RemoveLanguagePairFromWhitelist(const std::string& original_language,
68                                        const std::string& target_language);
69
70   // Will return true if at least one language has been blacklisted.
71   bool HasBlockedLanguages() const;
72
73   // Will return true if at least one site has been blacklisted.
74   bool HasBlacklistedSites() const;
75
76   // These methods are used to track how many times the user has denied the
77   // translation for a specific language. (So we can present a UI to black-list
78   // that language if the user keeps denying translations).
79   int GetTranslationDeniedCount(const std::string& language) const;
80   void IncrementTranslationDeniedCount(const std::string& language);
81   void ResetTranslationDeniedCount(const std::string& language);
82
83   // These methods are used to track how many times the user has accepted the
84   // translation for a specific language. (So we can present a UI to white-list
85   // that language if the user keeps accepting translations).
86   int GetTranslationAcceptedCount(const std::string& language);
87   void IncrementTranslationAcceptedCount(const std::string& language);
88   void ResetTranslationAcceptedCount(const std::string& language);
89
90   // Gets the language list of the language settings.
91   void GetLanguageList(std::vector<std::string>* languages);
92
93   // Updates the language list of the language settings.
94   void UpdateLanguageList(const std::vector<std::string>& languages);
95
96   bool CanTranslateLanguage(TranslateAcceptLanguages* accept_languages,
97                             const std::string& language);
98   bool ShouldAutoTranslate(const std::string& original_language,
99                            std::string* target_language);
100   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
101   static void MigrateUserPrefs(PrefService* user_prefs,
102                                const char* accept_languages_pref);
103
104   // Converts the language code for Translate. This removes the sub code (like
105   // -US) except for Chinese, and converts the synonyms.
106   // The same logic exists at language_options.js, and please keep consistency
107   // with the JavaScript file.
108   static std::string ConvertLangCodeForTranslation(const std::string& lang);
109
110  private:
111   friend class TranslatePrefsTest;
112   FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, CreateBlockedLanguages);
113   FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest,
114                            CreateBlockedLanguagesNonEnglishUI);
115
116   // Merges two language sets to migrate to the language setting UI.
117   static void CreateBlockedLanguages(
118       std::vector<std::string>* blocked_languages,
119       const std::vector<std::string>& blacklisted_languages,
120       const std::vector<std::string>& accept_languages);
121
122   void ClearBlockedLanguages();
123   void ClearBlacklistedSites();
124   void ClearWhitelistedLanguagePairs();
125   bool IsValueBlacklisted(const char* pref_id, const std::string& value) const;
126   void BlacklistValue(const char* pref_id, const std::string& value);
127   void RemoveValueFromBlacklist(const char* pref_id, const std::string& value);
128   bool IsValueInList(const base::ListValue* list,
129                      const std::string& value) const;
130   bool IsListEmpty(const char* pref_id) const;
131   bool IsDictionaryEmpty(const char* pref_id) const;
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
141   // Retrieves the dictionary mapping the number of times translation has been
142   // denied for a language, creating it if necessary.
143   base::DictionaryValue* GetTranslationDeniedCountDictionary();
144
145   // Retrieves the dictionary mapping the number of times translation has been
146   // accepted for a language, creating it if necessary.
147   base::DictionaryValue* GetTranslationAcceptedCountDictionary() const;
148
149   PrefService* prefs_;  // Weak.
150
151   DISALLOW_COPY_AND_ASSIGN(TranslatePrefs);
152 };
153
154 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_PREFS_H_