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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_
13 #include "base/basictypes.h"
14 #include "base/id_map.h"
15 #include "base/memory/ref_counted.h"
16 #include "content/browser/fileapi/chrome_blob_storage_context.h"
17 #include "content/public/browser/browser_message_filter.h"
18 #include "net/url_request/url_request_context_getter.h"
19 #include "storage/browser/blob/blob_data_handle.h"
22 struct IndexedDBDatabaseMetadata
;
23 struct IndexedDBHostMsg_DatabaseCount_Params
;
24 struct IndexedDBHostMsg_DatabaseCreateIndex_Params
;
25 struct IndexedDBHostMsg_DatabaseCreateObjectStore_Params
;
26 struct IndexedDBHostMsg_DatabaseCreateTransaction_Params
;
27 struct IndexedDBHostMsg_DatabaseDeleteRange_Params
;
28 struct IndexedDBHostMsg_DatabaseGet_Params
;
29 struct IndexedDBHostMsg_DatabaseGetAll_Params
;
30 struct IndexedDBHostMsg_DatabaseOpenCursor_Params
;
31 struct IndexedDBHostMsg_DatabasePut_Params
;
32 struct IndexedDBHostMsg_DatabaseSetIndexKeys_Params
;
33 struct IndexedDBHostMsg_FactoryDeleteDatabase_Params
;
34 struct IndexedDBHostMsg_FactoryGetDatabaseNames_Params
;
35 struct IndexedDBHostMsg_FactoryOpen_Params
;
38 class IndexedDBBlobInfo
;
39 class IndexedDBConnection
;
40 class IndexedDBContextImpl
;
41 class IndexedDBCursor
;
43 class IndexedDBKeyPath
;
44 class IndexedDBKeyRange
;
45 struct IndexedDBDatabaseMetadata
;
47 // Handles all IndexedDB related messages from a particular renderer process.
48 class IndexedDBDispatcherHost
: public BrowserMessageFilter
{
50 // Only call the constructor from the UI thread.
51 IndexedDBDispatcherHost(int ipc_process_id
,
52 net::URLRequestContextGetter
* request_context_getter
,
53 IndexedDBContextImpl
* indexed_db_context
,
54 ChromeBlobStorageContext
* blob_storage_context
);
55 IndexedDBDispatcherHost(int ipc_process_id
,
56 net::URLRequestContext
* request_context
,
57 IndexedDBContextImpl
* indexed_db_context
,
58 ChromeBlobStorageContext
* blob_storage_context
);
60 static ::IndexedDBDatabaseMetadata
ConvertMetadata(
61 const content::IndexedDBDatabaseMetadata
& metadata
);
63 // BrowserMessageFilter implementation.
64 void OnChannelConnected(int32 peer_pid
) override
;
65 void OnChannelClosing() override
;
66 void OnDestruct() const override
;
67 base::TaskRunner
* OverrideTaskRunnerForMessage(
68 const IPC::Message
& message
) override
;
69 bool OnMessageReceived(const IPC::Message
& message
) override
;
71 void FinishTransaction(int64 host_transaction_id
, bool committed
);
73 // A shortcut for accessing our context.
74 IndexedDBContextImpl
* Context() { return indexed_db_context_
.get(); }
75 storage::BlobStorageContext
* blob_storage_context() const {
76 return blob_storage_context_
->context();
79 // IndexedDBCallbacks call these methods to add the results into the
80 // applicable map. See below for more details.
81 int32
Add(IndexedDBCursor
* cursor
);
82 int32
Add(IndexedDBConnection
* connection
,
84 const GURL
& origin_url
);
86 void RegisterTransactionId(int64 host_transaction_id
, const GURL
& origin_url
);
88 IndexedDBCursor
* GetCursorFromId(int32 ipc_cursor_id
);
90 // These are called to map a 32-bit front-end (renderer-specific) transaction
91 // id to and from a back-end ("host") transaction id that encodes the process
92 // id in the high 32 bits. The mapping is host-specific and ids are validated.
93 int64
HostTransactionId(int64 transaction_id
);
94 int64
RendererTransactionId(int64 host_transaction_id
);
96 // These are called to decode a host transaction ID, for diagnostic purposes.
97 static uint32
TransactionIdToRendererTransactionId(int64 host_transaction_id
);
98 static uint32
TransactionIdToProcessId(int64 host_transaction_id
);
100 std::string
HoldBlobData(const IndexedDBBlobInfo
& blob_info
);
103 // Friends to enable OnDestruct() delegation.
104 friend class BrowserThread
;
105 friend class base::DeleteHelper
<IndexedDBDispatcherHost
>;
107 // Used in nested classes.
108 typedef std::map
<std::string
, std::pair
<storage::BlobDataHandle
*, int>>
110 typedef std::map
<int64
, int64
> TransactionIDToDatabaseIDMap
;
111 typedef std::map
<int64
, uint64
> TransactionIDToSizeMap
;
112 typedef std::map
<int64
, GURL
> TransactionIDToURLMap
;
113 typedef std::map
<int32
, GURL
> WebIDBObjectIDToURLMap
;
115 // IDMap for RefCounted types
116 template <typename RefCountedType
>
119 typedef int32 KeyType
;
124 KeyType
Add(RefCountedType
* data
) {
125 return map_
.Add(new scoped_refptr
<RefCountedType
>(data
));
128 RefCountedType
* Lookup(KeyType id
) {
129 scoped_refptr
<RefCountedType
>* ptr
= map_
.Lookup(id
);
135 void Remove(KeyType id
) { map_
.Remove(id
); }
137 void set_check_on_null_data(bool value
) {
138 map_
.set_check_on_null_data(value
);
142 IDMap
<scoped_refptr
<RefCountedType
>, IDMapOwnPointer
> map_
;
144 DISALLOW_COPY_AND_ASSIGN(RefIDMap
);
147 class DatabaseDispatcherHost
{
149 explicit DatabaseDispatcherHost(IndexedDBDispatcherHost
* parent
);
150 ~DatabaseDispatcherHost();
153 bool OnMessageReceived(const IPC::Message
& message
);
155 void OnCreateObjectStore(
156 const IndexedDBHostMsg_DatabaseCreateObjectStore_Params
& params
);
157 void OnDeleteObjectStore(int32 ipc_database_id
,
158 int64 transaction_id
,
159 int64 object_store_id
);
160 void OnCreateTransaction(
161 const IndexedDBHostMsg_DatabaseCreateTransaction_Params
&);
162 void OnClose(int32 ipc_database_id
);
163 void OnVersionChangeIgnored(int32 ipc_database_id
);
164 void OnDestroyed(int32 ipc_database_id
);
166 void OnGet(const IndexedDBHostMsg_DatabaseGet_Params
& params
);
167 void OnGetAll(const IndexedDBHostMsg_DatabaseGetAll_Params
& params
);
168 // OnPutWrapper starts on the IO thread so that it can grab BlobDataHandles
169 // before posting to the IDB TaskRunner for the rest of the job.
170 void OnPutWrapper(const IndexedDBHostMsg_DatabasePut_Params
& params
);
171 void OnPut(const IndexedDBHostMsg_DatabasePut_Params
& params
,
172 std::vector
<storage::BlobDataHandle
*> handles
);
174 const IndexedDBHostMsg_DatabaseSetIndexKeys_Params
& params
);
175 void OnSetIndexesReady(int32 ipc_database_id
,
176 int64 transaction_id
,
177 int64 object_store_id
,
178 const std::vector
<int64
>& ids
);
179 void OnOpenCursor(const IndexedDBHostMsg_DatabaseOpenCursor_Params
& params
);
180 void OnCount(const IndexedDBHostMsg_DatabaseCount_Params
& params
);
182 const IndexedDBHostMsg_DatabaseDeleteRange_Params
& params
);
183 void OnClear(int32 ipc_thread_id
,
184 int32 ipc_callbacks_id
,
185 int32 ipc_database_id
,
186 int64 transaction_id
,
187 int64 object_store_id
);
189 const IndexedDBHostMsg_DatabaseCreateIndex_Params
& params
);
190 void OnDeleteIndex(int32 ipc_database_id
,
191 int64 transaction_id
,
192 int64 object_store_id
,
195 void OnAbort(int32 ipc_database_id
, int64 transaction_id
);
196 void OnCommit(int32 ipc_database_id
, int64 transaction_id
);
197 IndexedDBDispatcherHost
* parent_
;
198 IDMap
<IndexedDBConnection
, IDMapOwnPointer
> map_
;
199 WebIDBObjectIDToURLMap database_url_map_
;
200 TransactionIDToSizeMap transaction_size_map_
;
201 TransactionIDToURLMap transaction_url_map_
;
202 TransactionIDToDatabaseIDMap transaction_database_map_
;
205 DISALLOW_COPY_AND_ASSIGN(DatabaseDispatcherHost
);
208 class CursorDispatcherHost
{
210 explicit CursorDispatcherHost(IndexedDBDispatcherHost
* parent
);
211 ~CursorDispatcherHost();
213 bool OnMessageReceived(const IPC::Message
& message
);
215 void OnAdvance(int32 ipc_object_store_id
,
217 int32 ipc_callbacks_id
,
219 void OnContinue(int32 ipc_object_store_id
,
221 int32 ipc_callbacks_id
,
222 const IndexedDBKey
& key
,
223 const IndexedDBKey
& primary_key
);
224 void OnPrefetch(int32 ipc_cursor_id
,
226 int32 ipc_callbacks_id
,
228 void OnPrefetchReset(int32 ipc_cursor_id
,
230 int unused_prefetches
);
231 void OnDestroyed(int32 ipc_cursor_id
);
233 IndexedDBDispatcherHost
* parent_
;
234 RefIDMap
<IndexedDBCursor
> map_
;
237 DISALLOW_COPY_AND_ASSIGN(CursorDispatcherHost
);
240 ~IndexedDBDispatcherHost() override
;
243 template <class ReturnType
>
244 ReturnType
* GetOrTerminateProcess(IDMap
<ReturnType
, IDMapOwnPointer
>* map
,
245 int32 ipc_return_object_id
);
246 template <class ReturnType
>
247 ReturnType
* GetOrTerminateProcess(RefIDMap
<ReturnType
>* map
,
248 int32 ipc_return_object_id
);
250 template <typename MapType
>
251 void DestroyObject(MapType
* map
, int32 ipc_object_id
);
253 // Message processing. Most of the work is delegated to the dispatcher hosts
255 void OnIDBFactoryGetDatabaseNames(
256 const IndexedDBHostMsg_FactoryGetDatabaseNames_Params
& p
);
257 void OnIDBFactoryOpen(const IndexedDBHostMsg_FactoryOpen_Params
& p
);
259 void OnIDBFactoryDeleteDatabase(
260 const IndexedDBHostMsg_FactoryDeleteDatabase_Params
& p
);
262 void OnAckReceivedBlobs(const std::vector
<std::string
>& uuids
);
263 void OnPutHelper(const IndexedDBHostMsg_DatabasePut_Params
& params
,
264 std::vector
<storage::BlobDataHandle
*> handles
);
266 void ResetDispatcherHosts();
267 void DropBlobData(const std::string
& uuid
);
269 // The getter holds the context until OnChannelConnected() can be called from
270 // the IO thread, which will extract the net::URLRequestContext from it.
271 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
272 net::URLRequestContext
* request_context_
;
273 scoped_refptr
<IndexedDBContextImpl
> indexed_db_context_
;
274 scoped_refptr
<ChromeBlobStorageContext
> blob_storage_context_
;
276 BlobDataHandleMap blob_data_handle_map_
;
278 // Only access on IndexedDB thread.
279 scoped_ptr
<DatabaseDispatcherHost
> database_dispatcher_host_
;
280 scoped_ptr
<CursorDispatcherHost
> cursor_dispatcher_host_
;
282 // Used to set file permissions for blob storage.
285 DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost
);
288 } // namespace content
290 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_