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