1 // Copyright 2018 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. 4 5 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_STORAGE_STORAGE_AREA_MAP_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_STORAGE_STORAGE_AREA_MAP_H_ 7 8 #include "third_party/blink/renderer/modules/modules_export.h" 9 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h" 10 #include "third_party/blink/renderer/platform/wtf/text/string_hash.h" 11 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" 12 13 namespace blink { 14 15 // This class is used to represent the in-memory version of the data in a 16 // StorageArea. This class is responsible for enforcing a quota, and for 17 // providing somewhat efficient iteration over all the items in the map via 18 // GetLength/GetKey/GetItem. 19 // Any modifications to the data in the map can cause all items to be reordered. 20 // Nothing is guaranteed about the order of items in the map. 21 // For the purpose of quota each character in the key and value strings is 22 // counted as two bytes, even if the actual in-memory representation of the 23 // string only uses one byte per character. 24 class MODULES_EXPORT StorageAreaMap { 25 USING_FAST_MALLOC(StorageAreaMap); 26 27 public: 28 explicit StorageAreaMap(size_t quota); 29 30 unsigned GetLength() const; 31 String GetKey(unsigned index) const; 32 String GetItem(const String& key) const; 33 34 // Returns false iff quota would be exceeded. 35 bool SetItem(const String& key, const String& value, String* old_value); 36 void SetItemIgnoringQuota(const String& key, const String& value); 37 // Returns fals iff item wasn't found. 38 bool RemoveItem(const String& key, String* old_value); 39 quota_used()40 size_t quota_used() const { return quota_used_; } memory_used()41 size_t memory_used() const { return memory_used_; } quota()42 size_t quota() const { return quota_; } 43 44 private: 45 void ResetKeyIterator() const; 46 bool SetItemInternal(const String& key, 47 const String& value, 48 String* old_value, 49 bool check_quota); 50 51 HashMap<String, String> keys_values_; 52 53 // To make iterating over all keys somewhat less inefficient, we keep track of 54 // an iterator to and index of the last key returned by GetKey(). 55 mutable HashMap<String, String>::const_iterator key_iterator_; 56 mutable unsigned last_key_index_; 57 58 size_t quota_used_ = 0; 59 size_t memory_used_ = 0; 60 const size_t quota_; 61 }; 62 63 } // namespace blink 64 65 #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_STORAGE_STORAGE_AREA_MAP_H_ 66