summaryrefslogtreecommitdiffstats
path: root/chrome/browser/value_store/testing_value_store.cc
blob: 594e0bdd14b192b11ec86516b0642992ea0c0eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 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/browser/value_store/testing_value_store.h"

#include "base/logging.h"

namespace {

const char kGenericErrorMessage[] = "TestingValueStore configured to error";

}  // namespace

TestingValueStore::TestingValueStore()
    : read_count_(0), write_count_(0), error_code_(OK) {}

TestingValueStore::~TestingValueStore() {}

size_t TestingValueStore::GetBytesInUse(const std::string& key) {
  // Let SettingsStorageQuotaEnforcer implement this.
  NOTREACHED() << "Not implemented";
  return 0;
}

size_t TestingValueStore::GetBytesInUse(
    const std::vector<std::string>& keys) {
  // Let SettingsStorageQuotaEnforcer implement this.
  NOTREACHED() << "Not implemented";
  return 0;
}

size_t TestingValueStore::GetBytesInUse() {
  // Let SettingsStorageQuotaEnforcer implement this.
  NOTREACHED() << "Not implemented";
  return 0;
}

ValueStore::ReadResult TestingValueStore::Get(const std::string& key) {
  return Get(std::vector<std::string>(1, key));
}

ValueStore::ReadResult TestingValueStore::Get(
    const std::vector<std::string>& keys) {
  read_count_++;
  if (error_code_ != OK)
    return MakeReadResult(TestingError());

  DictionaryValue* settings = new DictionaryValue();
  for (std::vector<std::string>::const_iterator it = keys.begin();
      it != keys.end(); ++it) {
    Value* value = NULL;
    if (storage_.GetWithoutPathExpansion(*it, &value)) {
      settings->SetWithoutPathExpansion(*it, value->DeepCopy());
    }
  }
  return MakeReadResult(make_scoped_ptr(settings));
}

ValueStore::ReadResult TestingValueStore::Get() {
  read_count_++;
  if (error_code_ != OK)
    return MakeReadResult(TestingError());
  return MakeReadResult(make_scoped_ptr(storage_.DeepCopy()));
}

ValueStore::WriteResult TestingValueStore::Set(
    WriteOptions options, const std::string& key, const Value& value) {
  DictionaryValue settings;
  settings.SetWithoutPathExpansion(key, value.DeepCopy());
  return Set(options, settings);
}

ValueStore::WriteResult TestingValueStore::Set(
    WriteOptions options, const DictionaryValue& settings) {
  write_count_++;
  if (error_code_ != OK)
    return MakeWriteResult(TestingError());

  scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
  for (DictionaryValue::Iterator it(settings); !it.IsAtEnd(); it.Advance()) {
    Value* old_value = NULL;
    if (!storage_.GetWithoutPathExpansion(it.key(), &old_value) ||
        !old_value->Equals(&it.value())) {
      changes->push_back(
          ValueStoreChange(
              it.key(),
              old_value ? old_value->DeepCopy() : old_value,
              it.value().DeepCopy()));
      storage_.SetWithoutPathExpansion(it.key(), it.value().DeepCopy());
    }
  }
  return MakeWriteResult(changes.Pass());
}

ValueStore::WriteResult TestingValueStore::Remove(const std::string& key) {
  return Remove(std::vector<std::string>(1, key));
}

ValueStore::WriteResult TestingValueStore::Remove(
    const std::vector<std::string>& keys) {
  write_count_++;
  if (error_code_ != OK)
    return MakeWriteResult(TestingError());

  scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
  for (std::vector<std::string>::const_iterator it = keys.begin();
      it != keys.end(); ++it) {
    scoped_ptr<Value> old_value;
    if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) {
      changes->push_back(ValueStoreChange(*it, old_value.release(), NULL));
    }
  }
  return MakeWriteResult(changes.Pass());
}

ValueStore::WriteResult TestingValueStore::Clear() {
  std::vector<std::string> keys;
  for (DictionaryValue::Iterator it(storage_); !it.IsAtEnd(); it.Advance()) {
    keys.push_back(it.key());
  }
  return Remove(keys);
}

scoped_ptr<ValueStore::Error> TestingValueStore::TestingError() {
  return make_scoped_ptr(new ValueStore::Error(
      error_code_, kGenericErrorMessage, scoped_ptr<std::string>()));
}