| OLD | NEW | 
|    1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |    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 |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ |    5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ | 
|    6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ |    6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ | 
|    7  |    7  | 
|    8 #include <stdint.h> |    8 #include <stdint.h> | 
|    9  |    9  | 
|   10 #include <map> |   10 #include <map> | 
|   11 #include <memory> |   11 #include <memory> | 
|   12 #include <string> |   12 #include <string> | 
|   13 #include <utility> |   13 #include <utility> | 
|   14 #include <vector> |   14 #include <vector> | 
|   15  |   15  | 
|   16 #include "base/id_map.h" |  | 
|   17 #include "base/macros.h" |   16 #include "base/macros.h" | 
|   18 #include "base/memory/ref_counted.h" |   17 #include "base/memory/ref_counted.h" | 
|   19 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |   18 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 
|   20 #include "content/common/indexed_db/indexed_db.mojom.h" |   19 #include "content/common/indexed_db/indexed_db.mojom.h" | 
|   21 #include "content/public/browser/browser_associated_interface.h" |   20 #include "content/public/browser/browser_associated_interface.h" | 
|   22 #include "content/public/browser/browser_message_filter.h" |   21 #include "content/public/browser/browser_message_filter.h" | 
|   23 #include "net/url_request/url_request_context_getter.h" |   22 #include "net/url_request/url_request_context_getter.h" | 
|   24 #include "storage/browser/blob/blob_data_handle.h" |   23 #include "storage/browser/blob/blob_data_handle.h" | 
|   25 #include "url/gurl.h" |   24 #include "url/gurl.h" | 
|   26  |   25  | 
|   27 struct IndexedDBHostMsg_DatabaseObserve_Params; |   26 struct IndexedDBHostMsg_DatabaseObserve_Params; | 
|   28 struct IndexedDBMsg_Observation; |   27 struct IndexedDBMsg_Observation; | 
|   29 struct IndexedDBMsg_ObserverChanges; |   28 struct IndexedDBMsg_ObserverChanges; | 
|   30  |   29  | 
|   31 namespace url { |   30 namespace url { | 
|   32 class Origin; |   31 class Origin; | 
|   33 } |   32 } | 
|   34  |   33  | 
|   35 namespace content { |   34 namespace content { | 
|   36 class IndexedDBBlobInfo; |   35 class IndexedDBBlobInfo; | 
|   37 class IndexedDBCallbacks; |   36 class IndexedDBCallbacks; | 
|   38 class IndexedDBConnection; |   37 class IndexedDBConnection; | 
|   39 class IndexedDBContextImpl; |   38 class IndexedDBContextImpl; | 
|   40 class IndexedDBCursor; |  | 
|   41 class IndexedDBDatabaseCallbacks; |   39 class IndexedDBDatabaseCallbacks; | 
|   42 class IndexedDBKey; |  | 
|   43 class IndexedDBObservation; |   40 class IndexedDBObservation; | 
|   44 class IndexedDBObserverChanges; |   41 class IndexedDBObserverChanges; | 
|   45  |   42  | 
|   46 // Handles all IndexedDB related messages from a particular renderer process. |   43 // Handles all IndexedDB related messages from a particular renderer process. | 
|   47 class IndexedDBDispatcherHost |   44 class IndexedDBDispatcherHost | 
|   48     : public BrowserMessageFilter, |   45     : public BrowserMessageFilter, | 
|   49       public BrowserAssociatedInterface<::indexed_db::mojom::Factory>, |   46       public BrowserAssociatedInterface<::indexed_db::mojom::Factory>, | 
|   50       public ::indexed_db::mojom::Factory { |   47       public ::indexed_db::mojom::Factory { | 
|   51  public: |   48  public: | 
|   52   // Only call the constructor from the UI thread. |   49   // Only call the constructor from the UI thread. | 
|   53   IndexedDBDispatcherHost(int ipc_process_id, |   50   IndexedDBDispatcherHost(int ipc_process_id, | 
|   54                           net::URLRequestContextGetter* request_context_getter, |   51                           net::URLRequestContextGetter* request_context_getter, | 
|   55                           IndexedDBContextImpl* indexed_db_context, |   52                           IndexedDBContextImpl* indexed_db_context, | 
|   56                           ChromeBlobStorageContext* blob_storage_context); |   53                           ChromeBlobStorageContext* blob_storage_context); | 
|   57  |   54  | 
|   58   static IndexedDBMsg_ObserverChanges ConvertObserverChanges( |   55   static IndexedDBMsg_ObserverChanges ConvertObserverChanges( | 
|   59       std::unique_ptr<IndexedDBObserverChanges> changes); |   56       std::unique_ptr<IndexedDBObserverChanges> changes); | 
|   60   static IndexedDBMsg_Observation ConvertObservation( |   57   static IndexedDBMsg_Observation ConvertObservation( | 
|   61       const IndexedDBObservation* observation); |   58       const IndexedDBObservation* observation); | 
|   62  |   59  | 
|   63   // BrowserMessageFilter implementation. |   60   // BrowserMessageFilter implementation. | 
|   64   void OnChannelClosing() override; |   61   void OnChannelClosing() override; | 
|   65   void OnDestruct() const override; |   62   void OnDestruct() const override; | 
|   66   base::TaskRunner* OverrideTaskRunnerForMessage( |  | 
|   67       const IPC::Message& message) override; |  | 
|   68   bool OnMessageReceived(const IPC::Message& message) override; |   63   bool OnMessageReceived(const IPC::Message& message) override; | 
|   69  |   64  | 
|   70   void FinishTransaction(int64_t host_transaction_id, bool committed); |   65   void FinishTransaction(int64_t host_transaction_id, bool committed); | 
|   71  |   66  | 
|   72   // A shortcut for accessing our context. |   67   // A shortcut for accessing our context. | 
|   73   IndexedDBContextImpl* context() const { return indexed_db_context_.get(); } |   68   IndexedDBContextImpl* context() const { return indexed_db_context_.get(); } | 
|   74   storage::BlobStorageContext* blob_storage_context() const { |   69   storage::BlobStorageContext* blob_storage_context() const { | 
|   75     return blob_storage_context_->context(); |   70     return blob_storage_context_->context(); | 
|   76   } |   71   } | 
|   77   int ipc_process_id() const { return ipc_process_id_; } |   72   int ipc_process_id() const { return ipc_process_id_; } | 
|   78  |   73  | 
|   79   // IndexedDBCallbacks call these methods to add the results into the |  | 
|   80   // applicable map.  See below for more details. |  | 
|   81   int32_t Add(IndexedDBCursor* cursor); |  | 
|   82  |  | 
|   83   bool RegisterTransactionId(int64_t host_transaction_id, |   74   bool RegisterTransactionId(int64_t host_transaction_id, | 
|   84                              const url::Origin& origin); |   75                              const url::Origin& origin); | 
|   85   bool GetTransactionSize(int64_t host_transaction_id, |   76   bool GetTransactionSize(int64_t host_transaction_id, | 
|   86                           int64_t* transaction_size); |   77                           int64_t* transaction_size); | 
|   87   void AddToTransaction(int64_t host_transaction_id, int64_t value_length); |   78   void AddToTransaction(int64_t host_transaction_id, int64_t value_length); | 
|   88  |   79  | 
|   89   IndexedDBCursor* GetCursorFromId(int32_t ipc_cursor_id); |  | 
|   90  |  | 
|   91   // These are called to map a 32-bit front-end (renderer-specific) transaction |   80   // These are called to map a 32-bit front-end (renderer-specific) transaction | 
|   92   // id to and from a back-end ("host") transaction id that encodes the process |   81   // id to and from a back-end ("host") transaction id that encodes the process | 
|   93   // id in the high 32 bits. The mapping is host-specific and ids are validated. |   82   // id in the high 32 bits. The mapping is host-specific and ids are validated. | 
|   94   int64_t HostTransactionId(int64_t transaction_id); |   83   int64_t HostTransactionId(int64_t transaction_id); | 
|   95   int64_t RendererTransactionId(int64_t host_transaction_id); |   84   int64_t RendererTransactionId(int64_t host_transaction_id); | 
|   96  |   85  | 
|   97   // These are called to decode a host transaction ID, for diagnostic purposes. |   86   // These are called to decode a host transaction ID, for diagnostic purposes. | 
|   98   static uint32_t TransactionIdToRendererTransactionId( |   87   static uint32_t TransactionIdToRendererTransactionId( | 
|   99       int64_t host_transaction_id); |   88       int64_t host_transaction_id); | 
|  100   static uint32_t TransactionIdToProcessId(int64_t host_transaction_id); |   89   static uint32_t TransactionIdToProcessId(int64_t host_transaction_id); | 
|  101  |   90  | 
|  102   std::string HoldBlobData(const IndexedDBBlobInfo& blob_info); |   91   std::string HoldBlobData(const IndexedDBBlobInfo& blob_info); | 
|  103   void DropBlobData(const std::string& uuid); |   92   void DropBlobData(const std::string& uuid); | 
|  104  |   93  | 
|  105   // True if the channel is closing/closed and outstanding requests |   94   // True if the channel is closing/closed and outstanding requests | 
|  106   // can be abandoned. Only access on IndexedDB thread. |   95   // can be abandoned. Only access on IndexedDB thread. | 
|  107   bool IsOpen() const; |   96   bool IsOpen() const; | 
|  108  |   97  | 
|  109  private: |   98  private: | 
|  110   // Friends to enable OnDestruct() delegation. |   99   // Friends to enable OnDestruct() delegation. | 
|  111   friend class BrowserThread; |  100   friend class BrowserThread; | 
|  112   friend class base::DeleteHelper<IndexedDBDispatcherHost>; |  101   friend class base::DeleteHelper<IndexedDBDispatcherHost>; | 
|  113  |  102  | 
|  114   // Used in nested classes. |  103   // Used in nested classes. | 
|  115   typedef std::map<int64_t, int64_t> TransactionIDToDatabaseIDMap; |  | 
|  116   typedef std::map<int64_t, int64_t> TransactionIDToSizeMap; |  104   typedef std::map<int64_t, int64_t> TransactionIDToSizeMap; | 
|  117   typedef std::map<int64_t, url::Origin> TransactionIDToOriginMap; |  105   typedef std::map<int64_t, url::Origin> TransactionIDToOriginMap; | 
|  118  |  106  | 
|  119   // IDMap for RefCounted types |  | 
|  120   template <typename RefCountedType> |  | 
|  121   class RefIDMap { |  | 
|  122    public: |  | 
|  123     typedef int32_t KeyType; |  | 
|  124  |  | 
|  125     RefIDMap() {} |  | 
|  126     ~RefIDMap() {} |  | 
|  127  |  | 
|  128     KeyType Add(RefCountedType* data) { |  | 
|  129       return map_.Add(new scoped_refptr<RefCountedType>(data)); |  | 
|  130     } |  | 
|  131  |  | 
|  132     RefCountedType* Lookup(KeyType id) { |  | 
|  133       scoped_refptr<RefCountedType>* ptr = map_.Lookup(id); |  | 
|  134       if (ptr == NULL) |  | 
|  135         return NULL; |  | 
|  136       return ptr->get(); |  | 
|  137     } |  | 
|  138  |  | 
|  139     void Remove(KeyType id) { map_.Remove(id); } |  | 
|  140  |  | 
|  141     void set_check_on_null_data(bool value) { |  | 
|  142       map_.set_check_on_null_data(value); |  | 
|  143     } |  | 
|  144  |  | 
|  145    private: |  | 
|  146     IDMap<scoped_refptr<RefCountedType>, IDMapOwnPointer> map_; |  | 
|  147  |  | 
|  148     DISALLOW_COPY_AND_ASSIGN(RefIDMap); |  | 
|  149   }; |  | 
|  150  |  | 
|  151   class CursorDispatcherHost { |  | 
|  152    public: |  | 
|  153     explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent); |  | 
|  154     ~CursorDispatcherHost(); |  | 
|  155  |  | 
|  156     bool OnMessageReceived(const IPC::Message& message); |  | 
|  157  |  | 
|  158     void OnAdvance(int32_t ipc_object_store_id, |  | 
|  159                    int32_t ipc_thread_id, |  | 
|  160                    int32_t ipc_callbacks_id, |  | 
|  161                    uint32_t count); |  | 
|  162     void OnContinue(int32_t ipc_object_store_id, |  | 
|  163                     int32_t ipc_thread_id, |  | 
|  164                     int32_t ipc_callbacks_id, |  | 
|  165                     const IndexedDBKey& key, |  | 
|  166                     const IndexedDBKey& primary_key); |  | 
|  167     void OnPrefetch(int32_t ipc_cursor_id, |  | 
|  168                     int32_t ipc_thread_id, |  | 
|  169                     int32_t ipc_callbacks_id, |  | 
|  170                     int n); |  | 
|  171     void OnPrefetchReset(int32_t ipc_cursor_id, |  | 
|  172                          int used_prefetches, |  | 
|  173                          int unused_prefetches); |  | 
|  174     void OnDestroyed(int32_t ipc_cursor_id); |  | 
|  175  |  | 
|  176     IndexedDBDispatcherHost* parent_; |  | 
|  177     RefIDMap<IndexedDBCursor> map_; |  | 
|  178  |  | 
|  179    private: |  | 
|  180     DISALLOW_COPY_AND_ASSIGN(CursorDispatcherHost); |  | 
|  181   }; |  | 
|  182  |  | 
|  183   ~IndexedDBDispatcherHost() override; |  107   ~IndexedDBDispatcherHost() override; | 
|  184  |  108  | 
|  185   // Helper templates. |  | 
|  186   template <class ReturnType> |  | 
|  187   ReturnType* GetOrTerminateProcess(RefIDMap<ReturnType>* map, |  | 
|  188                                     int32_t ipc_return_object_id); |  | 
|  189  |  | 
|  190   template <typename MapType> |  | 
|  191   void DestroyObject(MapType* map, int32_t ipc_object_id); |  | 
|  192  |  | 
|  193   // indexed_db::mojom::Factory implementation: |  109   // indexed_db::mojom::Factory implementation: | 
|  194   void GetDatabaseNames( |  110   void GetDatabaseNames( | 
|  195       ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, |  111       ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, | 
|  196       const url::Origin& origin) override; |  112       const url::Origin& origin) override; | 
|  197   void Open(int32_t worker_thread, |  113   void Open(int32_t worker_thread, | 
|  198             ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, |  114             ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, | 
|  199             ::indexed_db::mojom::DatabaseCallbacksAssociatedPtrInfo |  115             ::indexed_db::mojom::DatabaseCallbacksAssociatedPtrInfo | 
|  200                 database_callbacks_info, |  116                 database_callbacks_info, | 
|  201             const url::Origin& origin, |  117             const url::Origin& origin, | 
|  202             const base::string16& name, |  118             const base::string16& name, | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|  213       scoped_refptr<IndexedDBCallbacks> callbacks, |  129       scoped_refptr<IndexedDBCallbacks> callbacks, | 
|  214       scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, |  130       scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, | 
|  215       const url::Origin& origin, |  131       const url::Origin& origin, | 
|  216       const base::string16& name, |  132       const base::string16& name, | 
|  217       int64_t version, |  133       int64_t version, | 
|  218       int64_t transaction_id); |  134       int64_t transaction_id); | 
|  219   void DeleteDatabaseOnIDBThread(scoped_refptr<IndexedDBCallbacks> callbacks, |  135   void DeleteDatabaseOnIDBThread(scoped_refptr<IndexedDBCallbacks> callbacks, | 
|  220                                  const url::Origin& origin, |  136                                  const url::Origin& origin, | 
|  221                                  const base::string16& name); |  137                                  const base::string16& name); | 
|  222  |  138  | 
|  223   // Message processing. Most of the work is delegated to the dispatcher hosts |  | 
|  224   // below. |  | 
|  225   void OnAckReceivedBlobs(const std::vector<std::string>& uuids); |  | 
|  226  |  | 
|  227   void ResetDispatcherHosts(); |  139   void ResetDispatcherHosts(); | 
|  228  |  140  | 
|  229   scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |  141   scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 
|  230   scoped_refptr<IndexedDBContextImpl> indexed_db_context_; |  142   scoped_refptr<IndexedDBContextImpl> indexed_db_context_; | 
|  231   scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; |  143   scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; | 
|  232  |  144  | 
|  233   // Maps blob uuid to a pair (handle, ref count). Entry is added and/or count |  145   // Maps blob uuid to a pair (handle, ref count). Entry is added and/or count | 
|  234   // is incremented in HoldBlobData(), and count is decremented and/or entry |  146   // is incremented in HoldBlobData(), and count is decremented and/or entry | 
|  235   // removed in DropBlobData(). |  147   // removed in DropBlobData(). | 
|  236   std::map<std::string, |  148   std::map<std::string, | 
|  237            std::pair<std::unique_ptr<storage::BlobDataHandle>, int>> |  149            std::pair<std::unique_ptr<storage::BlobDataHandle>, int>> | 
|  238       blob_data_handle_map_; |  150       blob_data_handle_map_; | 
|  239  |  151  | 
|  240   // Only access on IndexedDB thread. |  152   // Only access on IndexedDB thread. | 
|  241   bool is_open_ = true; |  153   bool is_open_ = true; | 
|  242   TransactionIDToSizeMap transaction_size_map_; |  154   TransactionIDToSizeMap transaction_size_map_; | 
|  243   TransactionIDToOriginMap transaction_origin_map_; |  155   TransactionIDToOriginMap transaction_origin_map_; | 
|  244   std::unique_ptr<CursorDispatcherHost> cursor_dispatcher_host_; |  | 
|  245  |  156  | 
|  246   // Used to set file permissions for blob storage. |  157   // Used to set file permissions for blob storage. | 
|  247   int ipc_process_id_; |  158   int ipc_process_id_; | 
|  248  |  159  | 
|  249   DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost); |  160   DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost); | 
|  250 }; |  161 }; | 
|  251  |  162  | 
|  252 }  // namespace content |  163 }  // namespace content | 
|  253  |  164  | 
|  254 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ |  165 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ | 
| OLD | NEW |