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_DatabaseOpenCursor_Params
;
30 struct IndexedDBHostMsg_DatabasePut_Params
;
31 struct IndexedDBHostMsg_DatabaseSetIndexKeys_Params
;
32 struct IndexedDBHostMsg_FactoryDeleteDatabase_Params
;
33 struct IndexedDBHostMsg_FactoryGetDatabaseNames_Params
;
34 struct IndexedDBHostMsg_FactoryOpen_Params
;
37 class IndexedDBBlobInfo
;
38 class IndexedDBConnection
;
39 class IndexedDBContextImpl
;
40 class IndexedDBCursor
;
42 class IndexedDBKeyPath
;
43 class IndexedDBKeyRange
;
44 struct IndexedDBDatabaseMetadata
;
46 // Handles all IndexedDB related messages from a particular renderer process.
47 class IndexedDBDispatcherHost
: public BrowserMessageFilter
{
49 // Only call the constructor from the UI thread.
50 IndexedDBDispatcherHost(int ipc_process_id
,
51 net::URLRequestContextGetter
* request_context_getter
,
52 IndexedDBContextImpl
* indexed_db_context
,
53 ChromeBlobStorageContext
* blob_storage_context
);
54 IndexedDBDispatcherHost(int ipc_process_id
,
55 net::URLRequestContext
* request_context
,
56 IndexedDBContextImpl
* indexed_db_context
,
57 ChromeBlobStorageContext
* blob_storage_context
);
59 static ::IndexedDBDatabaseMetadata
ConvertMetadata(
60 const content::IndexedDBDatabaseMetadata
& metadata
);
62 // BrowserMessageFilter implementation.
63 void OnChannelConnected(int32 peer_pid
) override
;
64 void OnChannelClosing() override
;
65 void OnDestruct() const override
;
66 base::TaskRunner
* OverrideTaskRunnerForMessage(
67 const IPC::Message
& message
) override
;
68 bool OnMessageReceived(const IPC::Message
& message
) override
;
70 void FinishTransaction(int64 host_transaction_id
, bool committed
);
72 // A shortcut for accessing our context.
73 IndexedDBContextImpl
* Context() { return indexed_db_context_
.get(); }
74 storage::BlobStorageContext
* blob_storage_context() const {
75 return blob_storage_context_
->context();
78 // IndexedDBCallbacks call these methods to add the results into the
79 // applicable map. See below for more details.
80 int32
Add(IndexedDBCursor
* cursor
);
81 int32
Add(IndexedDBConnection
* connection
,
83 const GURL
& origin_url
);
85 void RegisterTransactionId(int64 host_transaction_id
, const GURL
& origin_url
);
87 IndexedDBCursor
* GetCursorFromId(int32 ipc_cursor_id
);
89 // These are called to map a 32-bit front-end (renderer-specific) transaction
90 // id to and from a back-end ("host") transaction id that encodes the process
91 // id in the high 32 bits. The mapping is host-specific and ids are validated.
92 int64
HostTransactionId(int64 transaction_id
);
93 int64
RendererTransactionId(int64 host_transaction_id
);
95 // These are called to decode a host transaction ID, for diagnostic purposes.
96 static uint32
TransactionIdToRendererTransactionId(int64 host_transaction_id
);
97 static uint32
TransactionIdToProcessId(int64 host_transaction_id
);
99 std::string
HoldBlobData(const IndexedDBBlobInfo
& blob_info
);
102 // Friends to enable OnDestruct() delegation.
103 friend class BrowserThread
;
104 friend class base::DeleteHelper
<IndexedDBDispatcherHost
>;
106 // Used in nested classes.
107 typedef std::map
<std::string
, std::pair
<storage::BlobDataHandle
*, int>>
109 typedef std::map
<int64
, int64
> TransactionIDToDatabaseIDMap
;
110 typedef std::map
<int64
, uint64
> TransactionIDToSizeMap
;
111 typedef std::map
<int64
, GURL
> TransactionIDToURLMap
;
112 typedef std::map
<int32
, GURL
> WebIDBObjectIDToURLMap
;
114 // IDMap for RefCounted types
115 template <typename RefCountedType
>
118 typedef int32 KeyType
;
123 KeyType
Add(RefCountedType
* data
) {
124 return map_
.Add(new scoped_refptr
<RefCountedType
>(data
));
127 RefCountedType
* Lookup(KeyType id
) {
128 scoped_refptr
<RefCountedType
>* ptr
= map_
.Lookup(id
);
134 void Remove(KeyType id
) { map_
.Remove(id
); }
136 void set_check_on_null_data(bool value
) {
137 map_
.set_check_on_null_data(value
);
141 IDMap
<scoped_refptr
<RefCountedType
>, IDMapOwnPointer
> map_
;
143 DISALLOW_COPY_AND_ASSIGN(RefIDMap
);
146 class DatabaseDispatcherHost
{
148 explicit DatabaseDispatcherHost(IndexedDBDispatcherHost
* parent
);
149 ~DatabaseDispatcherHost();
152 bool OnMessageReceived(const IPC::Message
& message
);
154 void OnCreateObjectStore(
155 const IndexedDBHostMsg_DatabaseCreateObjectStore_Params
& params
);
156 void OnDeleteObjectStore(int32 ipc_database_id
,
157 int64 transaction_id
,
158 int64 object_store_id
);
159 void OnCreateTransaction(
160 const IndexedDBHostMsg_DatabaseCreateTransaction_Params
&);
161 void OnClose(int32 ipc_database_id
);
162 void OnVersionChangeIgnored(int32 ipc_database_id
);
163 void OnDestroyed(int32 ipc_database_id
);
165 void OnGet(const IndexedDBHostMsg_DatabaseGet_Params
& params
);
166 // OnPutWrapper starts on the IO thread so that it can grab BlobDataHandles
167 // before posting to the IDB TaskRunner for the rest of the job.
168 void OnPutWrapper(const IndexedDBHostMsg_DatabasePut_Params
& params
);
169 void OnPut(const IndexedDBHostMsg_DatabasePut_Params
& params
,
170 std::vector
<storage::BlobDataHandle
*> handles
);
172 const IndexedDBHostMsg_DatabaseSetIndexKeys_Params
& params
);
173 void OnSetIndexesReady(int32 ipc_database_id
,
174 int64 transaction_id
,
175 int64 object_store_id
,
176 const std::vector
<int64
>& ids
);
177 void OnOpenCursor(const IndexedDBHostMsg_DatabaseOpenCursor_Params
& params
);
178 void OnCount(const IndexedDBHostMsg_DatabaseCount_Params
& params
);
180 const IndexedDBHostMsg_DatabaseDeleteRange_Params
& params
);
181 void OnClear(int32 ipc_thread_id
,
182 int32 ipc_callbacks_id
,
183 int32 ipc_database_id
,
184 int64 transaction_id
,
185 int64 object_store_id
);
187 const IndexedDBHostMsg_DatabaseCreateIndex_Params
& params
);
188 void OnDeleteIndex(int32 ipc_database_id
,
189 int64 transaction_id
,
190 int64 object_store_id
,
193 void OnAbort(int32 ipc_database_id
, int64 transaction_id
);
194 void OnCommit(int32 ipc_database_id
, int64 transaction_id
);
195 IndexedDBDispatcherHost
* parent_
;
196 IDMap
<IndexedDBConnection
, IDMapOwnPointer
> map_
;
197 WebIDBObjectIDToURLMap database_url_map_
;
198 TransactionIDToSizeMap transaction_size_map_
;
199 TransactionIDToURLMap transaction_url_map_
;
200 TransactionIDToDatabaseIDMap transaction_database_map_
;
203 DISALLOW_COPY_AND_ASSIGN(DatabaseDispatcherHost
);
206 class CursorDispatcherHost
{
208 explicit CursorDispatcherHost(IndexedDBDispatcherHost
* parent
);
209 ~CursorDispatcherHost();
211 bool OnMessageReceived(const IPC::Message
& message
);
213 void OnAdvance(int32 ipc_object_store_id
,
215 int32 ipc_callbacks_id
,
217 void OnContinue(int32 ipc_object_store_id
,
219 int32 ipc_callbacks_id
,
220 const IndexedDBKey
& key
,
221 const IndexedDBKey
& primary_key
);
222 void OnPrefetch(int32 ipc_cursor_id
,
224 int32 ipc_callbacks_id
,
226 void OnPrefetchReset(int32 ipc_cursor_id
,
228 int unused_prefetches
);
229 void OnDestroyed(int32 ipc_cursor_id
);
231 IndexedDBDispatcherHost
* parent_
;
232 RefIDMap
<IndexedDBCursor
> map_
;
235 DISALLOW_COPY_AND_ASSIGN(CursorDispatcherHost
);
238 ~IndexedDBDispatcherHost() override
;
241 template <class ReturnType
>
242 ReturnType
* GetOrTerminateProcess(IDMap
<ReturnType
, IDMapOwnPointer
>* map
,
243 int32 ipc_return_object_id
);
244 template <class ReturnType
>
245 ReturnType
* GetOrTerminateProcess(RefIDMap
<ReturnType
>* map
,
246 int32 ipc_return_object_id
);
248 template <typename MapType
>
249 void DestroyObject(MapType
* map
, int32 ipc_object_id
);
251 // Message processing. Most of the work is delegated to the dispatcher hosts
253 void OnIDBFactoryGetDatabaseNames(
254 const IndexedDBHostMsg_FactoryGetDatabaseNames_Params
& p
);
255 void OnIDBFactoryOpen(const IndexedDBHostMsg_FactoryOpen_Params
& p
);
257 void OnIDBFactoryDeleteDatabase(
258 const IndexedDBHostMsg_FactoryDeleteDatabase_Params
& p
);
260 void OnAckReceivedBlobs(const std::vector
<std::string
>& uuids
);
261 void OnPutHelper(const IndexedDBHostMsg_DatabasePut_Params
& params
,
262 std::vector
<storage::BlobDataHandle
*> handles
);
264 void ResetDispatcherHosts();
265 void DropBlobData(const std::string
& uuid
);
267 // The getter holds the context until OnChannelConnected() can be called from
268 // the IO thread, which will extract the net::URLRequestContext from it.
269 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
270 net::URLRequestContext
* request_context_
;
271 scoped_refptr
<IndexedDBContextImpl
> indexed_db_context_
;
272 scoped_refptr
<ChromeBlobStorageContext
> blob_storage_context_
;
274 BlobDataHandleMap blob_data_handle_map_
;
276 // Only access on IndexedDB thread.
277 scoped_ptr
<DatabaseDispatcherHost
> database_dispatcher_host_
;
278 scoped_ptr
<CursorDispatcherHost
> cursor_dispatcher_host_
;
280 // Used to set file permissions for blob storage.
283 DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost
);
286 } // namespace content
288 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_