1 // Copyright 2013 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.
5 #include "content/common/dom_storage/dom_storage_map.h"
7 #include "base/logging.h"
13 size_t size_of_item(const base::string16
& key
, const base::string16
& value
) {
14 return (key
.length() + value
.length()) * sizeof(base::char16
);
17 size_t CountBytes(const DOMStorageValuesMap
& values
) {
18 if (values
.size() == 0)
22 DOMStorageValuesMap::const_iterator it
= values
.begin();
23 for (; it
!= values
.end(); ++it
)
24 count
+= size_of_item(it
->first
, it
->second
.string());
30 DOMStorageMap::DOMStorageMap(size_t quota
)
36 DOMStorageMap::~DOMStorageMap() {}
38 unsigned DOMStorageMap::Length() const {
39 return values_
.size();
42 base::NullableString16
DOMStorageMap::Key(unsigned index
) {
43 if (index
>= values_
.size())
44 return base::NullableString16();
45 while (last_key_index_
!= index
) {
46 if (last_key_index_
> index
) {
54 return base::NullableString16(key_iterator_
->first
, false);
57 base::NullableString16
DOMStorageMap::GetItem(const base::string16
& key
) const {
58 DOMStorageValuesMap::const_iterator found
= values_
.find(key
);
59 if (found
== values_
.end())
60 return base::NullableString16();
64 bool DOMStorageMap::SetItem(
65 const base::string16
& key
, const base::string16
& value
,
66 base::NullableString16
* old_value
) {
67 DOMStorageValuesMap::const_iterator found
= values_
.find(key
);
68 if (found
== values_
.end())
69 *old_value
= base::NullableString16();
71 *old_value
= found
->second
;
73 size_t old_item_size
= old_value
->is_null() ?
74 0 : size_of_item(key
, old_value
->string());
75 size_t new_item_size
= size_of_item(key
, value
);
76 size_t new_bytes_used
= bytes_used_
- old_item_size
+ new_item_size
;
78 // Only check quota if the size is increasing, this allows
79 // shrinking changes to pre-existing files that are over budget.
80 if (new_item_size
> old_item_size
&& new_bytes_used
> quota_
)
83 values_
[key
] = base::NullableString16(value
, false);
85 bytes_used_
= new_bytes_used
;
89 bool DOMStorageMap::RemoveItem(
90 const base::string16
& key
,
91 base::string16
* old_value
) {
92 DOMStorageValuesMap::iterator found
= values_
.find(key
);
93 if (found
== values_
.end())
95 *old_value
= found
->second
.string();
98 bytes_used_
-= size_of_item(key
, *old_value
);
102 void DOMStorageMap::SwapValues(DOMStorageValuesMap
* values
) {
103 // Note: A pre-existing file may be over the quota budget.
104 values_
.swap(*values
);
105 bytes_used_
= CountBytes(values_
);
109 DOMStorageMap
* DOMStorageMap::DeepCopy() const {
110 DOMStorageMap
* copy
= new DOMStorageMap(quota_
);
111 copy
->values_
= values_
;
112 copy
->bytes_used_
= bytes_used_
;
113 copy
->ResetKeyIterator();
117 void DOMStorageMap::ResetKeyIterator() {
118 key_iterator_
= values_
.begin();
122 } // namespace content