| 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 scoped_ptr<PrefFilter> pref_filter) | 130 scoped_ptr<PrefFilter> pref_filter) |
| 131 : path_(filename), | 131 : path_(filename), |
| 132 sequenced_task_runner_(sequenced_task_runner), | 132 sequenced_task_runner_(sequenced_task_runner), |
| 133 prefs_(new base::DictionaryValue()), | 133 prefs_(new base::DictionaryValue()), |
| 134 read_only_(false), | 134 read_only_(false), |
| 135 writer_(filename, sequenced_task_runner), | 135 writer_(filename, sequenced_task_runner), |
| 136 pref_filter_(pref_filter.Pass()), | 136 pref_filter_(pref_filter.Pass()), |
| 137 initialized_(false), | 137 initialized_(false), |
| 138 filtering_in_progress_(false), | 138 filtering_in_progress_(false), |
| 139 read_error_(PREF_READ_ERROR_NONE) { | 139 read_error_(PREF_READ_ERROR_NONE) { |
| 140 DCHECK(!path_.empty()); |
| 140 } | 141 } |
| 141 | 142 |
| 142 JsonPrefStore::JsonPrefStore( | 143 JsonPrefStore::JsonPrefStore( |
| 143 const base::FilePath& filename, | 144 const base::FilePath& filename, |
| 144 const base::FilePath& alternate_filename, | 145 const base::FilePath& alternate_filename, |
| 145 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 146 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
| 146 scoped_ptr<PrefFilter> pref_filter) | 147 scoped_ptr<PrefFilter> pref_filter) |
| 147 : path_(filename), | 148 : path_(filename), |
| 148 alternate_path_(alternate_filename), | 149 alternate_path_(alternate_filename), |
| 149 sequenced_task_runner_(sequenced_task_runner), | 150 sequenced_task_runner_(sequenced_task_runner), |
| 150 prefs_(new base::DictionaryValue()), | 151 prefs_(new base::DictionaryValue()), |
| 151 read_only_(false), | 152 read_only_(false), |
| 152 writer_(filename, sequenced_task_runner), | 153 writer_(filename, sequenced_task_runner), |
| 153 pref_filter_(pref_filter.Pass()), | 154 pref_filter_(pref_filter.Pass()), |
| 154 initialized_(false), | 155 initialized_(false), |
| 155 filtering_in_progress_(false), | 156 filtering_in_progress_(false), |
| 156 read_error_(PREF_READ_ERROR_NONE) { | 157 read_error_(PREF_READ_ERROR_NONE) { |
| 158 DCHECK(!path_.empty()); |
| 157 } | 159 } |
| 158 | 160 |
| 159 bool JsonPrefStore::GetValue(const std::string& key, | 161 bool JsonPrefStore::GetValue(const std::string& key, |
| 160 const base::Value** result) const { | 162 const base::Value** result) const { |
| 161 DCHECK(CalledOnValidThread()); | 163 DCHECK(CalledOnValidThread()); |
| 162 | 164 |
| 163 base::Value* tmp = NULL; | 165 base::Value* tmp = NULL; |
| 164 if (!prefs_->Get(key, &tmp)) | 166 if (!prefs_->Get(key, &tmp)) |
| 165 return false; | 167 return false; |
| 166 | 168 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 | 253 |
| 252 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { | 254 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { |
| 253 DCHECK(CalledOnValidThread()); | 255 DCHECK(CalledOnValidThread()); |
| 254 | 256 |
| 255 return read_error_; | 257 return read_error_; |
| 256 } | 258 } |
| 257 | 259 |
| 258 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { | 260 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { |
| 259 DCHECK(CalledOnValidThread()); | 261 DCHECK(CalledOnValidThread()); |
| 260 | 262 |
| 261 if (path_.empty()) { | |
| 262 scoped_ptr<ReadResult> no_file_result; | |
| 263 no_file_result->error = PREF_READ_ERROR_FILE_NOT_SPECIFIED; | |
| 264 OnFileRead(no_file_result.Pass()); | |
| 265 return PREF_READ_ERROR_FILE_NOT_SPECIFIED; | |
| 266 } | |
| 267 | |
| 268 OnFileRead(ReadPrefsFromDisk(path_, alternate_path_)); | 263 OnFileRead(ReadPrefsFromDisk(path_, alternate_path_)); |
| 269 return filtering_in_progress_ ? PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE | 264 return filtering_in_progress_ ? PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE |
| 270 : read_error_; | 265 : read_error_; |
| 271 } | 266 } |
| 272 | 267 |
| 273 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { | 268 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { |
| 274 DCHECK(CalledOnValidThread()); | 269 DCHECK(CalledOnValidThread()); |
| 275 | 270 |
| 276 initialized_ = false; | 271 initialized_ = false; |
| 277 error_delegate_.reset(error_delegate); | 272 error_delegate_.reset(error_delegate); |
| 278 if (path_.empty()) { | |
| 279 scoped_ptr<ReadResult> no_file_result; | |
| 280 no_file_result->error = PREF_READ_ERROR_FILE_NOT_SPECIFIED; | |
| 281 OnFileRead(no_file_result.Pass()); | |
| 282 return; | |
| 283 } | |
| 284 | 273 |
| 285 // Weakly binds the read task so that it doesn't kick in during shutdown. | 274 // Weakly binds the read task so that it doesn't kick in during shutdown. |
| 286 base::PostTaskAndReplyWithResult( | 275 base::PostTaskAndReplyWithResult( |
| 287 sequenced_task_runner_.get(), | 276 sequenced_task_runner_.get(), |
| 288 FROM_HERE, | 277 FROM_HERE, |
| 289 base::Bind(&ReadPrefsFromDisk, path_, alternate_path_), | 278 base::Bind(&ReadPrefsFromDisk, path_, alternate_path_), |
| 290 base::Bind(&JsonPrefStore::OnFileRead, AsWeakPtr())); | 279 base::Bind(&JsonPrefStore::OnFileRead, AsWeakPtr())); |
| 291 } | 280 } |
| 292 | 281 |
| 293 void JsonPrefStore::CommitPendingWrite() { | 282 void JsonPrefStore::CommitPendingWrite() { |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 | 425 |
| 437 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 426 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
| 438 error_delegate_->OnError(read_error_); | 427 error_delegate_->OnError(read_error_); |
| 439 | 428 |
| 440 FOR_EACH_OBSERVER(PrefStore::Observer, | 429 FOR_EACH_OBSERVER(PrefStore::Observer, |
| 441 observers_, | 430 observers_, |
| 442 OnInitializationCompleted(true)); | 431 OnInitializationCompleted(true)); |
| 443 | 432 |
| 444 return; | 433 return; |
| 445 } | 434 } |
| OLD | NEW |