blob: 69db984e380faaf8875cd933b48050198915b928 [file] [log] [blame]
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001// 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.
4
5#ifndef WEBKIT_COMMON_DOM_STORAGE_DOM_STORAGE_MAP_H_
6#define WEBKIT_COMMON_DOM_STORAGE_DOM_STORAGE_MAP_H_
7
8#include <map>
9
10#include "base/memory/ref_counted.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010011#include "base/strings/nullable_string16.h"
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010012#include "base/strings/string16.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010013#include "webkit/common/dom_storage/dom_storage_types.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010014#include "webkit/common/webkit_storage_common_export.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010015
16namespace dom_storage {
17
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 DomStorageContext for a larger overview.
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010021class WEBKIT_STORAGE_COMMON_EXPORT DomStorageMap
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010022 : public base::RefCountedThreadSafe<DomStorageMap> {
23 public:
24 explicit DomStorageMap(size_t quota);
25
26 unsigned Length() const;
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010027 base::NullableString16 Key(unsigned index);
28 base::NullableString16 GetItem(const base::string16& key) const;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010029 bool SetItem(const base::string16& key, const base::string16& value,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010030 base::NullableString16* old_value);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010031 bool RemoveItem(const base::string16& key, base::string16* old_value);
32
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(ValuesMap* map);
37
38 // Writes a copy of the current set of values_ to the |map|.
39 void ExtractValues(ValuesMap* map) const { *map = values_; }
40
41 // Creates a new instance of DomStorageMap containing
42 // a deep copy of values_.
43 DomStorageMap* DeepCopy() const;
44
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; }
48
49 private:
50 friend class base::RefCountedThreadSafe<DomStorageMap>;
51 ~DomStorageMap();
52
53 void ResetKeyIterator();
54
55 ValuesMap values_;
56 ValuesMap::const_iterator key_iterator_;
57 unsigned last_key_index_;
58 size_t bytes_used_;
59 size_t quota_;
60};
61
62} // namespace dom_storage
63
64#endif // WEBKIT_COMMON_DOM_STORAGE_DOM_STORAGE_MAP_H_