| 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 "webkit/common/dom_storage/dom_storage_map.h" | 5 #include "webkit/common/dom_storage/dom_storage_map.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 quota_(quota) { | 32 quota_(quota) { |
| 33 ResetKeyIterator(); | 33 ResetKeyIterator(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 DomStorageMap::~DomStorageMap() {} | 36 DomStorageMap::~DomStorageMap() {} |
| 37 | 37 |
| 38 unsigned DomStorageMap::Length() const { | 38 unsigned DomStorageMap::Length() const { |
| 39 return values_.size(); | 39 return values_.size(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 NullableString16 DomStorageMap::Key(unsigned index) { | 42 base::NullableString16 DomStorageMap::Key(unsigned index) { |
| 43 if (index >= values_.size()) | 43 if (index >= values_.size()) |
| 44 return NullableString16(true); | 44 return base::NullableString16(true); |
| 45 while (last_key_index_ != index) { | 45 while (last_key_index_ != index) { |
| 46 if (last_key_index_ > index) { | 46 if (last_key_index_ > index) { |
| 47 --key_iterator_; | 47 --key_iterator_; |
| 48 --last_key_index_; | 48 --last_key_index_; |
| 49 } else { | 49 } else { |
| 50 ++key_iterator_; | 50 ++key_iterator_; |
| 51 ++last_key_index_; | 51 ++last_key_index_; |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 return NullableString16(key_iterator_->first, false); | 54 return base::NullableString16(key_iterator_->first, false); |
| 55 } | 55 } |
| 56 | 56 |
| 57 NullableString16 DomStorageMap::GetItem(const base::string16& key) const { | 57 base::NullableString16 DomStorageMap::GetItem(const base::string16& key) const { |
| 58 ValuesMap::const_iterator found = values_.find(key); | 58 ValuesMap::const_iterator found = values_.find(key); |
| 59 if (found == values_.end()) | 59 if (found == values_.end()) |
| 60 return NullableString16(true); | 60 return base::NullableString16(true); |
| 61 return found->second; | 61 return found->second; |
| 62 } | 62 } |
| 63 | 63 |
| 64 bool DomStorageMap::SetItem( | 64 bool DomStorageMap::SetItem( |
| 65 const base::string16& key, const base::string16& value, | 65 const base::string16& key, const base::string16& value, |
| 66 NullableString16* old_value) { | 66 base::NullableString16* old_value) { |
| 67 ValuesMap::const_iterator found = values_.find(key); | 67 ValuesMap::const_iterator found = values_.find(key); |
| 68 if (found == values_.end()) | 68 if (found == values_.end()) |
| 69 *old_value = NullableString16(true); | 69 *old_value = base::NullableString16(true); |
| 70 else | 70 else |
| 71 *old_value = found->second; | 71 *old_value = found->second; |
| 72 | 72 |
| 73 size_t old_item_size = old_value->is_null() ? | 73 size_t old_item_size = old_value->is_null() ? |
| 74 0 : size_of_item(key, old_value->string()); | 74 0 : size_of_item(key, old_value->string()); |
| 75 size_t new_item_size = size_of_item(key, value); | 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; | 76 size_t new_bytes_used = bytes_used_ - old_item_size + new_item_size; |
| 77 | 77 |
| 78 // Only check quota if the size is increasing, this allows | 78 // Only check quota if the size is increasing, this allows |
| 79 // shrinking changes to pre-existing files that are over budget. | 79 // shrinking changes to pre-existing files that are over budget. |
| 80 if (new_item_size > old_item_size && new_bytes_used > quota_) | 80 if (new_item_size > old_item_size && new_bytes_used > quota_) |
| 81 return false; | 81 return false; |
| 82 | 82 |
| 83 values_[key] = NullableString16(value, false); | 83 values_[key] = base::NullableString16(value, false); |
| 84 ResetKeyIterator(); | 84 ResetKeyIterator(); |
| 85 bytes_used_ = new_bytes_used; | 85 bytes_used_ = new_bytes_used; |
| 86 return true; | 86 return true; |
| 87 } | 87 } |
| 88 | 88 |
| 89 bool DomStorageMap::RemoveItem( | 89 bool DomStorageMap::RemoveItem( |
| 90 const base::string16& key, | 90 const base::string16& key, |
| 91 base::string16* old_value) { | 91 base::string16* old_value) { |
| 92 ValuesMap::iterator found = values_.find(key); | 92 ValuesMap::iterator found = values_.find(key); |
| 93 if (found == values_.end()) | 93 if (found == values_.end()) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 113 copy->ResetKeyIterator(); | 113 copy->ResetKeyIterator(); |
| 114 return copy; | 114 return copy; |
| 115 } | 115 } |
| 116 | 116 |
| 117 void DomStorageMap::ResetKeyIterator() { | 117 void DomStorageMap::ResetKeyIterator() { |
| 118 key_iterator_ = values_.begin(); | 118 key_iterator_ = values_.begin(); |
| 119 last_key_index_ = 0; | 119 last_key_index_ = 0; |
| 120 } | 120 } |
| 121 | 121 |
| 122 } // namespace dom_storage | 122 } // namespace dom_storage |
| OLD | NEW |