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 "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/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 } | 72 } |
73 | 73 |
74 static void HandleErrors(const Value* value, | 74 static void HandleErrors(const Value* value, |
75 const FilePath& path, | 75 const FilePath& path, |
76 int error_code, | 76 int error_code, |
77 const std::string& error_msg, | 77 const std::string& error_msg, |
78 PersistentPrefStore::PrefReadError* error); | 78 PersistentPrefStore::PrefReadError* error); |
79 | 79 |
80 private: | 80 private: |
81 friend class base::RefCountedThreadSafe<FileThreadDeserializer>; | 81 friend class base::RefCountedThreadSafe<FileThreadDeserializer>; |
82 ~FileThreadDeserializer() {} | |
82 | 83 |
83 bool no_dir_; | 84 bool no_dir_; |
84 PersistentPrefStore::PrefReadError error_; | 85 PersistentPrefStore::PrefReadError error_; |
85 scoped_ptr<Value> value_; | 86 scoped_ptr<Value> value_; |
86 scoped_refptr<JsonPrefStore> delegate_; | 87 scoped_refptr<JsonPrefStore> delegate_; |
87 scoped_refptr<base::MessageLoopProxy> file_loop_proxy_; | 88 scoped_refptr<base::MessageLoopProxy> file_loop_proxy_; |
88 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_; | 89 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_; |
89 }; | 90 }; |
90 | 91 |
91 // static | 92 // static |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 : path_(filename), | 142 : path_(filename), |
142 file_message_loop_proxy_(file_message_loop_proxy), | 143 file_message_loop_proxy_(file_message_loop_proxy), |
143 prefs_(new DictionaryValue()), | 144 prefs_(new DictionaryValue()), |
144 read_only_(false), | 145 read_only_(false), |
145 writer_(filename, file_message_loop_proxy), | 146 writer_(filename, file_message_loop_proxy), |
146 error_delegate_(NULL), | 147 error_delegate_(NULL), |
147 initialized_(false), | 148 initialized_(false), |
148 read_error_(PREF_READ_ERROR_OTHER) { | 149 read_error_(PREF_READ_ERROR_OTHER) { |
149 } | 150 } |
150 | 151 |
151 JsonPrefStore::~JsonPrefStore() { | |
152 CommitPendingWrite(); | |
153 } | |
154 | |
155 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, | 152 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, |
156 const Value** result) const { | 153 const Value** result) const { |
157 Value* tmp = NULL; | 154 Value* tmp = NULL; |
158 if (prefs_->Get(key, &tmp)) { | 155 if (prefs_->Get(key, &tmp)) { |
159 if (result) | 156 if (result) |
160 *result = tmp; | 157 *result = tmp; |
161 return READ_OK; | 158 return READ_OK; |
162 } | 159 } |
163 return READ_NO_VALUE; | 160 return READ_NO_VALUE; |
164 } | 161 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 } | 214 } |
218 | 215 |
219 bool JsonPrefStore::ReadOnly() const { | 216 bool JsonPrefStore::ReadOnly() const { |
220 return read_only_; | 217 return read_only_; |
221 } | 218 } |
222 | 219 |
223 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { | 220 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { |
224 return read_error_; | 221 return read_error_; |
225 } | 222 } |
226 | 223 |
224 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { | |
225 if (path_.empty()) { | |
226 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | |
227 return PREF_READ_ERROR_FILE_NOT_SPECIFIED; | |
228 } | |
229 | |
230 PrefReadError error; | |
231 bool no_dir; | |
232 Value* value = FileThreadDeserializer::DoReading(path_, &error, &no_dir); | |
233 OnFileRead(value, error, no_dir); | |
234 return error; | |
235 } | |
236 | |
237 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate *error_delegate) { | |
238 initialized_ = false; | |
239 error_delegate_.reset(error_delegate); | |
240 if (path_.empty()) { | |
241 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | |
242 return; | |
243 } | |
244 | |
245 // Start async reading of the preferences file. It will delete itself | |
246 // in the end. | |
247 scoped_refptr<FileThreadDeserializer> deserializer( | |
248 new FileThreadDeserializer(this, file_message_loop_proxy_.get())); | |
249 deserializer->Start(path_); | |
250 } | |
251 | |
252 void JsonPrefStore::CommitPendingWrite() { | |
253 if (writer_.HasPendingWrite() && !read_only_) | |
254 writer_.DoScheduledWrite(); | |
255 } | |
256 | |
257 void JsonPrefStore::ReportValueChanged(const std::string& key) { | |
258 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | |
259 if (!read_only_) | |
260 writer_.ScheduleWrite(this); | |
261 } | |
262 | |
227 void JsonPrefStore::OnFileRead(Value* value_owned, | 263 void JsonPrefStore::OnFileRead(Value* value_owned, |
228 PersistentPrefStore::PrefReadError error, | 264 PersistentPrefStore::PrefReadError error, |
229 bool no_dir) { | 265 bool no_dir) { |
230 scoped_ptr<Value> value(value_owned); | 266 scoped_ptr<Value> value(value_owned); |
231 initialized_ = true; | 267 initialized_ = true; |
232 read_error_ = error; | 268 read_error_ = error; |
233 | 269 |
234 if (no_dir) { | 270 if (no_dir) { |
235 FOR_EACH_OBSERVER(PrefStore::Observer, | 271 FOR_EACH_OBSERVER(PrefStore::Observer, |
236 observers_, | 272 observers_, |
(...skipping 25 matching lines...) Expand all Loading... | |
262 } | 298 } |
263 | 299 |
264 if (error_delegate_.get() && error != PREF_READ_ERROR_NONE) | 300 if (error_delegate_.get() && error != PREF_READ_ERROR_NONE) |
265 error_delegate_->OnError(error); | 301 error_delegate_->OnError(error); |
266 | 302 |
267 FOR_EACH_OBSERVER(PrefStore::Observer, | 303 FOR_EACH_OBSERVER(PrefStore::Observer, |
268 observers_, | 304 observers_, |
269 OnInitializationCompleted(true)); | 305 OnInitializationCompleted(true)); |
270 } | 306 } |
271 | 307 |
272 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate *error_delegate) { | 308 JsonPrefStore::~JsonPrefStore() { |
273 initialized_ = false; | 309 CommitPendingWrite(); |
274 error_delegate_.reset(error_delegate); | |
275 if (path_.empty()) { | |
276 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | |
277 return; | |
278 } | |
279 | |
280 // Start async reading of the preferences file. It will delete itself | |
281 // in the end. | |
282 scoped_refptr<FileThreadDeserializer> deserializer( | |
283 new FileThreadDeserializer(this, file_message_loop_proxy_.get())); | |
284 deserializer->Start(path_); | |
285 } | |
286 | |
287 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { | |
288 if (path_.empty()) { | |
289 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | |
290 return PREF_READ_ERROR_FILE_NOT_SPECIFIED; | |
291 } | |
292 | |
293 PrefReadError error; | |
294 bool no_dir; | |
Nico
2012/04/25 13:20:37
why are you moving unrelated functions around?
Ryan Sleevi
2012/04/25 15:13:59
Places where the order of definition /almost/ matc
| |
295 Value* value = FileThreadDeserializer::DoReading(path_, &error, &no_dir); | |
296 OnFileRead(value, error, no_dir); | |
297 return error; | |
298 } | |
299 | |
300 void JsonPrefStore::CommitPendingWrite() { | |
301 if (writer_.HasPendingWrite() && !read_only_) | |
302 writer_.DoScheduledWrite(); | |
303 } | |
304 | |
305 void JsonPrefStore::ReportValueChanged(const std::string& key) { | |
306 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | |
307 if (!read_only_) | |
308 writer_.ScheduleWrite(this); | |
309 } | 310 } |
310 | 311 |
311 bool JsonPrefStore::SerializeData(std::string* output) { | 312 bool JsonPrefStore::SerializeData(std::string* output) { |
312 // TODO(tc): Do we want to prune webkit preferences that match the default | 313 // TODO(tc): Do we want to prune webkit preferences that match the default |
313 // value? | 314 // value? |
314 JSONStringValueSerializer serializer(output); | 315 JSONStringValueSerializer serializer(output); |
315 serializer.set_pretty_print(true); | 316 serializer.set_pretty_print(true); |
316 scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); | 317 scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); |
317 | 318 |
318 // Iterates |keys_need_empty_value_| and if the key exists in |prefs_|, | 319 // Iterates |keys_need_empty_value_| and if the key exists in |prefs_|, |
(...skipping 14 matching lines...) Expand all Loading... | |
333 copy->Set(key, new base::ListValue); | 334 copy->Set(key, new base::ListValue); |
334 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { | 335 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { |
335 const base::DictionaryValue* dict = NULL; | 336 const base::DictionaryValue* dict = NULL; |
336 if (value->GetAsDictionary(&dict) && dict->empty()) | 337 if (value->GetAsDictionary(&dict) && dict->empty()) |
337 copy->Set(key, new base::DictionaryValue); | 338 copy->Set(key, new base::DictionaryValue); |
338 } | 339 } |
339 } | 340 } |
340 | 341 |
341 return serializer.Serialize(*(copy.get())); | 342 return serializer.Serialize(*(copy.get())); |
342 } | 343 } |
OLD | NEW |