Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / common / dom_storage / dom_storage_map.h
blob7130d27a5c9edc731afda83850893d55b16b8589
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 #ifndef CONTENT_COMMON_DOM_STORAGE_DOM_STORAGE_MAP_H_
6 #define CONTENT_COMMON_DOM_STORAGE_DOM_STORAGE_MAP_H_
8 #include <map>
10 #include "base/memory/ref_counted.h"
11 #include "base/strings/nullable_string16.h"
12 #include "base/strings/string16.h"
13 #include "content/common/content_export.h"
14 #include "content/common/dom_storage/dom_storage_types.h"
16 namespace content {
18 // A wrapper around a std::map that adds refcounting and
19 // tracks the size in bytes of the keys/values, enforcing a quota.
20 // See class comments for DOMStorageContextImpl for a larger overview.
21 class CONTENT_EXPORT DOMStorageMap
22 : public base::RefCountedThreadSafe<DOMStorageMap> {
23 public:
24 explicit DOMStorageMap(size_t quota);
26 unsigned Length() const;
27 base::NullableString16 Key(unsigned index);
28 base::NullableString16 GetItem(const base::string16& key) const;
29 bool SetItem(const base::string16& key, const base::string16& value,
30 base::NullableString16* old_value);
31 bool RemoveItem(const base::string16& key, base::string16* old_value);
33 // Swaps this instances values_ with |map|.
34 // Note: to grandfather in pre-existing files that are overbudget,
35 // this method does not do quota checking.
36 void SwapValues(DOMStorageValuesMap* map);
38 // Writes a copy of the current set of values_ to the |map|.
39 void ExtractValues(DOMStorageValuesMap* map) const { *map = values_; }
41 // Creates a new instance of DOMStorageMap containing
42 // a deep copy of values_.
43 DOMStorageMap* DeepCopy() const;
45 size_t bytes_used() const { return bytes_used_; }
46 size_t quota() const { return quota_; }
47 void set_quota(size_t quota) { quota_ = quota; }
49 static size_t CountBytes(const DOMStorageValuesMap& values);
51 private:
52 friend class base::RefCountedThreadSafe<DOMStorageMap>;
53 ~DOMStorageMap();
55 void ResetKeyIterator();
57 DOMStorageValuesMap values_;
58 DOMStorageValuesMap::const_iterator key_iterator_;
59 unsigned last_key_index_;
60 size_t bytes_used_;
61 size_t quota_;
64 } // namespace content
66 #endif // CONTENT_COMMON_DOM_STORAGE_DOM_STORAGE_MAP_H_