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
);
19 DOMStorageMap::DOMStorageMap(size_t quota
)
25 DOMStorageMap::~DOMStorageMap() {}
27 unsigned DOMStorageMap::Length() const {
28 return values_
.size();
31 base::NullableString16
DOMStorageMap::Key(unsigned index
) {
32 if (index
>= values_
.size())
33 return base::NullableString16();
34 while (last_key_index_
!= index
) {
35 if (last_key_index_
> index
) {
43 return base::NullableString16(key_iterator_
->first
, false);
46 base::NullableString16
DOMStorageMap::GetItem(const base::string16
& key
) const {
47 DOMStorageValuesMap::const_iterator found
= values_
.find(key
);
48 if (found
== values_
.end())
49 return base::NullableString16();
53 bool DOMStorageMap::SetItem(
54 const base::string16
& key
, const base::string16
& value
,
55 base::NullableString16
* old_value
) {
56 DOMStorageValuesMap::const_iterator found
= values_
.find(key
);
57 if (found
== values_
.end())
58 *old_value
= base::NullableString16();
60 *old_value
= found
->second
;
62 size_t old_item_size
= old_value
->is_null() ?
63 0 : size_of_item(key
, old_value
->string());
64 size_t new_item_size
= size_of_item(key
, value
);
65 size_t new_bytes_used
= bytes_used_
- old_item_size
+ new_item_size
;
67 // Only check quota if the size is increasing, this allows
68 // shrinking changes to pre-existing files that are over budget.
69 if (new_item_size
> old_item_size
&& new_bytes_used
> quota_
)
72 values_
[key
] = base::NullableString16(value
, false);
74 bytes_used_
= new_bytes_used
;
78 bool DOMStorageMap::RemoveItem(
79 const base::string16
& key
,
80 base::string16
* old_value
) {
81 DOMStorageValuesMap::iterator found
= values_
.find(key
);
82 if (found
== values_
.end())
84 *old_value
= found
->second
.string();
87 bytes_used_
-= size_of_item(key
, *old_value
);
91 void DOMStorageMap::SwapValues(DOMStorageValuesMap
* values
) {
92 // Note: A pre-existing file may be over the quota budget.
93 values_
.swap(*values
);
94 bytes_used_
= CountBytes(values_
);
98 DOMStorageMap
* DOMStorageMap::DeepCopy() const {
99 DOMStorageMap
* copy
= new DOMStorageMap(quota_
);
100 copy
->values_
= values_
;
101 copy
->bytes_used_
= bytes_used_
;
102 copy
->ResetKeyIterator();
106 void DOMStorageMap::ResetKeyIterator() {
107 key_iterator_
= values_
.begin();
111 size_t DOMStorageMap::CountBytes(const DOMStorageValuesMap
& values
) {
116 for (const auto& pair
: values
)
117 count
+= size_of_item(pair
.first
, pair
.second
.string());
121 } // namespace content