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/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 17 matching lines...) Expand all Loading... |
28 void Allocate( | 28 void Allocate( |
29 const std::string& key, | 29 const std::string& key, |
30 const Value& value, | 30 const Value& value, |
31 size_t* used_total, | 31 size_t* used_total, |
32 std::map<std::string, size_t>* used_per_setting) { | 32 std::map<std::string, size_t>* used_per_setting) { |
33 // Calculate the setting size based on its JSON serialization size. | 33 // Calculate the setting size based on its JSON serialization size. |
34 // TODO(kalman): Does this work with different encodings? | 34 // TODO(kalman): Does this work with different encodings? |
35 // TODO(kalman): This is duplicating work that the leveldb delegate | 35 // TODO(kalman): This is duplicating work that the leveldb delegate |
36 // implementation is about to do, and it would be nice to avoid this. | 36 // implementation is about to do, and it would be nice to avoid this. |
37 std::string value_as_json; | 37 std::string value_as_json; |
38 base::JSONWriter::Write(&value, false, &value_as_json); | 38 base::JSONWriter::Write(&value, &value_as_json); |
39 size_t new_size = key.size() + value_as_json.size(); | 39 size_t new_size = key.size() + value_as_json.size(); |
40 size_t existing_size = (*used_per_setting)[key]; | 40 size_t existing_size = (*used_per_setting)[key]; |
41 | 41 |
42 *used_total += (new_size - existing_size); | 42 *used_total += (new_size - existing_size); |
43 (*used_per_setting)[key] = new_size; | 43 (*used_per_setting)[key] = new_size; |
44 } | 44 } |
45 | 45 |
46 // Frees the allocation of a setting in a record of total and per-setting usage. | 46 // Frees the allocation of a setting in a record of total and per-setting usage. |
47 void Free( | 47 void Free( |
48 size_t* used_total, | 48 size_t* used_total, |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 return result; | 220 return result; |
221 } | 221 } |
222 | 222 |
223 while (!used_per_setting_.empty()) { | 223 while (!used_per_setting_.empty()) { |
224 Free(&used_total_, &used_per_setting_, used_per_setting_.begin()->first); | 224 Free(&used_total_, &used_per_setting_, used_per_setting_.begin()->first); |
225 } | 225 } |
226 return result; | 226 return result; |
227 } | 227 } |
228 | 228 |
229 } // namespace extensions | 229 } // namespace extensions |
OLD | NEW |