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.
4
5#include "chrome/browser/value_store/testing_value_store.h"
6
7#include "base/logging.h"
8
9namespace {
10
11const char kGenericErrorMessage[] = "TestingValueStore configured to error";
12
13}  // namespace
14
15TestingValueStore::TestingValueStore()
16    : read_count_(0), write_count_(0), error_code_(OK) {}
17
18TestingValueStore::~TestingValueStore() {}
19
20size_t TestingValueStore::GetBytesInUse(const std::string& key) {
21  // Let SettingsStorageQuotaEnforcer implement this.
22  NOTREACHED() << "Not implemented";
23  return 0;
24}
25
26size_t TestingValueStore::GetBytesInUse(
27    const std::vector<std::string>& keys) {
28  // Let SettingsStorageQuotaEnforcer implement this.
29  NOTREACHED() << "Not implemented";
30  return 0;
31}
32
33size_t TestingValueStore::GetBytesInUse() {
34  // Let SettingsStorageQuotaEnforcer implement this.
35  NOTREACHED() << "Not implemented";
36  return 0;
37}
38
39ValueStore::ReadResult TestingValueStore::Get(const std::string& key) {
40  return Get(std::vector<std::string>(1, key));
41}
42
43ValueStore::ReadResult TestingValueStore::Get(
44    const std::vector<std::string>& keys) {
45  read_count_++;
46  if (error_code_ != OK)
47    return MakeReadResult(TestingError());
48
49  DictionaryValue* settings = new DictionaryValue();
50  for (std::vector<std::string>::const_iterator it = keys.begin();
51      it != keys.end(); ++it) {
52    Value* value = NULL;
53    if (storage_.GetWithoutPathExpansion(*it, &value)) {
54      settings->SetWithoutPathExpansion(*it, value->DeepCopy());
55    }
56  }
57  return MakeReadResult(make_scoped_ptr(settings));
58}
59
60ValueStore::ReadResult TestingValueStore::Get() {
61  read_count_++;
62  if (error_code_ != OK)
63    return MakeReadResult(TestingError());
64  return MakeReadResult(make_scoped_ptr(storage_.DeepCopy()));
65}
66
67ValueStore::WriteResult TestingValueStore::Set(
68    WriteOptions options, const std::string& key, const Value& value) {
69  DictionaryValue settings;
70  settings.SetWithoutPathExpansion(key, value.DeepCopy());
71  return Set(options, settings);
72}
73
74ValueStore::WriteResult TestingValueStore::Set(
75    WriteOptions options, const DictionaryValue& settings) {
76  write_count_++;
77  if (error_code_ != OK)
78    return MakeWriteResult(TestingError());
79
80  scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
81  for (DictionaryValue::Iterator it(settings); !it.IsAtEnd(); it.Advance()) {
82    Value* old_value = NULL;
83    if (!storage_.GetWithoutPathExpansion(it.key(), &old_value) ||
84        !old_value->Equals(&it.value())) {
85      changes->push_back(
86          ValueStoreChange(
87              it.key(),
88              old_value ? old_value->DeepCopy() : old_value,
89              it.value().DeepCopy()));
90      storage_.SetWithoutPathExpansion(it.key(), it.value().DeepCopy());
91    }
92  }
93  return MakeWriteResult(changes.Pass());
94}
95
96ValueStore::WriteResult TestingValueStore::Remove(const std::string& key) {
97  return Remove(std::vector<std::string>(1, key));
98}
99
100ValueStore::WriteResult TestingValueStore::Remove(
101    const std::vector<std::string>& keys) {
102  write_count_++;
103  if (error_code_ != OK)
104    return MakeWriteResult(TestingError());
105
106  scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
107  for (std::vector<std::string>::const_iterator it = keys.begin();
108      it != keys.end(); ++it) {
109    scoped_ptr<Value> old_value;
110    if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) {
111      changes->push_back(ValueStoreChange(*it, old_value.release(), NULL));
112    }
113  }
114  return MakeWriteResult(changes.Pass());
115}
116
117ValueStore::WriteResult TestingValueStore::Clear() {
118  std::vector<std::string> keys;
119  for (DictionaryValue::Iterator it(storage_); !it.IsAtEnd(); it.Advance()) {
120    keys.push_back(it.key());
121  }
122  return Remove(keys);
123}
124
125scoped_ptr<ValueStore::Error> TestingValueStore::TestingError() {
126  return make_scoped_ptr(new ValueStore::Error(
127      error_code_, kGenericErrorMessage, scoped_ptr<std::string>()));
128}
129