We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / content / common / dom_storage / dom_storage_map.cc
blob71368bdd55039ddc76278e7b5706e30517ab8aae
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"
9 namespace content {
11 namespace {
13 size_t size_of_item(const base::string16& key, const base::string16& value) {
14 return (key.length() + value.length()) * sizeof(base::char16);
17 } // namespace
19 DOMStorageMap::DOMStorageMap(size_t quota)
20 : bytes_used_(0),
21 quota_(quota) {
22 ResetKeyIterator();
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) {
36 --key_iterator_;
37 --last_key_index_;
38 } else {
39 ++key_iterator_;
40 ++last_key_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();
50 return found->second;
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();
59 else
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_)
70 return false;
72 values_[key] = base::NullableString16(value, false);
73 ResetKeyIterator();
74 bytes_used_ = new_bytes_used;
75 return true;
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())
83 return false;
84 *old_value = found->second.string();
85 values_.erase(found);
86 ResetKeyIterator();
87 bytes_used_ -= size_of_item(key, *old_value);
88 return true;
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_);
95 ResetKeyIterator();
98 DOMStorageMap* DOMStorageMap::DeepCopy() const {
99 DOMStorageMap* copy = new DOMStorageMap(quota_);
100 copy->values_ = values_;
101 copy->bytes_used_ = bytes_used_;
102 copy->ResetKeyIterator();
103 return copy;
106 void DOMStorageMap::ResetKeyIterator() {
107 key_iterator_ = values_.begin();
108 last_key_index_ = 0;
111 size_t DOMStorageMap::CountBytes(const DOMStorageValuesMap& values) {
112 if (values.empty())
113 return 0;
115 size_t count = 0;
116 for (const auto& pair : values)
117 count += size_of_item(pair.first, pair.second.string());
118 return count;
121 } // namespace content