Home | History | Annotate | Download | only in translate
      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 
     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 class TranslatePrefs {
     27  public:
     28   static const char kPrefTranslateLanguageBlacklist[];
     29   static const char kPrefTranslateSiteBlacklist[];
     30   static const char kPrefTranslateWhitelists[];
     31   static const char kPrefTranslateDeniedCount[];
     32   static const char kPrefTranslateAcceptedCount[];
     33   static const char kPrefTranslateBlockedLanguages[];
     34 
     35   explicit TranslatePrefs(PrefService* user_prefs);
     36 
     37   bool IsBlockedLanguage(const std::string& original_language) const;
     38   void BlockLanguage(const std::string& original_language);
     39   void UnblockLanguage(const std::string& original_language);
     40 
     41   // Removes a language from the old blacklist. This method is for
     42   // chrome://translate-internals/.  Don't use this if there is no special
     43   // reason.
     44   void RemoveLanguageFromLegacyBlacklist(const std::string& original_language);
     45 
     46   bool IsSiteBlacklisted(const std::string& site) const;
     47   void BlacklistSite(const std::string& site);
     48   void RemoveSiteFromBlacklist(const std::string& site);
     49 
     50   bool HasWhitelistedLanguagePairs() const;
     51   void ClearWhitelistedLanguagePairs();
     52 
     53   bool IsLanguagePairWhitelisted(const std::string& original_language,
     54       const std::string& target_language);
     55   void WhitelistLanguagePair(const std::string& original_language,
     56       const std::string& target_language);
     57   void RemoveLanguagePairFromWhitelist(const std::string& original_language,
     58       const std::string& target_language);
     59 
     60   // Will return true if at least one language has been blacklisted.
     61   bool HasBlacklistedLanguages() const;
     62   void ClearBlacklistedLanguages();
     63 
     64   // Will return true if at least one site has been blacklisted.
     65   bool HasBlacklistedSites() const;
     66   void ClearBlacklistedSites();
     67 
     68   // These methods are used to track how many times the user has denied the
     69   // translation for a specific language. (So we can present a UI to black-list
     70   // that language if the user keeps denying translations).
     71   int GetTranslationDeniedCount(const std::string& language) const;
     72   void IncrementTranslationDeniedCount(const std::string& language);
     73   void ResetTranslationDeniedCount(const std::string& language);
     74 
     75   // These methods are used to track how many times the user has accepted the
     76   // translation for a specific language. (So we can present a UI to white-list
     77   // that language if the user keeps accepting translations).
     78   int GetTranslationAcceptedCount(const std::string& language);
     79   void IncrementTranslationAcceptedCount(const std::string& language);
     80   void ResetTranslationAcceptedCount(const std::string& language);
     81 
     82   static bool CanTranslateLanguage(
     83       Profile* profile, const std::string& language);
     84   static bool ShouldAutoTranslate(PrefService* user_prefs,
     85       const std::string& original_language, std::string* target_language);
     86   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
     87   static void MigrateUserPrefs(PrefService* user_prefs);
     88 
     89  private:
     90   friend class TranslatePrefsTest;
     91   FRIEND_TEST_ALL_PREFIXES(TranslatePrefsTest, CreateBlockedLanguages);
     92 
     93   // Merges two language sets to migrate to the language setting UI.
     94   static void CreateBlockedLanguages(
     95       std::vector<std::string>* blocked_languages,
     96       const std::vector<std::string>& blacklisted_languages,
     97       const std::vector<std::string>& accept_languages);
     98 
     99   bool IsValueBlacklisted(const char* pref_id, const std::string& value) const;
    100   void BlacklistValue(const char* pref_id, const std::string& value);
    101   void RemoveValueFromBlacklist(const char* pref_id, const std::string& value);
    102   bool IsValueInList(
    103       const base::ListValue* list, const std::string& value) const;
    104   bool IsLanguageWhitelisted(const std::string& original_language,
    105       std::string* target_language) const;
    106   bool IsListEmpty(const char* pref_id) const;
    107   bool IsDictionaryEmpty(const char* pref_id) const;
    108 
    109   // Retrieves the dictionary mapping the number of times translation has been
    110   // denied for a language, creating it if necessary.
    111   base::DictionaryValue* GetTranslationDeniedCountDictionary();
    112 
    113   // Retrieves the dictionary mapping the number of times translation has been
    114   // accepted for a language, creating it if necessary.
    115   base::DictionaryValue* GetTranslationAcceptedCountDictionary() const;
    116 
    117   PrefService* prefs_;  // Weak.
    118 };
    119 
    120 #endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_PREFS_H_
    121