OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/prefs/json_pref_store.h" | 5 #include "base/prefs/json_pref_store.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 Value* old_value = NULL; | 211 Value* old_value = NULL; |
212 prefs_->Get(key, &old_value); | 212 prefs_->Get(key, &old_value); |
213 if (!old_value || !value->Equals(old_value)) { | 213 if (!old_value || !value->Equals(old_value)) { |
214 prefs_->Set(key, new_value.release()); | 214 prefs_->Set(key, new_value.release()); |
215 if (!read_only_) | 215 if (!read_only_) |
216 writer_.ScheduleWrite(this); | 216 writer_.ScheduleWrite(this); |
217 } | 217 } |
218 } | 218 } |
219 | 219 |
220 void JsonPrefStore::RemoveValue(const std::string& key) { | 220 void JsonPrefStore::RemoveValue(const std::string& key) { |
221 if (prefs_->Remove(key, NULL)) | 221 if (prefs_->RemovePath(key, NULL)) |
222 ReportValueChanged(key); | 222 ReportValueChanged(key); |
223 } | 223 } |
224 | 224 |
225 void JsonPrefStore::MarkNeedsEmptyValue(const std::string& key) { | |
226 keys_need_empty_value_.insert(key); | |
227 } | |
228 | |
229 bool JsonPrefStore::ReadOnly() const { | 225 bool JsonPrefStore::ReadOnly() const { |
230 return read_only_; | 226 return read_only_; |
231 } | 227 } |
232 | 228 |
233 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { | 229 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { |
234 return read_error_; | 230 return read_error_; |
235 } | 231 } |
236 | 232 |
237 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { | 233 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { |
238 if (path_.empty()) { | 234 if (path_.empty()) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 317 |
322 JsonPrefStore::~JsonPrefStore() { | 318 JsonPrefStore::~JsonPrefStore() { |
323 CommitPendingWrite(); | 319 CommitPendingWrite(); |
324 } | 320 } |
325 | 321 |
326 bool JsonPrefStore::SerializeData(std::string* output) { | 322 bool JsonPrefStore::SerializeData(std::string* output) { |
327 // TODO(tc): Do we want to prune webkit preferences that match the default | 323 // TODO(tc): Do we want to prune webkit preferences that match the default |
328 // value? | 324 // value? |
329 JSONStringValueSerializer serializer(output); | 325 JSONStringValueSerializer serializer(output); |
330 serializer.set_pretty_print(true); | 326 serializer.set_pretty_print(true); |
331 scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); | 327 return serializer.Serialize(*prefs_); |
332 | |
333 // Iterates |keys_need_empty_value_| and if the key exists in |prefs_|, | |
334 // ensure its empty ListValue or DictonaryValue is preserved. | |
335 for (std::set<std::string>::const_iterator | |
336 it = keys_need_empty_value_.begin(); | |
337 it != keys_need_empty_value_.end(); | |
338 ++it) { | |
339 const std::string& key = *it; | |
340 | |
341 base::Value* value = NULL; | |
342 if (!prefs_->Get(key, &value)) | |
343 continue; | |
344 | |
345 if (value->IsType(base::Value::TYPE_LIST)) { | |
346 const base::ListValue* list = NULL; | |
347 if (value->GetAsList(&list) && list->empty()) | |
348 copy->Set(key, new base::ListValue); | |
349 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { | |
350 const base::DictionaryValue* dict = NULL; | |
351 if (value->GetAsDictionary(&dict) && dict->empty()) | |
352 copy->Set(key, new base::DictionaryValue); | |
353 } | |
354 } | |
355 | |
356 return serializer.Serialize(*(copy.get())); | |
357 } | 328 } |
OLD | NEW |