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

Side by Side Diff: base/prefs/json_pref_store.cc

Issue 11027070: Moved JsonPrefStore to use SequencedWorkerPool (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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 "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
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;
100 if (!value) { 102 if (!value) {
101 DVLOG(1) << "Error while loading JSON file: " << error_msg; 103 DVLOG(1) << "Error while loading JSON file: " << error_msg
104 << ", file: " << path.value();
102 switch (error_code) { 105 switch (error_code) {
103 case JSONFileValueSerializer::JSON_ACCESS_DENIED: 106 case JSONFileValueSerializer::JSON_ACCESS_DENIED:
104 *error = PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED; 107 *error = PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED;
105 break; 108 break;
106 case JSONFileValueSerializer::JSON_CANNOT_READ_FILE: 109 case JSONFileValueSerializer::JSON_CANNOT_READ_FILE:
107 *error = PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER; 110 *error = PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER;
108 break; 111 break;
109 case JSONFileValueSerializer::JSON_FILE_LOCKED: 112 case JSONFileValueSerializer::JSON_FILE_LOCKED:
110 *error = PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED; 113 *error = PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED;
111 break; 114 break;
(...skipping 18 matching lines...) Expand all
130 file_util::Move(path, bad); 133 file_util::Move(path, bad);
131 break; 134 break;
132 } 135 }
133 } else if (!value->IsType(Value::TYPE_DICTIONARY)) { 136 } else if (!value->IsType(Value::TYPE_DICTIONARY)) {
134 *error = PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; 137 *error = PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE;
135 } 138 }
136 } 139 }
137 140
138 } // namespace 141 } // namespace
139 142
143 scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile(
144 const FilePath& filename,
145 base::SequencedWorkerPool* worker_pool) {
146 std::string token("json_pref_store-");
147 token.append(filename.AsUTF8Unsafe());
148 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
149 worker_pool->GetNamedSequenceToken(token),
150 base::SequencedWorkerPool::BLOCK_SHUTDOWN);
151 }
152
140 JsonPrefStore::JsonPrefStore(const FilePath& filename, 153 JsonPrefStore::JsonPrefStore(const FilePath& filename,
141 base::MessageLoopProxy* file_message_loop_proxy) 154 base::SequencedTaskRunner* sequenced_task_runner)
142 : path_(filename), 155 : path_(filename),
143 file_message_loop_proxy_(file_message_loop_proxy), 156 sequenced_task_runner_(sequenced_task_runner),
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, sequenced_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
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, sequenced_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
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698