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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
// 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"
#include "chrome/browser/scoped_pref_update.h"
const wchar_t TranslatePrefs::kPrefTranslateLanguageBlacklist[] =
L"translate_language_blacklist";
const wchar_t TranslatePrefs::kPrefTranslateSiteBlacklist[] =
L"translate_site_blacklist";
const wchar_t TranslatePrefs::kPrefTranslateWhitelists[] =
L"translate_whitelists";
// TranslatePrefs: public: -----------------------------------------------------
TranslatePrefs::TranslatePrefs(PrefService* user_prefs)
: prefs_(user_prefs) {
}
bool TranslatePrefs::IsLanguageBlacklisted(
const std::string& original_language) {
return IsValueBlacklisted(kPrefTranslateLanguageBlacklist, original_language);
}
void TranslatePrefs::BlacklistLanguage(const std::string& original_language) {
ScopedPrefUpdate update(prefs_, kPrefTranslateLanguageBlacklist);
BlacklistValue(kPrefTranslateLanguageBlacklist, original_language);
}
void TranslatePrefs::RemoveLanguageFromBlacklist(
const std::string& original_language) {
ScopedPrefUpdate update(prefs_, kPrefTranslateLanguageBlacklist);
RemoveValueFromBlacklist(kPrefTranslateLanguageBlacklist, original_language);
}
bool TranslatePrefs::IsSiteBlacklisted(const std::string& site) {
return IsValueBlacklisted(kPrefTranslateSiteBlacklist, site);
}
void TranslatePrefs::BlacklistSite(const std::string& site) {
ScopedPrefUpdate update(prefs_, kPrefTranslateSiteBlacklist);
BlacklistValue(kPrefTranslateSiteBlacklist, site);
}
void TranslatePrefs::RemoveSiteFromBlacklist(const std::string& site) {
ScopedPrefUpdate update(prefs_, kPrefTranslateSiteBlacklist);
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()) {
std::string auto_target_lang;
if (dict->GetString(ASCIIToWide(original_language), &auto_target_lang) &&
auto_target_lang == 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;
}
ScopedPrefUpdate update(prefs_, kPrefTranslateWhitelists);
dict->SetString(ASCIIToWide(original_language), target_language);
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;
}
ScopedPrefUpdate update(prefs_, kPrefTranslateWhitelists);
if (dict->Remove(ASCIIToWide(original_language), NULL))
prefs_->ScheduleSavePersistentPrefs();
}
// TranslatePrefs: public, 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, std::string* target_language) {
TranslatePrefs prefs(user_prefs);
return prefs.IsLanguageWhitelisted(original_language, target_language);
}
void TranslatePrefs::RegisterUserPrefs(PrefService* user_prefs) {
if (!user_prefs->FindPreference(kPrefTranslateLanguageBlacklist))
user_prefs->RegisterListPref(kPrefTranslateLanguageBlacklist);
if (!user_prefs->FindPreference(kPrefTranslateSiteBlacklist))
user_prefs->RegisterListPref(kPrefTranslateSiteBlacklist);
if (!user_prefs->FindPreference(kPrefTranslateWhitelists)) {
user_prefs->RegisterDictionaryPref(kPrefTranslateWhitelists);
MigrateTranslateWhitelists(user_prefs);
}
}
// TranslatePrefs: private, static: --------------------------------------------
void TranslatePrefs::MigrateTranslateWhitelists(PrefService* user_prefs) {
// Old format of kPrefTranslateWhitelists
// - original language -> list of target langs to auto-translate
// - list of langs is in order of being enabled i.e. last in list is the
// most recent language that user enabled via
// Always translate |source_lang| to |target_lang|"
// - this results in a one-to-n relationship between source lang and target
// langs.
// New format:
// - original language -> one target language to auto-translate
// - each time that the user enables the "Always translate..." option, that
// target lang overwrites the previous one.
// - this results in a one-to-one relationship between source lang and target
// lang
// - we replace old list of target langs with the last target lang in list,
// assuming the last (i.e. most recent) target lang is what user wants to
// keep auto-translated.
DictionaryValue* dict = user_prefs->GetMutableDictionary(
kPrefTranslateWhitelists);
if (!dict || dict->empty())
return;
bool save_prefs = false;
for (DictionaryValue::key_iterator iter(dict->begin_keys());
iter != dict->end_keys(); ++iter) {
ListValue* list = NULL;
if (!dict->GetList(*iter, &list) || !list)
break; // Dictionary has either been migrated or new format.
save_prefs = true;
std::string target_lang;
if (list->empty() || !list->GetString(list->GetSize() - 1, &target_lang) ||
target_lang.empty())
dict->Remove(*iter, NULL);
else
dict->SetString(*iter, target_lang);
}
if (!save_prefs)
return;
ScopedPrefUpdate update(user_prefs, kPrefTranslateWhitelists);
user_prefs->ScheduleSavePersistentPrefs();
}
// TranslatePrefs: private: ----------------------------------------------------
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();
}
bool TranslatePrefs::IsLanguageWhitelisted(
const std::string& original_language, std::string* target_language) {
const DictionaryValue* dict = prefs_->GetDictionary(kPrefTranslateWhitelists);
if (dict &&
dict->GetString(ASCIIToWide(original_language), target_language)) {
DCHECK(!target_language->empty());
return !target_language->empty();
}
return false;
}
|