Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / chrome / browser / value_store / value_store_change.cc
blobfd9b9c9cb282c3318dd1cb9538817243eaaa7f76
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/value_store/value_store_change.h"
7 #include "base/json/json_writer.h"
8 #include "base/logging.h"
10 // static
11 std::string ValueStoreChange::ToJson(
12 const ValueStoreChangeList& changes) {
13 DictionaryValue changes_value;
14 for (ValueStoreChangeList::const_iterator it = changes.begin();
15 it != changes.end(); ++it) {
16 DictionaryValue* change_value = new DictionaryValue();
17 if (it->old_value()) {
18 change_value->Set("oldValue", it->old_value()->DeepCopy());
20 if (it->new_value()) {
21 change_value->Set("newValue", it->new_value()->DeepCopy());
23 changes_value.SetWithoutPathExpansion(it->key(), change_value);
25 std::string json;
26 base::JSONWriter::Write(&changes_value, &json);
27 return json;
30 ValueStoreChange::ValueStoreChange(
31 const std::string& key, Value* old_value, Value* new_value)
32 : inner_(new Inner(key, old_value, new_value)) {}
34 ValueStoreChange::~ValueStoreChange() {}
36 const std::string& ValueStoreChange::key() const {
37 DCHECK(inner_.get());
38 return inner_->key_;
41 const Value* ValueStoreChange::old_value() const {
42 DCHECK(inner_.get());
43 return inner_->old_value_.get();
46 const Value* ValueStoreChange::new_value() const {
47 DCHECK(inner_.get());
48 return inner_->new_value_.get();
51 ValueStoreChange::Inner::Inner(
52 const std::string& key, base::Value* old_value, base::Value* new_value)
53 : key_(key), old_value_(old_value), new_value_(new_value) {}
55 ValueStoreChange::Inner::~Inner() {}