Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(702)

Side by Side Diff: chrome/common/json_pref_store.cc

Issue 11027070: Moved JsonPrefStore to use SequencedWorkerPool (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
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
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
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, base::SequencedWorkerPool* worker_pool) {
Mattias Nissler (ping if slow) 2012/10/22 17:28:21 parameters on separate lines.
zel 2012/10/24 02:20:11 Done.
144 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
145 worker_pool->GetNamedSequenceToken(filename.AsUTF8Unsafe()),
Mattias Nissler (ping if slow) 2012/10/22 17:28:21 indentation
Mattias Nissler (ping if slow) 2012/10/22 17:28:21 The token name should probably be json_pref_store-
zel 2012/10/24 02:20:11 Done.
zel 2012/10/24 02:20:11 Done.
146 base::SequencedWorkerPool::BLOCK_SHUTDOWN);
147 }
148
149 JsonPrefStore* JsonPrefStore::Create(
150 const FilePath& filename, base::SequencedTaskRunner* task_runner) {
Mattias Nissler (ping if slow) 2012/10/22 17:28:21 ditto. Also, what is this factory function good f
zel 2012/10/24 02:20:11 long time ago it used to do waaay more but then CR
151 return new JsonPrefStore(filename, task_runner);
152 }
153
140 JsonPrefStore::JsonPrefStore(const FilePath& filename, 154 JsonPrefStore::JsonPrefStore(const FilePath& filename,
141 base::MessageLoopProxy* file_message_loop_proxy) 155 base::SequencedTaskRunner* sequenced_task_runner)
142 : path_(filename), 156 : path_(filename),
143 file_message_loop_proxy_(file_message_loop_proxy), 157 sequenced_task_runner_(sequenced_task_runner),
144 prefs_(new DictionaryValue()), 158 prefs_(new DictionaryValue()),
145 read_only_(false), 159 read_only_(false),
146 writer_(filename, file_message_loop_proxy), 160 writer_(filename, sequenced_task_runner),
147 error_delegate_(NULL), 161 error_delegate_(NULL),
148 initialized_(false), 162 initialized_(false),
149 read_error_(PREF_READ_ERROR_OTHER) { 163 read_error_(PREF_READ_ERROR_OTHER) {
150 } 164 }
151 165
152 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, 166 PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key,
153 const Value** result) const { 167 const Value** result) const {
154 Value* tmp = NULL; 168 Value* tmp = NULL;
155 if (prefs_->Get(key, &tmp)) { 169 if (prefs_->Get(key, &tmp)) {
156 if (result) 170 if (result)
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 initialized_ = false; 252 initialized_ = false;
239 error_delegate_.reset(error_delegate); 253 error_delegate_.reset(error_delegate);
240 if (path_.empty()) { 254 if (path_.empty()) {
241 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); 255 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false);
242 return; 256 return;
243 } 257 }
244 258
245 // Start async reading of the preferences file. It will delete itself 259 // Start async reading of the preferences file. It will delete itself
246 // in the end. 260 // in the end.
247 scoped_refptr<FileThreadDeserializer> deserializer( 261 scoped_refptr<FileThreadDeserializer> deserializer(
248 new FileThreadDeserializer(this, file_message_loop_proxy_.get())); 262 new FileThreadDeserializer(this, sequenced_task_runner_.get()));
249 deserializer->Start(path_); 263 deserializer->Start(path_);
250 } 264 }
251 265
252 void JsonPrefStore::CommitPendingWrite() { 266 void JsonPrefStore::CommitPendingWrite() {
253 if (writer_.HasPendingWrite() && !read_only_) 267 if (writer_.HasPendingWrite() && !read_only_)
254 writer_.DoScheduledWrite(); 268 writer_.DoScheduledWrite();
255 } 269 }
256 270
257 void JsonPrefStore::ReportValueChanged(const std::string& key) { 271 void JsonPrefStore::ReportValueChanged(const std::string& key) {
258 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); 272 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 copy->Set(key, new base::ListValue); 349 copy->Set(key, new base::ListValue);
336 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { 350 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) {
337 const base::DictionaryValue* dict = NULL; 351 const base::DictionaryValue* dict = NULL;
338 if (value->GetAsDictionary(&dict) && dict->empty()) 352 if (value->GetAsDictionary(&dict) && dict->empty())
339 copy->Set(key, new base::DictionaryValue); 353 copy->Set(key, new base::DictionaryValue);
340 } 354 }
341 } 355 }
342 356
343 return serializer.Serialize(*(copy.get())); 357 return serializer.Serialize(*(copy.get()));
344 } 358 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698