- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / indexed_db / indexed_db_dispatcher_host.h
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.
4
5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_
7
8 #include <map>
9 #include <vector>
10
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"
15 #include "url/gurl.h"
16
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;
30
31 namespace content {
32 class IndexedDBConnection;
33 class IndexedDBContextImpl;
34 class IndexedDBCursor;
35 class IndexedDBKey;
36 class IndexedDBKeyPath;
37 class IndexedDBKeyRange;
38 struct IndexedDBDatabaseMetadata;
39
40 // Handles all IndexedDB related messages from a particular renderer process.
41 class IndexedDBDispatcherHost : public BrowserMessageFilter {
42  public:
43   // Only call the constructor from the UI thread.
44   IndexedDBDispatcherHost(int ipc_process_id,
45                           IndexedDBContextImpl* indexed_db_context);
46
47   static ::IndexedDBDatabaseMetadata ConvertMetadata(
48       const content::IndexedDBDatabaseMetadata& metadata);
49
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;
57
58   void FinishTransaction(int64 host_transaction_id, bool committed);
59
60   // A shortcut for accessing our context.
61   IndexedDBContextImpl* Context() { return indexed_db_context_; }
62
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,
67             int32 ipc_thread_id,
68             const GURL& origin_url);
69
70   void RegisterTransactionId(int64 host_transaction_id, const GURL& origin_url);
71
72   IndexedDBCursor* GetCursorFromId(int32 ipc_cursor_id);
73
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);
79
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);
83
84  private:
85   // Friends to enable OnDestruct() delegation.
86   friend class BrowserThread;
87   friend class base::DeleteHelper<IndexedDBDispatcherHost>;
88
89   virtual ~IndexedDBDispatcherHost();
90
91   // Message processing. Most of the work is delegated to the dispatcher hosts
92   // below.
93   void OnIDBFactoryGetDatabaseNames(
94       const IndexedDBHostMsg_FactoryGetDatabaseNames_Params& p);
95   void OnIDBFactoryOpen(const IndexedDBHostMsg_FactoryOpen_Params& p);
96
97   void OnIDBFactoryDeleteDatabase(
98       const IndexedDBHostMsg_FactoryDeleteDatabase_Params& p);
99
100   void ResetDispatcherHosts();
101
102   // IDMap for RefCounted types
103   template <typename RefCountedType>
104   class RefIDMap {
105    private:
106     typedef int32 KeyType;
107
108    public:
109     RefIDMap() {}
110     ~RefIDMap() {}
111
112     KeyType Add(RefCountedType* data) {
113       return map_.Add(new scoped_refptr<RefCountedType>(data));
114     }
115
116     RefCountedType* Lookup(KeyType id) {
117       scoped_refptr<RefCountedType>* ptr = map_.Lookup(id);
118       if (ptr == NULL)
119         return NULL;
120       return ptr->get();
121     }
122
123     void Remove(KeyType id) { map_.Remove(id); }
124
125     void set_check_on_null_data(bool value) {
126       map_.set_check_on_null_data(value);
127     }
128
129    private:
130     IDMap<scoped_refptr<RefCountedType>, IDMapOwnPointer> map_;
131   };
132
133   // Helper templates.
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);
140
141   template <typename MapType>
142   void DestroyObject(MapType* map, int32 ipc_object_id);
143
144   // Used in nested classes.
145   typedef std::map<int32, GURL> WebIDBObjectIDToURLMap;
146
147   typedef std::map<int64, GURL> TransactionIDToURLMap;
148   typedef std::map<int64, uint64> TransactionIDToSizeMap;
149   typedef std::map<int64, int64> TransactionIDToDatabaseIDMap;
150
151   class DatabaseDispatcherHost {
152    public:
153     explicit DatabaseDispatcherHost(IndexedDBDispatcherHost* parent);
154     ~DatabaseDispatcherHost();
155
156     void CloseAll();
157     bool OnMessageReceived(const IPC::Message& message, bool* msg_is_ok);
158     void Send(IPC::Message* message);
159
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,
168                 int32 ipc_thread_id,
169                 int32 ipc_callbacks_id);
170     void OnClose(int32 ipc_database_id);
171     void OnDestroyed(int32 ipc_database_id);
172
173     void OnGet(const IndexedDBHostMsg_DatabaseGet_Params& params);
174     void OnPut(const IndexedDBHostMsg_DatabasePut_Params& params);
175     void OnSetIndexKeys(
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);
183     void OnDeleteRange(
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);
190     void OnCreateIndex(
191         const IndexedDBHostMsg_DatabaseCreateIndex_Params& params);
192     void OnDeleteIndex(int32 ipc_database_id,
193                        int64 transaction_id,
194                        int64 object_store_id,
195                        int64 index_id);
196
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_;
205   };
206
207   class CursorDispatcherHost {
208    public:
209     explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent);
210     ~CursorDispatcherHost();
211
212     bool OnMessageReceived(const IPC::Message& message, bool* msg_is_ok);
213     void Send(IPC::Message* message);
214
215     void OnAdvance(int32 ipc_object_store_id,
216                    int32 ipc_thread_id,
217                    int32 ipc_callbacks_id,
218                    unsigned long count);
219     void OnContinue(int32 ipc_object_store_id,
220                     int32 ipc_thread_id,
221                     int32 ipc_callbacks_id,
222                     const IndexedDBKey& key);
223     void OnPrefetch(int32 ipc_cursor_id,
224                     int32 ipc_thread_id,
225                     int32 ipc_callbacks_id,
226                     int n);
227     void OnPrefetchReset(int32 ipc_cursor_id,
228                          int used_prefetches,
229                          int unused_prefetches);
230     void OnDestroyed(int32 ipc_cursor_id);
231
232     IndexedDBDispatcherHost* parent_;
233     RefIDMap<IndexedDBCursor> map_;
234   };
235
236   scoped_refptr<IndexedDBContextImpl> indexed_db_context_;
237
238   // Only access on IndexedDB thread.
239   scoped_ptr<DatabaseDispatcherHost> database_dispatcher_host_;
240   scoped_ptr<CursorDispatcherHost> cursor_dispatcher_host_;
241
242   // Used to dispatch messages to the correct view host.
243   int ipc_process_id_;
244
245   DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost);
246 };
247
248 }  // namespace content
249
250 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_