blob: d63d800db99f9b200b474b5d136cd760977417e1 [file] [log] [blame]
[email protected]d3b05ea2012-01-24 22:57:051// Copyright (c) 2012 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
[email protected]844a1002011-04-19 11:37:219#include "base/bind.h"
10#include "base/callback.h"
[email protected]277404c22010-04-22 13:09:4511#include "base/file_util.h"
[email protected]ffbec692012-02-26 20:26:4212#include "base/json/json_file_value_serializer.h"
13#include "base/json/json_string_value_serializer.h"
[email protected]844a1002011-04-19 11:37:2114#include "base/memory/ref_counted.h"
[email protected]845b43a82011-05-11 10:14:4315#include "base/message_loop_proxy.h"
[email protected]277404c22010-04-22 13:09:4516#include "base/values.h"
[email protected]277404c22010-04-22 13:09:4517
18namespace {
19
20// Some extensions we'll tack on to copies of the Preferences files.
21const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad");
22
[email protected]845b43a82011-05-11 10:14:4323// Differentiates file loading between origin thread and passed
24// (aka file) thread.
[email protected]844a1002011-04-19 11:37:2125class FileThreadDeserializer
26 : public base::RefCountedThreadSafe<FileThreadDeserializer> {
27 public:
[email protected]e7e38032011-07-26 17:25:2528 FileThreadDeserializer(JsonPrefStore* delegate,
29 base::MessageLoopProxy* file_loop_proxy)
[email protected]68e83602011-06-14 13:09:2830 : no_dir_(false),
31 error_(PersistentPrefStore::PREF_READ_ERROR_NONE),
32 delegate_(delegate),
[email protected]845b43a82011-05-11 10:14:4333 file_loop_proxy_(file_loop_proxy),
[email protected]edd685f2011-08-15 20:33:4634 origin_loop_proxy_(base::MessageLoopProxy::current()) {
[email protected]844a1002011-04-19 11:37:2135 }
36
37 void Start(const FilePath& path) {
[email protected]845b43a82011-05-11 10:14:4338 DCHECK(origin_loop_proxy_->BelongsToCurrentThread());
39 file_loop_proxy_->PostTask(
[email protected]844a1002011-04-19 11:37:2140 FROM_HERE,
[email protected]3a1a4052011-10-17 18:04:4141 base::Bind(&FileThreadDeserializer::ReadFileAndReport,
42 this, path));
[email protected]844a1002011-04-19 11:37:2143 }
44
[email protected]845b43a82011-05-11 10:14:4345 // Deserializes JSON on the file thread.
[email protected]844a1002011-04-19 11:37:2146 void ReadFileAndReport(const FilePath& path) {
[email protected]845b43a82011-05-11 10:14:4347 DCHECK(file_loop_proxy_->BelongsToCurrentThread());
[email protected]844a1002011-04-19 11:37:2148
[email protected]845b43a82011-05-11 10:14:4349 value_.reset(DoReading(path, &error_, &no_dir_));
50
51 origin_loop_proxy_->PostTask(
52 FROM_HERE,
[email protected]3a1a4052011-10-17 18:04:4153 base::Bind(&FileThreadDeserializer::ReportOnOriginThread, this));
[email protected]845b43a82011-05-11 10:14:4354 }
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]844a1002011-04-19 11:37:2165 int error_code;
66 std::string error_msg;
67 JSONFileValueSerializer serializer(path);
[email protected]845b43a82011-05-11 10:14:4368 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]844a1002011-04-19 11:37:2172 }
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]845b43a82011-05-11 10:14:4387 scoped_refptr<base::MessageLoopProxy> file_loop_proxy_;
88 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_;
[email protected]844a1002011-04-19 11:37:2189};
90
91// static
92void 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]26f025e2011-10-28 22:49:27100 DVLOG(1) << "Error while loading JSON file: " << error_msg;
[email protected]844a1002011-04-19 11:37:21101 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]277404c22010-04-22 13:09:45137} // namespace
138
[email protected]ea587b02010-05-21 15:01:35139JsonPrefStore::JsonPrefStore(const FilePath& filename,
140 base::MessageLoopProxy* file_message_loop_proxy)
[email protected]277404c22010-04-22 13:09:45141 : path_(filename),
[email protected]845b43a82011-05-11 10:14:43142 file_message_loop_proxy_(file_message_loop_proxy),
[email protected]277404c22010-04-22 13:09:45143 prefs_(new DictionaryValue()),
144 read_only_(false),
[email protected]845b43a82011-05-11 10:14:43145 writer_(filename, file_message_loop_proxy),
146 error_delegate_(NULL),
147 initialized_(false) {
[email protected]6658ca82010-05-20 18:20:29148}
[email protected]277404c22010-04-22 13:09:45149
150JsonPrefStore::~JsonPrefStore() {
[email protected]3826fed2011-03-25 10:59:56151 CommitPendingWrite();
[email protected]277404c22010-04-22 13:09:45152}
153
[email protected]f2d1f612010-12-09 15:10:17154PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key,
[email protected]68bf41a2011-03-25 16:38:31155 const Value** result) const {
156 Value* tmp = NULL;
157 if (prefs_->Get(key, &tmp)) {
[email protected]a78e9482011-07-14 19:22:29158 if (result)
159 *result = tmp;
[email protected]68bf41a2011-03-25 16:38:31160 return READ_OK;
161 }
162 return READ_NO_VALUE;
[email protected]f2d1f612010-12-09 15:10:17163}
164
165void JsonPrefStore::AddObserver(PrefStore::Observer* observer) {
166 observers_.AddObserver(observer);
167}
168
169void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) {
170 observers_.RemoveObserver(observer);
171}
172
[email protected]d3b05ea2012-01-24 22:57:05173size_t JsonPrefStore::NumberOfObservers() const {
174 return observers_.size();
175}
176
[email protected]845b43a82011-05-11 10:14:43177bool JsonPrefStore::IsInitializationComplete() const {
178 return initialized_;
179}
180
[email protected]68bf41a2011-03-25 16:38:31181PrefStore::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]f2d1f612010-12-09 15:10:17186void JsonPrefStore::SetValue(const std::string& key, Value* value) {
[email protected]9b5f56b42011-08-24 21:17:59187 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]25308672011-10-10 17:22:50193 ReportValueChanged(key);
[email protected]9b5f56b42011-08-24 21:17:59194 }
[email protected]f2d1f612010-12-09 15:10:17195}
196
197void JsonPrefStore::SetValueSilently(const std::string& key, Value* value) {
[email protected]9b5f56b42011-08-24 21:17:59198 DCHECK(value);
199 scoped_ptr<Value> new_value(value);
200 Value* old_value = NULL;
201 prefs_->Get(key, &old_value);
[email protected]25308672011-10-10 17:22:50202 if (!old_value || !value->Equals(old_value)) {
[email protected]9b5f56b42011-08-24 21:17:59203 prefs_->Set(key, new_value.release());
[email protected]25308672011-10-10 17:22:50204 if (!read_only_)
205 writer_.ScheduleWrite(this);
206 }
[email protected]f2d1f612010-12-09 15:10:17207}
208
209void JsonPrefStore::RemoveValue(const std::string& key) {
[email protected]25308672011-10-10 17:22:50210 if (prefs_->Remove(key, NULL))
211 ReportValueChanged(key);
[email protected]f2d1f612010-12-09 15:10:17212}
213
[email protected]ddb1e5a2010-12-13 20:10:45214bool JsonPrefStore::ReadOnly() const {
215 return read_only_;
216}
217
[email protected]844a1002011-04-19 11:37:21218void JsonPrefStore::OnFileRead(Value* value_owned,
219 PersistentPrefStore::PrefReadError error,
220 bool no_dir) {
221 scoped_ptr<Value> value(value_owned);
[email protected]845b43a82011-05-11 10:14:43222 initialized_ = true;
223
224 if (no_dir) {
225 FOR_EACH_OBSERVER(PrefStore::Observer,
226 observers_,
227 OnInitializationCompleted(false));
228 return;
229 }
230
[email protected]844a1002011-04-19 11:37:21231 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]845b43a82011-05-11 10:14:43236 case PREF_READ_ERROR_FILE_NOT_SPECIFIED:
[email protected]844a1002011-04-19 11:37:21237 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]845b43a82011-05-11 10:14:43254 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]844a1002011-04-19 11:37:21260}
261
[email protected]845b43a82011-05-11 10:14:43262void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate *error_delegate) {
263 initialized_ = false;
264 error_delegate_.reset(error_delegate);
[email protected]844a1002011-04-19 11:37:21265 if (path_.empty()) {
[email protected]845b43a82011-05-11 10:14:43266 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false);
[email protected]844a1002011-04-19 11:37:21267 return;
268 }
269
[email protected]844a1002011-04-19 11:37:21270 // Start async reading of the preferences file. It will delete itself
271 // in the end.
272 scoped_refptr<FileThreadDeserializer> deserializer(
[email protected]845b43a82011-05-11 10:14:43273 new FileThreadDeserializer(this, file_message_loop_proxy_.get()));
[email protected]844a1002011-04-19 11:37:21274 deserializer->Start(path_);
275}
276
[email protected]f2d1f612010-12-09 15:10:17277PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() {
[email protected]fd6159a2010-09-03 09:38:39278 if (path_.empty()) {
[email protected]845b43a82011-05-11 10:14:43279 OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false);
[email protected]fd6159a2010-09-03 09:38:39280 return PREF_READ_ERROR_FILE_NOT_SPECIFIED;
281 }
[email protected]277404c22010-04-22 13:09:45282
[email protected]845b43a82011-05-11 10:14:43283 PrefReadError error;
284 bool no_dir;
285 Value* value = FileThreadDeserializer::DoReading(path_, &error, &no_dir);
286 OnFileRead(value, error, no_dir);
[email protected]844a1002011-04-19 11:37:21287 return error;
[email protected]277404c22010-04-22 13:09:45288}
289
[email protected]3826fed2011-03-25 10:59:56290void JsonPrefStore::CommitPendingWrite() {
291 if (writer_.HasPendingWrite() && !read_only_)
292 writer_.DoScheduledWrite();
293}
294
[email protected]f89ee342011-03-07 09:28:27295void JsonPrefStore::ReportValueChanged(const std::string& key) {
296 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
[email protected]25308672011-10-10 17:22:50297 if (!read_only_)
298 writer_.ScheduleWrite(this);
[email protected]f89ee342011-03-07 09:28:27299}
300
[email protected]277404c22010-04-22 13:09:45301bool 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}