Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(599)

Side by Side Diff: chrome/browser/translate/translate_prefs.cc

Issue 8198007: Remove PrefService::ScheduleSavePersistentPrefs and SavePersistentPrefs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix chromeos Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/translate/translate_prefs.h" 5 #include "chrome/browser/translate/translate_prefs.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "chrome/browser/prefs/pref_service.h" 8 #include "chrome/browser/prefs/pref_service.h"
9 #include "chrome/browser/prefs/scoped_user_pref_update.h" 9 #include "chrome/browser/prefs/scoped_user_pref_update.h"
10 10
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 void TranslatePrefs::WhitelistLanguagePair( 67 void TranslatePrefs::WhitelistLanguagePair(
68 const std::string& original_language, 68 const std::string& original_language,
69 const std::string& target_language) { 69 const std::string& target_language) {
70 DictionaryPrefUpdate update(prefs_, kPrefTranslateWhitelists); 70 DictionaryPrefUpdate update(prefs_, kPrefTranslateWhitelists);
71 DictionaryValue* dict = update.Get(); 71 DictionaryValue* dict = update.Get();
72 if (!dict) { 72 if (!dict) {
73 NOTREACHED() << "Unregistered translate whitelist pref"; 73 NOTREACHED() << "Unregistered translate whitelist pref";
74 return; 74 return;
75 } 75 }
76 dict->SetString(original_language, target_language); 76 dict->SetString(original_language, target_language);
77 prefs_->ScheduleSavePersistentPrefs();
78 } 77 }
79 78
80 void TranslatePrefs::RemoveLanguagePairFromWhitelist( 79 void TranslatePrefs::RemoveLanguagePairFromWhitelist(
81 const std::string& original_language, 80 const std::string& original_language,
82 const std::string& target_language) { 81 const std::string& target_language) {
83 DictionaryPrefUpdate update(prefs_, kPrefTranslateWhitelists); 82 DictionaryPrefUpdate update(prefs_, kPrefTranslateWhitelists);
84 DictionaryValue* dict = update.Get(); 83 DictionaryValue* dict = update.Get();
85 if (!dict) { 84 if (!dict) {
86 NOTREACHED() << "Unregistered translate whitelist pref"; 85 NOTREACHED() << "Unregistered translate whitelist pref";
87 return; 86 return;
88 } 87 }
89 if (dict->Remove(original_language, NULL)) 88 dict->Remove(original_language, NULL);
90 prefs_->ScheduleSavePersistentPrefs();
91 } 89 }
92 90
93 int TranslatePrefs::GetTranslationDeniedCount(const std::string& language) { 91 int TranslatePrefs::GetTranslationDeniedCount(const std::string& language) {
94 const DictionaryValue* dict = 92 const DictionaryValue* dict =
95 prefs_->GetDictionary(kPrefTranslateDeniedCount); 93 prefs_->GetDictionary(kPrefTranslateDeniedCount);
96 int count = 0; 94 int count = 0;
97 return dict->GetInteger(language, &count) ? count : 0; 95 return dict->GetInteger(language, &count) ? count : 0;
98 } 96 }
99 97
100 void TranslatePrefs::IncrementTranslationDeniedCount( 98 void TranslatePrefs::IncrementTranslationDeniedCount(
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 // target lang overwrites the previous one. 184 // target lang overwrites the previous one.
187 // - this results in a one-to-one relationship between source lang and target 185 // - this results in a one-to-one relationship between source lang and target
188 // lang 186 // lang
189 // - we replace old list of target langs with the last target lang in list, 187 // - we replace old list of target langs with the last target lang in list,
190 // assuming the last (i.e. most recent) target lang is what user wants to 188 // assuming the last (i.e. most recent) target lang is what user wants to
191 // keep auto-translated. 189 // keep auto-translated.
192 DictionaryPrefUpdate update(user_prefs, kPrefTranslateWhitelists); 190 DictionaryPrefUpdate update(user_prefs, kPrefTranslateWhitelists);
193 DictionaryValue* dict = update.Get(); 191 DictionaryValue* dict = update.Get();
194 if (!dict || dict->empty()) 192 if (!dict || dict->empty())
195 return; 193 return;
196 bool save_prefs = false;
197 for (DictionaryValue::key_iterator iter(dict->begin_keys()); 194 for (DictionaryValue::key_iterator iter(dict->begin_keys());
198 iter != dict->end_keys(); ++iter) { 195 iter != dict->end_keys(); ++iter) {
199 ListValue* list = NULL; 196 ListValue* list = NULL;
200 if (!dict->GetList(*iter, &list) || !list) 197 if (!dict->GetList(*iter, &list) || !list)
201 break; // Dictionary has either been migrated or new format. 198 break; // Dictionary has either been migrated or new format.
202 save_prefs = true;
203 std::string target_lang; 199 std::string target_lang;
204 if (list->empty() || !list->GetString(list->GetSize() - 1, &target_lang) || 200 if (list->empty() || !list->GetString(list->GetSize() - 1, &target_lang) ||
205 target_lang.empty()) 201 target_lang.empty())
206 dict->Remove(*iter, NULL); 202 dict->Remove(*iter, NULL);
207 else 203 else
208 dict->SetString(*iter, target_lang); 204 dict->SetString(*iter, target_lang);
209 } 205 }
210 if (!save_prefs)
211 return;
212 user_prefs->ScheduleSavePersistentPrefs();
213 } 206 }
214 207
215 // TranslatePrefs: private: ---------------------------------------------------- 208 // TranslatePrefs: private: ----------------------------------------------------
216 209
217 bool TranslatePrefs::IsValueInList(const ListValue* list, 210 bool TranslatePrefs::IsValueInList(const ListValue* list,
218 const std::string& in_value) { 211 const std::string& in_value) {
219 for (size_t i = 0; i < list->GetSize(); ++i) { 212 for (size_t i = 0; i < list->GetSize(); ++i) {
220 std::string value; 213 std::string value;
221 if (list->GetString(i, &value) && value == in_value) 214 if (list->GetString(i, &value) && value == in_value)
222 return true; 215 return true;
(...skipping 11 matching lines...) Expand all
234 const std::string& value) { 227 const std::string& value) {
235 { 228 {
236 ListPrefUpdate update(prefs_, pref_id); 229 ListPrefUpdate update(prefs_, pref_id);
237 ListValue* blacklist = update.Get(); 230 ListValue* blacklist = update.Get();
238 if (!blacklist) { 231 if (!blacklist) {
239 NOTREACHED() << "Unregistered translate blacklist pref"; 232 NOTREACHED() << "Unregistered translate blacklist pref";
240 return; 233 return;
241 } 234 }
242 blacklist->Append(new StringValue(value)); 235 blacklist->Append(new StringValue(value));
243 } 236 }
244 prefs_->ScheduleSavePersistentPrefs();
245 } 237 }
246 238
247 void TranslatePrefs::RemoveValueFromBlacklist(const char* pref_id, 239 void TranslatePrefs::RemoveValueFromBlacklist(const char* pref_id,
248 const std::string& value) { 240 const std::string& value) {
249 bool schedule_save = false; 241 ListPrefUpdate update(prefs_, pref_id);
250 { 242 ListValue* blacklist = update.Get();
251 ListPrefUpdate update(prefs_, pref_id); 243 if (!blacklist) {
252 ListValue* blacklist = update.Get(); 244 NOTREACHED() << "Unregistered translate blacklist pref";
253 if (!blacklist) { 245 return;
254 NOTREACHED() << "Unregistered translate blacklist pref";
255 return;
256 }
257 StringValue string_value(value);
258 schedule_save = blacklist->Remove(string_value, NULL);
259 } 246 }
260 if (schedule_save) 247 StringValue string_value(value);
261 prefs_->ScheduleSavePersistentPrefs(); 248 blacklist->Remove(string_value, NULL);
262 } 249 }
263 250
264 bool TranslatePrefs::IsLanguageWhitelisted( 251 bool TranslatePrefs::IsLanguageWhitelisted(
265 const std::string& original_language, std::string* target_language) { 252 const std::string& original_language, std::string* target_language) {
266 const DictionaryValue* dict = prefs_->GetDictionary(kPrefTranslateWhitelists); 253 const DictionaryValue* dict = prefs_->GetDictionary(kPrefTranslateWhitelists);
267 if (dict && dict->GetString(original_language, target_language)) { 254 if (dict && dict->GetString(original_language, target_language)) {
268 DCHECK(!target_language->empty()); 255 DCHECK(!target_language->empty());
269 return !target_language->empty(); 256 return !target_language->empty();
270 } 257 }
271 return false; 258 return false;
272 } 259 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698