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.
4
5#ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_
6#define CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/gtest_prod_util.h"
12#include "url/gurl.h"
13
14class PrefService;
15class Profile;
16
17namespace base {
18class DictionaryValue;
19class ListValue;
20}
21
22namespace user_prefs {
23class PrefRegistrySyncable;
24}
25
26// The wrapper of PrefService object for Translate.
27//
28// It is assumed that |prefs_| is alive while this instance is alive.
29class 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[];
37
38  explicit TranslatePrefs(PrefService* user_prefs);
39
40  // Resets the blocked languages list, the sites blacklist, the languages
41  // whitelist, and the accepted/denied counts.
42  void ResetToDefaults();
43
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);
47
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);
52
53  bool IsSiteBlacklisted(const std::string& site) const;
54  void BlacklistSite(const std::string& site);
55  void RemoveSiteFromBlacklist(const std::string& site);
56
57  bool HasWhitelistedLanguagePairs() const;
58
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);
65
66  // Will return true if at least one language has been blacklisted.
67  bool HasBlockedLanguages() const;
68
69  // Will return true if at least one site has been blacklisted.
70  bool HasBlacklistedSites() const;
71
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);
78
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);
85
86  // Sets the language list of chrome://settings/languages.
87  void GetLanguageList(std::vector<std::string>* languages);
88
89  // Updates the language list of chrome://settings/languages.
90  void UpdateLanguageList(const std::vector<std::string>& languages);
91
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);
98
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);
104
105 private:
106  friend class TranslatePrefsTest;
107  FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, CreateBlockedLanguages);
108  FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest,
109                           CreateBlockedLanguagesNonEnglishUI);
110
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);
116
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;
129
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();
133
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;
137
138  PrefService* prefs_;  // Weak.
139
140  DISALLOW_COPY_AND_ASSIGN(TranslatePrefs);
141};
142
143#endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_
144