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