| 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 "base/prefs/json_pref_store.h" | 5 #include "base/prefs/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" |
| 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::SequencedTaskRunner* sequenced_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 sequenced_task_runner_(sequenced_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 sequenced_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 sequenced task runner. |
| 46 void ReadFileAndReport(const FilePath& path) { | 48 void ReadFileAndReport(const FilePath& path) { |
| 47 DCHECK(file_loop_proxy_->BelongsToCurrentThread()); | 49 DCHECK(sequenced_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 19 matching lines...) Expand all Loading... |
| 77 const std::string& error_msg, | 79 const std::string& error_msg, |
| 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 const scoped_refptr<JsonPrefStore> delegate_; |
| 88 scoped_refptr<base::MessageLoopProxy> file_loop_proxy_; | 90 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
| 89 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_; | 91 const 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) { |
| 99 *error = PersistentPrefStore::PREF_READ_ERROR_NONE; | 101 *error = PersistentPrefStore::PREF_READ_ERROR_NONE; |
| (...skipping 30 matching lines...) Expand all 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 scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( |
| 143 const FilePath& filename, |
| 144 base::SequencedWorkerPool* worker_pool) { |
| 145 std::string token("json_pref_store-"); |
| 146 token.append(filename.AsUTF8Unsafe()); |
| 147 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 148 worker_pool->GetNamedSequenceToken(token), |
| 149 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 150 } |
| 151 |
| 140 JsonPrefStore::JsonPrefStore(const FilePath& filename, | 152 JsonPrefStore::JsonPrefStore(const FilePath& filename, |
| 141 base::MessageLoopProxy* file_message_loop_proxy) | 153 base::SequencedTaskRunner* sequenced_task_runner) |
| 142 : path_(filename), | 154 : path_(filename), |
| 143 file_message_loop_proxy_(file_message_loop_proxy), | 155 sequenced_task_runner_(sequenced_task_runner), |
| 144 prefs_(new DictionaryValue()), | 156 prefs_(new DictionaryValue()), |
| 145 read_only_(false), | 157 read_only_(false), |
| 146 writer_(filename, file_message_loop_proxy), | 158 writer_(filename, sequenced_task_runner), |
| 147 error_delegate_(NULL), | 159 error_delegate_(NULL), |
| 148 initialized_(false), | 160 initialized_(false), |
| 149 read_error_(PREF_READ_ERROR_OTHER) { | 161 read_error_(PREF_READ_ERROR_OTHER) { |
| 150 } | 162 } |
| 151 | 163 |
| 152 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, | 164 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, |
| 153 const Value** result) const { | 165 const Value** result) const { |
| 154 Value* tmp = NULL; | 166 Value* tmp = NULL; |
| 155 if (prefs_->Get(key, &tmp)) { | 167 if (prefs_->Get(key, &tmp)) { |
| 156 if (result) | 168 if (result) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 initialized_ = false; | 250 initialized_ = false; |
| 239 error_delegate_.reset(error_delegate); | 251 error_delegate_.reset(error_delegate); |
| 240 if (path_.empty()) { | 252 if (path_.empty()) { |
| 241 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | 253 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); |
| 242 return; | 254 return; |
| 243 } | 255 } |
| 244 | 256 |
| 245 // Start async reading of the preferences file. It will delete itself | 257 // Start async reading of the preferences file. It will delete itself |
| 246 // in the end. | 258 // in the end. |
| 247 scoped_refptr<FileThreadDeserializer> deserializer( | 259 scoped_refptr<FileThreadDeserializer> deserializer( |
| 248 new FileThreadDeserializer(this, file_message_loop_proxy_.get())); | 260 new FileThreadDeserializer(this, sequenced_task_runner_.get())); |
| 249 deserializer->Start(path_); | 261 deserializer->Start(path_); |
| 250 } | 262 } |
| 251 | 263 |
| 252 void JsonPrefStore::CommitPendingWrite() { | 264 void JsonPrefStore::CommitPendingWrite() { |
| 253 if (writer_.HasPendingWrite() && !read_only_) | 265 if (writer_.HasPendingWrite() && !read_only_) |
| 254 writer_.DoScheduledWrite(); | 266 writer_.DoScheduledWrite(); |
| 255 } | 267 } |
| 256 | 268 |
| 257 void JsonPrefStore::ReportValueChanged(const std::string& key) { | 269 void JsonPrefStore::ReportValueChanged(const std::string& key) { |
| 258 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 270 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); | 347 copy->Set(key, new base::ListValue); |
| 336 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { | 348 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 337 const base::DictionaryValue* dict = NULL; | 349 const base::DictionaryValue* dict = NULL; |
| 338 if (value->GetAsDictionary(&dict) && dict->empty()) | 350 if (value->GetAsDictionary(&dict) && dict->empty()) |
| 339 copy->Set(key, new base::DictionaryValue); | 351 copy->Set(key, new base::DictionaryValue); |
| 340 } | 352 } |
| 341 } | 353 } |
| 342 | 354 |
| 343 return serializer.Serialize(*(copy.get())); | 355 return serializer.Serialize(*(copy.get())); |
| 344 } | 356 } |
| OLD | NEW |