Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8893)

Unified Diff: chrome/common/json_pref_store.cc

Issue 11243002: Move the bits of Prefs where production code has only trivially easy (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments, fix gypi problem Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/common/json_pref_store.h ('k') | chrome/common/json_pref_store_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/common/json_pref_store.cc
diff --git a/chrome/common/json_pref_store.cc b/chrome/common/json_pref_store.cc
deleted file mode 100644
index ed30346ff048190d9ad004b210f14be3ffca8cf7..0000000000000000000000000000000000000000
--- a/chrome/common/json_pref_store.cc
+++ /dev/null
@@ -1,344 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/common/json_pref_store.h"
-
-#include <algorithm>
-
-#include "base/bind.h"
-#include "base/callback.h"
-#include "base/file_util.h"
-#include "base/json/json_file_value_serializer.h"
-#include "base/json/json_string_value_serializer.h"
-#include "base/memory/ref_counted.h"
-#include "base/message_loop_proxy.h"
-#include "base/values.h"
-
-namespace {
-
-// Some extensions we'll tack on to copies of the Preferences files.
-const FilePath::CharType* kBadExtension = FILE_PATH_LITERAL("bad");
-
-// Differentiates file loading between origin thread and passed
-// (aka file) thread.
-class FileThreadDeserializer
- : public base::RefCountedThreadSafe<FileThreadDeserializer> {
- public:
- FileThreadDeserializer(JsonPrefStore* delegate,
- base::MessageLoopProxy* file_loop_proxy)
- : no_dir_(false),
- error_(PersistentPrefStore::PREF_READ_ERROR_NONE),
- delegate_(delegate),
- file_loop_proxy_(file_loop_proxy),
- origin_loop_proxy_(base::MessageLoopProxy::current()) {
- }
-
- void Start(const FilePath& path) {
- DCHECK(origin_loop_proxy_->BelongsToCurrentThread());
- file_loop_proxy_->PostTask(
- FROM_HERE,
- base::Bind(&FileThreadDeserializer::ReadFileAndReport,
- this, path));
- }
-
- // Deserializes JSON on the file thread.
- void ReadFileAndReport(const FilePath& path) {
- DCHECK(file_loop_proxy_->BelongsToCurrentThread());
-
- value_.reset(DoReading(path, &error_, &no_dir_));
-
- origin_loop_proxy_->PostTask(
- FROM_HERE,
- base::Bind(&FileThreadDeserializer::ReportOnOriginThread, this));
- }
-
- // Reports deserialization result on the origin thread.
- void ReportOnOriginThread() {
- DCHECK(origin_loop_proxy_->BelongsToCurrentThread());
- delegate_->OnFileRead(value_.release(), error_, no_dir_);
- }
-
- static Value* DoReading(const FilePath& path,
- PersistentPrefStore::PrefReadError* error,
- bool* no_dir) {
- int error_code;
- std::string error_msg;
- JSONFileValueSerializer serializer(path);
- Value* value = serializer.Deserialize(&error_code, &error_msg);
- HandleErrors(value, path, error_code, error_msg, error);
- *no_dir = !file_util::PathExists(path.DirName());
- return value;
- }
-
- static void HandleErrors(const Value* value,
- const FilePath& path,
- int error_code,
- const std::string& error_msg,
- PersistentPrefStore::PrefReadError* error);
-
- private:
- friend class base::RefCountedThreadSafe<FileThreadDeserializer>;
- ~FileThreadDeserializer() {}
-
- bool no_dir_;
- PersistentPrefStore::PrefReadError error_;
- scoped_ptr<Value> value_;
- scoped_refptr<JsonPrefStore> delegate_;
- scoped_refptr<base::MessageLoopProxy> file_loop_proxy_;
- scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_;
-};
-
-// static
-void FileThreadDeserializer::HandleErrors(
- const Value* value,
- const FilePath& path,
- int error_code,
- const std::string& error_msg,
- PersistentPrefStore::PrefReadError* error) {
- *error = PersistentPrefStore::PREF_READ_ERROR_NONE;
- if (!value) {
- DVLOG(1) << "Error while loading JSON file: " << error_msg;
- switch (error_code) {
- case JSONFileValueSerializer::JSON_ACCESS_DENIED:
- *error = PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED;
- break;
- case JSONFileValueSerializer::JSON_CANNOT_READ_FILE:
- *error = PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER;
- break;
- case JSONFileValueSerializer::JSON_FILE_LOCKED:
- *error = PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED;
- break;
- case JSONFileValueSerializer::JSON_NO_SUCH_FILE:
- *error = PersistentPrefStore::PREF_READ_ERROR_NO_FILE;
- break;
- default:
- *error = PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE;
- // JSON errors indicate file corruption of some sort.
- // Since the file is corrupt, move it to the side and continue with
- // empty preferences. This will result in them losing their settings.
- // We keep the old file for possible support and debugging assistance
- // as well as to detect if they're seeing these errors repeatedly.
- // TODO(erikkay) Instead, use the last known good file.
- FilePath bad = path.ReplaceExtension(kBadExtension);
-
- // If they've ever had a parse error before, put them in another bucket.
- // TODO(erikkay) if we keep this error checking for very long, we may
- // want to differentiate between recent and long ago errors.
- if (file_util::PathExists(bad))
- *error = PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT;
- file_util::Move(path, bad);
- break;
- }
- } else if (!value->IsType(Value::TYPE_DICTIONARY)) {
- *error = PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE;
- }
-}
-
-} // namespace
-
-JsonPrefStore::JsonPrefStore(const FilePath& filename,
- base::MessageLoopProxy* file_message_loop_proxy)
- : path_(filename),
- file_message_loop_proxy_(file_message_loop_proxy),
- prefs_(new DictionaryValue()),
- read_only_(false),
- writer_(filename, file_message_loop_proxy),
- error_delegate_(NULL),
- initialized_(false),
- read_error_(PREF_READ_ERROR_OTHER) {
-}
-
-PrefStore::ReadResult JsonPrefStore::GetValue(const std::string& key,
- const Value** result) const {
- Value* tmp = NULL;
- if (prefs_->Get(key, &tmp)) {
- if (result)
- *result = tmp;
- return READ_OK;
- }
- return READ_NO_VALUE;
-}
-
-void JsonPrefStore::AddObserver(PrefStore::Observer* observer) {
- observers_.AddObserver(observer);
-}
-
-void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) {
- observers_.RemoveObserver(observer);
-}
-
-size_t JsonPrefStore::NumberOfObservers() const {
- return observers_.size();
-}
-
-bool JsonPrefStore::IsInitializationComplete() const {
- return initialized_;
-}
-
-PrefStore::ReadResult JsonPrefStore::GetMutableValue(const std::string& key,
- Value** result) {
- return prefs_->Get(key, result) ? READ_OK : READ_NO_VALUE;
-}
-
-void JsonPrefStore::SetValue(const std::string& key, Value* value) {
- DCHECK(value);
- scoped_ptr<Value> new_value(value);
- Value* old_value = NULL;
- prefs_->Get(key, &old_value);
- if (!old_value || !value->Equals(old_value)) {
- prefs_->Set(key, new_value.release());
- ReportValueChanged(key);
- }
-}
-
-void JsonPrefStore::SetValueSilently(const std::string& key, Value* value) {
- DCHECK(value);
- scoped_ptr<Value> new_value(value);
- Value* old_value = NULL;
- prefs_->Get(key, &old_value);
- if (!old_value || !value->Equals(old_value)) {
- prefs_->Set(key, new_value.release());
- if (!read_only_)
- writer_.ScheduleWrite(this);
- }
-}
-
-void JsonPrefStore::RemoveValue(const std::string& key) {
- if (prefs_->Remove(key, NULL))
- ReportValueChanged(key);
-}
-
-void JsonPrefStore::MarkNeedsEmptyValue(const std::string& key) {
- keys_need_empty_value_.insert(key);
-}
-
-bool JsonPrefStore::ReadOnly() const {
- return read_only_;
-}
-
-PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const {
- return read_error_;
-}
-
-PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() {
- if (path_.empty()) {
- OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false);
- return PREF_READ_ERROR_FILE_NOT_SPECIFIED;
- }
-
- PrefReadError error;
- bool no_dir;
- Value* value = FileThreadDeserializer::DoReading(path_, &error, &no_dir);
- OnFileRead(value, error, no_dir);
- return error;
-}
-
-void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate *error_delegate) {
- initialized_ = false;
- error_delegate_.reset(error_delegate);
- if (path_.empty()) {
- OnFileRead(NULL, PREF_READ_ERROR_FILE_NOT_SPECIFIED, false);
- return;
- }
-
- // Start async reading of the preferences file. It will delete itself
- // in the end.
- scoped_refptr<FileThreadDeserializer> deserializer(
- new FileThreadDeserializer(this, file_message_loop_proxy_.get()));
- deserializer->Start(path_);
-}
-
-void JsonPrefStore::CommitPendingWrite() {
- if (writer_.HasPendingWrite() && !read_only_)
- writer_.DoScheduledWrite();
-}
-
-void JsonPrefStore::ReportValueChanged(const std::string& key) {
- FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
- if (!read_only_)
- writer_.ScheduleWrite(this);
-}
-
-void JsonPrefStore::OnFileRead(Value* value_owned,
- PersistentPrefStore::PrefReadError error,
- bool no_dir) {
- scoped_ptr<Value> value(value_owned);
- read_error_ = error;
-
- if (no_dir) {
- FOR_EACH_OBSERVER(PrefStore::Observer,
- observers_,
- OnInitializationCompleted(false));
- return;
- }
-
- initialized_ = true;
-
- switch (error) {
- case PREF_READ_ERROR_ACCESS_DENIED:
- case PREF_READ_ERROR_FILE_OTHER:
- case PREF_READ_ERROR_FILE_LOCKED:
- case PREF_READ_ERROR_JSON_TYPE:
- case PREF_READ_ERROR_FILE_NOT_SPECIFIED:
- read_only_ = true;
- break;
- case PREF_READ_ERROR_NONE:
- DCHECK(value.get());
- prefs_.reset(static_cast<DictionaryValue*>(value.release()));
- break;
- case PREF_READ_ERROR_NO_FILE:
- // If the file just doesn't exist, maybe this is first run. In any case
- // there's no harm in writing out default prefs in this case.
- break;
- case PREF_READ_ERROR_JSON_PARSE:
- case PREF_READ_ERROR_JSON_REPEAT:
- break;
- default:
- NOTREACHED() << "Unknown error: " << error;
- }
-
- if (error_delegate_.get() && error != PREF_READ_ERROR_NONE)
- error_delegate_->OnError(error);
-
- FOR_EACH_OBSERVER(PrefStore::Observer,
- observers_,
- OnInitializationCompleted(true));
-}
-
-JsonPrefStore::~JsonPrefStore() {
- CommitPendingWrite();
-}
-
-bool JsonPrefStore::SerializeData(std::string* output) {
- // TODO(tc): Do we want to prune webkit preferences that match the default
- // value?
- JSONStringValueSerializer serializer(output);
- serializer.set_pretty_print(true);
- scoped_ptr<DictionaryValue> copy(prefs_->DeepCopyWithoutEmptyChildren());
-
- // Iterates |keys_need_empty_value_| and if the key exists in |prefs_|,
- // ensure its empty ListValue or DictonaryValue is preserved.
- for (std::set<std::string>::const_iterator
- it = keys_need_empty_value_.begin();
- it != keys_need_empty_value_.end();
- ++it) {
- const std::string& key = *it;
-
- base::Value* value = NULL;
- if (!prefs_->Get(key, &value))
- continue;
-
- if (value->IsType(base::Value::TYPE_LIST)) {
- const base::ListValue* list = NULL;
- if (value->GetAsList(&list) && list->empty())
- copy->Set(key, new base::ListValue);
- } else if (value->IsType(base::Value::TYPE_DICTIONARY)) {
- const base::DictionaryValue* dict = NULL;
- if (value->GetAsDictionary(&dict) && dict->empty())
- copy->Set(key, new base::DictionaryValue);
- }
- }
-
- return serializer.Serialize(*(copy.get()));
-}
« no previous file with comments | « chrome/common/json_pref_store.h ('k') | chrome/common/json_pref_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698