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/settings_storage_quota_enforcer.h" | 5 #include "chrome/browser/extensions/settings/settings_storage_quota_enforcer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 maybe_settings.error(); | 91 maybe_settings.error(); |
92 return; | 92 return; |
93 } | 93 } |
94 | 94 |
95 for (DictionaryValue::Iterator it(maybe_settings.settings()); it.HasNext(); | 95 for (DictionaryValue::Iterator it(maybe_settings.settings()); it.HasNext(); |
96 it.Advance()) { | 96 it.Advance()) { |
97 Allocate(it.key(), it.value(), &used_total_, &used_per_setting_); | 97 Allocate(it.key(), it.value(), &used_total_, &used_per_setting_); |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 SettingsStorageQuotaEnforcer::~SettingsStorageQuotaEnforcer( | 101 SettingsStorageQuotaEnforcer::~SettingsStorageQuotaEnforcer() {} |
102 ) {} | |
103 | 102 |
104 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get( | 103 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get( |
105 const std::string& key) { | 104 const std::string& key) { |
106 return delegate_->Get(key); | 105 return delegate_->Get(key); |
107 } | 106 } |
108 | 107 |
109 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get( | 108 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get( |
110 const std::vector<std::string>& keys) { | 109 const std::vector<std::string>& keys) { |
111 return delegate_->Get(keys); | 110 return delegate_->Get(keys); |
112 } | 111 } |
113 | 112 |
114 SettingsStorage::ReadResult | 113 SettingsStorage::ReadResult SettingsStorageQuotaEnforcer::Get() { |
115 SettingsStorageQuotaEnforcer::Get() { | |
116 return delegate_->Get(); | 114 return delegate_->Get(); |
117 } | 115 } |
118 | 116 |
119 SettingsStorage::WriteResult | 117 SettingsStorage::WriteResult SettingsStorageQuotaEnforcer::Set( |
120 SettingsStorageQuotaEnforcer::Set( | 118 WriteOptions options, const std::string& key, const Value& value) { |
121 const std::string& key, const Value& value) { | |
122 size_t new_used_total = used_total_; | 119 size_t new_used_total = used_total_; |
123 std::map<std::string, size_t> new_used_per_setting = used_per_setting_; | 120 std::map<std::string, size_t> new_used_per_setting = used_per_setting_; |
124 Allocate(key, value, &new_used_total, &new_used_per_setting); | 121 Allocate(key, value, &new_used_total, &new_used_per_setting); |
125 | 122 |
126 if (new_used_total > quota_bytes_) { | 123 if (options != FORCE) { |
127 return QuotaExceededFor(TOTAL_BYTES); | 124 if (new_used_total > quota_bytes_) { |
128 } | 125 return QuotaExceededFor(TOTAL_BYTES); |
129 if (new_used_per_setting[key] > quota_bytes_per_setting_) { | 126 } |
130 return QuotaExceededFor(BYTES_PER_SETTING); | 127 if (new_used_per_setting[key] > quota_bytes_per_setting_) { |
131 } | 128 return QuotaExceededFor(BYTES_PER_SETTING); |
132 if (new_used_per_setting.size() > max_keys_) { | 129 } |
133 return QuotaExceededFor(KEY_COUNT); | 130 if (new_used_per_setting.size() > max_keys_) { |
| 131 return QuotaExceededFor(KEY_COUNT); |
| 132 } |
134 } | 133 } |
135 | 134 |
136 WriteResult result = delegate_->Set(key, value); | 135 WriteResult result = delegate_->Set(options, key, value); |
137 if (result.HasError()) { | 136 if (result.HasError()) { |
138 return result; | 137 return result; |
139 } | 138 } |
140 | 139 |
141 used_total_ = new_used_total; | 140 used_total_ = new_used_total; |
142 used_per_setting_.swap(new_used_per_setting); | 141 used_per_setting_.swap(new_used_per_setting); |
143 return result; | 142 return result; |
144 } | 143 } |
145 | 144 |
146 SettingsStorage::WriteResult | 145 SettingsStorage::WriteResult SettingsStorageQuotaEnforcer::Set( |
147 SettingsStorageQuotaEnforcer::Set( | 146 WriteOptions options, const DictionaryValue& values) { |
148 const DictionaryValue& values) { | |
149 size_t new_used_total = used_total_; | 147 size_t new_used_total = used_total_; |
150 std::map<std::string, size_t> new_used_per_setting = used_per_setting_; | 148 std::map<std::string, size_t> new_used_per_setting = used_per_setting_; |
151 for (DictionaryValue::Iterator it(values); it.HasNext(); it.Advance()) { | 149 for (DictionaryValue::Iterator it(values); it.HasNext(); it.Advance()) { |
152 Allocate(it.key(), it.value(), &new_used_total, &new_used_per_setting); | 150 Allocate(it.key(), it.value(), &new_used_total, &new_used_per_setting); |
153 if (new_used_per_setting[it.key()] > quota_bytes_per_setting_) { | 151 |
| 152 if (options != FORCE && |
| 153 new_used_per_setting[it.key()] > quota_bytes_per_setting_) { |
154 return QuotaExceededFor(BYTES_PER_SETTING); | 154 return QuotaExceededFor(BYTES_PER_SETTING); |
155 } | 155 } |
156 } | 156 } |
157 | 157 |
158 if (new_used_total > quota_bytes_) { | 158 if (options != FORCE) { |
159 return QuotaExceededFor(TOTAL_BYTES); | 159 if (new_used_total > quota_bytes_) { |
160 } | 160 return QuotaExceededFor(TOTAL_BYTES); |
161 if (new_used_per_setting.size() > max_keys_) { | 161 } |
162 return QuotaExceededFor(KEY_COUNT); | 162 if (new_used_per_setting.size() > max_keys_) { |
| 163 return QuotaExceededFor(KEY_COUNT); |
| 164 } |
163 } | 165 } |
164 | 166 |
165 WriteResult result = delegate_->Set(values); | 167 WriteResult result = delegate_->Set(options, values); |
166 if (result.HasError()) { | 168 if (result.HasError()) { |
167 return result; | 169 return result; |
168 } | 170 } |
169 | 171 |
170 used_total_ = new_used_total; | 172 used_total_ = new_used_total; |
171 used_per_setting_ = new_used_per_setting; | 173 used_per_setting_ = new_used_per_setting; |
172 return result; | 174 return result; |
173 } | 175 } |
174 | 176 |
175 SettingsStorage::WriteResult | 177 SettingsStorage::WriteResult SettingsStorageQuotaEnforcer::Remove( |
176 SettingsStorageQuotaEnforcer::Remove( | |
177 const std::string& key) { | 178 const std::string& key) { |
178 WriteResult result = delegate_->Remove(key); | 179 WriteResult result = delegate_->Remove(key); |
179 if (result.HasError()) { | 180 if (result.HasError()) { |
180 return result; | 181 return result; |
181 } | 182 } |
182 Free(&used_total_, &used_per_setting_, key); | 183 Free(&used_total_, &used_per_setting_, key); |
183 return result; | 184 return result; |
184 } | 185 } |
185 | 186 |
186 SettingsStorage::WriteResult | 187 SettingsStorage::WriteResult SettingsStorageQuotaEnforcer::Remove( |
187 SettingsStorageQuotaEnforcer::Remove( | |
188 const std::vector<std::string>& keys) { | 188 const std::vector<std::string>& keys) { |
189 WriteResult result = delegate_->Remove(keys); | 189 WriteResult result = delegate_->Remove(keys); |
190 if (result.HasError()) { | 190 if (result.HasError()) { |
191 return result; | 191 return result; |
192 } | 192 } |
193 | 193 |
194 for (std::vector<std::string>::const_iterator it = keys.begin(); | 194 for (std::vector<std::string>::const_iterator it = keys.begin(); |
195 it != keys.end(); ++it) { | 195 it != keys.end(); ++it) { |
196 Free(&used_total_, &used_per_setting_, *it); | 196 Free(&used_total_, &used_per_setting_, *it); |
197 } | 197 } |
198 return result; | 198 return result; |
199 } | 199 } |
200 | 200 |
201 SettingsStorage::WriteResult | 201 SettingsStorage::WriteResult SettingsStorageQuotaEnforcer::Clear() { |
202 SettingsStorageQuotaEnforcer::Clear( | |
203 ) { | |
204 WriteResult result = delegate_->Clear(); | 202 WriteResult result = delegate_->Clear(); |
205 if (result.HasError()) { | 203 if (result.HasError()) { |
206 return result; | 204 return result; |
207 } | 205 } |
208 | 206 |
209 while (!used_per_setting_.empty()) { | 207 while (!used_per_setting_.empty()) { |
210 Free(&used_total_, &used_per_setting_, used_per_setting_.begin()->first); | 208 Free(&used_total_, &used_per_setting_, used_per_setting_.begin()->first); |
211 } | 209 } |
212 return result; | 210 return result; |
213 } | 211 } |
214 | 212 |
215 } // namespace extensions | 213 } // namespace extensions |
OLD | NEW |