Chromium Code Reviews| 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" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/json/json_file_value_serializer.h" | 12 #include "base/json/json_file_value_serializer.h" |
| 13 #include "base/json/json_string_value_serializer.h" | 13 #include "base/json/json_string_value_serializer.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/message_loop_proxy.h" | 15 #include "base/message_loop_proxy.h" |
| 16 #include "base/sequenced_task_runner.h" | |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Some extensions we'll tack on to copies of the Preferences files. | 21 // Some extensions we'll tack on to copies of the Preferences files. |
| 21 const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); | 22 const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); |
| 22 | 23 |
| 23 // Differentiates file loading between origin thread and passed | 24 // Differentiates file loading between origin thread and passed |
| 24 // (aka file) thread. | 25 // (aka file) thread. |
| 25 class FileThreadDeserializer | 26 class FileThreadDeserializer |
| 26 : public base::RefCountedThreadSafe<FileThreadDeserializer> { | 27 : public base::RefCountedThreadSafe<FileThreadDeserializer> { |
| 27 public: | 28 public: |
| 28 FileThreadDeserializer(JsonPrefStore* delegate, | 29 FileThreadDeserializer(JsonPrefStore* delegate, |
| 29 base::MessageLoopProxy* file_loop_proxy) | 30 base::SequencedTaskRunner* blocking_task_runner) |
| 30 : no_dir_(false), | 31 : no_dir_(false), |
| 31 error_(PersistentPrefStore::PREF_READ_ERROR_NONE), | 32 error_(PersistentPrefStore::PREF_READ_ERROR_NONE), |
| 32 delegate_(delegate), | 33 delegate_(delegate), |
| 33 file_loop_proxy_(file_loop_proxy), | 34 blocking_task_runner_(blocking_task_runner), |
| 34 origin_loop_proxy_(base::MessageLoopProxy::current()) { | 35 origin_loop_proxy_(base::MessageLoopProxy::current()) { |
| 35 } | 36 } |
| 36 | 37 |
| 37 void Start(const FilePath& path) { | 38 void Start(const FilePath& path) { |
| 38 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); | 39 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); |
| 39 file_loop_proxy_->PostTask( | 40 blocking_task_runner_->PostTask( |
| 40 FROM_HERE, | 41 FROM_HERE, |
| 41 base::Bind(&FileThreadDeserializer::ReadFileAndReport, | 42 base::Bind(&FileThreadDeserializer::ReadFileAndReport, |
| 42 this, path)); | 43 this, path)); |
| 43 } | 44 } |
| 44 | 45 |
| 45 // Deserializes JSON on the file thread. | 46 // Deserializes JSON on the file thread. |
| 46 void ReadFileAndReport(const FilePath& path) { | 47 void ReadFileAndReport(const FilePath& path) { |
| 47 DCHECK(file_loop_proxy_->BelongsToCurrentThread()); | |
| 48 | |
| 49 value_.reset(DoReading(path, &error_, &no_dir_)); | 48 value_.reset(DoReading(path, &error_, &no_dir_)); |
| 50 | 49 |
| 51 origin_loop_proxy_->PostTask( | 50 origin_loop_proxy_->PostTask( |
| 52 FROM_HERE, | 51 FROM_HERE, |
| 53 base::Bind(&FileThreadDeserializer::ReportOnOriginThread, this)); | 52 base::Bind(&FileThreadDeserializer::ReportOnOriginThread, this)); |
| 54 } | 53 } |
| 55 | 54 |
| 56 // Reports deserialization result on the origin thread. | 55 // Reports deserialization result on the origin thread. |
| 57 void ReportOnOriginThread() { | 56 void ReportOnOriginThread() { |
| 58 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); | 57 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 78 PersistentPrefStore::PrefReadError* error); | 77 PersistentPrefStore::PrefReadError* error); |
| 79 | 78 |
| 80 private: | 79 private: |
| 81 friend class base::RefCountedThreadSafe<FileThreadDeserializer>; | 80 friend class base::RefCountedThreadSafe<FileThreadDeserializer>; |
| 82 ~FileThreadDeserializer() {} | 81 ~FileThreadDeserializer() {} |
| 83 | 82 |
| 84 bool no_dir_; | 83 bool no_dir_; |
| 85 PersistentPrefStore::PrefReadError error_; | 84 PersistentPrefStore::PrefReadError error_; |
| 86 scoped_ptr<Value> value_; | 85 scoped_ptr<Value> value_; |
| 87 scoped_refptr<JsonPrefStore> delegate_; | 86 scoped_refptr<JsonPrefStore> delegate_; |
| 88 scoped_refptr<base::MessageLoopProxy> file_loop_proxy_; | 87 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 89 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_; | 88 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_; |
| 90 }; | 89 }; |
| 91 | 90 |
| 92 // static | 91 // static |
| 93 void FileThreadDeserializer::HandleErrors( | 92 void FileThreadDeserializer::HandleErrors( |
| 94 const Value* value, | 93 const Value* value, |
| 95 const FilePath& path, | 94 const FilePath& path, |
| 96 int error_code, | 95 int error_code, |
| 97 const std::string& error_msg, | 96 const std::string& error_msg, |
| 98 PersistentPrefStore::PrefReadError* error) { | 97 PersistentPrefStore::PrefReadError* error) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 break; | 130 break; |
| 132 } | 131 } |
| 133 } else if (!value->IsType(Value::TYPE_DICTIONARY)) { | 132 } else if (!value->IsType(Value::TYPE_DICTIONARY)) { |
| 134 *error = PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; | 133 *error = PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; |
| 135 } | 134 } |
| 136 } | 135 } |
| 137 | 136 |
| 138 } // namespace | 137 } // namespace |
| 139 | 138 |
| 140 JsonPrefStore::JsonPrefStore(const FilePath& filename, | 139 JsonPrefStore::JsonPrefStore(const FilePath& filename, |
| 141 base::MessageLoopProxy* file_message_loop_proxy) | 140 base::SequencedTaskRunner* blocking_task_runner) |
| 142 : path_(filename), | 141 : path_(filename), |
| 143 file_message_loop_proxy_(file_message_loop_proxy), | 142 blocking_task_runner_(blocking_task_runner), |
| 144 prefs_(new DictionaryValue()), | 143 prefs_(new DictionaryValue()), |
| 145 read_only_(false), | 144 read_only_(false), |
| 146 writer_(filename, file_message_loop_proxy), | 145 writer_(filename, blocking_task_runner), |
| 147 error_delegate_(NULL), | 146 error_delegate_(NULL), |
| 148 initialized_(false), | 147 initialized_(false), |
| 149 read_error_(PREF_READ_ERROR_OTHER) { | 148 read_error_(PREF_READ_ERROR_OTHER) { |
| 150 } | 149 } |
| 151 | 150 |
| 152 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, | 151 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, |
| 153 const Value** result) const { | 152 const Value** result) const { |
| 154 Value* tmp = NULL; | 153 Value* tmp = NULL; |
| 155 if (prefs_->Get(key, &tmp)) { | 154 if (prefs_->Get(key, &tmp)) { |
| 156 if (result) | 155 if (result) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 initialized_ = false; | 237 initialized_ = false; |
| 239 error_delegate_.reset(error_delegate); | 238 error_delegate_.reset(error_delegate); |
| 240 if (path_.empty()) { | 239 if (path_.empty()) { |
| 241 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | 240 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); |
| 242 return; | 241 return; |
| 243 } | 242 } |
| 244 | 243 |
| 245 // Start async reading of the preferences file. It will delete itself | 244 // Start async reading of the preferences file. It will delete itself |
| 246 // in the end. | 245 // in the end. |
| 247 scoped_refptr<FileThreadDeserializer> deserializer( | 246 scoped_refptr<FileThreadDeserializer> deserializer( |
| 248 new FileThreadDeserializer(this, file_message_loop_proxy_.get())); | 247 new FileThreadDeserializer(this, blocking_task_runner_)); |
| 249 deserializer->Start(path_); | 248 deserializer->Start(path_); |
| 250 } | 249 } |
| 251 | 250 |
| 252 void JsonPrefStore::CommitPendingWrite() { | 251 void JsonPrefStore::CommitPendingWrite() { |
| 253 if (writer_.HasPendingWrite() && !read_only_) | 252 if (writer_.HasPendingWrite() && !read_only_) |
| 254 writer_.DoScheduledWrite(); | 253 writer_.DoScheduledWrite(); |
| 255 } | 254 } |
| 256 | 255 |
| 257 void JsonPrefStore::ReportValueChanged(const std::string& key) { | 256 void JsonPrefStore::ReportValueChanged(const std::string& key) { |
| 258 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 257 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 copy->Set(key, new base::ListValue); | 333 copy->Set(key, new base::ListValue); |
| 335 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { | 334 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 336 const base::DictionaryValue* dict = NULL; | 335 const base::DictionaryValue* dict = NULL; |
| 337 if (value->GetAsDictionary(&dict) && dict->empty()) | 336 if (value->GetAsDictionary(&dict) && dict->empty()) |
| 338 copy->Set(key, new base::DictionaryValue); | 337 copy->Set(key, new base::DictionaryValue); |
| 339 } | 338 } |
| 340 } | 339 } |
| 341 | 340 |
| 342 return serializer.Serialize(*(copy.get())); | 341 return serializer.Serialize(*(copy.get())); |
| 343 } | 342 } |
| 343 | |
|
brettw
2012/05/03 20:46:22
You added blank lines to the bottom of some of the
Sergey Ulanov
2012/05/03 20:56:48
Added it in attempt to workaround git-cl-upload no
| |
| OLD | NEW |