summaryrefslogtreecommitdiffstats
path: root/chrome/browser/in_process_webkit/indexed_db_dispatcher_host.h
blob: 982c86144ba4729b32e4834ee185fce349950788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_IN_PROCESS_WEBKIT_INDEXED_DB_DISPATCHER_HOST_H_
#define CHROME_BROWSER_IN_PROCESS_WEBKIT_INDEXED_DB_DISPATCHER_HOST_H_
#pragma once

#include "base/basictypes.h"
#include "base/id_map.h"
#include "base/process.h"
#include "base/ref_counted.h"
#include "chrome/browser/in_process_webkit/webkit_context.h"
#include "ipc/ipc_message.h"

class HostContentSettingsMap;
class IndexedDBKey;
class Profile;
class SerializedScriptValue;
struct ViewHostMsg_IDBDatabaseCreateObjectStore_Params;
struct ViewHostMsg_IDBFactoryOpen_Params;
struct ViewHostMsg_IDBIndexOpenCursor_Params;
struct ViewHostMsg_IDBObjectStoreCreateIndex_Params;
struct ViewHostMsg_IDBObjectStoreOpenCursor_Params;
struct ViewHostMsg_IDBObjectStorePut_Params;

namespace WebKit {
class WebIDBCursor;
class WebIDBDatabase;
class WebIDBIndex;
class WebIDBObjectStore;
class WebIDBTransaction;
}

// Handles all IndexedDB related messages from a particular renderer process.
class IndexedDBDispatcherHost
    : public base::RefCountedThreadSafe<IndexedDBDispatcherHost> {
 public:
  // Only call the constructor from the UI thread.
  IndexedDBDispatcherHost(IPC::Message::Sender* sender, Profile* profile);

  // Only call from ResourceMessageFilter on the IO thread.
  void Init(int process_id, base::ProcessHandle process_handle);

  // Only call from ResourceMessageFilter on the IO thread.  Calls self on the
  // WebKit thread in some cases.
  void Shutdown();

  // Only call from ResourceMessageFilter on the IO thread.
  bool OnMessageReceived(const IPC::Message& message);

  // Send a message to the renderer process associated with our sender_ via the
  // IO thread.  May be called from any thread.
  void Send(IPC::Message* message);

  // A shortcut for accessing our context.
  IndexedDBContext* Context() {
    return webkit_context_->indexed_db_context();
  }

  // The various IndexedDBCallbacks children call these methods to add the
  // results into the applicable map.  See below for more details.
  int32 Add(WebKit::WebIDBCursor* idb_cursor);
  int32 Add(WebKit::WebIDBDatabase* idb_database);
  int32 Add(WebKit::WebIDBIndex* idb_index);
  int32 Add(WebKit::WebIDBObjectStore* idb_object_store);
  void Add(WebKit::WebIDBTransaction* idb_transaction);

 private:
  friend class base::RefCountedThreadSafe<IndexedDBDispatcherHost>;
  ~IndexedDBDispatcherHost();

  // Message processing. Most of the work is delegated to the dispatcher hosts
  // below.
  void OnMessageReceivedWebKit(const IPC::Message& message);
  void OnIDBFactoryOpen(const ViewHostMsg_IDBFactoryOpen_Params& p);
  void OnIDBFactoryAbortPendingTransactions(const std::vector<int32>& ids);

  // Helper templates.
  template <class ReturnType>
  ReturnType* GetOrTerminateProcess(
    IDMap<ReturnType, IDMapOwnPointer>* map, int32 return_object_id,
    IPC::Message* reply_msg, uint32 message_type);

  template <typename ReplyType, typename MessageType,
            typename WebObjectType, typename Method>
  void SyncGetter(IDMap<WebObjectType, IDMapOwnPointer>* map, int32 object_id,
                  IPC::Message* reply_msg, Method method);

  template <typename ObjectType>
  void DestroyObject(IDMap<ObjectType, IDMapOwnPointer>* map, int32 object_id,
                     uint32 message_type);

  class DatabaseDispatcherHost {
   public:
    explicit DatabaseDispatcherHost(IndexedDBDispatcherHost* parent);
    ~DatabaseDispatcherHost();

    bool OnMessageReceived(const IPC::Message& message, bool *msg_is_ok);
    void Send(IPC::Message* message);

    void OnName(int32 idb_database_id, IPC::Message* reply_msg);
    void OnDescription(int32 idb_database_id, IPC::Message* reply_msg);
    void OnVersion(int32 idb_database_id, IPC::Message* reply_msg);
    void OnObjectStores(int32 idb_database_id, IPC::Message* reply_msg);
    void OnCreateObjectStore(
        const ViewHostMsg_IDBDatabaseCreateObjectStore_Params& params);
    void OnObjectStore(int32 idb_database_id, const string16& name, int32 mode,
                       IPC::Message* reply_msg);
    void OnRemoveObjectStore(int32 idb_database_id, int32 response_id,
                             const string16& name);
    void OnSetVersion(int32 idb_database_id,
                      int32 response_id,
                      const string16& version);
    void OnTransaction(int32 idb_database_id,
                       const std::vector<string16>& names,
                       int32 mode, int32 timeout, IPC::Message* reply_msg);
    void OnDestroyed(int32 idb_database_id);

    IndexedDBDispatcherHost* parent_;
    IDMap<WebKit::WebIDBDatabase, IDMapOwnPointer> map_;
  };

  class IndexDispatcherHost {
   public:
    explicit IndexDispatcherHost(IndexedDBDispatcherHost* parent);
    ~IndexDispatcherHost();

    bool OnMessageReceived(const IPC::Message& message, bool *msg_is_ok);
    void Send(IPC::Message* message);

    void OnName(int32 idb_index_id, IPC::Message* reply_msg);
    void OnStoreName(int32 idb_index_id, IPC::Message* reply_msg);
    void OnKeyPath(int32 idb_index_id, IPC::Message* reply_msg);
    void OnUnique(int32 idb_index_id, IPC::Message* reply_msg);
    void OnOpenObjectCursor(
        const ViewHostMsg_IDBIndexOpenCursor_Params& params);
    void OnOpenCursor(const ViewHostMsg_IDBIndexOpenCursor_Params& params);
    void OnGetObject(int idb_index_id,
                     int32 response_id,
                     const IndexedDBKey& key,
                     int transaction_id);
    void OnGet(int idb_index_id,
               int32 response_id,
               const IndexedDBKey& key,
               int transaction_id);
    void OnDestroyed(int32 idb_index_id);

    IndexedDBDispatcherHost* parent_;
    IDMap<WebKit::WebIDBIndex, IDMapOwnPointer> map_;
  };

  class ObjectStoreDispatcherHost {
   public:
    explicit ObjectStoreDispatcherHost(IndexedDBDispatcherHost* parent);
    ~ObjectStoreDispatcherHost();

    bool OnMessageReceived(const IPC::Message& message, bool *msg_is_ok);
    void Send(IPC::Message* message);

    void OnName(int32 idb_object_store_id, IPC::Message* reply_msg);
    void OnKeyPath(int32 idb_object_store_id, IPC::Message* reply_msg);
    void OnIndexNames(int32 idb_object_store_id, IPC::Message* reply_msg);
    void OnGet(int idb_object_store_id,
               int32 response_id,
               const IndexedDBKey& key,
               int transaction_id);
    void OnPut(const ViewHostMsg_IDBObjectStorePut_Params& params);
    void OnRemove(int idb_object_store_id,
                  int32 response_id,
                  const IndexedDBKey& key,
                  int transaction_id);
    void OnCreateIndex(
        const ViewHostMsg_IDBObjectStoreCreateIndex_Params& params);
    void OnIndex(int32 idb_object_store_id, const string16& name,
                 IPC::Message* reply_msg);
    void OnRemoveIndex(int32 idb_object_store_id, int32 response_id,
                       const string16& name);
    void OnOpenCursor(
        const ViewHostMsg_IDBObjectStoreOpenCursor_Params& params);
    void OnDestroyed(int32 idb_object_store_id);

    IndexedDBDispatcherHost* parent_;
    IDMap<WebKit::WebIDBObjectStore, IDMapOwnPointer> map_;
  };

  class CursorDispatcherHost {
   public:
    explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent);
    ~CursorDispatcherHost();

    bool OnMessageReceived(const IPC::Message& message, bool *msg_is_ok);
    void Send(IPC::Message* message);

    void OnDirection(int32 idb_object_store_id, IPC::Message* reply_msg);
    void OnKey(int32 idb_object_store_id, IPC::Message* reply_msg);
    void OnValue(int32 idb_object_store_id, IPC::Message* reply_msg);
    void OnUpdate(int32 idb_object_store_id,
                  int32 response_id,
                  const SerializedScriptValue& value);
    void OnContinue(int32 idb_object_store_id,
                    int32 response_id,
                    const IndexedDBKey& key);
    void OnRemove(int32 idb_object_store_id,
                  int32 response_id);
    void OnDestroyed(int32 idb_cursor_id);

    IndexedDBDispatcherHost* parent_;
    IDMap<WebKit::WebIDBCursor, IDMapOwnPointer> map_;
  };

  class TransactionDispatcherHost {
   public:
    explicit TransactionDispatcherHost(IndexedDBDispatcherHost* parent);
    ~TransactionDispatcherHost();

    bool OnMessageReceived(const IPC::Message& message, bool *msg_is_ok);
    void Send(IPC::Message* message);

    // TODO: add the rest of the transaction methods.
    void OnAbort(int32 transaction_id);
    void OnObjectStore(int32 transaction_id, const string16& name,
                       IPC::Message* reply_msg);
    void OnDestroyed(int32 idb_transaction_id);

    IndexedDBDispatcherHost* parent_;
    IDMap<WebKit::WebIDBTransaction, IDMapOwnPointer> map_;
  };

  // Only use on the IO thread.
  IPC::Message::Sender* sender_;

  // Data shared between renderer processes with the same profile.
  scoped_refptr<WebKitContext> webkit_context_;

  // Tells us whether the user wants to allow databases to be opened.
  scoped_refptr<HostContentSettingsMap> host_content_settings_map_;

  // Only access on WebKit thread.
  scoped_ptr<DatabaseDispatcherHost> database_dispatcher_host_;
  scoped_ptr<IndexDispatcherHost> index_dispatcher_host_;
  scoped_ptr<ObjectStoreDispatcherHost> object_store_dispatcher_host_;
  scoped_ptr<CursorDispatcherHost> cursor_dispatcher_host_;
  scoped_ptr<TransactionDispatcherHost> transaction_dispatcher_host_;


  // If we get a corrupt message from a renderer, we need to kill it using this
  // handle.
  base::ProcessHandle process_handle_;

  // Used to dispatch messages to the correct view host.
  int process_id_;

  DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost);
};

#endif  // CHROME_BROWSER_IN_PROCESS_WEBKIT_INDEXED_DB_DISPATCHER_HOST_H_