Home | History | Annotate | Download | only in indexed_db
      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 
     16 class GURL;
     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   int64 HostTransactionId(int64 transaction_id);
     75   int64 RendererTransactionId(int64 host_transaction_id);
     76 
     77  private:
     78   // Friends to enable OnDestruct() delegation.
     79   friend class BrowserThread;
     80   friend class base::DeleteHelper<IndexedDBDispatcherHost>;
     81 
     82   virtual ~IndexedDBDispatcherHost();
     83 
     84   // Message processing. Most of the work is delegated to the dispatcher hosts
     85   // below.
     86   void OnIDBFactoryGetDatabaseNames(
     87       const IndexedDBHostMsg_FactoryGetDatabaseNames_Params& p);
     88   void OnIDBFactoryOpen(const IndexedDBHostMsg_FactoryOpen_Params& p);
     89 
     90   void OnIDBFactoryDeleteDatabase(
     91       const IndexedDBHostMsg_FactoryDeleteDatabase_Params& p);
     92 
     93   void ResetDispatcherHosts();
     94 
     95   // IDMap for RefCounted types
     96   template <typename RefCountedType>
     97   class RefIDMap {
     98    private:
     99     typedef int32 KeyType;
    100 
    101    public:
    102     RefIDMap() {}
    103     ~RefIDMap() {}
    104 
    105     KeyType Add(RefCountedType* data) {
    106       return map_.Add(new scoped_refptr<RefCountedType>(data));
    107     }
    108 
    109     RefCountedType* Lookup(KeyType id) {
    110       scoped_refptr<RefCountedType>* ptr = map_.Lookup(id);
    111       if (ptr == NULL)
    112         return NULL;
    113       return ptr->get();
    114     }
    115 
    116     void Remove(KeyType id) { map_.Remove(id); }
    117 
    118     void set_check_on_null_data(bool value) {
    119       map_.set_check_on_null_data(value);
    120     }
    121 
    122    private:
    123     IDMap<scoped_refptr<RefCountedType>, IDMapOwnPointer> map_;
    124   };
    125 
    126   // Helper templates.
    127   template <class ReturnType>
    128   ReturnType* GetOrTerminateProcess(IDMap<ReturnType, IDMapOwnPointer>* map,
    129                                     int32 ipc_return_object_id);
    130   template <class ReturnType>
    131   ReturnType* GetOrTerminateProcess(RefIDMap<ReturnType>* map,
    132                                     int32 ipc_return_object_id);
    133 
    134   template <typename MapType>
    135   void DestroyObject(MapType* map, int32 ipc_object_id);
    136 
    137   // Used in nested classes.
    138   typedef std::map<int32, GURL> WebIDBObjectIDToURLMap;
    139 
    140   typedef std::map<int64, GURL> TransactionIDToURLMap;
    141   typedef std::map<int64, uint64> TransactionIDToSizeMap;
    142   typedef std::map<int64, int64> TransactionIDToDatabaseIDMap;
    143 
    144   class DatabaseDispatcherHost {
    145    public:
    146     explicit DatabaseDispatcherHost(IndexedDBDispatcherHost* parent);
    147     ~DatabaseDispatcherHost();
    148 
    149     void CloseAll();
    150     bool OnMessageReceived(const IPC::Message& message, bool* msg_is_ok);
    151     void Send(IPC::Message* message);
    152 
    153     void OnCreateObjectStore(
    154         const IndexedDBHostMsg_DatabaseCreateObjectStore_Params& params);
    155     void OnDeleteObjectStore(int32 ipc_database_id,
    156                              int64 transaction_id,
    157                              int64 object_store_id);
    158     void OnCreateTransaction(
    159         const IndexedDBHostMsg_DatabaseCreateTransaction_Params&);
    160     void OnOpen(int32 ipc_database_id,
    161                 int32 ipc_thread_id,
    162                 int32 ipc_callbacks_id);
    163     void OnClose(int32 ipc_database_id);
    164     void OnDestroyed(int32 ipc_database_id);
    165 
    166     void OnGet(const IndexedDBHostMsg_DatabaseGet_Params& params);
    167     void OnPut(const IndexedDBHostMsg_DatabasePut_Params& params);
    168     void OnSetIndexKeys(
    169         const IndexedDBHostMsg_DatabaseSetIndexKeys_Params& params);
    170     void OnSetIndexesReady(int32 ipc_database_id,
    171                            int64 transaction_id,
    172                            int64 object_store_id,
    173                            const std::vector<int64>& ids);
    174     void OnOpenCursor(const IndexedDBHostMsg_DatabaseOpenCursor_Params& params);
    175     void OnCount(const IndexedDBHostMsg_DatabaseCount_Params& params);
    176     void OnDeleteRange(
    177         const IndexedDBHostMsg_DatabaseDeleteRange_Params& params);
    178     void OnClear(int32 ipc_thread_id,
    179                  int32 ipc_callbacks_id,
    180                  int32 ipc_database_id,
    181                  int64 transaction_id,
    182                  int64 object_store_id);
    183     void OnCreateIndex(
    184         const IndexedDBHostMsg_DatabaseCreateIndex_Params& params);
    185     void OnDeleteIndex(int32 ipc_database_id,
    186                        int64 transaction_id,
    187                        int64 object_store_id,
    188                        int64 index_id);
    189 
    190     void OnAbort(int32 ipc_database_id, int64 transaction_id);
    191     void OnCommit(int32 ipc_database_id, int64 transaction_id);
    192     IndexedDBDispatcherHost* parent_;
    193     IDMap<IndexedDBConnection, IDMapOwnPointer> map_;
    194     WebIDBObjectIDToURLMap database_url_map_;
    195     TransactionIDToSizeMap transaction_size_map_;
    196     TransactionIDToURLMap transaction_url_map_;
    197     TransactionIDToDatabaseIDMap transaction_database_map_;
    198   };
    199 
    200   class CursorDispatcherHost {
    201    public:
    202     explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent);
    203     ~CursorDispatcherHost();
    204 
    205     bool OnMessageReceived(const IPC::Message& message, bool* msg_is_ok);
    206     void Send(IPC::Message* message);
    207 
    208     void OnAdvance(int32 ipc_object_store_id,
    209                    int32 ipc_thread_id,
    210                    int32 ipc_callbacks_id,
    211                    unsigned long count);
    212     void OnContinue(int32 ipc_object_store_id,
    213                     int32 ipc_thread_id,
    214                     int32 ipc_callbacks_id,
    215                     const IndexedDBKey& key);
    216     void OnPrefetch(int32 ipc_cursor_id,
    217                     int32 ipc_thread_id,
    218                     int32 ipc_callbacks_id,
    219                     int n);
    220     void OnPrefetchReset(int32 ipc_cursor_id,
    221                          int used_prefetches,
    222                          int unused_prefetches);
    223     void OnDestroyed(int32 ipc_cursor_id);
    224 
    225     IndexedDBDispatcherHost* parent_;
    226     RefIDMap<IndexedDBCursor> map_;
    227   };
    228 
    229   scoped_refptr<IndexedDBContextImpl> indexed_db_context_;
    230 
    231   // Only access on IndexedDB thread.
    232   scoped_ptr<DatabaseDispatcherHost> database_dispatcher_host_;
    233   scoped_ptr<CursorDispatcherHost> cursor_dispatcher_host_;
    234 
    235   // Used to dispatch messages to the correct view host.
    236   int ipc_process_id_;
    237 
    238   DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost);
    239 };
    240 
    241 }  // namespace content
    242 
    243 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_
    244