OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_BROWSER_IN_PROCESS_WEBKIT_DOM_STORAGE_NAMESPACE_H_ | 5 #ifndef WEBKIT_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_ |
6 #define CONTENT_BROWSER_IN_PROCESS_WEBKIT_DOM_STORAGE_NAMESPACE_H_ | 6 #define WEBKIT_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/hash_tables.h" | 9 #include <map> |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/string16.h" | |
12 #include "content/common/dom_storage_common.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
14 | 10 |
15 class DOMStorageArea; | 11 #include "base/basictypes.h" |
16 class DOMStorageContext; | 12 #include "base/file_path.h" |
17 class FilePath; | 13 #include "base/memory/ref_counted.h" |
| 14 #include "webkit/dom_storage/dom_storage_area.h" |
18 | 15 |
19 namespace WebKit { | 16 class GURL; |
20 class WebStorageArea; | |
21 class WebStorageNamespace; | |
22 } | |
23 | 17 |
24 // Only to be used on the WebKit thread. | 18 namespace dom_storage { |
25 class DOMStorageNamespace { | 19 |
| 20 class DomStorageArea; |
| 21 class DomStorageTaskRunner; |
| 22 |
| 23 // Container for the set of per-origin Areas. |
| 24 // See class comments for DomStorageContext for a larger overview. |
| 25 class DomStorageNamespace |
| 26 : public base::RefCountedThreadSafe<DomStorageNamespace> { |
26 public: | 27 public: |
27 static DOMStorageNamespace* CreateLocalStorageNamespace( | 28 // Constructor for a LocalStorage namespace with id of 0 |
28 DOMStorageContext* dom_storage_context, const FilePath& data_dir_path); | 29 // and an optional backing directory on disk. |
29 static DOMStorageNamespace* CreateSessionStorageNamespace( | 30 DomStorageNamespace(const FilePath& directory, // may be empty |
30 DOMStorageContext* dom_storage_context, int64 namespace_id); | 31 DomStorageTaskRunner* task_runner); |
31 | 32 |
32 ~DOMStorageNamespace(); | 33 // Constructor for a SessionStorage namespace with a non-zero id |
| 34 // and no backing directory on disk. |
| 35 DomStorageNamespace(int64 namespace_id, |
| 36 DomStorageTaskRunner* task_runner); |
33 | 37 |
34 DOMStorageArea* GetStorageArea(const string16& origin); | 38 int64 namespace_id() const { return namespace_id_; } |
35 DOMStorageNamespace* Copy(int64 clone_namespace_id); | |
36 | 39 |
37 void PurgeMemory(); | 40 // Returns the storage area for the given origin, |
| 41 // creating instance if needed. Each call to open |
| 42 // must be balanced with a call to CloseStorageArea. |
| 43 DomStorageArea* OpenStorageArea(const GURL& origin); |
| 44 void CloseStorageArea(DomStorageArea* area); |
38 | 45 |
39 const DOMStorageContext* dom_storage_context() const { | 46 // Creates a clone of |this| namespace including |
40 return dom_storage_context_; | 47 // shallow copies of all contained areas. |
41 } | 48 // Should only be called for session storage namespaces. |
42 int64 id() const { return id_; } | 49 DomStorageNamespace* Clone(int64 clone_namespace_id); |
43 const WebKit::WebString& data_dir_path() const { return data_dir_path_; } | |
44 DOMStorageType dom_storage_type() const { return dom_storage_type_; } | |
45 | |
46 // Creates a WebStorageArea for the given origin. This should only be called | |
47 // by an owned DOMStorageArea. | |
48 WebKit::WebStorageArea* CreateWebStorageArea(const string16& origin); | |
49 | 50 |
50 private: | 51 private: |
51 // Called by the static factory methods above. | 52 friend class base::RefCountedThreadSafe<DomStorageNamespace>; |
52 DOMStorageNamespace(DOMStorageContext* dom_storage_context, | |
53 int64 id, | |
54 const WebKit::WebString& data_dir_path, | |
55 DOMStorageType storage_type); | |
56 | 53 |
57 // Creates the underlying WebStorageNamespace on demand. | 54 // Struct to hold references to our contained areas and |
58 void CreateWebStorageNamespaceIfNecessary(); | 55 // to keep track of how many tabs have a given area open. |
| 56 struct AreaHolder { |
| 57 scoped_refptr<DomStorageArea> area_; |
| 58 int open_count_; |
| 59 AreaHolder(); |
| 60 AreaHolder(DomStorageArea* area, int count); |
| 61 ~AreaHolder(); |
| 62 }; |
| 63 typedef std::map<GURL, AreaHolder> AreaMap; |
59 | 64 |
60 // All the storage areas we own. | 65 ~DomStorageNamespace(); |
61 typedef base::hash_map<string16, DOMStorageArea*> OriginToStorageAreaMap; | |
62 OriginToStorageAreaMap origin_to_storage_area_; | |
63 | 66 |
64 // The DOMStorageContext that owns us. | 67 // Returns a pointer to the area holder in our map or NULL. |
65 DOMStorageContext* dom_storage_context_; | 68 AreaHolder* GetAreaHolder(const GURL& origin); |
66 | 69 |
67 // The WebKit storage namespace we manage. | 70 int64 namespace_id_; |
68 scoped_ptr<WebKit::WebStorageNamespace> storage_namespace_; | 71 FilePath directory_; |
69 | 72 AreaMap areas_; |
70 // Our id. Unique to our parent WebKitContext class. | 73 scoped_refptr<DomStorageTaskRunner> task_runner_; |
71 int64 id_; | |
72 | |
73 // The path used to create us, so we can recreate our WebStorageNamespace on | |
74 // demand. | |
75 WebKit::WebString data_dir_path_; | |
76 | |
77 // SessionStorage vs. LocalStorage. | |
78 const DOMStorageType dom_storage_type_; | |
79 | |
80 DISALLOW_IMPLICIT_CONSTRUCTORS(DOMStorageNamespace); | |
81 }; | 74 }; |
82 | 75 |
83 #endif // CONTENT_BROWSER_IN_PROCESS_WEBKIT_DOM_STORAGE_NAMESPACE_H_ | 76 } // namespace dom_storage |
| 77 |
| 78 |
| 79 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_ |
OLD | NEW |