OLD | NEW |
1 // Copyright (c) 2012 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_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ |
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ | 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 #include <utility> | 13 #include <utility> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/id_map.h" | 16 #include "base/id_map.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/memory/weak_ptr.h" | |
20 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 19 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
21 #include "content/common/indexed_db/indexed_db.mojom.h" | 20 #include "content/common/indexed_db/indexed_db.mojom.h" |
22 #include "content/public/browser/browser_associated_interface.h" | 21 #include "content/public/browser/browser_associated_interface.h" |
23 #include "content/public/browser/browser_message_filter.h" | 22 #include "content/public/browser/browser_message_filter.h" |
24 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
25 #include "storage/browser/blob/blob_data_handle.h" | 24 #include "storage/browser/blob/blob_data_handle.h" |
26 #include "storage/browser/quota/quota_manager.h" | |
27 #include "storage/common/quota/quota_status_code.h" | |
28 #include "url/gurl.h" | 25 #include "url/gurl.h" |
29 | 26 |
30 struct IndexedDBDatabaseMetadata; | |
31 struct IndexedDBHostMsg_DatabaseCount_Params; | |
32 struct IndexedDBHostMsg_DatabaseCreateIndex_Params; | |
33 struct IndexedDBHostMsg_DatabaseCreateObjectStore_Params; | |
34 struct IndexedDBHostMsg_DatabaseCreateTransaction_Params; | |
35 struct IndexedDBHostMsg_DatabaseDeleteRange_Params; | |
36 struct IndexedDBHostMsg_DatabaseGet_Params; | |
37 struct IndexedDBHostMsg_DatabaseGetAll_Params; | |
38 struct IndexedDBHostMsg_DatabaseObserve_Params; | 27 struct IndexedDBHostMsg_DatabaseObserve_Params; |
39 struct IndexedDBHostMsg_DatabaseOpenCursor_Params; | |
40 struct IndexedDBHostMsg_DatabasePut_Params; | |
41 struct IndexedDBHostMsg_DatabaseSetIndexKeys_Params; | |
42 struct IndexedDBMsg_Observation; | 28 struct IndexedDBMsg_Observation; |
43 struct IndexedDBMsg_ObserverChanges; | 29 struct IndexedDBMsg_ObserverChanges; |
44 | 30 |
45 namespace url { | 31 namespace url { |
46 class Origin; | 32 class Origin; |
47 } | 33 } |
48 | 34 |
49 namespace content { | 35 namespace content { |
50 class IndexedDBBlobInfo; | 36 class IndexedDBBlobInfo; |
51 class IndexedDBCallbacks; | 37 class IndexedDBCallbacks; |
52 class IndexedDBConnection; | 38 class IndexedDBConnection; |
53 class IndexedDBContextImpl; | 39 class IndexedDBContextImpl; |
54 class IndexedDBCursor; | 40 class IndexedDBCursor; |
55 class IndexedDBDatabaseCallbacks; | 41 class IndexedDBDatabaseCallbacks; |
56 class IndexedDBKey; | 42 class IndexedDBKey; |
57 class IndexedDBObservation; | 43 class IndexedDBObservation; |
58 class IndexedDBObserverChanges; | 44 class IndexedDBObserverChanges; |
59 struct IndexedDBDatabaseMetadata; | |
60 | 45 |
61 // Handles all IndexedDB related messages from a particular renderer process. | 46 // Handles all IndexedDB related messages from a particular renderer process. |
62 class IndexedDBDispatcherHost | 47 class IndexedDBDispatcherHost |
63 : public BrowserMessageFilter, | 48 : public BrowserMessageFilter, |
64 public BrowserAssociatedInterface<::indexed_db::mojom::Factory>, | 49 public BrowserAssociatedInterface<::indexed_db::mojom::Factory>, |
65 public ::indexed_db::mojom::Factory { | 50 public ::indexed_db::mojom::Factory { |
66 public: | 51 public: |
67 // Only call the constructor from the UI thread. | 52 // Only call the constructor from the UI thread. |
68 IndexedDBDispatcherHost(int ipc_process_id, | 53 IndexedDBDispatcherHost(int ipc_process_id, |
69 net::URLRequestContextGetter* request_context_getter, | 54 net::URLRequestContextGetter* request_context_getter, |
(...skipping 12 matching lines...) Expand all Loading... |
82 const IPC::Message& message) override; | 67 const IPC::Message& message) override; |
83 bool OnMessageReceived(const IPC::Message& message) override; | 68 bool OnMessageReceived(const IPC::Message& message) override; |
84 | 69 |
85 void FinishTransaction(int64_t host_transaction_id, bool committed); | 70 void FinishTransaction(int64_t host_transaction_id, bool committed); |
86 | 71 |
87 // A shortcut for accessing our context. | 72 // A shortcut for accessing our context. |
88 IndexedDBContextImpl* context() const { return indexed_db_context_.get(); } | 73 IndexedDBContextImpl* context() const { return indexed_db_context_.get(); } |
89 storage::BlobStorageContext* blob_storage_context() const { | 74 storage::BlobStorageContext* blob_storage_context() const { |
90 return blob_storage_context_->context(); | 75 return blob_storage_context_->context(); |
91 } | 76 } |
| 77 int ipc_process_id() const { return ipc_process_id_; } |
92 | 78 |
93 // IndexedDBCallbacks call these methods to add the results into the | 79 // IndexedDBCallbacks call these methods to add the results into the |
94 // applicable map. See below for more details. | 80 // applicable map. See below for more details. |
95 int32_t Add(IndexedDBCursor* cursor); | 81 int32_t Add(IndexedDBCursor* cursor); |
96 int32_t Add(IndexedDBConnection* connection, | |
97 const url::Origin& origin); | |
98 | 82 |
99 void RegisterTransactionId(int64_t host_transaction_id, | 83 bool RegisterTransactionId(int64_t host_transaction_id, |
100 const url::Origin& origin); | 84 const url::Origin& origin); |
| 85 bool GetTransactionSize(int64_t host_transaction_id, |
| 86 int64_t* transaction_size); |
| 87 void AddToTransaction(int64_t host_transaction_id, int64_t value_length); |
101 | 88 |
102 IndexedDBCursor* GetCursorFromId(int32_t ipc_cursor_id); | 89 IndexedDBCursor* GetCursorFromId(int32_t ipc_cursor_id); |
103 | 90 |
104 // These are called to map a 32-bit front-end (renderer-specific) transaction | 91 // These are called to map a 32-bit front-end (renderer-specific) transaction |
105 // id to and from a back-end ("host") transaction id that encodes the process | 92 // id to and from a back-end ("host") transaction id that encodes the process |
106 // id in the high 32 bits. The mapping is host-specific and ids are validated. | 93 // id in the high 32 bits. The mapping is host-specific and ids are validated. |
107 int64_t HostTransactionId(int64_t transaction_id); | 94 int64_t HostTransactionId(int64_t transaction_id); |
108 int64_t RendererTransactionId(int64_t host_transaction_id); | 95 int64_t RendererTransactionId(int64_t host_transaction_id); |
109 | 96 |
110 // These are called to decode a host transaction ID, for diagnostic purposes. | 97 // These are called to decode a host transaction ID, for diagnostic purposes. |
111 static uint32_t TransactionIdToRendererTransactionId( | 98 static uint32_t TransactionIdToRendererTransactionId( |
112 int64_t host_transaction_id); | 99 int64_t host_transaction_id); |
113 static uint32_t TransactionIdToProcessId(int64_t host_transaction_id); | 100 static uint32_t TransactionIdToProcessId(int64_t host_transaction_id); |
114 | 101 |
115 std::string HoldBlobData(const IndexedDBBlobInfo& blob_info); | 102 std::string HoldBlobData(const IndexedDBBlobInfo& blob_info); |
| 103 void DropBlobData(const std::string& uuid); |
116 | 104 |
117 // True if the channel is closing/closed and outstanding requests | 105 // True if the channel is closing/closed and outstanding requests |
118 // can be abandoned. Only access on IndexedDB thread. | 106 // can be abandoned. Only access on IndexedDB thread. |
119 bool IsOpen() const; | 107 bool IsOpen() const; |
120 | 108 |
121 private: | 109 private: |
122 // Friends to enable OnDestruct() delegation. | 110 // Friends to enable OnDestruct() delegation. |
123 friend class BrowserThread; | 111 friend class BrowserThread; |
124 friend class base::DeleteHelper<IndexedDBDispatcherHost>; | 112 friend class base::DeleteHelper<IndexedDBDispatcherHost>; |
125 | 113 |
126 // Used in nested classes. | 114 // Used in nested classes. |
127 typedef std::map<int64_t, int64_t> TransactionIDToDatabaseIDMap; | 115 typedef std::map<int64_t, int64_t> TransactionIDToDatabaseIDMap; |
128 typedef std::map<int64_t, uint64_t> TransactionIDToSizeMap; | 116 typedef std::map<int64_t, int64_t> TransactionIDToSizeMap; |
129 typedef std::map<int64_t, url::Origin> TransactionIDToOriginMap; | 117 typedef std::map<int64_t, url::Origin> TransactionIDToOriginMap; |
130 typedef std::map<int32_t, url::Origin> WebIDBObjectIDToOriginMap; | |
131 | 118 |
132 // IDMap for RefCounted types | 119 // IDMap for RefCounted types |
133 template <typename RefCountedType> | 120 template <typename RefCountedType> |
134 class RefIDMap { | 121 class RefIDMap { |
135 public: | 122 public: |
136 typedef int32_t KeyType; | 123 typedef int32_t KeyType; |
137 | 124 |
138 RefIDMap() {} | 125 RefIDMap() {} |
139 ~RefIDMap() {} | 126 ~RefIDMap() {} |
140 | 127 |
(...skipping 13 matching lines...) Expand all Loading... |
154 void set_check_on_null_data(bool value) { | 141 void set_check_on_null_data(bool value) { |
155 map_.set_check_on_null_data(value); | 142 map_.set_check_on_null_data(value); |
156 } | 143 } |
157 | 144 |
158 private: | 145 private: |
159 IDMap<scoped_refptr<RefCountedType>, IDMapOwnPointer> map_; | 146 IDMap<scoped_refptr<RefCountedType>, IDMapOwnPointer> map_; |
160 | 147 |
161 DISALLOW_COPY_AND_ASSIGN(RefIDMap); | 148 DISALLOW_COPY_AND_ASSIGN(RefIDMap); |
162 }; | 149 }; |
163 | 150 |
164 class DatabaseDispatcherHost { | |
165 public: | |
166 explicit DatabaseDispatcherHost(IndexedDBDispatcherHost* parent); | |
167 ~DatabaseDispatcherHost(); | |
168 | |
169 void CloseAll(); | |
170 bool OnMessageReceived(const IPC::Message& message); | |
171 | |
172 void OnCreateObjectStore( | |
173 const IndexedDBHostMsg_DatabaseCreateObjectStore_Params& params); | |
174 void OnDeleteObjectStore(int32_t ipc_database_id, | |
175 int64_t transaction_id, | |
176 int64_t object_store_id); | |
177 void OnRenameObjectStore(int32_t ipc_database_id, | |
178 int64_t transaction_id, | |
179 int64_t object_store_id, | |
180 const base::string16& new_name); | |
181 void OnCreateTransaction( | |
182 const IndexedDBHostMsg_DatabaseCreateTransaction_Params&); | |
183 void OnClose(int32_t ipc_database_id); | |
184 void OnVersionChangeIgnored(int32_t ipc_database_id); | |
185 void OnDestroyed(int32_t ipc_database_id); | |
186 | |
187 void OnObserve(const IndexedDBHostMsg_DatabaseObserve_Params&); | |
188 void OnUnobserve(int32_t ipc_database_id, | |
189 const std::vector<int32_t>& observer_ids_to_remove); | |
190 | |
191 void OnGet(const IndexedDBHostMsg_DatabaseGet_Params& params); | |
192 void OnGetAll(const IndexedDBHostMsg_DatabaseGetAll_Params& params); | |
193 // OnPutWrapper starts on the IO thread so that it can grab BlobDataHandles | |
194 // before posting to the IDB TaskRunner for the rest of the job. | |
195 void OnPutWrapper(const IndexedDBHostMsg_DatabasePut_Params& params); | |
196 void OnPut(const IndexedDBHostMsg_DatabasePut_Params& params, | |
197 std::vector<std::unique_ptr<storage::BlobDataHandle>> handles); | |
198 void OnSetIndexKeys( | |
199 const IndexedDBHostMsg_DatabaseSetIndexKeys_Params& params); | |
200 void OnSetIndexesReady(int32_t ipc_database_id, | |
201 int64_t transaction_id, | |
202 int64_t object_store_id, | |
203 const std::vector<int64_t>& ids); | |
204 void OnOpenCursor(const IndexedDBHostMsg_DatabaseOpenCursor_Params& params); | |
205 void OnCount(const IndexedDBHostMsg_DatabaseCount_Params& params); | |
206 void OnDeleteRange( | |
207 const IndexedDBHostMsg_DatabaseDeleteRange_Params& params); | |
208 void OnClear(int32_t ipc_thread_id, | |
209 int32_t ipc_callbacks_id, | |
210 int32_t ipc_database_id, | |
211 int64_t transaction_id, | |
212 int64_t object_store_id); | |
213 void OnCreateIndex( | |
214 const IndexedDBHostMsg_DatabaseCreateIndex_Params& params); | |
215 void OnDeleteIndex(int32_t ipc_database_id, | |
216 int64_t transaction_id, | |
217 int64_t object_store_id, | |
218 int64_t index_id); | |
219 void OnRenameIndex(int32_t ipc_database_id, | |
220 int64_t transaction_id, | |
221 int64_t object_store_id, | |
222 int64_t index_id, | |
223 const base::string16& new_name); | |
224 | |
225 void OnAbort(int32_t ipc_database_id, int64_t transaction_id); | |
226 void OnCommit(int32_t ipc_database_id, int64_t transaction_id); | |
227 void OnGotUsageAndQuotaForCommit(int32_t ipc_database_id, | |
228 int64_t transaction_id, | |
229 storage::QuotaStatusCode status, | |
230 int64_t usage, | |
231 int64_t quota); | |
232 | |
233 IndexedDBDispatcherHost* parent_; | |
234 IDMap<IndexedDBConnection, IDMapOwnPointer> map_; | |
235 WebIDBObjectIDToOriginMap database_origin_map_; | |
236 TransactionIDToSizeMap transaction_size_map_; | |
237 TransactionIDToOriginMap transaction_origin_map_; | |
238 TransactionIDToDatabaseIDMap transaction_database_map_; | |
239 | |
240 // Weak pointers are used when an asynchronous quota request is made, in | |
241 // case the dispatcher is torn down before the response returns. | |
242 base::WeakPtrFactory<DatabaseDispatcherHost> weak_factory_; | |
243 | |
244 private: | |
245 DISALLOW_COPY_AND_ASSIGN(DatabaseDispatcherHost); | |
246 }; | |
247 | |
248 class CursorDispatcherHost { | 151 class CursorDispatcherHost { |
249 public: | 152 public: |
250 explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent); | 153 explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent); |
251 ~CursorDispatcherHost(); | 154 ~CursorDispatcherHost(); |
252 | 155 |
253 bool OnMessageReceived(const IPC::Message& message); | 156 bool OnMessageReceived(const IPC::Message& message); |
254 | 157 |
255 void OnAdvance(int32_t ipc_object_store_id, | 158 void OnAdvance(int32_t ipc_object_store_id, |
256 int32_t ipc_thread_id, | 159 int32_t ipc_thread_id, |
257 int32_t ipc_callbacks_id, | 160 int32_t ipc_callbacks_id, |
(...skipping 16 matching lines...) Expand all Loading... |
274 RefIDMap<IndexedDBCursor> map_; | 177 RefIDMap<IndexedDBCursor> map_; |
275 | 178 |
276 private: | 179 private: |
277 DISALLOW_COPY_AND_ASSIGN(CursorDispatcherHost); | 180 DISALLOW_COPY_AND_ASSIGN(CursorDispatcherHost); |
278 }; | 181 }; |
279 | 182 |
280 ~IndexedDBDispatcherHost() override; | 183 ~IndexedDBDispatcherHost() override; |
281 | 184 |
282 // Helper templates. | 185 // Helper templates. |
283 template <class ReturnType> | 186 template <class ReturnType> |
284 ReturnType* GetOrTerminateProcess(IDMap<ReturnType, IDMapOwnPointer>* map, | |
285 int32_t ipc_return_object_id); | |
286 template <class ReturnType> | |
287 ReturnType* GetOrTerminateProcess(RefIDMap<ReturnType>* map, | 187 ReturnType* GetOrTerminateProcess(RefIDMap<ReturnType>* map, |
288 int32_t ipc_return_object_id); | 188 int32_t ipc_return_object_id); |
289 | 189 |
290 template <typename MapType> | 190 template <typename MapType> |
291 void DestroyObject(MapType* map, int32_t ipc_object_id); | 191 void DestroyObject(MapType* map, int32_t ipc_object_id); |
292 | 192 |
293 // indexed_db::mojom::Factory implementation: | 193 // indexed_db::mojom::Factory implementation: |
294 void GetDatabaseNames( | 194 void GetDatabaseNames( |
295 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, | 195 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, |
296 const url::Origin& origin) override; | 196 const url::Origin& origin) override; |
(...skipping 19 matching lines...) Expand all Loading... |
316 const base::string16& name, | 216 const base::string16& name, |
317 int64_t version, | 217 int64_t version, |
318 int64_t transaction_id); | 218 int64_t transaction_id); |
319 void DeleteDatabaseOnIDBThread(scoped_refptr<IndexedDBCallbacks> callbacks, | 219 void DeleteDatabaseOnIDBThread(scoped_refptr<IndexedDBCallbacks> callbacks, |
320 const url::Origin& origin, | 220 const url::Origin& origin, |
321 const base::string16& name); | 221 const base::string16& name); |
322 | 222 |
323 // Message processing. Most of the work is delegated to the dispatcher hosts | 223 // Message processing. Most of the work is delegated to the dispatcher hosts |
324 // below. | 224 // below. |
325 void OnAckReceivedBlobs(const std::vector<std::string>& uuids); | 225 void OnAckReceivedBlobs(const std::vector<std::string>& uuids); |
326 void OnPutHelper( | |
327 const IndexedDBHostMsg_DatabasePut_Params& params, | |
328 std::vector<std::unique_ptr<storage::BlobDataHandle>> handles); | |
329 | 226 |
330 void ResetDispatcherHosts(); | 227 void ResetDispatcherHosts(); |
331 void DropBlobData(const std::string& uuid); | |
332 | 228 |
333 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 229 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
334 scoped_refptr<IndexedDBContextImpl> indexed_db_context_; | 230 scoped_refptr<IndexedDBContextImpl> indexed_db_context_; |
335 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; | 231 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; |
336 | 232 |
337 // Maps blob uuid to a pair (handle, ref count). Entry is added and/or count | 233 // Maps blob uuid to a pair (handle, ref count). Entry is added and/or count |
338 // is incremented in HoldBlobData(), and count is decremented and/or entry | 234 // is incremented in HoldBlobData(), and count is decremented and/or entry |
339 // removed in DropBlobData(). | 235 // removed in DropBlobData(). |
340 std::map<std::string, | 236 std::map<std::string, |
341 std::pair<std::unique_ptr<storage::BlobDataHandle>, int>> | 237 std::pair<std::unique_ptr<storage::BlobDataHandle>, int>> |
342 blob_data_handle_map_; | 238 blob_data_handle_map_; |
343 | 239 |
344 // Only access on IndexedDB thread. | 240 // Only access on IndexedDB thread. |
345 bool is_open_ = true; | 241 bool is_open_ = true; |
346 std::unique_ptr<DatabaseDispatcherHost> database_dispatcher_host_; | 242 TransactionIDToSizeMap transaction_size_map_; |
| 243 TransactionIDToOriginMap transaction_origin_map_; |
347 std::unique_ptr<CursorDispatcherHost> cursor_dispatcher_host_; | 244 std::unique_ptr<CursorDispatcherHost> cursor_dispatcher_host_; |
348 | 245 |
349 // Used to set file permissions for blob storage. | 246 // Used to set file permissions for blob storage. |
350 int ipc_process_id_; | 247 int ipc_process_id_; |
351 | 248 |
352 DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost); | 249 DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost); |
353 }; | 250 }; |
354 | 251 |
355 } // namespace content | 252 } // namespace content |
356 | 253 |
357 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ | 254 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ |
OLD | NEW |