[email protected] | 3826fed | 2011-03-25 10:59:56 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
| 9 | #include "base/file_util.h" |
| 10 | #include "base/values.h" |
[email protected] | c311302 | 2011-04-16 03:26:30 | [diff] [blame^] | 11 | #include "content/common/json_value_serializer.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 12 | |
| 13 | namespace { |
| 14 | |
| 15 | // Some extensions we'll tack on to copies of the Preferences files. |
| 16 | const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad"); |
| 17 | |
| 18 | } // namespace |
| 19 | |
[email protected] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 20 | JsonPrefStore::JsonPrefStore(const FilePath& filename, |
| 21 | base::MessageLoopProxy* file_message_loop_proxy) |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 22 | : path_(filename), |
| 23 | prefs_(new DictionaryValue()), |
| 24 | read_only_(false), |
[email protected] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 25 | writer_(filename, file_message_loop_proxy) { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 26 | } |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 27 | |
| 28 | JsonPrefStore::~JsonPrefStore() { |
[email protected] | 3826fed | 2011-03-25 10:59:56 | [diff] [blame] | 29 | CommitPendingWrite(); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 32 | PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key, |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 33 | const Value** result) const { |
| 34 | Value* tmp = NULL; |
| 35 | if (prefs_->Get(key, &tmp)) { |
| 36 | *result = tmp; |
| 37 | return READ_OK; |
| 38 | } |
| 39 | return READ_NO_VALUE; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 43 | observers_.AddObserver(observer); |
| 44 | } |
| 45 | |
| 46 | void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) { |
| 47 | observers_.RemoveObserver(observer); |
| 48 | } |
| 49 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 50 | PrefStore::ReadResult JsonPrefStore::GetMutableValue(const std::string& key, |
| 51 | Value** result) { |
| 52 | return prefs_->Get(key, result) ? READ_OK : READ_NO_VALUE; |
| 53 | } |
| 54 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 55 | void JsonPrefStore::SetValue(const std::string& key, Value* value) { |
| 56 | DCHECK(value); |
| 57 | scoped_ptr<Value> new_value(value); |
| 58 | Value* old_value = NULL; |
| 59 | prefs_->Get(key, &old_value); |
| 60 | if (!old_value || !value->Equals(old_value)) { |
| 61 | prefs_->Set(key, new_value.release()); |
| 62 | FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void JsonPrefStore::SetValueSilently(const std::string& key, Value* value) { |
| 67 | DCHECK(value); |
| 68 | scoped_ptr<Value> new_value(value); |
| 69 | Value* old_value = NULL; |
| 70 | prefs_->Get(key, &old_value); |
| 71 | if (!old_value || !value->Equals(old_value)) |
| 72 | prefs_->Set(key, new_value.release()); |
| 73 | } |
| 74 | |
| 75 | void JsonPrefStore::RemoveValue(const std::string& key) { |
| 76 | if (prefs_->Remove(key, NULL)) { |
| 77 | FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 78 | } |
| 79 | } |
| 80 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 81 | bool JsonPrefStore::ReadOnly() const { |
| 82 | return read_only_; |
| 83 | } |
| 84 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 85 | PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { |
[email protected] | fd6159a | 2010-09-03 09:38:39 | [diff] [blame] | 86 | if (path_.empty()) { |
| 87 | read_only_ = true; |
| 88 | return PREF_READ_ERROR_FILE_NOT_SPECIFIED; |
| 89 | } |
[email protected] | e4f8649 | 2011-04-13 12:23:53 | [diff] [blame] | 90 | JSONFileValueSerializer serializer(path_); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 91 | |
| 92 | int error_code = 0; |
| 93 | std::string error_msg; |
| 94 | scoped_ptr<Value> value(serializer.Deserialize(&error_code, &error_msg)); |
[email protected] | e4f8649 | 2011-04-13 12:23:53 | [diff] [blame] | 95 | if (!value.get()) { |
| 96 | PrefReadError error; |
| 97 | switch (error_code) { |
| 98 | case JSONFileValueSerializer::JSON_ACCESS_DENIED: |
| 99 | // If the file exists but is simply unreadable, put the file into a |
| 100 | // state where we don't try to save changes. Otherwise, we could |
| 101 | // clobber the existing prefs. |
| 102 | error = PREF_READ_ERROR_ACCESS_DENIED; |
| 103 | read_only_ = true; |
| 104 | break; |
| 105 | case JSONFileValueSerializer::JSON_CANNOT_READ_FILE: |
| 106 | error = PREF_READ_ERROR_FILE_OTHER; |
| 107 | read_only_ = true; |
| 108 | break; |
| 109 | case JSONFileValueSerializer::JSON_FILE_LOCKED: |
| 110 | error = PREF_READ_ERROR_FILE_LOCKED; |
| 111 | read_only_ = true; |
| 112 | break; |
| 113 | case JSONFileValueSerializer::JSON_NO_SUCH_FILE: |
| 114 | // If the file just doesn't exist, maybe this is first run. In any case |
| 115 | // there's no harm in writing out default prefs in this case. |
| 116 | error = PREF_READ_ERROR_NO_FILE; |
| 117 | break; |
| 118 | default: |
| 119 | error = PREF_READ_ERROR_JSON_PARSE; |
| 120 | // JSON errors indicate file corruption of some sort. |
| 121 | // Since the file is corrupt, move it to the side and continue with |
| 122 | // empty preferences. This will result in them losing their settings. |
| 123 | // We keep the old file for possible support and debugging assistance |
| 124 | // as well as to detect if they're seeing these errors repeatedly. |
| 125 | // TODO(erikkay) Instead, use the last known good file. |
| 126 | FilePath bad = path_.ReplaceExtension(kBadExtension); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 127 | |
[email protected] | e4f8649 | 2011-04-13 12:23:53 | [diff] [blame] | 128 | // If they've ever had a parse error before, put them in another bucket. |
| 129 | // TODO(erikkay) if we keep this error checking for very long, we may |
| 130 | // want to differentiate between recent and long ago errors. |
| 131 | if (file_util::PathExists(bad)) |
| 132 | error = PREF_READ_ERROR_JSON_REPEAT; |
| 133 | file_util::Move(path_, bad); |
| 134 | break; |
| 135 | } |
| 136 | return error; |
| 137 | } |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 138 | |
[email protected] | e4f8649 | 2011-04-13 12:23:53 | [diff] [blame] | 139 | // Preferences should always have a dictionary root. |
| 140 | if (!value->IsType(Value::TYPE_DICTIONARY)) { |
| 141 | // See comment for the default case above. |
| 142 | read_only_ = true; |
| 143 | return PREF_READ_ERROR_JSON_TYPE; |
| 144 | } |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 145 | |
[email protected] | e4f8649 | 2011-04-13 12:23:53 | [diff] [blame] | 146 | prefs_.reset(static_cast<DictionaryValue*>(value.release())); |
| 147 | |
| 148 | return PREF_READ_ERROR_NONE; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | bool JsonPrefStore::WritePrefs() { |
| 152 | std::string data; |
| 153 | if (!SerializeData(&data)) |
| 154 | return false; |
| 155 | |
| 156 | // Lie about our ability to save. |
| 157 | if (read_only_) |
| 158 | return true; |
| 159 | |
| 160 | writer_.WriteNow(data); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | void JsonPrefStore::ScheduleWritePrefs() { |
| 165 | if (read_only_) |
| 166 | return; |
| 167 | |
| 168 | writer_.ScheduleWrite(this); |
| 169 | } |
| 170 | |
[email protected] | 3826fed | 2011-03-25 10:59:56 | [diff] [blame] | 171 | void JsonPrefStore::CommitPendingWrite() { |
| 172 | if (writer_.HasPendingWrite() && !read_only_) |
| 173 | writer_.DoScheduledWrite(); |
| 174 | } |
| 175 | |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 176 | void JsonPrefStore::ReportValueChanged(const std::string& key) { |
| 177 | FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 178 | } |
| 179 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 180 | bool JsonPrefStore::SerializeData(std::string* output) { |
| 181 | // TODO(tc): Do we want to prune webkit preferences that match the default |
| 182 | // value? |
| 183 | JSONStringValueSerializer serializer(output); |
| 184 | serializer.set_pretty_print(true); |
| 185 | scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren()); |
| 186 | return serializer.Serialize(*(copy.get())); |
| 187 | } |