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" | |
| 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) |
| 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. |
| 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_; |
| 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 | |
|
eroman
2012/10/05 20:38:36
this sentence doesn't end.
zel
2012/10/05 20:54:28
Done.
| |
| 146 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
| 147 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 148 worker_pool->GetNamedSequenceToken(filename.value()), | |
| 149 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | |
| 150 return JsonPrefStore::Create(filename, task_runner.get()); | |
| 151 } | |
| 152 | |
| 153 JsonPrefStore* JsonPrefStore::Create(const FilePath& filename, | |
| 154 base::TaskRunner* task_runner) { | |
| 155 return new JsonPrefStore(filename, task_runner); | |
| 156 } | |
| 157 | |
| 140 JsonPrefStore::JsonPrefStore(const FilePath& filename, | 158 JsonPrefStore::JsonPrefStore(const FilePath& filename, |
| 141 base::MessageLoopProxy* file_message_loop_proxy) | 159 base::TaskRunner* task_runner) |
| 142 : path_(filename), | 160 : path_(filename), |
| 143 file_message_loop_proxy_(file_message_loop_proxy), | 161 task_runner_(task_runner), |
| 144 prefs_(new DictionaryValue()), | 162 prefs_(new DictionaryValue()), |
| 145 read_only_(false), | 163 read_only_(false), |
| 146 writer_(filename, file_message_loop_proxy), | 164 writer_(filename, task_runner), |
| 147 error_delegate_(NULL), | 165 error_delegate_(NULL), |
| 148 initialized_(false), | 166 initialized_(false), |
| 149 read_error_(PREF_READ_ERROR_OTHER) { | 167 read_error_(PREF_READ_ERROR_OTHER) { |
| 150 } | 168 } |
| 151 | 169 |
| 170 | |
|
eroman
2012/10/05 20:38:36
is the extra space intentional?
zel
2012/10/05 20:54:28
Done.
| |
| 152 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, | 171 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, |
| 153 const Value** result) const { | 172 const Value** result) const { |
| 154 Value* tmp = NULL; | 173 Value* tmp = NULL; |
| 155 if (prefs_->Get(key, &tmp)) { | 174 if (prefs_->Get(key, &tmp)) { |
| 156 if (result) | 175 if (result) |
| 157 *result = tmp; | 176 *result = tmp; |
| 158 return READ_OK; | 177 return READ_OK; |
| 159 } | 178 } |
| 160 return READ_NO_VALUE; | 179 return READ_NO_VALUE; |
| 161 } | 180 } |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 initialized_ = false; | 257 initialized_ = false; |
| 239 error_delegate_.reset(error_delegate); | 258 error_delegate_.reset(error_delegate); |
| 240 if (path_.empty()) { | 259 if (path_.empty()) { |
| 241 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | 260 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); |
| 242 return; | 261 return; |
| 243 } | 262 } |
| 244 | 263 |
| 245 // Start async reading of the preferences file. It will delete itself | 264 // Start async reading of the preferences file. It will delete itself |
| 246 // in the end. | 265 // in the end. |
| 247 scoped_refptr<FileThreadDeserializer> deserializer( | 266 scoped_refptr<FileThreadDeserializer> deserializer( |
| 248 new FileThreadDeserializer(this, file_message_loop_proxy_.get())); | 267 new FileThreadDeserializer(this, task_runner_.get())); |
| 249 deserializer->Start(path_); | 268 deserializer->Start(path_); |
| 250 } | 269 } |
| 251 | 270 |
| 252 void JsonPrefStore::CommitPendingWrite() { | 271 void JsonPrefStore::CommitPendingWrite() { |
| 253 if (writer_.HasPendingWrite() && !read_only_) | 272 if (writer_.HasPendingWrite() && !read_only_) |
| 254 writer_.DoScheduledWrite(); | 273 writer_.DoScheduledWrite(); |
| 255 } | 274 } |
| 256 | 275 |
| 257 void JsonPrefStore::ReportValueChanged(const std::string& key) { | 276 void JsonPrefStore::ReportValueChanged(const std::string& key) { |
| 258 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 277 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); | 354 copy->Set(key, new base::ListValue); |
| 336 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { | 355 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 337 const base::DictionaryValue* dict = NULL; | 356 const base::DictionaryValue* dict = NULL; |
| 338 if (value->GetAsDictionary(&dict) && dict->empty()) | 357 if (value->GetAsDictionary(&dict) && dict->empty()) |
| 339 copy->Set(key, new base::DictionaryValue); | 358 copy->Set(key, new base::DictionaryValue); |
| 340 } | 359 } |
| 341 } | 360 } |
| 342 | 361 |
| 343 return serializer.Serialize(*(copy.get())); | 362 return serializer.Serialize(*(copy.get())); |
| 344 } | 363 } |
| OLD | NEW |