[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] | ac771fb36 | 2014-07-16 06:04:44 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 13 | #include "base/json/json_file_value_serializer.h" |
| 14 | #include "base/json/json_string_value_serializer.h" |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
[email protected] | ac771fb36 | 2014-07-16 06:04:44 | [diff] [blame] | 16 | #include "base/metrics/histogram.h" |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 17 | #include "base/prefs/pref_filter.h" |
[email protected] | fb44196 | 2013-05-08 05:35:24 | [diff] [blame] | 18 | #include "base/sequenced_task_runner.h" |
[email protected] | ac771fb36 | 2014-07-16 06:04:44 | [diff] [blame] | 19 | #include "base/strings/string_util.h" |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 20 | #include "base/task_runner_util.h" |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 21 | #include "base/threading/sequenced_worker_pool.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 22 | #include "base/values.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 23 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 24 | // Result returned from internal read tasks. |
| 25 | struct JsonPrefStore::ReadResult { |
| 26 | public: |
| 27 | ReadResult(); |
| 28 | ~ReadResult(); |
| 29 | |
| 30 | scoped_ptr<base::Value> value; |
| 31 | PrefReadError error; |
| 32 | bool no_dir; |
| 33 | |
| 34 | private: |
| 35 | DISALLOW_COPY_AND_ASSIGN(ReadResult); |
| 36 | }; |
| 37 | |
| 38 | JsonPrefStore::ReadResult::ReadResult() |
| 39 | : error(PersistentPrefStore::PREF_READ_ERROR_NONE), no_dir(false) { |
| 40 | } |
| 41 | |
| 42 | JsonPrefStore::ReadResult::~ReadResult() { |
| 43 | } |
| 44 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | // Some extensions we'll tack on to copies of the Preferences files. |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 48 | const base::FilePath::CharType kBadExtension[] = FILE_PATH_LITERAL("bad"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 49 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 50 | PersistentPrefStore::PrefReadError HandleReadErrors( |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 51 | const base::Value* value, |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 52 | const base::FilePath& path, |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 53 | int error_code, |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 54 | const std::string& error_msg) { |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 55 | if (!value) { |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 56 | DVLOG(1) << "Error while loading JSON file: " << error_msg |
| 57 | << ", file: " << path.value(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 58 | switch (error_code) { |
| 59 | case JSONFileValueSerializer::JSON_ACCESS_DENIED: |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 60 | return PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 61 | break; |
| 62 | case JSONFileValueSerializer::JSON_CANNOT_READ_FILE: |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 63 | return PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 64 | break; |
| 65 | case JSONFileValueSerializer::JSON_FILE_LOCKED: |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 66 | return PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 67 | break; |
| 68 | case JSONFileValueSerializer::JSON_NO_SUCH_FILE: |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 69 | return PersistentPrefStore::PREF_READ_ERROR_NO_FILE; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 70 | break; |
| 71 | default: |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 72 | // JSON errors indicate file corruption of some sort. |
| 73 | // Since the file is corrupt, move it to the side and continue with |
| 74 | // empty preferences. This will result in them losing their settings. |
| 75 | // We keep the old file for possible support and debugging assistance |
| 76 | // as well as to detect if they're seeing these errors repeatedly. |
| 77 | // TODO(erikkay) Instead, use the last known good file. |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 78 | base::FilePath bad = path.ReplaceExtension(kBadExtension); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 79 | |
| 80 | // If they've ever had a parse error before, put them in another bucket. |
| 81 | // TODO(erikkay) if we keep this error checking for very long, we may |
| 82 | // want to differentiate between recent and long ago errors. |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 83 | bool bad_existed = base::PathExists(bad); |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 84 | base::Move(path, bad); |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 85 | return bad_existed ? PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT |
| 86 | : PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 87 | } |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 88 | } else if (!value->IsType(base::Value::TYPE_DICTIONARY)) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 89 | return PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 90 | } |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 91 | return PersistentPrefStore::PREF_READ_ERROR_NONE; |
| 92 | } |
| 93 | |
| 94 | scoped_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( |
| 95 | const base::FilePath& path, |
| 96 | const base::FilePath& alternate_path) { |
| 97 | if (!base::PathExists(path) && !alternate_path.empty() && |
| 98 | base::PathExists(alternate_path)) { |
| 99 | base::Move(alternate_path, path); |
| 100 | } |
| 101 | |
| 102 | int error_code; |
| 103 | std::string error_msg; |
| 104 | scoped_ptr<JsonPrefStore::ReadResult> read_result( |
| 105 | new JsonPrefStore::ReadResult); |
| 106 | JSONFileValueSerializer serializer(path); |
| 107 | read_result->value.reset(serializer.Deserialize(&error_code, &error_msg)); |
| 108 | read_result->error = |
| 109 | HandleReadErrors(read_result->value.get(), path, error_code, error_msg); |
| 110 | read_result->no_dir = !base::PathExists(path.DirName()); |
| 111 | return read_result.Pass(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 114 | } // namespace |
| 115 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 116 | // static |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 117 | scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 118 | const base::FilePath& filename, |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 119 | base::SequencedWorkerPool* worker_pool) { |
| 120 | std::string token("json_pref_store-"); |
| 121 | token.append(filename.AsUTF8Unsafe()); |
| 122 | return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 123 | worker_pool->GetNamedSequenceToken(token), |
| 124 | base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 125 | } |
| 126 | |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 127 | JsonPrefStore::JsonPrefStore(const base::FilePath& filename, |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 128 | base::SequencedTaskRunner* sequenced_task_runner, |
| 129 | scoped_ptr<PrefFilter> pref_filter) |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 130 | : path_(filename), |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 131 | sequenced_task_runner_(sequenced_task_runner), |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 132 | prefs_(new base::DictionaryValue()), |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 133 | read_only_(false), |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 134 | writer_(filename, sequenced_task_runner), |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 135 | pref_filter_(pref_filter.Pass()), |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 136 | initialized_(false), |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 137 | filtering_in_progress_(false), |
| 138 | read_error_(PREF_READ_ERROR_NONE) { |
| 139 | } |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 140 | |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 141 | JsonPrefStore::JsonPrefStore(const base::FilePath& filename, |
| 142 | const base::FilePath& alternate_filename, |
| 143 | base::SequencedTaskRunner* sequenced_task_runner, |
| 144 | scoped_ptr<PrefFilter> pref_filter) |
| 145 | : path_(filename), |
| 146 | alternate_path_(alternate_filename), |
| 147 | sequenced_task_runner_(sequenced_task_runner), |
| 148 | prefs_(new base::DictionaryValue()), |
| 149 | read_only_(false), |
| 150 | writer_(filename, sequenced_task_runner), |
| 151 | pref_filter_(pref_filter.Pass()), |
| 152 | initialized_(false), |
| 153 | filtering_in_progress_(false), |
| 154 | read_error_(PREF_READ_ERROR_NONE) { |
| 155 | } |
| 156 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 157 | bool JsonPrefStore::GetValue(const std::string& key, |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 158 | const base::Value** result) const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 159 | DCHECK(CalledOnValidThread()); |
| 160 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 161 | base::Value* tmp = NULL; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 162 | if (!prefs_->Get(key, &tmp)) |
| 163 | return false; |
| 164 | |
| 165 | if (result) |
| 166 | *result = tmp; |
| 167 | return true; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 171 | DCHECK(CalledOnValidThread()); |
| 172 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 173 | observers_.AddObserver(observer); |
| 174 | } |
| 175 | |
| 176 | void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 177 | DCHECK(CalledOnValidThread()); |
| 178 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 179 | observers_.RemoveObserver(observer); |
| 180 | } |
| 181 | |
[email protected] | 14e0ec6 | 2013-08-26 22:01:39 | [diff] [blame] | 182 | bool JsonPrefStore::HasObservers() const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 183 | DCHECK(CalledOnValidThread()); |
| 184 | |
[email protected] | 14e0ec6 | 2013-08-26 22:01:39 | [diff] [blame] | 185 | return observers_.might_have_observers(); |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 188 | bool JsonPrefStore::IsInitializationComplete() const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 189 | DCHECK(CalledOnValidThread()); |
| 190 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 191 | return initialized_; |
| 192 | } |
| 193 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 194 | bool JsonPrefStore::GetMutableValue(const std::string& key, |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 195 | base::Value** result) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 196 | DCHECK(CalledOnValidThread()); |
| 197 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 198 | return prefs_->Get(key, result); |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 201 | void JsonPrefStore::SetValue(const std::string& key, base::Value* value) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 202 | DCHECK(CalledOnValidThread()); |
| 203 | |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 204 | DCHECK(value); |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 205 | scoped_ptr<base::Value> new_value(value); |
| 206 | base::Value* old_value = NULL; |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 207 | prefs_->Get(key, &old_value); |
| 208 | if (!old_value || !value->Equals(old_value)) { |
| 209 | prefs_->Set(key, new_value.release()); |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 210 | ReportValueChanged(key); |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 211 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 214 | void JsonPrefStore::SetValueSilently(const std::string& key, |
| 215 | base::Value* value) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 216 | DCHECK(CalledOnValidThread()); |
| 217 | |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 218 | DCHECK(value); |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 219 | scoped_ptr<base::Value> new_value(value); |
| 220 | base::Value* old_value = NULL; |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 221 | prefs_->Get(key, &old_value); |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 222 | if (!old_value || !value->Equals(old_value)) { |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 223 | prefs_->Set(key, new_value.release()); |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 224 | if (!read_only_) |
| 225 | writer_.ScheduleWrite(this); |
| 226 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void JsonPrefStore::RemoveValue(const std::string& key) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 230 | DCHECK(CalledOnValidThread()); |
| 231 | |
[email protected] | aa328339 | 2013-11-27 01:38:24 | [diff] [blame] | 232 | if (prefs_->RemovePath(key, NULL)) |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 233 | ReportValueChanged(key); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 236 | void JsonPrefStore::RemoveValueSilently(const std::string& key) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 237 | DCHECK(CalledOnValidThread()); |
| 238 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 239 | prefs_->RemovePath(key, NULL); |
| 240 | if (!read_only_) |
| 241 | writer_.ScheduleWrite(this); |
| 242 | } |
| 243 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 244 | bool JsonPrefStore::ReadOnly() const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 245 | DCHECK(CalledOnValidThread()); |
| 246 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 247 | return read_only_; |
| 248 | } |
| 249 | |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 250 | PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 251 | DCHECK(CalledOnValidThread()); |
| 252 | |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 253 | return read_error_; |
| 254 | } |
| 255 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 256 | PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 257 | DCHECK(CalledOnValidThread()); |
| 258 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 259 | if (path_.empty()) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 260 | scoped_ptr<ReadResult> no_file_result; |
| 261 | no_file_result->error = PREF_READ_ERROR_FILE_NOT_SPECIFIED; |
| 262 | OnFileRead(no_file_result.Pass()); |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 263 | return PREF_READ_ERROR_FILE_NOT_SPECIFIED; |
| 264 | } |
| 265 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 266 | OnFileRead(ReadPrefsFromDisk(path_, alternate_path_)); |
| 267 | return filtering_in_progress_ ? PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE |
| 268 | : read_error_; |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 271 | void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 272 | DCHECK(CalledOnValidThread()); |
| 273 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 274 | initialized_ = false; |
| 275 | error_delegate_.reset(error_delegate); |
| 276 | if (path_.empty()) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 277 | scoped_ptr<ReadResult> no_file_result; |
| 278 | no_file_result->error = PREF_READ_ERROR_FILE_NOT_SPECIFIED; |
| 279 | OnFileRead(no_file_result.Pass()); |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
[email protected] | 3b268bc | 2014-07-28 17:43:15 | [diff] [blame] | 283 | // Weakly binds the read task so that it doesn't kick in during shutdown. |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 284 | base::PostTaskAndReplyWithResult( |
dcheng | d81e7d7 | 2014-08-27 00:23:23 | [diff] [blame^] | 285 | sequenced_task_runner_.get(), |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 286 | FROM_HERE, |
| 287 | base::Bind(&ReadPrefsFromDisk, path_, alternate_path_), |
[email protected] | 3b268bc | 2014-07-28 17:43:15 | [diff] [blame] | 288 | base::Bind(&JsonPrefStore::OnFileRead, AsWeakPtr())); |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void JsonPrefStore::CommitPendingWrite() { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 292 | DCHECK(CalledOnValidThread()); |
| 293 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 294 | if (writer_.HasPendingWrite() && !read_only_) |
| 295 | writer_.DoScheduledWrite(); |
| 296 | } |
| 297 | |
| 298 | void JsonPrefStore::ReportValueChanged(const std::string& key) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 299 | DCHECK(CalledOnValidThread()); |
| 300 | |
[email protected] | a0ce7e7 | 2014-01-21 16:50:56 | [diff] [blame] | 301 | if (pref_filter_) |
| 302 | pref_filter_->FilterUpdate(key); |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 303 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 304 | FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 305 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 306 | if (!read_only_) |
| 307 | writer_.ScheduleWrite(this); |
| 308 | } |
| 309 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 310 | void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback( |
| 311 | const base::Closure& on_next_successful_write) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 312 | DCHECK(CalledOnValidThread()); |
| 313 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 314 | writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); |
| 315 | } |
| 316 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 317 | void JsonPrefStore::OnFileRead(scoped_ptr<ReadResult> read_result) { |
| 318 | DCHECK(CalledOnValidThread()); |
| 319 | |
| 320 | DCHECK(read_result); |
| 321 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 322 | scoped_ptr<base::DictionaryValue> unfiltered_prefs(new base::DictionaryValue); |
| 323 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 324 | read_error_ = read_result->error; |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 325 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 326 | bool initialization_successful = !read_result->no_dir; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 327 | |
| 328 | if (initialization_successful) { |
| 329 | switch (read_error_) { |
| 330 | case PREF_READ_ERROR_ACCESS_DENIED: |
| 331 | case PREF_READ_ERROR_FILE_OTHER: |
| 332 | case PREF_READ_ERROR_FILE_LOCKED: |
| 333 | case PREF_READ_ERROR_JSON_TYPE: |
| 334 | case PREF_READ_ERROR_FILE_NOT_SPECIFIED: |
| 335 | read_only_ = true; |
| 336 | break; |
| 337 | case PREF_READ_ERROR_NONE: |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 338 | DCHECK(read_result->value.get()); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 339 | unfiltered_prefs.reset( |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 340 | static_cast<base::DictionaryValue*>(read_result->value.release())); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 341 | break; |
| 342 | case PREF_READ_ERROR_NO_FILE: |
| 343 | // If the file just doesn't exist, maybe this is first run. In any case |
| 344 | // there's no harm in writing out default prefs in this case. |
| 345 | break; |
| 346 | case PREF_READ_ERROR_JSON_PARSE: |
| 347 | case PREF_READ_ERROR_JSON_REPEAT: |
| 348 | break; |
| 349 | case PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE: |
| 350 | // This is a special error code to be returned by ReadPrefs when it |
| 351 | // can't complete synchronously, it should never be returned by the read |
| 352 | // operation itself. |
| 353 | NOTREACHED(); |
| 354 | break; |
[email protected] | 3edebd95 | 2014-05-28 08:27:58 | [diff] [blame] | 355 | case PREF_READ_ERROR_LEVELDB_IO: |
| 356 | case PREF_READ_ERROR_LEVELDB_CORRUPTION_READ_ONLY: |
| 357 | case PREF_READ_ERROR_LEVELDB_CORRUPTION: |
| 358 | // These are specific to LevelDBPrefStore. |
| 359 | NOTREACHED(); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 360 | case PREF_READ_ERROR_MAX_ENUM: |
| 361 | NOTREACHED(); |
| 362 | break; |
| 363 | } |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 364 | } |
| 365 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 366 | if (pref_filter_) { |
| 367 | filtering_in_progress_ = true; |
| 368 | const PrefFilter::PostFilterOnLoadCallback post_filter_on_load_callback( |
| 369 | base::Bind( |
[email protected] | 3b268bc | 2014-07-28 17:43:15 | [diff] [blame] | 370 | &JsonPrefStore::FinalizeFileRead, AsWeakPtr(), |
| 371 | initialization_successful)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 372 | pref_filter_->FilterOnLoad(post_filter_on_load_callback, |
| 373 | unfiltered_prefs.Pass()); |
| 374 | } else { |
| 375 | FinalizeFileRead(initialization_successful, unfiltered_prefs.Pass(), false); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 376 | } |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 379 | JsonPrefStore::~JsonPrefStore() { |
| 380 | CommitPendingWrite(); |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 381 | } |
| 382 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 383 | bool JsonPrefStore::SerializeData(std::string* output) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 384 | DCHECK(CalledOnValidThread()); |
| 385 | |
[email protected] | bc831ff1 | 2014-01-31 20:48:33 | [diff] [blame] | 386 | if (pref_filter_) |
[email protected] | a0ce7e7 | 2014-01-21 16:50:56 | [diff] [blame] | 387 | pref_filter_->FilterSerializeData(prefs_.get()); |
| 388 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 389 | JSONStringValueSerializer serializer(output); |
| 390 | serializer.set_pretty_print(true); |
[email protected] | ac771fb36 | 2014-07-16 06:04:44 | [diff] [blame] | 391 | bool result = serializer.Serialize(*prefs_); |
| 392 | |
| 393 | if (result) { |
| 394 | std::string spaceless_basename; |
| 395 | base::ReplaceChars(path_.BaseName().MaybeAsASCII(), " ", "_", |
| 396 | &spaceless_basename); |
| 397 | |
| 398 | // The histogram below is an expansion of the UMA_HISTOGRAM_COUNTS_10000 |
| 399 | // macro adapted to allow for a dynamically suffixed histogram name. |
| 400 | // Note: The factory creates and owns the histogram. |
| 401 | base::HistogramBase* histogram = |
| 402 | base::LinearHistogram::FactoryGet( |
| 403 | "Settings.JsonDataSizeKilobytes." + spaceless_basename, |
| 404 | 1, |
| 405 | 10000, |
| 406 | 50, |
| 407 | base::HistogramBase::kUmaTargetedHistogramFlag); |
| 408 | histogram->Add(static_cast<int>(output->size()) / 1024); |
| 409 | } |
| 410 | |
| 411 | return result; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 412 | } |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 413 | |
| 414 | void JsonPrefStore::FinalizeFileRead(bool initialization_successful, |
| 415 | scoped_ptr<base::DictionaryValue> prefs, |
| 416 | bool schedule_write) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 417 | DCHECK(CalledOnValidThread()); |
| 418 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 419 | filtering_in_progress_ = false; |
| 420 | |
| 421 | if (!initialization_successful) { |
| 422 | FOR_EACH_OBSERVER(PrefStore::Observer, |
| 423 | observers_, |
| 424 | OnInitializationCompleted(false)); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | prefs_ = prefs.Pass(); |
| 429 | |
| 430 | initialized_ = true; |
| 431 | |
| 432 | if (schedule_write && !read_only_) |
| 433 | writer_.ScheduleWrite(this); |
| 434 | |
| 435 | if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
| 436 | error_delegate_->OnError(read_error_); |
| 437 | |
| 438 | FOR_EACH_OBSERVER(PrefStore::Observer, |
| 439 | observers_, |
| 440 | OnInitializationCompleted(true)); |
| 441 | |
| 442 | return; |
| 443 | } |