Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_dispatcher_host.h
blob7ce6cffdbc15fd858c8be32a819a69e252245af1
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_
8 #include <map>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/id_map.h"
13 #include "base/memory/ref_counted.h"
14 #include "content/browser/fileapi/chrome_blob_storage_context.h"
15 #include "content/public/browser/browser_message_filter.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "url/gurl.h"
18 #include "webkit/browser/blob/blob_data_handle.h"
20 struct IndexedDBDatabaseMetadata;
21 struct IndexedDBHostMsg_DatabaseCount_Params;
22 struct IndexedDBHostMsg_DatabaseCreateIndex_Params;
23 struct IndexedDBHostMsg_DatabaseCreateObjectStore_Params;
24 struct IndexedDBHostMsg_DatabaseCreateTransaction_Params;
25 struct IndexedDBHostMsg_DatabaseDeleteRange_Params;
26 struct IndexedDBHostMsg_DatabaseGet_Params;
27 struct IndexedDBHostMsg_DatabaseOpenCursor_Params;
28 struct IndexedDBHostMsg_DatabasePut_Params;
29 struct IndexedDBHostMsg_DatabaseSetIndexKeys_Params;
30 struct IndexedDBHostMsg_FactoryDeleteDatabase_Params;
31 struct IndexedDBHostMsg_FactoryGetDatabaseNames_Params;
32 struct IndexedDBHostMsg_FactoryOpen_Params;
34 namespace content {
35 class IndexedDBConnection;
36 class IndexedDBContextImpl;
37 class IndexedDBCursor;
38 class IndexedDBKey;
39 class IndexedDBKeyPath;
40 class IndexedDBKeyRange;
41 struct IndexedDBDatabaseMetadata;
43 // Handles all IndexedDB related messages from a particular renderer process.
44 class IndexedDBDispatcherHost : public BrowserMessageFilter {
45 public:
46 // Only call the constructor from the UI thread.
47 IndexedDBDispatcherHost(int ipc_process_id,
48 net::URLRequestContextGetter* request_context_getter,
49 IndexedDBContextImpl* indexed_db_context,
50 ChromeBlobStorageContext* blob_storage_context);
51 IndexedDBDispatcherHost(int ipc_process_id,
52 net::URLRequestContext* request_context,
53 IndexedDBContextImpl* indexed_db_context,
54 ChromeBlobStorageContext* blob_storage_context);
56 static ::IndexedDBDatabaseMetadata ConvertMetadata(
57 const content::IndexedDBDatabaseMetadata& metadata);
59 // BrowserMessageFilter implementation.
60 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
61 virtual void OnChannelClosing() OVERRIDE;
62 virtual void OnDestruct() const OVERRIDE;
63 virtual base::TaskRunner* OverrideTaskRunnerForMessage(
64 const IPC::Message& message) OVERRIDE;
65 virtual bool OnMessageReceived(const IPC::Message& message,
66 bool* message_was_ok) OVERRIDE;
68 void FinishTransaction(int64 host_transaction_id, bool committed);
70 // A shortcut for accessing our context.
71 IndexedDBContextImpl* Context() { return indexed_db_context_; }
72 webkit_blob::BlobStorageContext* blob_storage_context() const {
73 return blob_storage_context_->context();
76 // IndexedDBCallbacks call these methods to add the results into the
77 // applicable map. See below for more details.
78 int32 Add(IndexedDBCursor* cursor);
79 int32 Add(IndexedDBConnection* connection,
80 int32 ipc_thread_id,
81 const GURL& origin_url);
83 void RegisterTransactionId(int64 host_transaction_id, const GURL& origin_url);
85 IndexedDBCursor* GetCursorFromId(int32 ipc_cursor_id);
87 // These are called to map a 32-bit front-end (renderer-specific) transaction
88 // id to and from a back-end ("host") transaction id that encodes the process
89 // id in the high 32 bits. The mapping is host-specific and ids are validated.
90 int64 HostTransactionId(int64 transaction_id);
91 int64 RendererTransactionId(int64 host_transaction_id);
93 // These are called to decode a host transaction ID, for diagnostic purposes.
94 static uint32 TransactionIdToRendererTransactionId(int64 host_transaction_id);
95 static uint32 TransactionIdToProcessId(int64 host_transaction_id);
97 void HoldBlobDataHandle(
98 const std::string& uuid,
99 scoped_ptr<webkit_blob::BlobDataHandle>& blob_data_handle);
100 void DropBlobDataHandle(const std::string& uuid);
102 private:
103 // Friends to enable OnDestruct() delegation.
104 friend class BrowserThread;
105 friend class base::DeleteHelper<IndexedDBDispatcherHost>;
107 virtual ~IndexedDBDispatcherHost();
109 // Message processing. Most of the work is delegated to the dispatcher hosts
110 // below.
111 void OnIDBFactoryGetDatabaseNames(
112 const IndexedDBHostMsg_FactoryGetDatabaseNames_Params& p);
113 void OnIDBFactoryOpen(const IndexedDBHostMsg_FactoryOpen_Params& p);
115 void OnIDBFactoryDeleteDatabase(
116 const IndexedDBHostMsg_FactoryDeleteDatabase_Params& p);
118 void OnAckReceivedBlobs(const std::vector<std::string>& uuids);
119 void OnPutHelper(const IndexedDBHostMsg_DatabasePut_Params& params,
120 std::vector<webkit_blob::BlobDataHandle*> handles);
122 void ResetDispatcherHosts();
124 // IDMap for RefCounted types
125 template <typename RefCountedType>
126 class RefIDMap {
127 private:
128 typedef int32 KeyType;
130 public:
131 RefIDMap() {}
132 ~RefIDMap() {}
134 KeyType Add(RefCountedType* data) {
135 return map_.Add(new scoped_refptr<RefCountedType>(data));
138 RefCountedType* Lookup(KeyType id) {
139 scoped_refptr<RefCountedType>* ptr = map_.Lookup(id);
140 if (ptr == NULL)
141 return NULL;
142 return ptr->get();
145 void Remove(KeyType id) { map_.Remove(id); }
147 void set_check_on_null_data(bool value) {
148 map_.set_check_on_null_data(value);
151 private:
152 IDMap<scoped_refptr<RefCountedType>, IDMapOwnPointer> map_;
155 // Helper templates.
156 template <class ReturnType>
157 ReturnType* GetOrTerminateProcess(IDMap<ReturnType, IDMapOwnPointer>* map,
158 int32 ipc_return_object_id);
159 template <class ReturnType>
160 ReturnType* GetOrTerminateProcess(RefIDMap<ReturnType>* map,
161 int32 ipc_return_object_id);
163 template <typename MapType>
164 void DestroyObject(MapType* map, int32 ipc_object_id);
166 // Used in nested classes.
167 typedef std::map<int32, GURL> WebIDBObjectIDToURLMap;
169 typedef std::map<int64, GURL> TransactionIDToURLMap;
170 typedef std::map<int64, uint64> TransactionIDToSizeMap;
171 typedef std::map<int64, int64> TransactionIDToDatabaseIDMap;
173 class DatabaseDispatcherHost {
174 public:
175 explicit DatabaseDispatcherHost(IndexedDBDispatcherHost* parent);
176 ~DatabaseDispatcherHost();
178 void CloseAll();
179 bool OnMessageReceived(const IPC::Message& message, bool* msg_is_ok);
181 void OnCreateObjectStore(
182 const IndexedDBHostMsg_DatabaseCreateObjectStore_Params& params);
183 void OnDeleteObjectStore(int32 ipc_database_id,
184 int64 transaction_id,
185 int64 object_store_id);
186 void OnCreateTransaction(
187 const IndexedDBHostMsg_DatabaseCreateTransaction_Params&);
188 void OnClose(int32 ipc_database_id);
189 void OnDestroyed(int32 ipc_database_id);
191 void OnGet(const IndexedDBHostMsg_DatabaseGet_Params& params);
192 // OnPutWrapper starts on the IO thread so that it can grab BlobDataHandles
193 // before posting to the IDB TaskRunner for the rest of the job.
194 void OnPutWrapper(const IndexedDBHostMsg_DatabasePut_Params& params);
195 void OnPut(const IndexedDBHostMsg_DatabasePut_Params& params,
196 std::vector<webkit_blob::BlobDataHandle*> handles);
197 void OnSetIndexKeys(
198 const IndexedDBHostMsg_DatabaseSetIndexKeys_Params& params);
199 void OnSetIndexesReady(int32 ipc_database_id,
200 int64 transaction_id,
201 int64 object_store_id,
202 const std::vector<int64>& ids);
203 void OnOpenCursor(const IndexedDBHostMsg_DatabaseOpenCursor_Params& params);
204 void OnCount(const IndexedDBHostMsg_DatabaseCount_Params& params);
205 void OnDeleteRange(
206 const IndexedDBHostMsg_DatabaseDeleteRange_Params& params);
207 void OnClear(int32 ipc_thread_id,
208 int32 ipc_callbacks_id,
209 int32 ipc_database_id,
210 int64 transaction_id,
211 int64 object_store_id);
212 void OnCreateIndex(
213 const IndexedDBHostMsg_DatabaseCreateIndex_Params& params);
214 void OnDeleteIndex(int32 ipc_database_id,
215 int64 transaction_id,
216 int64 object_store_id,
217 int64 index_id);
219 void OnAbort(int32 ipc_database_id, int64 transaction_id);
220 void OnCommit(int32 ipc_database_id, int64 transaction_id);
221 IndexedDBDispatcherHost* parent_;
222 IDMap<IndexedDBConnection, IDMapOwnPointer> map_;
223 WebIDBObjectIDToURLMap database_url_map_;
224 TransactionIDToSizeMap transaction_size_map_;
225 TransactionIDToURLMap transaction_url_map_;
226 TransactionIDToDatabaseIDMap transaction_database_map_;
229 class CursorDispatcherHost {
230 public:
231 explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent);
232 ~CursorDispatcherHost();
234 bool OnMessageReceived(const IPC::Message& message, bool* msg_is_ok);
236 void OnAdvance(int32 ipc_object_store_id,
237 int32 ipc_thread_id,
238 int32 ipc_callbacks_id,
239 unsigned long count);
240 void OnContinue(int32 ipc_object_store_id,
241 int32 ipc_thread_id,
242 int32 ipc_callbacks_id,
243 const IndexedDBKey& key,
244 const IndexedDBKey& primary_key);
245 void OnPrefetch(int32 ipc_cursor_id,
246 int32 ipc_thread_id,
247 int32 ipc_callbacks_id,
248 int n);
249 void OnPrefetchReset(int32 ipc_cursor_id,
250 int used_prefetches,
251 int unused_prefetches);
252 void OnDestroyed(int32 ipc_cursor_id);
254 IndexedDBDispatcherHost* parent_;
255 RefIDMap<IndexedDBCursor> map_;
258 // The getter holds the context until OnChannelConnected() can be called from
259 // the IO thread, which will extract the net::URLRequestContext from it.
260 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
261 net::URLRequestContext* request_context_;
262 scoped_refptr<IndexedDBContextImpl> indexed_db_context_;
263 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;
265 typedef std::map<std::string, webkit_blob::BlobDataHandle*> BlobDataHandleMap;
266 BlobDataHandleMap blob_data_handle_map_;
268 // Only access on IndexedDB thread.
269 scoped_ptr<DatabaseDispatcherHost> database_dispatcher_host_;
270 scoped_ptr<CursorDispatcherHost> cursor_dispatcher_host_;
272 // Used to set file permissions for blob storage.
273 int ipc_process_id_;
275 DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost);
278 } // namespace content
280 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_