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_CONTEXT_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_ |
6 #define CONTENT_BROWSER_IN_PROCESS_WEBKIT_DOM_STORAGE_CONTEXT_IMPL_H_ | 6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | |
10 #include <set> | |
11 | |
12 #include "base/compiler_specific.h" | |
13 #include "base/file_path.h" | 9 #include "base/file_path.h" |
14 #include "base/gtest_prod_util.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/string16.h" |
15 #include "base/time.h" | 12 #include "base/time.h" |
16 #include "content/public/browser/dom_storage_context.h" | 13 #include "content/public/browser/dom_storage_context.h" |
| 14 #include "webkit/dom_storage/dom_storage_types.h" |
17 | 15 |
18 class DOMStorageArea; | 16 #ifdef ENABLE_NEW_DOM_STORAGE_BACKEND |
19 class DOMStorageMessageFilter; | |
20 class DOMStorageNamespace; | |
21 | 17 |
22 namespace base { | 18 namespace dom_storage { |
23 class MessageLoopProxy; | 19 class DomStorageContext; |
24 class SequencedTaskRunner; | |
25 } | 20 } |
26 | 21 |
27 namespace quota { | 22 namespace quota { |
28 class SpecialStoragePolicy; | 23 class SpecialStoragePolicy; |
29 } | 24 } |
30 | 25 |
31 // This is owned by BrowserContext and is all the dom storage information that's | 26 // This is owned by BrowserContext (aka Profile) and encapsulates all |
32 // shared by all the DOMStorageMessageFilters that share the same browser | 27 // per-profile dom storage state. |
33 // context. The specifics of responsibilities are fairly well documented here | |
34 // and in StorageNamespace and StorageArea. Everything is only to be accessed | |
35 // on the WebKit thread unless noted otherwise. | |
36 class CONTENT_EXPORT DOMStorageContextImpl : | 28 class CONTENT_EXPORT DOMStorageContextImpl : |
37 NON_EXPORTED_BASE(public content::DOMStorageContext) { | 29 NON_EXPORTED_BASE(public content::DOMStorageContext), |
| 30 public base::RefCountedThreadSafe<DOMStorageContextImpl> { |
38 public: | 31 public: |
39 // If |data_path| is empty, nothing will be saved to disk. | 32 // If |data_path| is empty, nothing will be saved to disk. |
40 DOMStorageContextImpl(const FilePath& data_path, | 33 DOMStorageContextImpl(const FilePath& data_path, |
41 quota::SpecialStoragePolicy* special_storage_policy); | 34 quota::SpecialStoragePolicy* special_storage_policy); |
42 virtual ~DOMStorageContextImpl(); | |
43 | 35 |
44 // DOMStorageContext implementation: | 36 // DOMStorageContext implementation. |
45 virtual base::SequencedTaskRunner* task_runner() const OVERRIDE; | 37 virtual void GetAllStorageFiles( |
46 virtual std::vector<FilePath> GetAllStorageFiles() OVERRIDE; | 38 const GetAllStorageFilesCallback& callback) OVERRIDE; |
47 virtual FilePath GetFilePath(const string16& origin_id) const OVERRIDE; | 39 virtual FilePath GetFilePath(const string16& origin_id) const OVERRIDE; |
48 virtual void DeleteForOrigin(const string16& origin_id) OVERRIDE; | 40 virtual void DeleteForOrigin(const string16& origin_id) OVERRIDE; |
49 virtual void DeleteLocalStorageFile(const FilePath& file_path) OVERRIDE; | 41 virtual void DeleteLocalStorageFile(const FilePath& file_path) OVERRIDE; |
50 virtual void DeleteDataModifiedSince(const base::Time& cutoff) OVERRIDE; | 42 virtual void DeleteDataModifiedSince(const base::Time& cutoff) OVERRIDE; |
51 | 43 |
52 // Invalid storage id. No storage session will ever report this value. | 44 // Called to free up memory that's not strictly needed. |
53 // Used in DOMStorageMessageFilter::OnStorageAreaId when coping with | |
54 // interactions with non-existent storage sessions. | |
55 static const int64 kInvalidStorageId = -1; | |
56 | |
57 // Allocate a new storage area id. Only call on the WebKit thread. | |
58 int64 AllocateStorageAreaId(); | |
59 | |
60 // Allocate a new session storage id. Only call on the UI or IO thread. | |
61 int64 AllocateSessionStorageNamespaceId(); | |
62 | |
63 // Clones a session storage namespace and returns the cloned namespaces' id. | |
64 // Only call on the IO thread. | |
65 int64 CloneSessionStorage(int64 original_id); | |
66 | |
67 // Various storage area methods. The storage area is owned by one of the | |
68 // namespaces that's owned by this class. | |
69 void RegisterStorageArea(DOMStorageArea* storage_area); | |
70 void UnregisterStorageArea(DOMStorageArea* storage_area); | |
71 DOMStorageArea* GetStorageArea(int64 id); | |
72 | |
73 // Called on WebKit thread when a session storage namespace can be deleted. | |
74 void DeleteSessionStorageNamespace(int64 namespace_id); | |
75 | |
76 // Get a namespace from an id. What's returned is owned by this class. If | |
77 // allocation_allowed is true, then this function will create the storage | |
78 // namespace if it hasn't been already. | |
79 DOMStorageNamespace* GetStorageNamespace(int64 id, bool allocation_allowed); | |
80 | |
81 // Sometimes an event from one DOM storage message filter requires | |
82 // communication to all of them. | |
83 typedef std::set<DOMStorageMessageFilter*> MessageFilterSet; | |
84 void RegisterMessageFilter(DOMStorageMessageFilter* message_filter); | |
85 void UnregisterMessageFilter(DOMStorageMessageFilter* MessageFilter); | |
86 const MessageFilterSet* GetMessageFilterSet() const; | |
87 | |
88 // Tells storage namespaces to purge any memory they do not need. | |
89 void PurgeMemory(); | 45 void PurgeMemory(); |
90 | 46 |
91 // Deletes all local storage files. | 47 // Used by content settings to alter the behavior around |
92 void DeleteAllLocalStorageFiles(); | 48 // what data to keep and what data to discard at shutdown. |
| 49 // The policy is not so straight forward to describe, see |
| 50 // the implementation for details. |
| 51 void SetClearLocalState(bool clear_local_state); |
| 52 void SaveSessionState(); |
93 | 53 |
94 // The local storage directory. | 54 // Called when the BrowserContext/Profile is going away. |
95 static const FilePath::CharType kLocalStorageDirectory[]; | 55 void Shutdown(); |
96 | 56 |
97 // The local storage file extension. | 57 // See render_message_filter.cc for details. |
98 static const FilePath::CharType kLocalStorageExtension[]; | 58 // TODO(michaeln): Remove this method when that bug is fixed. |
99 | 59 int64 LeakyCloneSessionStorage(int64 existing_namespace_id); |
100 void set_clear_local_state_on_exit(bool clear_local_state) { | |
101 clear_local_state_on_exit_ = clear_local_state; | |
102 } | |
103 | |
104 // Disables the exit-time deletion for all data (also session-only data). | |
105 void SaveSessionState() { | |
106 save_session_state_ = true; | |
107 } | |
108 | |
109 void set_data_path_for_testing(const FilePath& data_path) { | |
110 data_path_ = data_path; | |
111 } | |
112 | 60 |
113 private: | 61 private: |
114 FRIEND_TEST_ALL_PREFIXES(DOMStorageTest, SessionOnly); | 62 friend class DOMStorageMessageFilter; // for access to context() |
115 FRIEND_TEST_ALL_PREFIXES(DOMStorageTest, SaveSessionState); | 63 friend class SessionStorageNamespaceImpl; // ditto |
| 64 friend class base::RefCountedThreadSafe<DOMStorageContextImpl>; |
116 | 65 |
117 // Get the local storage instance. The object is owned by this class. | 66 virtual ~DOMStorageContextImpl(); |
118 DOMStorageNamespace* CreateLocalStorage(); | 67 dom_storage::DomStorageContext* context() const { return context_.get(); } |
119 | 68 |
120 // Get a new session storage namespace. The object is owned by this class. | 69 scoped_refptr<dom_storage::DomStorageContext> context_; |
121 DOMStorageNamespace* CreateSessionStorage(int64 namespace_id); | |
122 | |
123 // Used internally to register storage namespaces we create. | |
124 void RegisterStorageNamespace(DOMStorageNamespace* storage_namespace); | |
125 | |
126 // The WebKit thread half of CloneSessionStorage above. | |
127 void CompleteCloningSessionStorage(int64 existing_id, int64 clone_id); | |
128 | |
129 // The last used storage_area_id and storage_namespace_id's. For the storage | |
130 // namespaces, IDs allocated on the UI thread are positive and count up while | |
131 // IDs allocated on the IO thread are negative and count down. This allows us | |
132 // to allocate unique IDs on both without any locking. All storage area ids | |
133 // are allocated on the WebKit thread. | |
134 int64 last_storage_area_id_; | |
135 int64 last_session_storage_namespace_id_on_ui_thread_; | |
136 int64 last_session_storage_namespace_id_on_io_thread_; | |
137 | |
138 // True if the destructor should delete its files. | |
139 bool clear_local_state_on_exit_; | |
140 | |
141 // If true, nothing (not even session-only data) should be deleted on exit. | |
142 bool save_session_state_; | |
143 | |
144 // Path where the browser context data is stored. | |
145 // TODO(pastarmovj): Keep in mind that unlike indexed db data_path_ variable | |
146 // this one still has to point to the upper level dir because of the | |
147 // MigrateLocalStorageDirectory function. Once this function disappears we can | |
148 // make it point directly to the dom storage path. | |
149 FilePath data_path_; | |
150 | |
151 // All the DOMStorageMessageFilters that are attached to us. ONLY USE ON THE | |
152 // IO THREAD! | |
153 MessageFilterSet message_filter_set_; | |
154 | |
155 // Maps ids to StorageAreas. We do NOT own these objects. StorageNamespace | |
156 // (which does own them) will notify us when we should remove the entries. | |
157 typedef std::map<int64, DOMStorageArea*> StorageAreaMap; | |
158 StorageAreaMap storage_area_map_; | |
159 | |
160 // Maps ids to StorageNamespaces. We own these objects. | |
161 typedef std::map<int64, DOMStorageNamespace*> StorageNamespaceMap; | |
162 StorageNamespaceMap storage_namespace_map_; | |
163 | |
164 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; | |
165 scoped_refptr<base::MessageLoopProxy> webkit_message_loop_; | |
166 | 70 |
167 DISALLOW_IMPLICIT_CONSTRUCTORS(DOMStorageContextImpl); | 71 DISALLOW_IMPLICIT_CONSTRUCTORS(DOMStorageContextImpl); |
168 }; | 72 }; |
169 | 73 |
170 #endif // CONTENT_BROWSER_IN_PROCESS_WEBKIT_DOM_STORAGE_CONTEXT_IMPL_H_ | 74 #endif // ENABLE_NEW_DOM_STORAGE_BACKEND |
| 75 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_ |
OLD | NEW |