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_
11 #include "base/basictypes.h"
12 #include "base/id_map.h"
13 #include "base/memory/ref_counted.h"
14 #include "content/public/browser/browser_message_filter.h"
17 struct IndexedDBDatabaseMetadata;
18 struct IndexedDBHostMsg_DatabaseCount_Params;
19 struct IndexedDBHostMsg_DatabaseCreateIndex_Params;
20 struct IndexedDBHostMsg_DatabaseCreateObjectStore_Params;
21 struct IndexedDBHostMsg_DatabaseCreateTransaction_Params;
22 struct IndexedDBHostMsg_DatabaseDeleteRange_Params;
23 struct IndexedDBHostMsg_DatabaseGet_Params;
24 struct IndexedDBHostMsg_DatabaseOpenCursor_Params;
25 struct IndexedDBHostMsg_DatabasePut_Params;
26 struct IndexedDBHostMsg_DatabaseSetIndexKeys_Params;
27 struct IndexedDBHostMsg_FactoryDeleteDatabase_Params;
28 struct IndexedDBHostMsg_FactoryGetDatabaseNames_Params;
29 struct IndexedDBHostMsg_FactoryOpen_Params;
32 class IndexedDBConnection;
33 class IndexedDBContextImpl;
34 class IndexedDBCursor;
36 class IndexedDBKeyPath;
37 class IndexedDBKeyRange;
38 struct IndexedDBDatabaseMetadata;
40 // Handles all IndexedDB related messages from a particular renderer process.
41 class IndexedDBDispatcherHost : public BrowserMessageFilter {
43 // Only call the constructor from the UI thread.
44 IndexedDBDispatcherHost(int ipc_process_id,
45 IndexedDBContextImpl* indexed_db_context);
47 static ::IndexedDBDatabaseMetadata ConvertMetadata(
48 const content::IndexedDBDatabaseMetadata& metadata);
50 // BrowserMessageFilter implementation.
51 virtual void OnChannelClosing() OVERRIDE;
52 virtual void OnDestruct() const OVERRIDE;
53 virtual base::TaskRunner* OverrideTaskRunnerForMessage(
54 const IPC::Message& message) OVERRIDE;
55 virtual bool OnMessageReceived(const IPC::Message& message,
56 bool* message_was_ok) OVERRIDE;
58 void FinishTransaction(int64 host_transaction_id, bool committed);
60 // A shortcut for accessing our context.
61 IndexedDBContextImpl* Context() { return indexed_db_context_; }
63 // IndexedDBCallbacks call these methods to add the results into the
64 // applicable map. See below for more details.
65 int32 Add(IndexedDBCursor* cursor);
66 int32 Add(IndexedDBConnection* connection,
68 const GURL& origin_url);
70 void RegisterTransactionId(int64 host_transaction_id, const GURL& origin_url);
72 IndexedDBCursor* GetCursorFromId(int32 ipc_cursor_id);
74 // These are called to map a 32-bit front-end (renderer-specific) transaction
75 // id to and from a back-end ("host") transaction id that encodes the process
76 // id in the high 32 bits. The mapping is host-specific and ids are validated.
77 int64 HostTransactionId(int64 transaction_id);
78 int64 RendererTransactionId(int64 host_transaction_id);
80 // These are called to decode a host transaction ID, for diagnostic purposes.
81 static uint32 TransactionIdToRendererTransactionId(int64 host_transaction_id);
82 static uint32 TransactionIdToProcessId(int64 host_transaction_id);
85 // Friends to enable OnDestruct() delegation.
86 friend class BrowserThread;
87 friend class base::DeleteHelper<IndexedDBDispatcherHost>;
89 virtual ~IndexedDBDispatcherHost();
91 // Message processing. Most of the work is delegated to the dispatcher hosts
93 void OnIDBFactoryGetDatabaseNames(
94 const IndexedDBHostMsg_FactoryGetDatabaseNames_Params& p);
95 void OnIDBFactoryOpen(const IndexedDBHostMsg_FactoryOpen_Params& p);
97 void OnIDBFactoryDeleteDatabase(
98 const IndexedDBHostMsg_FactoryDeleteDatabase_Params& p);
100 void ResetDispatcherHosts();
102 // IDMap for RefCounted types
103 template <typename RefCountedType>
106 typedef int32 KeyType;
112 KeyType Add(RefCountedType* data) {
113 return map_.Add(new scoped_refptr<RefCountedType>(data));
116 RefCountedType* Lookup(KeyType id) {
117 scoped_refptr<RefCountedType>* ptr = map_.Lookup(id);
123 void Remove(KeyType id) { map_.Remove(id); }
125 void set_check_on_null_data(bool value) {
126 map_.set_check_on_null_data(value);
130 IDMap<scoped_refptr<RefCountedType>, IDMapOwnPointer> map_;
134 template <class ReturnType>
135 ReturnType* GetOrTerminateProcess(IDMap<ReturnType, IDMapOwnPointer>* map,
136 int32 ipc_return_object_id);
137 template <class ReturnType>
138 ReturnType* GetOrTerminateProcess(RefIDMap<ReturnType>* map,
139 int32 ipc_return_object_id);
141 template <typename MapType>
142 void DestroyObject(MapType* map, int32 ipc_object_id);
144 // Used in nested classes.
145 typedef std::map<int32, GURL> WebIDBObjectIDToURLMap;
147 typedef std::map<int64, GURL> TransactionIDToURLMap;
148 typedef std::map<int64, uint64> TransactionIDToSizeMap;
149 typedef std::map<int64, int64> TransactionIDToDatabaseIDMap;
151 class DatabaseDispatcherHost {
153 explicit DatabaseDispatcherHost(IndexedDBDispatcherHost* parent);
154 ~DatabaseDispatcherHost();
157 bool OnMessageReceived(const IPC::Message& message, bool* msg_is_ok);
158 void Send(IPC::Message* message);
160 void OnCreateObjectStore(
161 const IndexedDBHostMsg_DatabaseCreateObjectStore_Params& params);
162 void OnDeleteObjectStore(int32 ipc_database_id,
163 int64 transaction_id,
164 int64 object_store_id);
165 void OnCreateTransaction(
166 const IndexedDBHostMsg_DatabaseCreateTransaction_Params&);
167 void OnOpen(int32 ipc_database_id,
169 int32 ipc_callbacks_id);
170 void OnClose(int32 ipc_database_id);
171 void OnDestroyed(int32 ipc_database_id);
173 void OnGet(const IndexedDBHostMsg_DatabaseGet_Params& params);
174 void OnPut(const IndexedDBHostMsg_DatabasePut_Params& params);
176 const IndexedDBHostMsg_DatabaseSetIndexKeys_Params& params);
177 void OnSetIndexesReady(int32 ipc_database_id,
178 int64 transaction_id,
179 int64 object_store_id,
180 const std::vector<int64>& ids);
181 void OnOpenCursor(const IndexedDBHostMsg_DatabaseOpenCursor_Params& params);
182 void OnCount(const IndexedDBHostMsg_DatabaseCount_Params& params);
184 const IndexedDBHostMsg_DatabaseDeleteRange_Params& params);
185 void OnClear(int32 ipc_thread_id,
186 int32 ipc_callbacks_id,
187 int32 ipc_database_id,
188 int64 transaction_id,
189 int64 object_store_id);
191 const IndexedDBHostMsg_DatabaseCreateIndex_Params& params);
192 void OnDeleteIndex(int32 ipc_database_id,
193 int64 transaction_id,
194 int64 object_store_id,
197 void OnAbort(int32 ipc_database_id, int64 transaction_id);
198 void OnCommit(int32 ipc_database_id, int64 transaction_id);
199 IndexedDBDispatcherHost* parent_;
200 IDMap<IndexedDBConnection, IDMapOwnPointer> map_;
201 WebIDBObjectIDToURLMap database_url_map_;
202 TransactionIDToSizeMap transaction_size_map_;
203 TransactionIDToURLMap transaction_url_map_;
204 TransactionIDToDatabaseIDMap transaction_database_map_;
207 class CursorDispatcherHost {
209 explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent);
210 ~CursorDispatcherHost();
212 bool OnMessageReceived(const IPC::Message& message, bool* msg_is_ok);
213 void Send(IPC::Message* message);
215 void OnAdvance(int32 ipc_object_store_id,
217 int32 ipc_callbacks_id,
218 unsigned long count);
219 void OnContinue(int32 ipc_object_store_id,
221 int32 ipc_callbacks_id,
222 const IndexedDBKey& key);
223 void OnPrefetch(int32 ipc_cursor_id,
225 int32 ipc_callbacks_id,
227 void OnPrefetchReset(int32 ipc_cursor_id,
229 int unused_prefetches);
230 void OnDestroyed(int32 ipc_cursor_id);
232 IndexedDBDispatcherHost* parent_;
233 RefIDMap<IndexedDBCursor> map_;
236 scoped_refptr<IndexedDBContextImpl> indexed_db_context_;
238 // Only access on IndexedDB thread.
239 scoped_ptr<DatabaseDispatcherHost> database_dispatcher_host_;
240 scoped_ptr<CursorDispatcherHost> cursor_dispatcher_host_;
242 // Used to dispatch messages to the correct view host.
245 DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost);
248 } // namespace content
250 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_