Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_RENDERER_DOM_STORAGE_LOCAL_STORAGE_CACHED_AREA_H_ | 5 #ifndef CONTENT_RENDERER_DOM_STORAGE_LOCAL_STORAGE_CACHED_AREA_H_ |
| 6 #define CONTENT_RENDERER_DOM_STORAGE_LOCAL_STORAGE_CACHED_AREA_H_ | 6 #define CONTENT_RENDERER_DOM_STORAGE_LOCAL_STORAGE_CACHED_AREA_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 9 | |
| 8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/strings/nullable_string16.h" | 11 #include "base/strings/nullable_string16.h" |
| 11 #include "content/common/leveldb_wrapper.mojom.h" | 12 #include "content/common/leveldb_wrapper.mojom.h" |
| 12 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
| 13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 14 #include "url/origin.h" | 15 #include "url/origin.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 18 class DOMStorageMap; | |
| 19 class LocalStorageArea; | |
| 17 class LocalStorageCachedAreas; | 20 class LocalStorageCachedAreas; |
| 18 class StoragePartitionService; | 21 class StoragePartitionService; |
| 19 | 22 |
| 20 // An in-process implementation of LocalStorage using a LevelDB Mojo service. | 23 // An in-process implementation of LocalStorage using a LevelDB Mojo service. |
| 21 // Maintains a complete cache of the origin's Map of key/value pairs for fast | 24 // Maintains a complete cache of the origin's Map of key/value pairs for fast |
| 22 // access. The cache is primed on first access and changes are written to the | 25 // access. The cache is primed on first access and changes are written to the |
| 23 // backend through the level db interface pointer. Mutations originating in | 26 // backend through the level db interface pointer. Mutations originating in |
| 24 // other processes are applied to the cache via LevelDBObserver callbacks. | 27 // other processes are applied to the cache via LevelDBObserver callbacks. |
| 25 class LocalStorageCachedArea : public LevelDBObserver, | 28 // There is one LocalStorageCachedArea for potentially many LocalStorageArea |
| 26 public base::RefCounted<LocalStorageCachedArea> { | 29 // objects. |
| 30 class LocalStorageCachedArea : public LevelDBObserver { | |
| 27 public: | 31 public: |
| 28 LocalStorageCachedArea(const url::Origin& origin, | 32 LocalStorageCachedArea(const url::Origin& origin, |
| 29 StoragePartitionService* storage_partition_service, | 33 StoragePartitionService* storage_partition_service, |
| 30 LocalStorageCachedAreas* cached_areas); | 34 LocalStorageCachedAreas* cached_areas); |
| 31 | 35 |
| 32 // These correspond to blink::WebStorageArea. | 36 // These correspond to blink::WebStorageArea. |
| 33 unsigned GetLength(); | 37 unsigned GetLength(); |
| 34 base::NullableString16 GetKey(unsigned index); | 38 base::NullableString16 GetKey(unsigned index); |
| 35 base::NullableString16 GetItem(const base::string16& key); | 39 base::NullableString16 GetItem(const base::string16& key); |
| 36 bool SetItem(const base::string16& key, | 40 bool SetItem(const base::string16& key, |
| 37 const base::string16& value, | 41 const base::string16& value, |
| 38 const GURL& page_url); | 42 const GURL& page_url, |
| 43 const std::string& storage_area_id); | |
| 39 void RemoveItem(const base::string16& key, | 44 void RemoveItem(const base::string16& key, |
| 40 const GURL& page_url); | 45 const GURL& page_url, |
| 41 void Clear(const GURL& page_url); | 46 const std::string& storage_area_id); |
| 47 void Clear(const GURL& page_url, const std::string& storage_area_id); | |
| 48 | |
| 49 // Allow this object to keep track of the LocalStorageAreas corresponding to | |
| 50 // it, which is needed for mutation event notifications. | |
| 51 void LocalStorageAreaCreated(LocalStorageArea* area); | |
| 52 void LocalStorageAreaDestroyed(LocalStorageArea* area); | |
| 42 | 53 |
| 43 const url::Origin& origin() { return origin_; } | 54 const url::Origin& origin() { return origin_; } |
| 44 | 55 |
| 45 private: | 56 private: |
| 46 friend class base::RefCounted<LocalStorageCachedArea>; | |
| 47 ~LocalStorageCachedArea() override; | 57 ~LocalStorageCachedArea() override; |
| 48 | 58 |
| 49 // LevelDBObserver: | 59 // LevelDBObserver: |
| 50 void KeyChanged(mojo::Array<uint8_t> key, | 60 void KeyChanged(mojo::Array<uint8_t> key, |
| 51 mojo::Array<uint8_t> new_value, | 61 mojo::Array<uint8_t> new_value, |
| 52 mojo::Array<uint8_t> old_value, | 62 mojo::Array<uint8_t> old_value, |
| 53 const mojo::String& source) override; | 63 const mojo::String& source) override; |
| 54 void KeyDeleted(mojo::Array<uint8_t> key, | 64 void KeyDeleted(mojo::Array<uint8_t> key, |
| 65 mojo::Array<uint8_t> old_value, | |
| 55 const mojo::String& source) override; | 66 const mojo::String& source) override; |
| 56 void AllDeleted(const mojo::String& source) override; | 67 void AllDeleted(const mojo::String& source) override; |
| 57 | 68 |
| 58 // Synchronously fetches the origin's local storage data if it hasn't been | 69 // Synchronously fetches the origin's local storage data if it hasn't been |
| 59 // fetched already. | 70 // fetched already. |
| 60 void EnsureLoaded(); | 71 void EnsureLoaded(); |
| 61 | 72 |
| 73 void OnSetItemComplete(const base::string16& key, | |
| 74 leveldb::DatabaseError result); | |
| 75 void OnRemoveItemComplete(const base::string16& key, | |
| 76 leveldb::DatabaseError result); | |
| 77 void OnClearComplete(leveldb::DatabaseError result); | |
| 78 | |
| 79 // Resets the object back to its newly constructed state. | |
| 80 void Reset(); | |
| 81 | |
| 62 bool loaded_; | 82 bool loaded_; |
| 63 url::Origin origin_; | 83 url::Origin origin_; |
| 84 scoped_refptr<DOMStorageMap> map_; | |
| 85 std::map<base::string16, int> ignore_key_mutations_; | |
| 64 LevelDBWrapperPtr leveldb_; | 86 LevelDBWrapperPtr leveldb_; |
| 65 mojo::Binding<LevelDBObserver> binding_; | 87 mojo::Binding<LevelDBObserver> binding_; |
| 66 LocalStorageCachedAreas* cached_areas_; | 88 LocalStorageCachedAreas* cached_areas_; |
|
michaeln
2016/03/18 01:05:17
have this one be weak
jam
2016/03/18 16:48:33
the way the code is structured is that LocalStorag
michaeln
2016/03/18 22:01:14
ok, but dont be surprised when your surprised by w
| |
| 89 std::map<std::string, LocalStorageArea*> areas_; | |
| 67 | 90 |
| 68 DISALLOW_COPY_AND_ASSIGN(LocalStorageCachedArea); | 91 DISALLOW_COPY_AND_ASSIGN(LocalStorageCachedArea); |
| 69 }; | 92 }; |
| 70 | 93 |
| 71 } // namespace content | 94 } // namespace content |
| 72 | 95 |
| 73 #endif // CONTENT_RENDERER_DOM_STORAGE_LOCAL_STORAGE_CACHED_AREA_H_ | 96 #endif // CONTENT_RENDERER_DOM_STORAGE_LOCAL_STORAGE_CACHED_AREA_H_ |
| OLD | NEW |