1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/translate/translate_prefs.h"
#include "base/string_util.h"
#include "chrome/browser/pref_service.h"
static const wchar_t kPrefTranslateLanguageBlacklist[] =
L"translate_language_blacklist";
static const wchar_t kPrefTranslateSiteBlacklist[] =
L"translate_site_blacklist";
static const wchar_t kPrefTranslateWhitelists[] = L"translate_whitelists";
// TranslatePrefs: public: -----------------------------------------------------
TranslatePrefs::TranslatePrefs(PrefService* user_prefs)
: prefs_(user_prefs) {
Register();
}
bool TranslatePrefs::IsLanguageBlacklisted(
const std::string& original_language) {
return IsValueBlacklisted(kPrefTranslateLanguageBlacklist, original_language);
}
void TranslatePrefs::BlacklistLanguage(const std::string& original_language) {
BlacklistValue(kPrefTranslateLanguageBlacklist, original_language);
}
void TranslatePrefs::RemoveLanguageFromBlacklist(
const std::string& original_language) {
RemoveValueFromBlacklist(kPrefTranslateLanguageBlacklist, original_language);
}
bool TranslatePrefs::IsSiteBlacklisted(const std::string& site) {
return IsValueBlacklisted(kPrefTranslateSiteBlacklist, site);
}
void TranslatePrefs::BlacklistSite(const std::string& site) {
BlacklistValue(kPrefTranslateSiteBlacklist, site);
}
void TranslatePrefs::RemoveSiteFromBlacklist(const std::string& site) {
RemoveValueFromBlacklist(kPrefTranslateSiteBlacklist, site);
}
bool TranslatePrefs::IsLanguagePairWhitelisted(
const std::string& original_language,
const std::string& target_language) {
const DictionaryValue* dict = prefs_->GetDictionary(kPrefTranslateWhitelists);
if (dict && !dict->empty()) {
ListValue* whitelist = NULL;
if (dict->GetList(ASCIIToWide(original_language), &whitelist) &&
whitelist && !whitelist->empty() &&
IsValueInList(whitelist, target_language))
return true;
}
return false;
}
void TranslatePrefs::WhitelistLanguagePair(
const std::string& original_language,
const std::string& target_language) {
DictionaryValue* dict = prefs_->GetMutableDictionary(
kPrefTranslateWhitelists);
if (!dict) {
NOTREACHED() << "Unregistered translate whitelist pref";
return;
}
std::wstring wide_original(ASCIIToWide(original_language));
StringValue* language = new StringValue(target_language);
ListValue* whitelist = NULL;
if (dict->GetList(wide_original, &whitelist) && whitelist) {
whitelist->Append(language);
} else {
ListValue new_whitelist;
new_whitelist.Append(language);
dict->Set(wide_original, new_whitelist.DeepCopy());
}
prefs_->ScheduleSavePersistentPrefs();
}
void TranslatePrefs::RemoveLanguagePairFromWhitelist(
const std::string& original_language,
const std::string& target_language) {
DictionaryValue* dict = prefs_->GetMutableDictionary(
kPrefTranslateWhitelists);
if (!dict) {
NOTREACHED() << "Unregistered translate whitelist pref";
return;
}
ListValue* whitelist = NULL;
std::wstring wide_original(ASCIIToWide(original_language));
if (dict->GetList(wide_original, &whitelist) && whitelist) {
StringValue language(target_language);
if (whitelist->Remove(language) != -1) {
if (whitelist->empty()) // If list is empty, remove key from dict.
dict->Remove(wide_original, NULL);
prefs_->ScheduleSavePersistentPrefs();
}
}
}
// TranslatePrefs: static: -----------------------------------------------------
bool TranslatePrefs::CanTranslate(PrefService* user_prefs,
const std::string& original_language, const GURL& url) {
TranslatePrefs prefs(user_prefs);
if (prefs.IsSiteBlacklisted(url.HostNoBrackets()))
return false;
return (!prefs.IsLanguageBlacklisted(original_language));
}
bool TranslatePrefs::ShouldAutoTranslate(PrefService* user_prefs,
const std::string& original_language,
const std::string& target_language) {
TranslatePrefs prefs(user_prefs);
return prefs.IsLanguagePairWhitelisted(original_language, target_language);
}
// TranslatePrefs: private: ----------------------------------------------------
void TranslatePrefs::Register() {
if (!prefs_->FindPreference(kPrefTranslateLanguageBlacklist))
prefs_->RegisterListPref(kPrefTranslateLanguageBlacklist);
if (!prefs_->FindPreference(kPrefTranslateSiteBlacklist))
prefs_->RegisterListPref(kPrefTranslateSiteBlacklist);
if (!prefs_->FindPreference(kPrefTranslateWhitelists))
prefs_->RegisterDictionaryPref(kPrefTranslateWhitelists);
}
bool TranslatePrefs::IsValueInList(const ListValue* list,
const std::string& in_value) {
for (size_t i = 0; i < list->GetSize(); ++i) {
std::string value;
if (list->GetString(i, &value) && value == in_value)
return true;
}
return false;
}
bool TranslatePrefs::IsValueBlacklisted(const wchar_t* pref_id,
const std::string& value) {
const ListValue* blacklist = prefs_->GetList(pref_id);
return (blacklist && !blacklist->empty() && IsValueInList(blacklist, value));
}
void TranslatePrefs::BlacklistValue(const wchar_t* pref_id,
const std::string& value) {
ListValue* blacklist = prefs_->GetMutableList(pref_id);
if (!blacklist) {
NOTREACHED() << "Unregistered translate blacklist pref";
return;
}
blacklist->Append(new StringValue(value));
prefs_->ScheduleSavePersistentPrefs();
}
void TranslatePrefs::RemoveValueFromBlacklist(const wchar_t* pref_id,
const std::string& value) {
ListValue* blacklist = prefs_->GetMutableList(pref_id);
if (!blacklist) {
NOTREACHED() << "Unregistered translate blacklist pref";
return;
}
StringValue string_value(value);
if (blacklist->Remove(string_value) != -1)
prefs_->ScheduleSavePersistentPrefs();
}
|