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