| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/common/json_pref_store.h" | 5 #include "chrome/common/json_pref_store.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 prefs_(new DictionaryValue()), | 23 prefs_(new DictionaryValue()), |
| 24 read_only_(false), | 24 read_only_(false), |
| 25 writer_(filename, file_message_loop_proxy) { | 25 writer_(filename, file_message_loop_proxy) { |
| 26 } | 26 } |
| 27 | 27 |
| 28 JsonPrefStore::~JsonPrefStore() { | 28 JsonPrefStore::~JsonPrefStore() { |
| 29 if (writer_.HasPendingWrite() && !read_only_) | 29 if (writer_.HasPendingWrite() && !read_only_) |
| 30 writer_.DoScheduledWrite(); | 30 writer_.DoScheduledWrite(); |
| 31 } | 31 } |
| 32 | 32 |
| 33 PrefStore::PrefReadError JsonPrefStore::ReadPrefs() { | 33 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, |
| 34 Value** result) const { |
| 35 return prefs_->Get(key, result) ? READ_OK : READ_NO_VALUE; |
| 36 } |
| 37 |
| 38 void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 39 observers_.AddObserver(observer); |
| 40 } |
| 41 |
| 42 void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) { |
| 43 observers_.RemoveObserver(observer); |
| 44 } |
| 45 |
| 46 void JsonPrefStore::SetValue(const std::string& key, Value* value) { |
| 47 DCHECK(value); |
| 48 scoped_ptr<Value> new_value(value); |
| 49 Value* old_value = NULL; |
| 50 prefs_->Get(key, &old_value); |
| 51 if (!old_value || !value->Equals(old_value)) { |
| 52 prefs_->Set(key, new_value.release()); |
| 53 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 54 } |
| 55 } |
| 56 |
| 57 void JsonPrefStore::SetValueSilently(const std::string& key, Value* value) { |
| 58 DCHECK(value); |
| 59 scoped_ptr<Value> new_value(value); |
| 60 Value* old_value = NULL; |
| 61 prefs_->Get(key, &old_value); |
| 62 if (!old_value || !value->Equals(old_value)) |
| 63 prefs_->Set(key, new_value.release()); |
| 64 } |
| 65 |
| 66 void JsonPrefStore::RemoveValue(const std::string& key) { |
| 67 if (prefs_->Remove(key, NULL)) { |
| 68 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 69 } |
| 70 } |
| 71 |
| 72 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { |
| 34 if (path_.empty()) { | 73 if (path_.empty()) { |
| 35 read_only_ = true; | 74 read_only_ = true; |
| 36 return PREF_READ_ERROR_FILE_NOT_SPECIFIED; | 75 return PREF_READ_ERROR_FILE_NOT_SPECIFIED; |
| 37 } | 76 } |
| 38 JSONFileValueSerializer serializer(path_); | 77 JSONFileValueSerializer serializer(path_); |
| 39 | 78 |
| 40 int error_code = 0; | 79 int error_code = 0; |
| 41 std::string error_msg; | 80 std::string error_msg; |
| 42 scoped_ptr<Value> value(serializer.Deserialize(&error_code, &error_msg)); | 81 scoped_ptr<Value> value(serializer.Deserialize(&error_code, &error_msg)); |
| 43 if (!value.get()) { | 82 if (!value.get()) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 } | 163 } |
| 125 | 164 |
| 126 bool JsonPrefStore::SerializeData(std::string* output) { | 165 bool JsonPrefStore::SerializeData(std::string* output) { |
| 127 // TODO(tc): Do we want to prune webkit preferences that match the default | 166 // TODO(tc): Do we want to prune webkit preferences that match the default |
| 128 // value? | 167 // value? |
| 129 JSONStringValueSerializer serializer(output); | 168 JSONStringValueSerializer serializer(output); |
| 130 serializer.set_pretty_print(true); | 169 serializer.set_pretty_print(true); |
| 131 scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); | 170 scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); |
| 132 return serializer.Serialize(*(copy.get())); | 171 return serializer.Serialize(*(copy.get())); |
| 133 } | 172 } |
| OLD | NEW |