[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 03b9b4e | 2012-10-22 20:01:52 | [diff] [blame] | 5 | #include "base/prefs/json_pref_store.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/callback.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 11 | #include "base/file_util.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 12 | #include "base/json/json_file_value_serializer.h" |
| 13 | #include "base/json/json_string_value_serializer.h" |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 7ccb707 | 2013-06-10 20:56:28 | [diff] [blame] | 15 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | fb44196 | 2013-05-08 05:35:24 | [diff] [blame] | 16 | #include "base/sequenced_task_runner.h" |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 17 | #include "base/threading/sequenced_worker_pool.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 18 | #include "base/values.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | // Some extensions we'll tack on to copies of the Preferences files. |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 23 | const base::FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 24 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 25 | // Differentiates file loading between origin thread and passed |
| 26 | // (aka file) thread. |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 27 | class FileThreadDeserializer |
| 28 | : public base::RefCountedThreadSafe<FileThreadDeserializer> { |
| 29 | public: |
[email protected] | e7e3803 | 2011-07-26 17:25:25 | [diff] [blame] | 30 | FileThreadDeserializer(JsonPrefStore* delegate, |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 31 | base::SequencedTaskRunner* sequenced_task_runner) |
[email protected] | 68e8360 | 2011-06-14 13:09:28 | [diff] [blame] | 32 | : no_dir_(false), |
| 33 | error_(PersistentPrefStore::PREF_READ_ERROR_NONE), |
| 34 | delegate_(delegate), |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 35 | sequenced_task_runner_(sequenced_task_runner), |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 36 | origin_loop_proxy_(base::MessageLoopProxy::current()) { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 37 | } |
| 38 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 39 | void Start(const base::FilePath& path) { |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 40 | DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 41 | sequenced_task_runner_->PostTask( |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 42 | FROM_HERE, |
[email protected] | 3a1a405 | 2011-10-17 18:04:41 | [diff] [blame] | 43 | base::Bind(&FileThreadDeserializer::ReadFileAndReport, |
| 44 | this, path)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 47 | // Deserializes JSON on the sequenced task runner. |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 48 | void ReadFileAndReport(const base::FilePath& path) { |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 49 | DCHECK(sequenced_task_runner_->RunsTasksOnCurrentThread()); |
[email protected] | 370bc73 | 2012-05-07 21:25:20 | [diff] [blame] | 50 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 51 | value_.reset(DoReading(path, &error_, &no_dir_)); |
| 52 | |
| 53 | origin_loop_proxy_->PostTask( |
| 54 | FROM_HERE, |
[email protected] | 3a1a405 | 2011-10-17 18:04:41 | [diff] [blame] | 55 | base::Bind(&FileThreadDeserializer::ReportOnOriginThread, this)); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Reports deserialization result on the origin thread. |
| 59 | void ReportOnOriginThread() { |
| 60 | DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); |
| 61 | delegate_->OnFileRead(value_.release(), error_, no_dir_); |
| 62 | } |
| 63 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 64 | static base::Value* DoReading(const base::FilePath& path, |
| 65 | PersistentPrefStore::PrefReadError* error, |
| 66 | bool* no_dir) { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 67 | int error_code; |
| 68 | std::string error_msg; |
| 69 | JSONFileValueSerializer serializer(path); |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 70 | base::Value* value = serializer.Deserialize(&error_code, &error_msg); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 71 | HandleErrors(value, path, error_code, error_msg, error); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 72 | *no_dir = !base::PathExists(path.DirName()); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 73 | return value; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 76 | static void HandleErrors(const base::Value* value, |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 77 | const base::FilePath& path, |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 78 | int error_code, |
| 79 | const std::string& error_msg, |
| 80 | PersistentPrefStore::PrefReadError* error); |
| 81 | |
| 82 | private: |
| 83 | friend class base::RefCountedThreadSafe<FileThreadDeserializer>; |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 84 | ~FileThreadDeserializer() {} |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 85 | |
| 86 | bool no_dir_; |
| 87 | PersistentPrefStore::PrefReadError error_; |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 88 | scoped_ptr<base::Value> value_; |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 89 | const scoped_refptr<JsonPrefStore> delegate_; |
| 90 | const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
| 91 | const scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | // static |
| 95 | void FileThreadDeserializer::HandleErrors( |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 96 | const base::Value* value, |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 97 | const base::FilePath& path, |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 98 | int error_code, |
| 99 | const std::string& error_msg, |
| 100 | PersistentPrefStore::PrefReadError* error) { |
| 101 | *error = PersistentPrefStore::PREF_READ_ERROR_NONE; |
| 102 | if (!value) { |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 103 | DVLOG(1) << "Error while loading JSON file: " << error_msg |
| 104 | << ", file: " << path.value(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 105 | switch (error_code) { |
| 106 | case JSONFileValueSerializer::JSON_ACCESS_DENIED: |
| 107 | *error = PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED; |
| 108 | break; |
| 109 | case JSONFileValueSerializer::JSON_CANNOT_READ_FILE: |
| 110 | *error = PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER; |
| 111 | break; |
| 112 | case JSONFileValueSerializer::JSON_FILE_LOCKED: |
| 113 | *error = PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED; |
| 114 | break; |
| 115 | case JSONFileValueSerializer::JSON_NO_SUCH_FILE: |
| 116 | *error = PersistentPrefStore::PREF_READ_ERROR_NO_FILE; |
| 117 | break; |
| 118 | default: |
| 119 | *error = PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE; |
| 120 | // JSON errors indicate file corruption of some sort. |
| 121 | // Since the file is corrupt, move it to the side and continue with |
| 122 | // empty preferences. This will result in them losing their settings. |
| 123 | // We keep the old file for possible support and debugging assistance |
| 124 | // as well as to detect if they're seeing these errors repeatedly. |
| 125 | // TODO(erikkay) Instead, use the last known good file. |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 126 | base::FilePath bad = path.ReplaceExtension(kBadExtension); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 127 | |
| 128 | // If they've ever had a parse error before, put them in another bucket. |
| 129 | // TODO(erikkay) if we keep this error checking for very long, we may |
| 130 | // want to differentiate between recent and long ago errors. |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 131 | if (base::PathExists(bad)) |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 132 | *error = PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT; |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 133 | base::Move(path, bad); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 134 | break; |
| 135 | } |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 136 | } else if (!value->IsType(base::Value::TYPE_DICTIONARY)) { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 137 | *error = PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; |
| 138 | } |
| 139 | } |
| 140 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 141 | } // namespace |
| 142 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 143 | scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 144 | const base::FilePath& filename, |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 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 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 153 | JsonPrefStore::JsonPrefStore(const base::FilePath& filename, |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 154 | base::SequencedTaskRunner* sequenced_task_runner) |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 155 | : path_(filename), |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 156 | sequenced_task_runner_(sequenced_task_runner), |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 157 | prefs_(new base::DictionaryValue()), |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 158 | read_only_(false), |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 159 | writer_(filename, sequenced_task_runner), |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 160 | initialized_(false), |
[email protected] | 02c29b38 | 2013-06-13 18:01:07 | [diff] [blame] | 161 | read_error_(PREF_READ_ERROR_OTHER) {} |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 162 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 163 | bool JsonPrefStore::GetValue(const std::string& key, |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 164 | const base::Value** result) const { |
| 165 | base::Value* tmp = NULL; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 166 | if (!prefs_->Get(key, &tmp)) |
| 167 | return false; |
| 168 | |
| 169 | if (result) |
| 170 | *result = tmp; |
| 171 | return true; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 175 | observers_.AddObserver(observer); |
| 176 | } |
| 177 | |
| 178 | void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) { |
| 179 | observers_.RemoveObserver(observer); |
| 180 | } |
| 181 | |
[email protected] | 14e0ec6 | 2013-08-26 22:01:39 | [diff] [blame] | 182 | bool JsonPrefStore::HasObservers() const { |
| 183 | return observers_.might_have_observers(); |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 186 | bool JsonPrefStore::IsInitializationComplete() const { |
| 187 | return initialized_; |
| 188 | } |
| 189 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 190 | bool JsonPrefStore::GetMutableValue(const std::string& key, |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 191 | base::Value** result) { |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 192 | return prefs_->Get(key, result); |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 195 | void JsonPrefStore::SetValue(const std::string& key, base::Value* value) { |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 196 | DCHECK(value); |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 197 | scoped_ptr<base::Value> new_value(value); |
| 198 | base::Value* old_value = NULL; |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 199 | prefs_->Get(key, &old_value); |
| 200 | if (!old_value || !value->Equals(old_value)) { |
| 201 | prefs_->Set(key, new_value.release()); |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 202 | ReportValueChanged(key); |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 203 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 206 | void JsonPrefStore::SetValueSilently(const std::string& key, |
| 207 | base::Value* value) { |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 208 | DCHECK(value); |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 209 | scoped_ptr<base::Value> new_value(value); |
| 210 | base::Value* old_value = NULL; |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 211 | prefs_->Get(key, &old_value); |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 212 | if (!old_value || !value->Equals(old_value)) { |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 213 | prefs_->Set(key, new_value.release()); |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 214 | if (!read_only_) |
| 215 | writer_.ScheduleWrite(this); |
| 216 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void JsonPrefStore::RemoveValue(const std::string& key) { |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame^] | 220 | if (prefs_->RemovePath(key, NULL)) |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 221 | ReportValueChanged(key); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 224 | bool JsonPrefStore::ReadOnly() const { |
| 225 | return read_only_; |
| 226 | } |
| 227 | |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 228 | PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { |
| 229 | return read_error_; |
| 230 | } |
| 231 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 232 | PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { |
| 233 | if (path_.empty()) { |
| 234 | OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); |
| 235 | return PREF_READ_ERROR_FILE_NOT_SPECIFIED; |
| 236 | } |
| 237 | |
| 238 | PrefReadError error; |
| 239 | bool no_dir; |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 240 | base::Value* value = |
| 241 | FileThreadDeserializer::DoReading(path_, &error, &no_dir); |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 242 | OnFileRead(value, error, no_dir); |
| 243 | return error; |
| 244 | } |
| 245 | |
| 246 | void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate *error_delegate) { |
| 247 | initialized_ = false; |
| 248 | error_delegate_.reset(error_delegate); |
| 249 | if (path_.empty()) { |
| 250 | OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | // Start async reading of the preferences file. It will delete itself |
| 255 | // in the end. |
| 256 | scoped_refptr<FileThreadDeserializer> deserializer( |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 257 | new FileThreadDeserializer(this, sequenced_task_runner_.get())); |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 258 | deserializer->Start(path_); |
| 259 | } |
| 260 | |
| 261 | void JsonPrefStore::CommitPendingWrite() { |
| 262 | if (writer_.HasPendingWrite() && !read_only_) |
| 263 | writer_.DoScheduledWrite(); |
| 264 | } |
| 265 | |
| 266 | void JsonPrefStore::ReportValueChanged(const std::string& key) { |
| 267 | FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 268 | if (!read_only_) |
| 269 | writer_.ScheduleWrite(this); |
| 270 | } |
| 271 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 272 | void JsonPrefStore::OnFileRead(base::Value* value_owned, |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 273 | PersistentPrefStore::PrefReadError error, |
| 274 | bool no_dir) { |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 275 | scoped_ptr<base::Value> value(value_owned); |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 276 | read_error_ = error; |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 277 | |
| 278 | if (no_dir) { |
| 279 | FOR_EACH_OBSERVER(PrefStore::Observer, |
| 280 | observers_, |
| 281 | OnInitializationCompleted(false)); |
| 282 | return; |
| 283 | } |
| 284 | |
[email protected] | 32a7a02 | 2012-05-10 23:36:39 | [diff] [blame] | 285 | initialized_ = true; |
| 286 | |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 287 | switch (error) { |
| 288 | case PREF_READ_ERROR_ACCESS_DENIED: |
| 289 | case PREF_READ_ERROR_FILE_OTHER: |
| 290 | case PREF_READ_ERROR_FILE_LOCKED: |
| 291 | case PREF_READ_ERROR_JSON_TYPE: |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 292 | case PREF_READ_ERROR_FILE_NOT_SPECIFIED: |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 293 | read_only_ = true; |
| 294 | break; |
| 295 | case PREF_READ_ERROR_NONE: |
| 296 | DCHECK(value.get()); |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 297 | prefs_.reset(static_cast<base::DictionaryValue*>(value.release())); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 298 | break; |
| 299 | case PREF_READ_ERROR_NO_FILE: |
| 300 | // If the file just doesn't exist, maybe this is first run. In any case |
| 301 | // there's no harm in writing out default prefs in this case. |
| 302 | break; |
| 303 | case PREF_READ_ERROR_JSON_PARSE: |
| 304 | case PREF_READ_ERROR_JSON_REPEAT: |
| 305 | break; |
| 306 | default: |
| 307 | NOTREACHED() << "Unknown error: " << error; |
| 308 | } |
| 309 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 310 | if (error_delegate_.get() && error != PREF_READ_ERROR_NONE) |
| 311 | error_delegate_->OnError(error); |
| 312 | |
| 313 | FOR_EACH_OBSERVER(PrefStore::Observer, |
| 314 | observers_, |
| 315 | OnInitializationCompleted(true)); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 316 | } |
| 317 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 318 | JsonPrefStore::~JsonPrefStore() { |
| 319 | CommitPendingWrite(); |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 320 | } |
| 321 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 322 | bool JsonPrefStore::SerializeData(std::string* output) { |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 323 | JSONStringValueSerializer serializer(output); |
| 324 | serializer.set_pretty_print(true); |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame^] | 325 | return serializer.Serialize(*prefs_); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 326 | } |