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" |
(...skipping 19 matching lines...) Expand all Loading... |
30 public: | 30 public: |
31 FileThreadDeserializer(JsonPrefStore* delegate, | 31 FileThreadDeserializer(JsonPrefStore* delegate, |
32 base::SequencedTaskRunner* sequenced_task_runner) | 32 base::SequencedTaskRunner* sequenced_task_runner) |
33 : no_dir_(false), | 33 : no_dir_(false), |
34 error_(PersistentPrefStore::PREF_READ_ERROR_NONE), | 34 error_(PersistentPrefStore::PREF_READ_ERROR_NONE), |
35 delegate_(delegate), | 35 delegate_(delegate), |
36 sequenced_task_runner_(sequenced_task_runner), | 36 sequenced_task_runner_(sequenced_task_runner), |
37 origin_loop_proxy_(base::MessageLoopProxy::current()) { | 37 origin_loop_proxy_(base::MessageLoopProxy::current()) { |
38 } | 38 } |
39 | 39 |
40 void Start(const base::FilePath& path) { | 40 void Start(const base::FilePath& path, |
| 41 const base::FilePath& alternate_path) { |
41 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); | 42 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); |
42 // TODO(gab): This should use PostTaskAndReplyWithResult instead of using | 43 // TODO(gab): This should use PostTaskAndReplyWithResult instead of using |
43 // the |error_| member to pass data across tasks. | 44 // the |error_| member to pass data across tasks. |
44 sequenced_task_runner_->PostTask( | 45 sequenced_task_runner_->PostTask( |
45 FROM_HERE, | 46 FROM_HERE, |
46 base::Bind(&FileThreadDeserializer::ReadFileAndReport, | 47 base::Bind(&FileThreadDeserializer::ReadFileAndReport, |
47 this, path)); | 48 this, path, alternate_path)); |
48 } | 49 } |
49 | 50 |
50 // Deserializes JSON on the sequenced task runner. | 51 // Deserializes JSON on the sequenced task runner. |
51 void ReadFileAndReport(const base::FilePath& path) { | 52 void ReadFileAndReport(const base::FilePath& path, |
| 53 const base::FilePath& alternate_path) { |
52 DCHECK(sequenced_task_runner_->RunsTasksOnCurrentThread()); | 54 DCHECK(sequenced_task_runner_->RunsTasksOnCurrentThread()); |
53 | 55 |
54 value_.reset(DoReading(path, &error_, &no_dir_)); | 56 value_.reset(DoReading(path, alternate_path, &error_, &no_dir_)); |
55 | 57 |
56 origin_loop_proxy_->PostTask( | 58 origin_loop_proxy_->PostTask( |
57 FROM_HERE, | 59 FROM_HERE, |
58 base::Bind(&FileThreadDeserializer::ReportOnOriginThread, this)); | 60 base::Bind(&FileThreadDeserializer::ReportOnOriginThread, this)); |
59 } | 61 } |
60 | 62 |
61 // Reports deserialization result on the origin thread. | 63 // Reports deserialization result on the origin thread. |
62 void ReportOnOriginThread() { | 64 void ReportOnOriginThread() { |
63 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); | 65 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); |
64 delegate_->OnFileRead(value_.Pass(), error_, no_dir_); | 66 delegate_->OnFileRead(value_.Pass(), error_, no_dir_); |
65 } | 67 } |
66 | 68 |
67 static base::Value* DoReading(const base::FilePath& path, | 69 static base::Value* DoReading(const base::FilePath& path, |
| 70 const base::FilePath& alternate_path, |
68 PersistentPrefStore::PrefReadError* error, | 71 PersistentPrefStore::PrefReadError* error, |
69 bool* no_dir) { | 72 bool* no_dir) { |
| 73 if (!base::PathExists(path) && !alternate_path.empty() && |
| 74 base::PathExists(alternate_path)) { |
| 75 base::Move(alternate_path, path); |
| 76 } |
| 77 |
70 int error_code; | 78 int error_code; |
71 std::string error_msg; | 79 std::string error_msg; |
72 JSONFileValueSerializer serializer(path); | 80 JSONFileValueSerializer serializer(path); |
73 base::Value* value = serializer.Deserialize(&error_code, &error_msg); | 81 base::Value* value = serializer.Deserialize(&error_code, &error_msg); |
74 HandleErrors(value, path, error_code, error_msg, error); | 82 HandleErrors(value, path, error_code, error_msg, error); |
75 *no_dir = !base::PathExists(path.DirName()); | 83 *no_dir = !base::PathExists(path.DirName()); |
76 return value; | 84 return value; |
77 } | 85 } |
78 | 86 |
79 static void HandleErrors(const base::Value* value, | 87 static void HandleErrors(const base::Value* value, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 sequenced_task_runner_(sequenced_task_runner), | 168 sequenced_task_runner_(sequenced_task_runner), |
161 prefs_(new base::DictionaryValue()), | 169 prefs_(new base::DictionaryValue()), |
162 read_only_(false), | 170 read_only_(false), |
163 writer_(filename, sequenced_task_runner), | 171 writer_(filename, sequenced_task_runner), |
164 pref_filter_(pref_filter.Pass()), | 172 pref_filter_(pref_filter.Pass()), |
165 initialized_(false), | 173 initialized_(false), |
166 filtering_in_progress_(false), | 174 filtering_in_progress_(false), |
167 read_error_(PREF_READ_ERROR_NONE) { | 175 read_error_(PREF_READ_ERROR_NONE) { |
168 } | 176 } |
169 | 177 |
| 178 JsonPrefStore::JsonPrefStore(const base::FilePath& filename, |
| 179 const base::FilePath& alternate_filename, |
| 180 base::SequencedTaskRunner* sequenced_task_runner, |
| 181 scoped_ptr<PrefFilter> pref_filter) |
| 182 : path_(filename), |
| 183 alternate_path_(alternate_filename), |
| 184 sequenced_task_runner_(sequenced_task_runner), |
| 185 prefs_(new base::DictionaryValue()), |
| 186 read_only_(false), |
| 187 writer_(filename, sequenced_task_runner), |
| 188 pref_filter_(pref_filter.Pass()), |
| 189 initialized_(false), |
| 190 filtering_in_progress_(false), |
| 191 read_error_(PREF_READ_ERROR_NONE) { |
| 192 } |
| 193 |
170 bool JsonPrefStore::GetValue(const std::string& key, | 194 bool JsonPrefStore::GetValue(const std::string& key, |
171 const base::Value** result) const { | 195 const base::Value** result) const { |
172 base::Value* tmp = NULL; | 196 base::Value* tmp = NULL; |
173 if (!prefs_->Get(key, &tmp)) | 197 if (!prefs_->Get(key, &tmp)) |
174 return false; | 198 return false; |
175 | 199 |
176 if (result) | 200 if (result) |
177 *result = tmp; | 201 *result = tmp; |
178 return true; | 202 return true; |
179 } | 203 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { | 269 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { |
246 if (path_.empty()) { | 270 if (path_.empty()) { |
247 OnFileRead( | 271 OnFileRead( |
248 scoped_ptr<base::Value>(), PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | 272 scoped_ptr<base::Value>(), PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); |
249 return PREF_READ_ERROR_FILE_NOT_SPECIFIED; | 273 return PREF_READ_ERROR_FILE_NOT_SPECIFIED; |
250 } | 274 } |
251 | 275 |
252 PrefReadError error; | 276 PrefReadError error; |
253 bool no_dir; | 277 bool no_dir; |
254 scoped_ptr<base::Value> value( | 278 scoped_ptr<base::Value> value( |
255 FileThreadDeserializer::DoReading(path_, &error, &no_dir)); | 279 FileThreadDeserializer::DoReading(path_, alternate_path_, &error, |
| 280 &no_dir)); |
256 OnFileRead(value.Pass(), error, no_dir); | 281 OnFileRead(value.Pass(), error, no_dir); |
257 return filtering_in_progress_ ? PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE : | 282 return filtering_in_progress_ ? PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE : |
258 error; | 283 error; |
259 } | 284 } |
260 | 285 |
261 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { | 286 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { |
262 initialized_ = false; | 287 initialized_ = false; |
263 error_delegate_.reset(error_delegate); | 288 error_delegate_.reset(error_delegate); |
264 if (path_.empty()) { | 289 if (path_.empty()) { |
265 OnFileRead( | 290 OnFileRead( |
266 scoped_ptr<base::Value>(), PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); | 291 scoped_ptr<base::Value>(), PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); |
267 return; | 292 return; |
268 } | 293 } |
269 | 294 |
270 // Start async reading of the preferences file. It will delete itself | 295 // Start async reading of the preferences file. It will delete itself |
271 // in the end. | 296 // in the end. |
272 scoped_refptr<FileThreadDeserializer> deserializer( | 297 scoped_refptr<FileThreadDeserializer> deserializer( |
273 new FileThreadDeserializer(this, sequenced_task_runner_.get())); | 298 new FileThreadDeserializer(this, sequenced_task_runner_.get())); |
274 deserializer->Start(path_); | 299 deserializer->Start(path_, alternate_path_); |
275 } | 300 } |
276 | 301 |
277 void JsonPrefStore::CommitPendingWrite() { | 302 void JsonPrefStore::CommitPendingWrite() { |
278 if (writer_.HasPendingWrite() && !read_only_) | 303 if (writer_.HasPendingWrite() && !read_only_) |
279 writer_.DoScheduledWrite(); | 304 writer_.DoScheduledWrite(); |
280 } | 305 } |
281 | 306 |
282 void JsonPrefStore::ReportValueChanged(const std::string& key) { | 307 void JsonPrefStore::ReportValueChanged(const std::string& key) { |
283 if (pref_filter_) | 308 if (pref_filter_) |
284 pref_filter_->FilterUpdate(key); | 309 pref_filter_->FilterUpdate(key); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 | 412 |
388 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 413 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
389 error_delegate_->OnError(read_error_); | 414 error_delegate_->OnError(read_error_); |
390 | 415 |
391 FOR_EACH_OBSERVER(PrefStore::Observer, | 416 FOR_EACH_OBSERVER(PrefStore::Observer, |
392 observers_, | 417 observers_, |
393 OnInitializationCompleted(true)); | 418 OnInitializationCompleted(true)); |
394 | 419 |
395 return; | 420 return; |
396 } | 421 } |
OLD | NEW |