blob: b22e8c24351d1ef5ac6344b192033a503342b44a [file] [log] [blame]
[email protected]3826fed2011-03-25 10:59:561// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]277404c22010-04-22 13:09:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ea587b02010-05-21 15:01:355#include "chrome/common/json_pref_store.h"
[email protected]277404c22010-04-22 13:09:456
7#include <algorithm>
8
9#include "base/file_util.h"
10#include "base/values.h"
[email protected]c3113022011-04-16 03:26:3011#include "content/common/json_value_serializer.h"
[email protected]277404c22010-04-22 13:09:4512
13namespace {
14
15// Some extensions we'll tack on to copies of the Preferences files.
16const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad");
17
18} // namespace
19
[email protected]ea587b02010-05-21 15:01:3520JsonPrefStore::JsonPrefStore(const FilePath& filename,
21 base::MessageLoopProxy* file_message_loop_proxy)
[email protected]277404c22010-04-22 13:09:4522 : path_(filename),
23 prefs_(new DictionaryValue()),
24 read_only_(false),
[email protected]ea587b02010-05-21 15:01:3525 writer_(filename, file_message_loop_proxy) {
[email protected]6658ca82010-05-20 18:20:2926}
[email protected]277404c22010-04-22 13:09:4527
28JsonPrefStore::~JsonPrefStore() {
[email protected]3826fed2011-03-25 10:59:5629 CommitPendingWrite();
[email protected]277404c22010-04-22 13:09:4530}
31
[email protected]f2d1f612010-12-09 15:10:1732PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key,
[email protected]68bf41a2011-03-25 16:38:3133 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]f2d1f612010-12-09 15:10:1740}
41
42void JsonPrefStore::AddObserver(PrefStore::Observer* observer) {
43 observers_.AddObserver(observer);
44}
45
46void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) {
47 observers_.RemoveObserver(observer);
48}
49
[email protected]68bf41a2011-03-25 16:38:3150PrefStore::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]f2d1f612010-12-09 15:10:1755void 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
66void 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
75void 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]ddb1e5a2010-12-13 20:10:4581bool JsonPrefStore::ReadOnly() const {
82 return read_only_;
83}
84
[email protected]f2d1f612010-12-09 15:10:1785PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() {
[email protected]fd6159a2010-09-03 09:38:3986 if (path_.empty()) {
87 read_only_ = true;
88 return PREF_READ_ERROR_FILE_NOT_SPECIFIED;
89 }
[email protected]e4f86492011-04-13 12:23:5390 JSONFileValueSerializer serializer(path_);
[email protected]277404c22010-04-22 13:09:4591
92 int error_code = 0;
93 std::string error_msg;
94 scoped_ptr<Value> value(serializer.Deserialize(&error_code, &error_msg));
[email protected]e4f86492011-04-13 12:23:5395 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]277404c22010-04-22 13:09:45127
[email protected]e4f86492011-04-13 12:23:53128 // 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]277404c22010-04-22 13:09:45138
[email protected]e4f86492011-04-13 12:23:53139 // 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]277404c22010-04-22 13:09:45145
[email protected]e4f86492011-04-13 12:23:53146 prefs_.reset(static_cast<DictionaryValue*>(value.release()));
147
148 return PREF_READ_ERROR_NONE;
[email protected]277404c22010-04-22 13:09:45149}
150
151bool 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
164void JsonPrefStore::ScheduleWritePrefs() {
165 if (read_only_)
166 return;
167
168 writer_.ScheduleWrite(this);
169}
170
[email protected]3826fed2011-03-25 10:59:56171void JsonPrefStore::CommitPendingWrite() {
172 if (writer_.HasPendingWrite() && !read_only_)
173 writer_.DoScheduledWrite();
174}
175
[email protected]f89ee342011-03-07 09:28:27176void JsonPrefStore::ReportValueChanged(const std::string& key) {
177 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
178}
179
[email protected]277404c22010-04-22 13:09:45180bool 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}