Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef CONTENT_RENDERER_DOM_STORAGE_DOM_STORAGE_CACHED_AREA_H_ |
| 6 | #define CONTENT_RENDERER_DOM_STORAGE_DOM_STORAGE_CACHED_AREA_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | |
| 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "base/memory/weak_ptr.h" |
| 12 | #include "base/strings/nullable_string16.h" |
| 13 | #include "content/common/content_export.h" |
| 14 | #include "url/gurl.h" |
| 15 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 16 | namespace content { |
| 17 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 18 | class DOMStorageMap; |
| 19 | class DOMStorageProxy; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 20 | |
| 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. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 27 | class CONTENT_EXPORT DOMStorageCachedArea |
| 28 | : public base::RefCounted<DOMStorageCachedArea> { |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 29 | public: |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 30 | DOMStorageCachedArea(int64 namespace_id, |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 31 | const GURL& origin, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 32 | DOMStorageProxy* proxy); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 33 | |
| 34 | int64 namespace_id() const { return namespace_id_; } |
| 35 | const GURL& origin() const { return origin_; } |
| 36 | |
| 37 | unsigned GetLength(int connection_id); |
| 38 | base::NullableString16 GetKey(int connection_id, unsigned index); |
| 39 | base::NullableString16 GetItem(int connection_id, const base::string16& key); |
| 40 | bool SetItem(int connection_id, |
| 41 | const base::string16& key, |
| 42 | const base::string16& value, |
| 43 | const GURL& page_url); |
| 44 | void RemoveItem(int connection_id, |
| 45 | const base::string16& key, |
| 46 | const GURL& page_url); |
| 47 | void Clear(int connection_id, const GURL& page_url); |
| 48 | |
| 49 | void ApplyMutation(const base::NullableString16& key, |
| 50 | const base::NullableString16& new_value); |
| 51 | |
| 52 | size_t MemoryBytesUsedByCache() const; |
| 53 | |
| 54 | private: |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 55 | friend class DOMStorageCachedAreaTest; |
| 56 | friend class base::RefCounted<DOMStorageCachedArea>; |
| 57 | ~DOMStorageCachedArea(); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 58 | |
| 59 | // Primes the cache, loading all values for the area. |
| 60 | void Prime(int connection_id); |
| 61 | void PrimeIfNeeded(int connection_id) { |
| 62 | if (!map_.get()) |
| 63 | Prime(connection_id); |
| 64 | } |
| 65 | |
| 66 | // Resets the object back to its newly constructed state. |
| 67 | void Reset(); |
| 68 | |
| 69 | // Async completion callbacks for proxied operations. |
| 70 | // These are used to maintain cache consistency by preventing |
| 71 | // mutation events from other processes from overwriting local |
| 72 | // changes made after the mutation. |
| 73 | void OnLoadComplete(bool success); |
| 74 | void OnSetItemComplete(const base::string16& key, bool success); |
| 75 | void OnClearComplete(bool success); |
| 76 | void OnRemoveItemComplete(const base::string16& key, bool success); |
| 77 | |
| 78 | bool should_ignore_key_mutation(const base::string16& key) const { |
| 79 | return ignore_key_mutations_.find(key) != ignore_key_mutations_.end(); |
| 80 | } |
| 81 | |
| 82 | bool ignore_all_mutations_; |
| 83 | std::map<base::string16, int> ignore_key_mutations_; |
| 84 | |
| 85 | int64 namespace_id_; |
| 86 | GURL origin_; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 87 | scoped_refptr<DOMStorageMap> map_; |
| 88 | scoped_refptr<DOMStorageProxy> proxy_; |
| 89 | base::WeakPtrFactory<DOMStorageCachedArea> weak_factory_; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace content |
| 93 | |
| 94 | #endif // CONTENT_RENDERER_DOM_STORAGE_DOM_STORAGE_CACHED_AREA_H_ |