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