| 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/command_line.h" |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/json/json_file_value_serializer.h" | 14 #include "base/json/json_file_value_serializer.h" |
| 14 #include "base/json/json_string_value_serializer.h" | 15 #include "base/json/json_string_value_serializer.h" |
| 15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 16 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/prefs/base_prefs_switches.h" |
| 17 #include "base/prefs/pref_filter.h" | 19 #include "base/prefs/pref_filter.h" |
| 18 #include "base/sequenced_task_runner.h" | 20 #include "base/sequenced_task_runner.h" |
| 19 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 20 #include "base/task_runner_util.h" | 22 #include "base/task_runner_util.h" |
| 21 #include "base/threading/sequenced_worker_pool.h" | 23 #include "base/threading/sequenced_worker_pool.h" |
| 22 #include "base/values.h" | 24 #include "base/values.h" |
| 23 | 25 |
| 24 // Result returned from internal read tasks. | 26 // Result returned from internal read tasks. |
| 25 struct JsonPrefStore::ReadResult { | 27 struct JsonPrefStore::ReadResult { |
| 26 public: | 28 public: |
| (...skipping 22 matching lines...) Expand all Loading... |
| 49 | 51 |
| 50 PersistentPrefStore::PrefReadError HandleReadErrors( | 52 PersistentPrefStore::PrefReadError HandleReadErrors( |
| 51 const base::Value* value, | 53 const base::Value* value, |
| 52 const base::FilePath& path, | 54 const base::FilePath& path, |
| 53 int error_code, | 55 int error_code, |
| 54 const std::string& error_msg) { | 56 const std::string& error_msg) { |
| 55 if (!value) { | 57 if (!value) { |
| 56 DVLOG(1) << "Error while loading JSON file: " << error_msg | 58 DVLOG(1) << "Error while loading JSON file: " << error_msg |
| 57 << ", file: " << path.value(); | 59 << ", file: " << path.value(); |
| 58 switch (error_code) { | 60 switch (error_code) { |
| 59 case JSONFileValueSerializer::JSON_ACCESS_DENIED: | 61 case JSONFileValueDeserializer::JSON_ACCESS_DENIED: |
| 60 return PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED; | 62 return PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED; |
| 61 break; | 63 break; |
| 62 case JSONFileValueSerializer::JSON_CANNOT_READ_FILE: | 64 case JSONFileValueDeserializer::JSON_CANNOT_READ_FILE: |
| 63 return PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER; | 65 return PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER; |
| 64 break; | 66 break; |
| 65 case JSONFileValueSerializer::JSON_FILE_LOCKED: | 67 case JSONFileValueDeserializer::JSON_FILE_LOCKED: |
| 66 return PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED; | 68 return PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED; |
| 67 break; | 69 break; |
| 68 case JSONFileValueSerializer::JSON_NO_SUCH_FILE: | 70 case JSONFileValueDeserializer::JSON_NO_SUCH_FILE: |
| 69 return PersistentPrefStore::PREF_READ_ERROR_NO_FILE; | 71 return PersistentPrefStore::PREF_READ_ERROR_NO_FILE; |
| 70 break; | 72 break; |
| 71 default: | 73 default: |
| 72 // JSON errors indicate file corruption of some sort. | 74 // JSON errors indicate file corruption of some sort. |
| 73 // Since the file is corrupt, move it to the side and continue with | 75 // Since the file is corrupt, move it to the side and continue with |
| 74 // empty preferences. This will result in them losing their settings. | 76 // empty preferences. This will result in them losing their settings. |
| 75 // We keep the old file for possible support and debugging assistance | 77 // We keep the old file for possible support and debugging assistance |
| 76 // as well as to detect if they're seeing these errors repeatedly. | 78 // as well as to detect if they're seeing these errors repeatedly. |
| 77 // TODO(erikkay) Instead, use the last known good file. | 79 // TODO(erikkay) Instead, use the last known good file. |
| 78 base::FilePath bad = path.ReplaceExtension(kBadExtension); | 80 base::FilePath bad = path.ReplaceExtension(kBadExtension); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 const base::FilePath& alternate_path) { | 114 const base::FilePath& alternate_path) { |
| 113 if (!base::PathExists(path) && !alternate_path.empty() && | 115 if (!base::PathExists(path) && !alternate_path.empty() && |
| 114 base::PathExists(alternate_path)) { | 116 base::PathExists(alternate_path)) { |
| 115 base::Move(alternate_path, path); | 117 base::Move(alternate_path, path); |
| 116 } | 118 } |
| 117 | 119 |
| 118 int error_code; | 120 int error_code; |
| 119 std::string error_msg; | 121 std::string error_msg; |
| 120 scoped_ptr<JsonPrefStore::ReadResult> read_result( | 122 scoped_ptr<JsonPrefStore::ReadResult> read_result( |
| 121 new JsonPrefStore::ReadResult); | 123 new JsonPrefStore::ReadResult); |
| 122 JSONFileValueSerializer serializer(path); | 124 JSONFileValueDeserializer deserializer(path); |
| 123 read_result->value.reset(serializer.Deserialize(&error_code, &error_msg)); | 125 read_result->value.reset(deserializer.Deserialize(&error_code, &error_msg)); |
| 124 read_result->error = | 126 read_result->error = |
| 125 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); | 127 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); |
| 126 read_result->no_dir = !base::PathExists(path.DirName()); | 128 read_result->no_dir = !base::PathExists(path.DirName()); |
| 127 | 129 |
| 128 if (read_result->error == PersistentPrefStore::PREF_READ_ERROR_NONE) | 130 if (read_result->error == PersistentPrefStore::PREF_READ_ERROR_NONE) |
| 129 RecordJsonDataSizeHistogram(path, serializer.get_last_read_size()); | 131 RecordJsonDataSizeHistogram(path, deserializer.get_last_read_size()); |
| 130 | 132 |
| 131 return read_result.Pass(); | 133 return read_result.Pass(); |
| 132 } | 134 } |
| 133 | 135 |
| 134 } // namespace | 136 } // namespace |
| 135 | 137 |
| 136 // static | 138 // static |
| 137 scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( | 139 scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( |
| 138 const base::FilePath& filename, | 140 const base::FilePath& filename, |
| 139 base::SequencedWorkerPool* worker_pool) { | 141 base::SequencedWorkerPool* worker_pool) { |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 CommitPendingWrite(); | 393 CommitPendingWrite(); |
| 392 } | 394 } |
| 393 | 395 |
| 394 bool JsonPrefStore::SerializeData(std::string* output) { | 396 bool JsonPrefStore::SerializeData(std::string* output) { |
| 395 DCHECK(CalledOnValidThread()); | 397 DCHECK(CalledOnValidThread()); |
| 396 | 398 |
| 397 if (pref_filter_) | 399 if (pref_filter_) |
| 398 pref_filter_->FilterSerializeData(prefs_.get()); | 400 pref_filter_->FilterSerializeData(prefs_.get()); |
| 399 | 401 |
| 400 JSONStringValueSerializer serializer(output); | 402 JSONStringValueSerializer serializer(output); |
| 401 serializer.set_pretty_print(true); | 403 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 404 switches::kPrettyPrintPrefs)) { |
| 405 serializer.set_pretty_print(true); |
| 406 } |
| 402 return serializer.Serialize(*prefs_); | 407 return serializer.Serialize(*prefs_); |
| 403 } | 408 } |
| 404 | 409 |
| 405 void JsonPrefStore::FinalizeFileRead(bool initialization_successful, | 410 void JsonPrefStore::FinalizeFileRead(bool initialization_successful, |
| 406 scoped_ptr<base::DictionaryValue> prefs, | 411 scoped_ptr<base::DictionaryValue> prefs, |
| 407 bool schedule_write) { | 412 bool schedule_write) { |
| 408 DCHECK(CalledOnValidThread()); | 413 DCHECK(CalledOnValidThread()); |
| 409 | 414 |
| 410 filtering_in_progress_ = false; | 415 filtering_in_progress_ = false; |
| 411 | 416 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 425 | 430 |
| 426 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 431 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
| 427 error_delegate_->OnError(read_error_); | 432 error_delegate_->OnError(read_error_); |
| 428 | 433 |
| 429 FOR_EACH_OBSERVER(PrefStore::Observer, | 434 FOR_EACH_OBSERVER(PrefStore::Observer, |
| 430 observers_, | 435 observers_, |
| 431 OnInitializationCompleted(true)); | 436 OnInitializationCompleted(true)); |
| 432 | 437 |
| 433 return; | 438 return; |
| 434 } | 439 } |
| OLD | NEW |