[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" |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 19 | #include "base/strings/string_number_conversions.h" |
[email protected] | ac771fb36 | 2014-07-16 06:04:44 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 21 | #include "base/task_runner_util.h" |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 22 | #include "base/threading/sequenced_worker_pool.h" |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 23 | #include "base/time/default_clock.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; |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 63 | case JSONFileValueDeserializer::JSON_CANNOT_READ_FILE: |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 64 | return PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER; |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 65 | case JSONFileValueDeserializer::JSON_FILE_LOCKED: |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 66 | return PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED; |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 67 | case JSONFileValueDeserializer::JSON_NO_SUCH_FILE: |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 68 | return PersistentPrefStore::PREF_READ_ERROR_NO_FILE; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 69 | default: |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 70 | // JSON errors indicate file corruption of some sort. |
| 71 | // Since the file is corrupt, move it to the side and continue with |
| 72 | // empty preferences. This will result in them losing their settings. |
| 73 | // We keep the old file for possible support and debugging assistance |
| 74 | // as well as to detect if they're seeing these errors repeatedly. |
| 75 | // TODO(erikkay) Instead, use the last known good file. |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 76 | base::FilePath bad = path.ReplaceExtension(kBadExtension); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 77 | |
| 78 | // If they've ever had a parse error before, put them in another bucket. |
| 79 | // TODO(erikkay) if we keep this error checking for very long, we may |
| 80 | // want to differentiate between recent and long ago errors. |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 81 | bool bad_existed = base::PathExists(bad); |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 82 | base::Move(path, bad); |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 83 | return bad_existed ? PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT |
| 84 | : PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE; |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 85 | } |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 86 | } |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 87 | if (!value->IsType(base::Value::TYPE_DICTIONARY)) |
| 88 | return PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 89 | return PersistentPrefStore::PREF_READ_ERROR_NONE; |
| 90 | } |
| 91 | |
gab | f16b59a | 2015-01-31 19:09:02 | [diff] [blame] | 92 | // Records a sample for |size| in the Settings.JsonDataReadSizeKilobytes |
| 93 | // histogram suffixed with the base name of the JSON file under |path|. |
| 94 | void RecordJsonDataSizeHistogram(const base::FilePath& path, size_t size) { |
| 95 | std::string spaceless_basename; |
| 96 | base::ReplaceChars(path.BaseName().MaybeAsASCII(), " ", "_", |
| 97 | &spaceless_basename); |
| 98 | |
| 99 | // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
| 100 | // macro adapted to allow for a dynamically suffixed histogram name. |
| 101 | // Note: The factory creates and owns the histogram. |
| 102 | base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| 103 | "Settings.JsonDataReadSizeKilobytes." + spaceless_basename, 1, 10000, 50, |
| 104 | base::HistogramBase::kUmaTargetedHistogramFlag); |
| 105 | histogram->Add(static_cast<int>(size) / 1024); |
| 106 | } |
| 107 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 108 | scoped_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( |
| 109 | const base::FilePath& path, |
| 110 | const base::FilePath& alternate_path) { |
| 111 | if (!base::PathExists(path) && !alternate_path.empty() && |
| 112 | base::PathExists(alternate_path)) { |
| 113 | base::Move(alternate_path, path); |
| 114 | } |
| 115 | |
| 116 | int error_code; |
| 117 | std::string error_msg; |
| 118 | scoped_ptr<JsonPrefStore::ReadResult> read_result( |
| 119 | new JsonPrefStore::ReadResult); |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 120 | JSONFileValueDeserializer deserializer(path); |
| 121 | read_result->value.reset(deserializer.Deserialize(&error_code, &error_msg)); |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 122 | read_result->error = |
| 123 | HandleReadErrors(read_result->value.get(), path, error_code, error_msg); |
| 124 | read_result->no_dir = !base::PathExists(path.DirName()); |
gab | f16b59a | 2015-01-31 19:09:02 | [diff] [blame] | 125 | |
| 126 | if (read_result->error == PersistentPrefStore::PREF_READ_ERROR_NONE) |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 127 | RecordJsonDataSizeHistogram(path, deserializer.get_last_read_size()); |
gab | f16b59a | 2015-01-31 19:09:02 | [diff] [blame] | 128 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 129 | return read_result.Pass(); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 132 | } // namespace |
| 133 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 134 | // static |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 135 | scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 136 | const base::FilePath& filename, |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 137 | base::SequencedWorkerPool* worker_pool) { |
| 138 | std::string token("json_pref_store-"); |
| 139 | token.append(filename.AsUTF8Unsafe()); |
| 140 | return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 141 | worker_pool->GetNamedSequenceToken(token), |
| 142 | base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 143 | } |
| 144 | |
dcheng | 077d1b2 | 2014-08-28 18:47:38 | [diff] [blame] | 145 | JsonPrefStore::JsonPrefStore( |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 146 | const base::FilePath& pref_filename, |
dcheng | 077d1b2 | 2014-08-28 18:47:38 | [diff] [blame] | 147 | const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
| 148 | scoped_ptr<PrefFilter> pref_filter) |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 149 | : JsonPrefStore(pref_filename, |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 150 | base::FilePath(), |
| 151 | sequenced_task_runner, |
| 152 | pref_filter.Pass()) { |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 153 | } |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 154 | |
dcheng | 077d1b2 | 2014-08-28 18:47:38 | [diff] [blame] | 155 | JsonPrefStore::JsonPrefStore( |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 156 | const base::FilePath& pref_filename, |
| 157 | const base::FilePath& pref_alternate_filename, |
dcheng | 077d1b2 | 2014-08-28 18:47:38 | [diff] [blame] | 158 | const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
| 159 | scoped_ptr<PrefFilter> pref_filter) |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 160 | : path_(pref_filename), |
| 161 | alternate_path_(pref_alternate_filename), |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 162 | sequenced_task_runner_(sequenced_task_runner), |
| 163 | prefs_(new base::DictionaryValue()), |
| 164 | read_only_(false), |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 165 | writer_(pref_filename, sequenced_task_runner), |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 166 | pref_filter_(pref_filter.Pass()), |
| 167 | initialized_(false), |
| 168 | filtering_in_progress_(false), |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 169 | pending_lossy_write_(false), |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 170 | read_error_(PREF_READ_ERROR_NONE), |
| 171 | write_count_histogram_(writer_.commit_interval(), path_) { |
bratell | 970f39d | 2015-01-07 16:40:58 | [diff] [blame] | 172 | DCHECK(!path_.empty()); |
[email protected] | cfcf0e5 | 2014-06-20 18:29:47 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 175 | bool JsonPrefStore::GetValue(const std::string& key, |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 176 | const base::Value** result) const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 177 | DCHECK(CalledOnValidThread()); |
| 178 | |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 179 | base::Value* tmp = nullptr; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 180 | if (!prefs_->Get(key, &tmp)) |
| 181 | return false; |
| 182 | |
| 183 | if (result) |
| 184 | *result = tmp; |
| 185 | return true; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 189 | DCHECK(CalledOnValidThread()); |
| 190 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 191 | observers_.AddObserver(observer); |
| 192 | } |
| 193 | |
| 194 | void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 195 | DCHECK(CalledOnValidThread()); |
| 196 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 197 | observers_.RemoveObserver(observer); |
| 198 | } |
| 199 | |
[email protected] | 14e0ec6 | 2013-08-26 22:01:39 | [diff] [blame] | 200 | bool JsonPrefStore::HasObservers() const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 201 | DCHECK(CalledOnValidThread()); |
| 202 | |
[email protected] | 14e0ec6 | 2013-08-26 22:01:39 | [diff] [blame] | 203 | return observers_.might_have_observers(); |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 206 | bool JsonPrefStore::IsInitializationComplete() const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 207 | DCHECK(CalledOnValidThread()); |
| 208 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 209 | return initialized_; |
| 210 | } |
| 211 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 212 | bool JsonPrefStore::GetMutableValue(const std::string& key, |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 213 | base::Value** result) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 214 | DCHECK(CalledOnValidThread()); |
| 215 | |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 216 | return prefs_->Get(key, result); |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 217 | } |
| 218 | |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 219 | void JsonPrefStore::SetValue(const std::string& key, |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 220 | scoped_ptr<base::Value> value, |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 221 | uint32 flags) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 222 | DCHECK(CalledOnValidThread()); |
| 223 | |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 224 | DCHECK(value); |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 225 | base::Value* old_value = nullptr; |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 226 | prefs_->Get(key, &old_value); |
| 227 | if (!old_value || !value->Equals(old_value)) { |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 228 | prefs_->Set(key, value.Pass()); |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 229 | ReportValueChanged(key, flags); |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 230 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 233 | void JsonPrefStore::SetValueSilently(const std::string& key, |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 234 | scoped_ptr<base::Value> value, |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 235 | uint32 flags) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 236 | DCHECK(CalledOnValidThread()); |
| 237 | |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 238 | DCHECK(value); |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 239 | base::Value* old_value = nullptr; |
[email protected] | 9b5f56b4 | 2011-08-24 21:17:59 | [diff] [blame] | 240 | prefs_->Get(key, &old_value); |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 241 | if (!old_value || !value->Equals(old_value)) { |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 242 | prefs_->Set(key, value.Pass()); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 243 | ScheduleWrite(flags); |
[email protected] | 2530867 | 2011-10-10 17:22:50 | [diff] [blame] | 244 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 245 | } |
| 246 | |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 247 | void JsonPrefStore::RemoveValue(const std::string& key, uint32 flags) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 248 | DCHECK(CalledOnValidThread()); |
| 249 | |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 250 | if (prefs_->RemovePath(key, nullptr)) |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 251 | ReportValueChanged(key, flags); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 252 | } |
| 253 | |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 254 | void JsonPrefStore::RemoveValueSilently(const std::string& key, uint32 flags) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 255 | DCHECK(CalledOnValidThread()); |
| 256 | |
thestig | 1433edb | 2015-08-06 21:45:27 | [diff] [blame^] | 257 | prefs_->RemovePath(key, nullptr); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 258 | ScheduleWrite(flags); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 259 | } |
| 260 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 261 | bool JsonPrefStore::ReadOnly() const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 262 | DCHECK(CalledOnValidThread()); |
| 263 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 264 | return read_only_; |
| 265 | } |
| 266 | |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 267 | PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 268 | DCHECK(CalledOnValidThread()); |
| 269 | |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 270 | return read_error_; |
| 271 | } |
| 272 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 273 | PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 274 | DCHECK(CalledOnValidThread()); |
| 275 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 276 | OnFileRead(ReadPrefsFromDisk(path_, alternate_path_)); |
| 277 | return filtering_in_progress_ ? PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE |
| 278 | : read_error_; |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 281 | void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 282 | DCHECK(CalledOnValidThread()); |
| 283 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 284 | initialized_ = false; |
| 285 | error_delegate_.reset(error_delegate); |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 286 | |
[email protected] | 3b268bc | 2014-07-28 17:43:15 | [diff] [blame] | 287 | // 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] | 288 | base::PostTaskAndReplyWithResult( |
dcheng | d81e7d7 | 2014-08-27 00:23:23 | [diff] [blame] | 289 | sequenced_task_runner_.get(), |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 290 | FROM_HERE, |
| 291 | base::Bind(&ReadPrefsFromDisk, path_, alternate_path_), |
[email protected] | 3b268bc | 2014-07-28 17:43:15 | [diff] [blame] | 292 | base::Bind(&JsonPrefStore::OnFileRead, AsWeakPtr())); |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void JsonPrefStore::CommitPendingWrite() { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 296 | DCHECK(CalledOnValidThread()); |
| 297 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 298 | // Schedule a write for any lossy writes that are outstanding to ensure that |
| 299 | // they get flushed when this function is called. |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 300 | SchedulePendingLossyWrites(); |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 301 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 302 | if (writer_.HasPendingWrite() && !read_only_) |
| 303 | writer_.DoScheduledWrite(); |
| 304 | } |
| 305 | |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 306 | void JsonPrefStore::SchedulePendingLossyWrites() { |
| 307 | if (pending_lossy_write_) |
| 308 | writer_.ScheduleWrite(this); |
| 309 | } |
| 310 | |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 311 | void JsonPrefStore::ReportValueChanged(const std::string& key, uint32 flags) { |
[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 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 319 | ScheduleWrite(flags); |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 320 | } |
| 321 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 322 | void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback( |
| 323 | const base::Closure& on_next_successful_write) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 324 | DCHECK(CalledOnValidThread()); |
| 325 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 326 | writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); |
| 327 | } |
| 328 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 329 | void JsonPrefStore::OnFileRead(scoped_ptr<ReadResult> read_result) { |
| 330 | DCHECK(CalledOnValidThread()); |
| 331 | |
| 332 | DCHECK(read_result); |
| 333 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 334 | scoped_ptr<base::DictionaryValue> unfiltered_prefs(new base::DictionaryValue); |
| 335 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 336 | read_error_ = read_result->error; |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 337 | |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 338 | bool initialization_successful = !read_result->no_dir; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 339 | |
| 340 | if (initialization_successful) { |
| 341 | switch (read_error_) { |
| 342 | case PREF_READ_ERROR_ACCESS_DENIED: |
| 343 | case PREF_READ_ERROR_FILE_OTHER: |
| 344 | case PREF_READ_ERROR_FILE_LOCKED: |
| 345 | case PREF_READ_ERROR_JSON_TYPE: |
| 346 | case PREF_READ_ERROR_FILE_NOT_SPECIFIED: |
| 347 | read_only_ = true; |
| 348 | break; |
| 349 | case PREF_READ_ERROR_NONE: |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 350 | DCHECK(read_result->value.get()); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 351 | unfiltered_prefs.reset( |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 352 | static_cast<base::DictionaryValue*>(read_result->value.release())); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 353 | break; |
| 354 | case PREF_READ_ERROR_NO_FILE: |
| 355 | // If the file just doesn't exist, maybe this is first run. In any case |
| 356 | // there's no harm in writing out default prefs in this case. |
| 357 | break; |
| 358 | case PREF_READ_ERROR_JSON_PARSE: |
| 359 | case PREF_READ_ERROR_JSON_REPEAT: |
| 360 | break; |
| 361 | case PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE: |
| 362 | // This is a special error code to be returned by ReadPrefs when it |
| 363 | // can't complete synchronously, it should never be returned by the read |
| 364 | // operation itself. |
| 365 | NOTREACHED(); |
| 366 | break; |
| 367 | case PREF_READ_ERROR_MAX_ENUM: |
| 368 | NOTREACHED(); |
| 369 | break; |
| 370 | } |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 371 | } |
| 372 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 373 | if (pref_filter_) { |
| 374 | filtering_in_progress_ = true; |
| 375 | const PrefFilter::PostFilterOnLoadCallback post_filter_on_load_callback( |
| 376 | base::Bind( |
[email protected] | 3b268bc | 2014-07-28 17:43:15 | [diff] [blame] | 377 | &JsonPrefStore::FinalizeFileRead, AsWeakPtr(), |
| 378 | initialization_successful)); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 379 | pref_filter_->FilterOnLoad(post_filter_on_load_callback, |
| 380 | unfiltered_prefs.Pass()); |
| 381 | } else { |
| 382 | FinalizeFileRead(initialization_successful, unfiltered_prefs.Pass(), false); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 383 | } |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 384 | } |
| 385 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 386 | JsonPrefStore::~JsonPrefStore() { |
| 387 | CommitPendingWrite(); |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 388 | } |
| 389 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 390 | bool JsonPrefStore::SerializeData(std::string* output) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 391 | DCHECK(CalledOnValidThread()); |
| 392 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 393 | pending_lossy_write_ = false; |
| 394 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 395 | write_count_histogram_.RecordWriteOccured(); |
| 396 | |
[email protected] | bc831ff1 | 2014-01-31 20:48:33 | [diff] [blame] | 397 | if (pref_filter_) |
[email protected] | a0ce7e7 | 2014-01-21 16:50:56 | [diff] [blame] | 398 | pref_filter_->FilterSerializeData(prefs_.get()); |
| 399 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 400 | JSONStringValueSerializer serializer(output); |
gab | 97c50ac | 2015-03-06 20:41:43 | [diff] [blame] | 401 | // Not pretty-printing prefs shrinks pref file size by ~30%. To obtain |
| 402 | // readable prefs for debugging purposes, you can dump your prefs into any |
| 403 | // command-line or online JSON pretty printing tool. |
| 404 | serializer.set_pretty_print(false); |
gab | f16b59a | 2015-01-31 19:09:02 | [diff] [blame] | 405 | return serializer.Serialize(*prefs_); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 406 | } |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 407 | |
| 408 | void JsonPrefStore::FinalizeFileRead(bool initialization_successful, |
| 409 | scoped_ptr<base::DictionaryValue> prefs, |
| 410 | bool schedule_write) { |
[email protected] | f245659 | 2014-07-22 19:30:17 | [diff] [blame] | 411 | DCHECK(CalledOnValidThread()); |
| 412 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 413 | filtering_in_progress_ = false; |
| 414 | |
| 415 | if (!initialization_successful) { |
| 416 | FOR_EACH_OBSERVER(PrefStore::Observer, |
| 417 | observers_, |
| 418 | OnInitializationCompleted(false)); |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | prefs_ = prefs.Pass(); |
| 423 | |
| 424 | initialized_ = true; |
| 425 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 426 | if (schedule_write) |
| 427 | ScheduleWrite(DEFAULT_PREF_WRITE_FLAGS); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 428 | |
| 429 | if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
| 430 | error_delegate_->OnError(read_error_); |
| 431 | |
| 432 | FOR_EACH_OBSERVER(PrefStore::Observer, |
| 433 | observers_, |
| 434 | OnInitializationCompleted(true)); |
| 435 | |
| 436 | return; |
| 437 | } |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 438 | |
raymes | 4b6e14e | 2015-05-12 00:10:30 | [diff] [blame] | 439 | void JsonPrefStore::ScheduleWrite(uint32 flags) { |
| 440 | if (read_only_) |
| 441 | return; |
| 442 | |
| 443 | if (flags & LOSSY_PREF_WRITE_FLAG) |
| 444 | pending_lossy_write_ = true; |
| 445 | else |
| 446 | writer_.ScheduleWrite(this); |
| 447 | } |
| 448 | |
raymes | bfb910a | 2015-04-29 07:43:09 | [diff] [blame] | 449 | // NOTE: This value should NOT be changed without renaming the histogram |
| 450 | // otherwise it will create incompatible buckets. |
| 451 | const int32_t |
| 452 | JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins = 5; |
| 453 | |
| 454 | JsonPrefStore::WriteCountHistogram::WriteCountHistogram( |
| 455 | const base::TimeDelta& commit_interval, |
| 456 | const base::FilePath& path) |
| 457 | : WriteCountHistogram(commit_interval, |
| 458 | path, |
| 459 | scoped_ptr<base::Clock>(new base::DefaultClock)) { |
| 460 | } |
| 461 | |
| 462 | JsonPrefStore::WriteCountHistogram::WriteCountHistogram( |
| 463 | const base::TimeDelta& commit_interval, |
| 464 | const base::FilePath& path, |
| 465 | scoped_ptr<base::Clock> clock) |
| 466 | : commit_interval_(commit_interval), |
| 467 | path_(path), |
| 468 | clock_(clock.release()), |
| 469 | report_interval_( |
| 470 | base::TimeDelta::FromMinutes(kHistogramWriteReportIntervalMins)), |
| 471 | last_report_time_(clock_->Now()), |
| 472 | writes_since_last_report_(0) { |
| 473 | } |
| 474 | |
| 475 | JsonPrefStore::WriteCountHistogram::~WriteCountHistogram() { |
| 476 | ReportOutstandingWrites(); |
| 477 | } |
| 478 | |
| 479 | void JsonPrefStore::WriteCountHistogram::RecordWriteOccured() { |
| 480 | ReportOutstandingWrites(); |
| 481 | |
| 482 | ++writes_since_last_report_; |
| 483 | } |
| 484 | |
| 485 | void JsonPrefStore::WriteCountHistogram::ReportOutstandingWrites() { |
| 486 | base::Time current_time = clock_->Now(); |
| 487 | base::TimeDelta time_since_last_report = current_time - last_report_time_; |
| 488 | |
| 489 | if (time_since_last_report <= report_interval_) |
| 490 | return; |
| 491 | |
| 492 | // If the time since the last report exceeds the report interval, report all |
| 493 | // the writes since the last report. They must have all occurred in the same |
| 494 | // report interval. |
| 495 | base::HistogramBase* histogram = GetHistogram(); |
| 496 | histogram->Add(writes_since_last_report_); |
| 497 | |
| 498 | // There may be several report intervals that elapsed that don't have any |
| 499 | // writes in them. Report these too. |
| 500 | int64 total_num_intervals_elapsed = |
| 501 | (time_since_last_report / report_interval_); |
| 502 | for (int64 i = 0; i < total_num_intervals_elapsed - 1; ++i) |
| 503 | histogram->Add(0); |
| 504 | |
| 505 | writes_since_last_report_ = 0; |
| 506 | last_report_time_ += total_num_intervals_elapsed * report_interval_; |
| 507 | } |
| 508 | |
| 509 | base::HistogramBase* JsonPrefStore::WriteCountHistogram::GetHistogram() { |
| 510 | std::string spaceless_basename; |
| 511 | base::ReplaceChars(path_.BaseName().MaybeAsASCII(), " ", "_", |
| 512 | &spaceless_basename); |
| 513 | std::string histogram_name = |
| 514 | "Settings.JsonDataWriteCount." + spaceless_basename; |
| 515 | |
| 516 | // The min value for a histogram is 1. The max value is the maximum number of |
| 517 | // writes that can occur in the window being recorded. The number of buckets |
| 518 | // used is the max value (plus the underflow/overflow buckets). |
| 519 | int32_t min_value = 1; |
| 520 | int32_t max_value = report_interval_ / commit_interval_; |
| 521 | int32_t num_buckets = max_value + 1; |
| 522 | |
| 523 | // NOTE: These values should NOT be changed without renaming the histogram |
| 524 | // otherwise it will create incompatible buckets. |
| 525 | DCHECK_EQ(30, max_value); |
| 526 | DCHECK_EQ(31, num_buckets); |
| 527 | |
| 528 | // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
| 529 | // macro adapted to allow for a dynamically suffixed histogram name. |
| 530 | // Note: The factory creates and owns the histogram. |
| 531 | base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| 532 | histogram_name, min_value, max_value, num_buckets, |
| 533 | base::HistogramBase::kUmaTargetedHistogramFlag); |
| 534 | return histogram; |
| 535 | } |