| OLD | NEW |
| 1 // Copyright (c) 2010 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/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" |
| 11 #include "chrome/common/json_value_serializer.h" | 11 #include "chrome/common/json_value_serializer.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // Some extensions we'll tack on to copies of the Preferences files. | 15 // Some extensions we'll tack on to copies of the Preferences files. |
| 16 const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); | 16 const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); |
| 17 | 17 |
| 18 } // namespace | 18 } // namespace |
| 19 | 19 |
| 20 JsonPrefStore::JsonPrefStore(const FilePath& filename, | 20 JsonPrefStore::JsonPrefStore(const FilePath& filename, |
| 21 base::MessageLoopProxy* file_message_loop_proxy) | 21 base::MessageLoopProxy* file_message_loop_proxy) |
| 22 : path_(filename), | 22 : path_(filename), |
| 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 CommitPendingWrite(); |
| 30 writer_.DoScheduledWrite(); | |
| 31 } | 30 } |
| 32 | 31 |
| 33 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, | 32 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, |
| 34 Value** result) const { | 33 Value** result) const { |
| 35 return prefs_->Get(key, result) ? READ_OK : READ_NO_VALUE; | 34 return prefs_->Get(key, result) ? READ_OK : READ_NO_VALUE; |
| 36 } | 35 } |
| 37 | 36 |
| 38 void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { | 37 void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 39 observers_.AddObserver(observer); | 38 observers_.AddObserver(observer); |
| 40 } | 39 } |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 return true; | 151 return true; |
| 153 } | 152 } |
| 154 | 153 |
| 155 void JsonPrefStore::ScheduleWritePrefs() { | 154 void JsonPrefStore::ScheduleWritePrefs() { |
| 156 if (read_only_) | 155 if (read_only_) |
| 157 return; | 156 return; |
| 158 | 157 |
| 159 writer_.ScheduleWrite(this); | 158 writer_.ScheduleWrite(this); |
| 160 } | 159 } |
| 161 | 160 |
| 161 void JsonPrefStore::CommitPendingWrite() { |
| 162 if (writer_.HasPendingWrite() && !read_only_) |
| 163 writer_.DoScheduledWrite(); |
| 164 } |
| 165 |
| 162 void JsonPrefStore::ReportValueChanged(const std::string& key) { | 166 void JsonPrefStore::ReportValueChanged(const std::string& key) { |
| 163 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 167 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 164 } | 168 } |
| 165 | 169 |
| 166 bool JsonPrefStore::SerializeData(std::string* output) { | 170 bool JsonPrefStore::SerializeData(std::string* output) { |
| 167 // TODO(tc): Do we want to prune webkit preferences that match the default | 171 // TODO(tc): Do we want to prune webkit preferences that match the default |
| 168 // value? | 172 // value? |
| 169 JSONStringValueSerializer serializer(output); | 173 JSONStringValueSerializer serializer(output); |
| 170 serializer.set_pretty_print(true); | 174 serializer.set_pretty_print(true); |
| 171 scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); | 175 scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); |
| 172 return serializer.Serialize(*(copy.get())); | 176 return serializer.Serialize(*(copy.get())); |
| 173 } | 177 } |
| OLD | NEW |