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
3 // found in the LICENSE file.
5 #ifndef WEBKIT_DOM_STORAGE_DOM_STORAGE_CACHED_AREA_H_
6 #define WEBKIT_DOM_STORAGE_DOM_STORAGE_CACHED_AREA_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/nullable_string16.h"
13 #include "googleurl/src/gurl.h"
14 #include "webkit/storage/webkit_storage_export.h"
16 namespace dom_storage
{
19 class DomStorageProxy
;
21 // Unlike the other classes in the dom_storage library, this one is intended
22 // for use in renderer processes. It maintains a complete cache of the
23 // origin's Map of key/value pairs for fast access. The cache is primed on
24 // first access and changes are written to the backend thru the |proxy|.
25 // Mutations originating in other processes are applied to the cache via
26 // the ApplyMutation method.
27 class WEBKIT_STORAGE_EXPORT DomStorageCachedArea
:
28 public base::RefCounted
<DomStorageCachedArea
> {
30 DomStorageCachedArea(int64 namespace_id
, const GURL
& origin
,
31 DomStorageProxy
* proxy
);
33 int64
namespace_id() const { return namespace_id_
; }
34 const GURL
& origin() const { return origin_
; }
36 unsigned GetLength(int connection_id
);
37 NullableString16
GetKey(int connection_id
, unsigned index
);
38 NullableString16
GetItem(int connection_id
, const base::string16
& key
);
39 bool SetItem(int connection_id
,
40 const base::string16
& key
,
41 const base::string16
& value
,
42 const GURL
& page_url
);
43 void RemoveItem(int connection_id
, const base::string16
& key
,
44 const GURL
& page_url
);
45 void Clear(int connection_id
, const GURL
& page_url
);
47 void ApplyMutation(const NullableString16
& key
,
48 const NullableString16
& new_value
);
50 size_t MemoryBytesUsedByCache() const;
53 friend class DomStorageCachedAreaTest
;
54 friend class base::RefCounted
<DomStorageCachedArea
>;
55 ~DomStorageCachedArea();
57 // Primes the cache, loading all values for the area.
58 void Prime(int connection_id
);
59 void PrimeIfNeeded(int connection_id
) {
64 // Resets the object back to its newly constructed state.
67 // Async completion callbacks for proxied operations.
68 // These are used to maintain cache consistency by preventing
69 // mutation events from other processes from overwriting local
70 // changes made after the mutation.
71 void OnLoadComplete(bool success
);
72 void OnSetItemComplete(const base::string16
& key
, bool success
);
73 void OnClearComplete(bool success
);
74 void OnRemoveItemComplete(const base::string16
& key
, bool success
);
76 bool should_ignore_key_mutation(const base::string16
& key
) const {
77 return ignore_key_mutations_
.find(key
) != ignore_key_mutations_
.end();
80 bool ignore_all_mutations_
;
81 std::map
<base::string16
, int> ignore_key_mutations_
;
85 scoped_refptr
<DomStorageMap
> map_
;
86 scoped_refptr
<DomStorageProxy
> proxy_
;
87 base::WeakPtrFactory
<DomStorageCachedArea
> weak_factory_
;
90 } // namespace dom_storage
92 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_CACHED_AREA_H_