blob: 0a313c29d90988b442a9ab78f604f1c67617807d [file] [log] [blame]
[email protected]2a10f6812009-07-24 01:22:511// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
2// source code is governed by a BSD-style license that can be found in the
3// LICENSE file.
4
5#ifndef CHROME_RENDERER_RENDERER_WEBSTORAGEAREA_IMPL_H_
6#define CHROME_RENDERER_RENDERER_WEBSTORAGEAREA_IMPL_H_
7
8#include "base/basictypes.h"
9#include "base/hash_tables.h"
10#include "base/string16.h"
11#include "webkit/api/public/WebStorageArea.h"
12#include "webkit/api/public/WebString.h"
13
14class RendererWebStorageAreaImpl : public WebKit::WebStorageArea {
15 public:
16 RendererWebStorageAreaImpl(int64 namespace_id,
17 const WebKit::WebString& origin);
18 virtual ~RendererWebStorageAreaImpl();
19
20 // See WebStorageArea.h for documentation on these functions.
21 virtual void lock(bool& invalidate_cache, size_t& bytes_left_in_quota);
22 virtual void unlock();
23 virtual unsigned length();
24 virtual WebKit::WebString key(unsigned index, bool& key_exception);
25 virtual WebKit::WebString getItem(const WebKit::WebString& key);
26 virtual void setItem(const WebKit::WebString& key,
27 const WebKit::WebString& value,
28 bool& quota_exception);
29 virtual void removeItem(const WebKit::WebString& key);
30 virtual void clear();
31
32 private:
33 // Calls lock if we haven't already done so, and also gets a storage_area_id
34 // if we haven't done so. Fetches quota and cache invalidation information
35 // while locking. Only call on the WebKit thread.
36 void EnsureInitializedAndLocked();
37
38 // Update our quota calculation. Returns true if we updated the quota.
39 // Returns false if we couldn't update because we would have exceeded the
40 // quota. If an item is not in our cache, it'll require a getItem IPC in
41 // order to determine the existing value's size.
42 bool UpdateQuota(const WebKit::WebString& key,
43 const WebKit::WebString& value);
44
45 // Set a key/value pair in our cache.
46 void SetCache(const string16& key, const WebKit::WebString& value);
47
48 // Used for initialization or storage_area_id_.
49 int64 namespace_id_;
50 string16 origin_;
51
52 // The ID we use for all IPC. Initialized lazily.
53 int64 storage_area_id_;
54
55 // storage_area_id_ should equal this iff its unitialized.
56 static const int64 kUninitializedStorageAreaId = -1;
57
58 // Do we currently hold the lock on this storage area?
59 bool lock_held_;
60
61 // We track how many bytes are left in the quota between lock and unlock
62 // calls. This allows us to avoid sync IPC on setItem.
63 size_t bytes_left_in_quota_;
64
65 // A cache of key/value pairs. If the item exists, it's put in the
66 // cached_items_ map. If not, it's added to the cached_invalid_items_ set.
67 // The lock IPC call tells us when to invalidate these caches.
68 // TODO(jorlow): Instead of a map + a set, use NullableString16 once it's
69 // implemented: http://crbug.com/17343
70 typedef base::hash_map<string16, string16> CacheMap;
71 typedef base::hash_set<string16> CacheSet;
72 CacheMap cached_items_;
73 CacheSet cached_invalid_items_;
74};
75
76#endif // CHROME_RENDERER_WEBSTORAGEAREA_IMPL_H_