| 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 #include "content/common/notification_service.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // Some extensions we'll tack on to copies of the Preferences files. | 16 // Some extensions we'll tack on to copies of the Preferences files. |
| 16 const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); | 17 const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); |
| 17 | 18 |
| 18 } // namespace | 19 } // namespace |
| 19 | 20 |
| 20 JsonPrefStore::JsonPrefStore(const FilePath& filename, | 21 JsonPrefStore::JsonPrefStore(const FilePath& filename, |
| 21 base::MessageLoopProxy* file_message_loop_proxy) | 22 base::MessageLoopProxy* file_message_loop_proxy) |
| 22 : path_(filename), | 23 : path_(filename), |
| 23 prefs_(new DictionaryValue()), | 24 prefs_(new DictionaryValue()), |
| 24 read_only_(false), | 25 read_only_(false), |
| 25 writer_(filename, file_message_loop_proxy) { | 26 writer_(filename, file_message_loop_proxy), |
| 27 source_(NotificationService::AllSources()) { |
| 26 } | 28 } |
| 27 | 29 |
| 28 JsonPrefStore::~JsonPrefStore() { | 30 JsonPrefStore::~JsonPrefStore() { |
| 29 if (writer_.HasPendingWrite() && !read_only_) | 31 CommitPendingWrite(); |
| 30 writer_.DoScheduledWrite(); | |
| 31 } | 32 } |
| 32 | 33 |
| 33 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, | 34 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, |
| 34 Value** result) const { | 35 Value** result) const { |
| 35 return prefs_->Get(key, result) ? READ_OK : READ_NO_VALUE; | 36 return prefs_->Get(key, result) ? READ_OK : READ_NO_VALUE; |
| 36 } | 37 } |
| 37 | 38 |
| 38 void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { | 39 void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 39 observers_.AddObserver(observer); | 40 observers_.AddObserver(observer); |
| 40 } | 41 } |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 return true; | 153 return true; |
| 153 } | 154 } |
| 154 | 155 |
| 155 void JsonPrefStore::ScheduleWritePrefs() { | 156 void JsonPrefStore::ScheduleWritePrefs() { |
| 156 if (read_only_) | 157 if (read_only_) |
| 157 return; | 158 return; |
| 158 | 159 |
| 159 writer_.ScheduleWrite(this); | 160 writer_.ScheduleWrite(this); |
| 160 } | 161 } |
| 161 | 162 |
| 163 void JsonPrefStore::CommitPendingWrite() { |
| 164 if (writer_.HasPendingWrite() && !read_only_) { |
| 165 writer_.DoScheduledWrite(); |
| 166 } else { |
| 167 #if defined(OS_CHROMEOS) |
| 168 if (NotificationService::current()) { |
| 169 NotificationService::current()->Notify( |
| 170 NotificationType::PREF_COMMITTED, |
| 171 source_, |
| 172 NotificationService::NoDetails()); |
| 173 } |
| 174 #endif |
| 175 } |
| 176 } |
| 177 |
| 178 void JsonPrefStore::SetNotificationSource(const NotificationSource& source) { |
| 179 source_ = source; |
| 180 writer_.set_notification_source(source); |
| 181 } |
| 182 |
| 162 void JsonPrefStore::ReportValueChanged(const std::string& key) { | 183 void JsonPrefStore::ReportValueChanged(const std::string& key) { |
| 163 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 184 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 164 } | 185 } |
| 165 | 186 |
| 166 bool JsonPrefStore::SerializeData(std::string* output) { | 187 bool JsonPrefStore::SerializeData(std::string* output) { |
| 167 // TODO(tc): Do we want to prune webkit preferences that match the default | 188 // TODO(tc): Do we want to prune webkit preferences that match the default |
| 168 // value? | 189 // value? |
| 169 JSONStringValueSerializer serializer(output); | 190 JSONStringValueSerializer serializer(output); |
| 170 serializer.set_pretty_print(true); | 191 serializer.set_pretty_print(true); |
| 171 scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); | 192 scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); |
| 172 return serializer.Serialize(*(copy.get())); | 193 return serializer.Serialize(*(copy.get())); |
| 173 } | 194 } |
| OLD | NEW |