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