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/task_runner.h" | |
|
akalin
2012/10/10 01:43:16
sequenced_task_runner.h
zel
2012/10/10 23:25:00
Done.
| |
| 17 #include "base/threading/sequenced_worker_pool.h" | |
| 16 #include "base/values.h" | 18 #include "base/values.h" |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 // Some extensions we'll tack on to copies of the Preferences files. | 22 // Some extensions we'll tack on to copies of the Preferences files. |
| 21 const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); | 23 const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); |
| 22 | 24 |
| 23 // Differentiates file loading between origin thread and passed | 25 // Differentiates file loading between origin thread and passed |
| 24 // (aka file) thread. | 26 // (aka file) thread. |
| 25 class FileThreadDeserializer | 27 class FileThreadDeserializer |
| 26 : public base::RefCountedThreadSafe<FileThreadDeserializer> { | 28 : public base::RefCountedThreadSafe<FileThreadDeserializer> { |
| 27 public: | 29 public: |
| 28 FileThreadDeserializer(JsonPrefStore* delegate, | 30 FileThreadDeserializer(JsonPrefStore* delegate, |
| 29 base::MessageLoopProxy* file_loop_proxy) | 31 base::TaskRunner* task_runner) |
|
akalin
2012/10/10 01:43:16
Sequenced
zel
2012/10/10 23:25:00
Done.
| |
| 30 : no_dir_(false), | 32 : no_dir_(false), |
| 31 error_(PersistentPrefStore::PREF_READ_ERROR_NONE), | 33 error_(PersistentPrefStore::PREF_READ_ERROR_NONE), |
| 32 delegate_(delegate), | 34 delegate_(delegate), |
| 33 file_loop_proxy_(file_loop_proxy), | 35 task_runner_(task_runner), |
| 34 origin_loop_proxy_(base::MessageLoopProxy::current()) { | 36 origin_loop_proxy_(base::MessageLoopProxy::current()) { |
| 35 } | 37 } |
| 36 | 38 |
| 37 void Start(const FilePath& path) { | 39 void Start(const FilePath& path) { |
| 38 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); | 40 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); |
| 39 file_loop_proxy_->PostTask( | 41 task_runner_->PostTask( |
| 40 FROM_HERE, | 42 FROM_HERE, |
| 41 base::Bind(&FileThreadDeserializer::ReadFileAndReport, | 43 base::Bind(&FileThreadDeserializer::ReadFileAndReport, |
| 42 this, path)); | 44 this, path)); |
| 43 } | 45 } |
| 44 | 46 |
| 45 // Deserializes JSON on the file thread. | 47 // Deserializes JSON on the file thread. |
|
akalin
2012/10/10 01:43:16
update comment
zel
2012/10/10 23:25:00
Done.
| |
| 46 void ReadFileAndReport(const FilePath& path) { | 48 void ReadFileAndReport(const FilePath& path) { |
| 47 DCHECK(file_loop_proxy_->BelongsToCurrentThread()); | 49 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 48 | 50 |
| 49 value_.reset(DoReading(path, &error_, &no_dir_)); | 51 value_.reset(DoReading(path, &error_, &no_dir_)); |
| 50 | 52 |
| 51 origin_loop_proxy_->PostTask( | 53 origin_loop_proxy_->PostTask( |
| 52 FROM_HERE, | 54 FROM_HERE, |
| 53 base::Bind(&FileThreadDeserializer::ReportOnOriginThread, this)); | 55 base::Bind(&FileThreadDeserializer::ReportOnOriginThread, this)); |
| 54 } | 56 } |
| 55 | 57 |
| 56 // Reports deserialization result on the origin thread. | 58 // Reports deserialization result on the origin thread. |
| 57 void ReportOnOriginThread() { | 59 void ReportOnOriginThread() { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 78 PersistentPrefStore::PrefReadError* error); | 80 PersistentPrefStore::PrefReadError* error); |
| 79 | 81 |
| 80 private: | 82 private: |
| 81 friend class base::RefCountedThreadSafe<FileThreadDeserializer>; | 83 friend class base::RefCountedThreadSafe<FileThreadDeserializer>; |
| 82 ~FileThreadDeserializer() {} | 84 ~FileThreadDeserializer() {} |
| 83 | 85 |
| 84 bool no_dir_; | 86 bool no_dir_; |
| 85 PersistentPrefStore::PrefReadError error_; | 87 PersistentPrefStore::PrefReadError error_; |
| 86 scoped_ptr<Value> value_; | 88 scoped_ptr<Value> value_; |
| 87 scoped_refptr<JsonPrefStore> delegate_; | 89 scoped_refptr<JsonPrefStore> delegate_; |
| 88 scoped_refptr<base::MessageLoopProxy> file_loop_proxy_; | 90 scoped_refptr<base::TaskRunner> task_runner_; |
|
akalin
2012/10/10 01:43:16
probably should be named file_task_runner_ or file
zel
2012/10/10 23:25:00
Done - sequenced_task_runner_
| |
| 89 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_; | 91 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_; |
| 90 }; | 92 }; |
| 91 | 93 |
| 92 // static | 94 // static |
| 93 void FileThreadDeserializer::HandleErrors( | 95 void FileThreadDeserializer::HandleErrors( |
| 94 const Value* value, | 96 const Value* value, |
| 95 const FilePath& path, | 97 const FilePath& path, |
| 96 int error_code, | 98 int error_code, |
| 97 const std::string& error_msg, | 99 const std::string& error_msg, |
| 98 PersistentPrefStore::PrefReadError* error) { | 100 PersistentPrefStore::PrefReadError* error) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 file_util::Move(path, bad); | 132 file_util::Move(path, bad); |
| 131 break; | 133 break; |
| 132 } | 134 } |
| 133 } else if (!value->IsType(Value::TYPE_DICTIONARY)) { | 135 } else if (!value->IsType(Value::TYPE_DICTIONARY)) { |
| 134 *error = PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; | 136 *error = PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; |
| 135 } | 137 } |
| 136 } | 138 } |
| 137 | 139 |
| 138 } // namespace | 140 } // namespace |
| 139 | 141 |
| 142 JsonPrefStore* JsonPrefStore::Create( | |
| 143 const FilePath& filename, base::SequencedWorkerPool* worker_pool) { | |
| 144 // Make sure that write requests for the same file don't step on each other | |
| 145 // by using filename-keyed sequenced task runner. | |
| 146 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
| 147 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 148 worker_pool->GetNamedSequenceToken(filename.value()), | |
| 149 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | |
| 150 return new JsonPrefStore(filename, task_runner.get()); | |
| 151 } | |
| 152 | |
| 140 JsonPrefStore::JsonPrefStore(const FilePath& filename, | 153 JsonPrefStore::JsonPrefStore(const FilePath& filename, |
| 141 base::MessageLoopProxy* file_message_loop_proxy) | 154 base::SequencedTaskRunner* task_runner) |
| 142 : path_(filename), | 155 : path_(filename), |
| 143 file_message_loop_proxy_(file_message_loop_proxy), | 156 task_runner_(task_runner), |
|
akalin
2012/10/10 01:43:16
file_task_runner_/file_sequenced_task_runner_?
zel
2012/10/10 23:25:00
sequenced_task_runner_. Done.
| |
| 144 prefs_(new DictionaryValue()), | 157 prefs_(new DictionaryValue()), |
| 145 read_only_(false), | 158 read_only_(false), |
| 146 writer_(filename, file_message_loop_proxy), | 159 writer_(filename, task_runner), |
| 147 error_delegate_(NULL), | 160 error_delegate_(NULL), |
| 148 initialized_(false), | 161 initialized_(false), |
| 149 read_error_(PREF_READ_ERROR_OTHER) { | 162 read_error_(PREF_READ_ERROR_OTHER) { |
| 150 } | 163 } |
| 151 | 164 |
| 152 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, | 165 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, |
| 153 const Value** result) const { | 166 const Value** result) const { |
| 154 Value* tmp = NULL; | 167 Value* tmp = NULL; |
| 155 if (prefs_->Get(key, &tmp)) { | 168 if (prefs_->Get(key, &tmp)) { |
| 156 if (result) | 169 if (result) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 initialized_ = false; | 251 initialized_ = false; |
| 239 error_delegate_.reset(error_delegate); | 252 error_delegate_.reset(error_delegate); |
| 240 if (path_.empty()) { | 253 if (path_.empty()) { |
| 241 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | 254 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); |
| 242 return; | 255 return; |
| 243 } | 256 } |
| 244 | 257 |
| 245 // Start async reading of the preferences file. It will delete itself | 258 // Start async reading of the preferences file. It will delete itself |
| 246 // in the end. | 259 // in the end. |
| 247 scoped_refptr<FileThreadDeserializer> deserializer( | 260 scoped_refptr<FileThreadDeserializer> deserializer( |
| 248 new FileThreadDeserializer(this, file_message_loop_proxy_.get())); | 261 new FileThreadDeserializer(this, task_runner_.get())); |
| 249 deserializer->Start(path_); | 262 deserializer->Start(path_); |
| 250 } | 263 } |
| 251 | 264 |
| 252 void JsonPrefStore::CommitPendingWrite() { | 265 void JsonPrefStore::CommitPendingWrite() { |
| 253 if (writer_.HasPendingWrite() && !read_only_) | 266 if (writer_.HasPendingWrite() && !read_only_) |
| 254 writer_.DoScheduledWrite(); | 267 writer_.DoScheduledWrite(); |
| 255 } | 268 } |
| 256 | 269 |
| 257 void JsonPrefStore::ReportValueChanged(const std::string& key) { | 270 void JsonPrefStore::ReportValueChanged(const std::string& key) { |
| 258 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 271 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 copy->Set(key, new base::ListValue); | 348 copy->Set(key, new base::ListValue); |
| 336 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { | 349 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 337 const base::DictionaryValue* dict = NULL; | 350 const base::DictionaryValue* dict = NULL; |
| 338 if (value->GetAsDictionary(&dict) && dict->empty()) | 351 if (value->GetAsDictionary(&dict) && dict->empty()) |
| 339 copy->Set(key, new base::DictionaryValue); | 352 copy->Set(key, new base::DictionaryValue); |
| 340 } | 353 } |
| 341 } | 354 } |
| 342 | 355 |
| 343 return serializer.Serialize(*(copy.get())); | 356 return serializer.Serialize(*(copy.get())); |
| 344 } | 357 } |
| OLD | NEW |