Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_dispatcher_host.h
blobeb727d15222a63c328aa141fb306b1718791eb57
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 <string>
10 #include <utility>
11 #include <vector>
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"
20 #include "url/gurl.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;
36 namespace content {
37 class IndexedDBBlobInfo;
38 class IndexedDBConnection;
39 class IndexedDBContextImpl;
40 class IndexedDBCursor;
41 class IndexedDBKey;
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 {
48 public:
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,
82 int32 ipc_thread_id,
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);
101 private:
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>>
108 BlobDataHandleMap;
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>
116 class RefIDMap {
117 public:
118 typedef int32 KeyType;
120 RefIDMap() {}
121 ~RefIDMap() {}
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);
129 if (ptr == NULL)
130 return NULL;
131 return ptr->get();
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);
140 private:
141 IDMap<scoped_refptr<RefCountedType>, IDMapOwnPointer> map_;
143 DISALLOW_COPY_AND_ASSIGN(RefIDMap);
146 class DatabaseDispatcherHost {
147 public:
148 explicit DatabaseDispatcherHost(IndexedDBDispatcherHost* parent);
149 ~DatabaseDispatcherHost();
151 void CloseAll();
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);
171 void OnSetIndexKeys(
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);
179 void OnDeleteRange(
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);
186 void OnCreateIndex(
187 const IndexedDBHostMsg_DatabaseCreateIndex_Params& params);
188 void OnDeleteIndex(int32 ipc_database_id,
189 int64 transaction_id,
190 int64 object_store_id,
191 int64 index_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_;
202 private:
203 DISALLOW_COPY_AND_ASSIGN(DatabaseDispatcherHost);
206 class CursorDispatcherHost {
207 public:
208 explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent);
209 ~CursorDispatcherHost();
211 bool OnMessageReceived(const IPC::Message& message);
213 void OnAdvance(int32 ipc_object_store_id,
214 int32 ipc_thread_id,
215 int32 ipc_callbacks_id,
216 uint32 count);
217 void OnContinue(int32 ipc_object_store_id,
218 int32 ipc_thread_id,
219 int32 ipc_callbacks_id,
220 const IndexedDBKey& key,
221 const IndexedDBKey& primary_key);
222 void OnPrefetch(int32 ipc_cursor_id,
223 int32 ipc_thread_id,
224 int32 ipc_callbacks_id,
225 int n);
226 void OnPrefetchReset(int32 ipc_cursor_id,
227 int used_prefetches,
228 int unused_prefetches);
229 void OnDestroyed(int32 ipc_cursor_id);
231 IndexedDBDispatcherHost* parent_;
232 RefIDMap<IndexedDBCursor> map_;
234 private:
235 DISALLOW_COPY_AND_ASSIGN(CursorDispatcherHost);
238 ~IndexedDBDispatcherHost() override;
240 // Helper templates.
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
252 // below.
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.
281 int ipc_process_id_;
283 DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost);
286 } // namespace content
288 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_