OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/settings/in_memory_settings_storage.h" | 5 #include "chrome/browser/extensions/settings/testing_settings_storage.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace extensions { | 9 namespace extensions { |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
| 13 const char* kGenericErrorMessage = "TestingSettingsStorage configured to error"; |
| 14 |
| 15 SettingsStorage::ReadResult ReadResultError() { |
| 16 return SettingsStorage::ReadResult(kGenericErrorMessage); |
| 17 } |
| 18 |
| 19 SettingsStorage::WriteResult WriteResultError() { |
| 20 return SettingsStorage::WriteResult(kGenericErrorMessage); |
| 21 } |
| 22 |
13 std::vector<std::string> CreateVector(const std::string& string) { | 23 std::vector<std::string> CreateVector(const std::string& string) { |
14 std::vector<std::string> strings; | 24 std::vector<std::string> strings; |
15 strings.push_back(string); | 25 strings.push_back(string); |
16 return strings; | 26 return strings; |
17 } | 27 } |
18 | 28 |
19 } // namespace | 29 } // namespace |
20 | 30 |
21 SettingsStorage::ReadResult InMemorySettingsStorage::Get( | 31 TestingSettingsStorage::TestingSettingsStorage() |
| 32 : fail_all_requests_(false) {} |
| 33 |
| 34 TestingSettingsStorage::~TestingSettingsStorage() {} |
| 35 |
| 36 void TestingSettingsStorage::SetFailAllRequests(bool fail_all_requests) { |
| 37 fail_all_requests_ = fail_all_requests; |
| 38 } |
| 39 |
| 40 SettingsStorage::ReadResult TestingSettingsStorage::Get( |
22 const std::string& key) { | 41 const std::string& key) { |
23 return Get(CreateVector(key)); | 42 return Get(CreateVector(key)); |
24 } | 43 } |
25 | 44 |
26 SettingsStorage::ReadResult InMemorySettingsStorage::Get( | 45 SettingsStorage::ReadResult TestingSettingsStorage::Get( |
27 const std::vector<std::string>& keys) { | 46 const std::vector<std::string>& keys) { |
| 47 if (fail_all_requests_) { |
| 48 return ReadResultError(); |
| 49 } |
| 50 |
28 DictionaryValue* settings = new DictionaryValue(); | 51 DictionaryValue* settings = new DictionaryValue(); |
29 for (std::vector<std::string>::const_iterator it = keys.begin(); | 52 for (std::vector<std::string>::const_iterator it = keys.begin(); |
30 it != keys.end(); ++it) { | 53 it != keys.end(); ++it) { |
31 Value* value = NULL; | 54 Value* value = NULL; |
32 if (storage_.GetWithoutPathExpansion(*it, &value)) { | 55 if (storage_.GetWithoutPathExpansion(*it, &value)) { |
33 settings->SetWithoutPathExpansion(*it, value->DeepCopy()); | 56 settings->SetWithoutPathExpansion(*it, value->DeepCopy()); |
34 } | 57 } |
35 } | 58 } |
36 return ReadResult(settings); | 59 return ReadResult(settings); |
37 } | 60 } |
38 | 61 |
39 SettingsStorage::ReadResult InMemorySettingsStorage::Get() { | 62 SettingsStorage::ReadResult TestingSettingsStorage::Get() { |
| 63 if (fail_all_requests_) { |
| 64 return ReadResultError(); |
| 65 } |
40 return ReadResult(storage_.DeepCopy()); | 66 return ReadResult(storage_.DeepCopy()); |
41 } | 67 } |
42 | 68 |
43 SettingsStorage::WriteResult InMemorySettingsStorage::Set( | 69 SettingsStorage::WriteResult TestingSettingsStorage::Set( |
44 const std::string& key, const Value& value) { | 70 const std::string& key, const Value& value) { |
45 DictionaryValue settings; | 71 DictionaryValue settings; |
46 settings.SetWithoutPathExpansion(key, value.DeepCopy()); | 72 settings.SetWithoutPathExpansion(key, value.DeepCopy()); |
47 return Set(settings); | 73 return Set(settings); |
48 } | 74 } |
49 | 75 |
50 SettingsStorage::WriteResult InMemorySettingsStorage::Set( | 76 SettingsStorage::WriteResult TestingSettingsStorage::Set( |
51 const DictionaryValue& settings) { | 77 const DictionaryValue& settings) { |
52 scoped_ptr<SettingChangeList> changes( | 78 if (fail_all_requests_) { |
53 new SettingChangeList()); | 79 return WriteResultError(); |
| 80 } |
| 81 |
| 82 scoped_ptr<SettingChangeList> changes(new SettingChangeList()); |
54 for (DictionaryValue::Iterator it(settings); it.HasNext(); it.Advance()) { | 83 for (DictionaryValue::Iterator it(settings); it.HasNext(); it.Advance()) { |
55 Value* old_value = NULL; | 84 Value* old_value = NULL; |
56 storage_.GetWithoutPathExpansion(it.key(), &old_value); | 85 storage_.GetWithoutPathExpansion(it.key(), &old_value); |
57 if (!old_value || !old_value->Equals(&it.value())) { | 86 if (!old_value || !old_value->Equals(&it.value())) { |
58 changes->push_back( | 87 changes->push_back( |
59 SettingChange( | 88 SettingChange( |
60 it.key(), | 89 it.key(), |
61 old_value ? old_value->DeepCopy() : old_value, | 90 old_value ? old_value->DeepCopy() : old_value, |
62 it.value().DeepCopy())); | 91 it.value().DeepCopy())); |
63 storage_.SetWithoutPathExpansion(it.key(), it.value().DeepCopy()); | 92 storage_.SetWithoutPathExpansion(it.key(), it.value().DeepCopy()); |
64 } | 93 } |
65 } | 94 } |
66 return WriteResult(changes.release()); | 95 return WriteResult(changes.release()); |
67 } | 96 } |
68 | 97 |
69 SettingsStorage::WriteResult InMemorySettingsStorage::Remove( | 98 SettingsStorage::WriteResult TestingSettingsStorage::Remove( |
70 const std::string& key) { | 99 const std::string& key) { |
71 return Remove(CreateVector(key)); | 100 return Remove(CreateVector(key)); |
72 } | 101 } |
73 | 102 |
74 SettingsStorage::WriteResult InMemorySettingsStorage::Remove( | 103 SettingsStorage::WriteResult TestingSettingsStorage::Remove( |
75 const std::vector<std::string>& keys) { | 104 const std::vector<std::string>& keys) { |
| 105 if (fail_all_requests_) { |
| 106 return WriteResultError(); |
| 107 } |
| 108 |
76 scoped_ptr<SettingChangeList> changes( | 109 scoped_ptr<SettingChangeList> changes( |
77 new SettingChangeList()); | 110 new SettingChangeList()); |
78 for (std::vector<std::string>::const_iterator it = keys.begin(); | 111 for (std::vector<std::string>::const_iterator it = keys.begin(); |
79 it != keys.end(); ++it) { | 112 it != keys.end(); ++it) { |
80 Value* old_value = NULL; | 113 Value* old_value = NULL; |
81 if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) { | 114 if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) { |
82 changes->push_back(SettingChange(*it, old_value, NULL)); | 115 changes->push_back(SettingChange(*it, old_value, NULL)); |
83 } | 116 } |
84 } | 117 } |
85 return WriteResult(changes.release()); | 118 return WriteResult(changes.release()); |
86 } | 119 } |
87 | 120 |
88 SettingsStorage::WriteResult | 121 SettingsStorage::WriteResult TestingSettingsStorage::Clear() { |
89 InMemorySettingsStorage::Clear() { | 122 if (fail_all_requests_) { |
| 123 return WriteResultError(); |
| 124 } |
| 125 |
90 std::vector<std::string> keys; | 126 std::vector<std::string> keys; |
91 for (DictionaryValue::Iterator it(storage_); it.HasNext(); it.Advance()) { | 127 for (DictionaryValue::Iterator it(storage_); it.HasNext(); it.Advance()) { |
92 keys.push_back(it.key()); | 128 keys.push_back(it.key()); |
93 } | 129 } |
94 return Remove(keys); | 130 return Remove(keys); |
95 } | 131 } |
96 | 132 |
97 } // namespace extensions | 133 } // namespace extensions |
OLD | NEW |