Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / dom_storage / dom_storage_map.cc
blobfa8eb0945b54c093f68046a3b9e687095afa6b1b
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
3 // found in the LICENSE file.
5 #include "webkit/dom_storage/dom_storage_map.h"
7 #include "base/logging.h"
9 namespace {
11 size_t size_of_item(const base::string16& key, const base::string16& value) {
12 return (key.length() + value.length()) * sizeof(char16);
15 size_t CountBytes(const dom_storage::ValuesMap& values) {
16 if (values.size() == 0)
17 return 0;
19 size_t count = 0;
20 dom_storage::ValuesMap::const_iterator it = values.begin();
21 for (; it != values.end(); ++it)
22 count += size_of_item(it->first, it->second.string());
23 return count;
26 } // namespace
28 namespace dom_storage {
30 DomStorageMap::DomStorageMap(size_t quota)
31 : bytes_used_(0),
32 quota_(quota) {
33 ResetKeyIterator();
36 DomStorageMap::~DomStorageMap() {}
38 unsigned DomStorageMap::Length() const {
39 return values_.size();
42 NullableString16 DomStorageMap::Key(unsigned index) {
43 if (index >= values_.size())
44 return NullableString16(true);
45 while (last_key_index_ != index) {
46 if (last_key_index_ > index) {
47 --key_iterator_;
48 --last_key_index_;
49 } else {
50 ++key_iterator_;
51 ++last_key_index_;
54 return NullableString16(key_iterator_->first, false);
57 NullableString16 DomStorageMap::GetItem(const base::string16& key) const {
58 ValuesMap::const_iterator found = values_.find(key);
59 if (found == values_.end())
60 return NullableString16(true);
61 return found->second;
64 bool DomStorageMap::SetItem(
65 const base::string16& key, const base::string16& value,
66 NullableString16* old_value) {
67 ValuesMap::const_iterator found = values_.find(key);
68 if (found == values_.end())
69 *old_value = NullableString16(true);
70 else
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_)
81 return false;
83 values_[key] = NullableString16(value, false);
84 ResetKeyIterator();
85 bytes_used_ = new_bytes_used;
86 return true;
89 bool DomStorageMap::RemoveItem(
90 const base::string16& key,
91 base::string16* old_value) {
92 ValuesMap::iterator found = values_.find(key);
93 if (found == values_.end())
94 return false;
95 *old_value = found->second.string();
96 values_.erase(found);
97 ResetKeyIterator();
98 bytes_used_ -= size_of_item(key, *old_value);
99 return true;
102 void DomStorageMap::SwapValues(ValuesMap* values) {
103 // Note: A pre-existing file may be over the quota budget.
104 values_.swap(*values);
105 bytes_used_ = CountBytes(values_);
106 ResetKeyIterator();
109 DomStorageMap* DomStorageMap::DeepCopy() const {
110 DomStorageMap* copy = new DomStorageMap(quota_);
111 copy->values_ = values_;
112 copy->bytes_used_ = bytes_used_;
113 copy->ResetKeyIterator();
114 return copy;
117 void DomStorageMap::ResetKeyIterator() {
118 key_iterator_ = values_.begin();
119 last_key_index_ = 0;
122 } // namespace dom_storage