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