| OLD | NEW |
| (Empty) |
| 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 <stdint.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <utility> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "base/id_map.h" | |
| 17 #include "base/macros.h" | |
| 18 #include "base/memory/ref_counted.h" | |
| 19 #include "base/memory/weak_ptr.h" | |
| 20 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | |
| 21 #include "content/public/browser/browser_message_filter.h" | |
| 22 #include "net/url_request/url_request_context_getter.h" | |
| 23 #include "storage/browser/blob/blob_data_handle.h" | |
| 24 #include "storage/browser/quota/quota_manager.h" | |
| 25 #include "storage/common/quota/quota_status_code.h" | |
| 26 #include "url/gurl.h" | |
| 27 | |
| 28 struct IndexedDBDatabaseMetadata; | |
| 29 struct IndexedDBHostMsg_DatabaseCount_Params; | |
| 30 struct IndexedDBHostMsg_DatabaseCreateIndex_Params; | |
| 31 struct IndexedDBHostMsg_DatabaseCreateObjectStore_Params; | |
| 32 struct IndexedDBHostMsg_DatabaseCreateTransaction_Params; | |
| 33 struct IndexedDBHostMsg_DatabaseDeleteRange_Params; | |
| 34 struct IndexedDBHostMsg_DatabaseGet_Params; | |
| 35 struct IndexedDBHostMsg_DatabaseGetAll_Params; | |
| 36 struct IndexedDBHostMsg_DatabaseObserve_Params; | |
| 37 struct IndexedDBHostMsg_DatabaseOpenCursor_Params; | |
| 38 struct IndexedDBHostMsg_DatabasePut_Params; | |
| 39 struct IndexedDBHostMsg_DatabaseSetIndexKeys_Params; | |
| 40 struct IndexedDBHostMsg_FactoryDeleteDatabase_Params; | |
| 41 struct IndexedDBHostMsg_FactoryGetDatabaseNames_Params; | |
| 42 struct IndexedDBHostMsg_FactoryOpen_Params; | |
| 43 struct IndexedDBMsg_Observation; | |
| 44 struct IndexedDBMsg_ObserverChanges; | |
| 45 | |
| 46 namespace url { | |
| 47 class Origin; | |
| 48 } | |
| 49 | |
| 50 namespace content { | |
| 51 class IndexedDBBlobInfo; | |
| 52 class IndexedDBConnection; | |
| 53 class IndexedDBContextImpl; | |
| 54 class IndexedDBCursor; | |
| 55 class IndexedDBKey; | |
| 56 class IndexedDBKeyPath; | |
| 57 class IndexedDBKeyRange; | |
| 58 class IndexedDBObservation; | |
| 59 class IndexedDBObserverChanges; | |
| 60 struct IndexedDBDatabaseMetadata; | |
| 61 | |
| 62 // Handles all IndexedDB related messages from a particular renderer process. | |
| 63 class IndexedDBDispatcherHost : public BrowserMessageFilter { | |
| 64 public: | |
| 65 // Only call the constructor from the UI thread. | |
| 66 IndexedDBDispatcherHost(int ipc_process_id, | |
| 67 net::URLRequestContextGetter* request_context_getter, | |
| 68 IndexedDBContextImpl* indexed_db_context, | |
| 69 ChromeBlobStorageContext* blob_storage_context); | |
| 70 | |
| 71 static ::IndexedDBDatabaseMetadata ConvertMetadata( | |
| 72 const content::IndexedDBDatabaseMetadata& metadata); | |
| 73 static IndexedDBMsg_ObserverChanges ConvertObserverChanges( | |
| 74 std::unique_ptr<IndexedDBObserverChanges> changes); | |
| 75 static IndexedDBMsg_Observation ConvertObservation( | |
| 76 const IndexedDBObservation* observation); | |
| 77 | |
| 78 // BrowserMessageFilter implementation. | |
| 79 void OnChannelClosing() override; | |
| 80 void OnDestruct() const override; | |
| 81 base::TaskRunner* OverrideTaskRunnerForMessage( | |
| 82 const IPC::Message& message) override; | |
| 83 bool OnMessageReceived(const IPC::Message& message) override; | |
| 84 | |
| 85 void FinishTransaction(int64_t host_transaction_id, bool committed); | |
| 86 | |
| 87 // A shortcut for accessing our context. | |
| 88 IndexedDBContextImpl* context() const { return indexed_db_context_.get(); } | |
| 89 storage::BlobStorageContext* blob_storage_context() const { | |
| 90 return blob_storage_context_->context(); | |
| 91 } | |
| 92 | |
| 93 // IndexedDBCallbacks call these methods to add the results into the | |
| 94 // applicable map. See below for more details. | |
| 95 int32_t Add(IndexedDBCursor* cursor); | |
| 96 int32_t Add(IndexedDBConnection* connection, | |
| 97 int32_t ipc_thread_id, | |
| 98 const url::Origin& origin); | |
| 99 | |
| 100 void RegisterTransactionId(int64_t host_transaction_id, | |
| 101 const url::Origin& origin); | |
| 102 | |
| 103 IndexedDBCursor* GetCursorFromId(int32_t ipc_cursor_id); | |
| 104 | |
| 105 // These are called to map a 32-bit front-end (renderer-specific) transaction | |
| 106 // id to and from a back-end ("host") transaction id that encodes the process | |
| 107 // id in the high 32 bits. The mapping is host-specific and ids are validated. | |
| 108 int64_t HostTransactionId(int64_t transaction_id); | |
| 109 int64_t RendererTransactionId(int64_t host_transaction_id); | |
| 110 | |
| 111 // These are called to decode a host transaction ID, for diagnostic purposes. | |
| 112 static uint32_t TransactionIdToRendererTransactionId( | |
| 113 int64_t host_transaction_id); | |
| 114 static uint32_t TransactionIdToProcessId(int64_t host_transaction_id); | |
| 115 | |
| 116 std::string HoldBlobData(const IndexedDBBlobInfo& blob_info); | |
| 117 | |
| 118 private: | |
| 119 // Friends to enable OnDestruct() delegation. | |
| 120 friend class BrowserThread; | |
| 121 friend class base::DeleteHelper<IndexedDBDispatcherHost>; | |
| 122 | |
| 123 // Used in nested classes. | |
| 124 typedef std::map<int64_t, int64_t> TransactionIDToDatabaseIDMap; | |
| 125 typedef std::map<int64_t, uint64_t> TransactionIDToSizeMap; | |
| 126 typedef std::map<int64_t, url::Origin> TransactionIDToOriginMap; | |
| 127 typedef std::map<int32_t, url::Origin> WebIDBObjectIDToOriginMap; | |
| 128 | |
| 129 // IDMap for RefCounted types | |
| 130 template <typename RefCountedType> | |
| 131 class RefIDMap { | |
| 132 public: | |
| 133 typedef int32_t KeyType; | |
| 134 | |
| 135 RefIDMap() {} | |
| 136 ~RefIDMap() {} | |
| 137 | |
| 138 KeyType Add(RefCountedType* data) { | |
| 139 return map_.Add(new scoped_refptr<RefCountedType>(data)); | |
| 140 } | |
| 141 | |
| 142 RefCountedType* Lookup(KeyType id) { | |
| 143 scoped_refptr<RefCountedType>* ptr = map_.Lookup(id); | |
| 144 if (ptr == NULL) | |
| 145 return NULL; | |
| 146 return ptr->get(); | |
| 147 } | |
| 148 | |
| 149 void Remove(KeyType id) { map_.Remove(id); } | |
| 150 | |
| 151 void set_check_on_null_data(bool value) { | |
| 152 map_.set_check_on_null_data(value); | |
| 153 } | |
| 154 | |
| 155 private: | |
| 156 IDMap<scoped_refptr<RefCountedType>, IDMapOwnPointer> map_; | |
| 157 | |
| 158 DISALLOW_COPY_AND_ASSIGN(RefIDMap); | |
| 159 }; | |
| 160 | |
| 161 class DatabaseDispatcherHost { | |
| 162 public: | |
| 163 explicit DatabaseDispatcherHost(IndexedDBDispatcherHost* parent); | |
| 164 ~DatabaseDispatcherHost(); | |
| 165 | |
| 166 void CloseAll(); | |
| 167 bool OnMessageReceived(const IPC::Message& message); | |
| 168 | |
| 169 void OnCreateObjectStore( | |
| 170 const IndexedDBHostMsg_DatabaseCreateObjectStore_Params& params); | |
| 171 void OnDeleteObjectStore(int32_t ipc_database_id, | |
| 172 int64_t transaction_id, | |
| 173 int64_t object_store_id); | |
| 174 void OnCreateTransaction( | |
| 175 const IndexedDBHostMsg_DatabaseCreateTransaction_Params&); | |
| 176 void OnClose(int32_t ipc_database_id); | |
| 177 void OnVersionChangeIgnored(int32_t ipc_database_id); | |
| 178 void OnDestroyed(int32_t ipc_database_id); | |
| 179 | |
| 180 void OnObserve(const IndexedDBHostMsg_DatabaseObserve_Params&); | |
| 181 void OnUnobserve(int32_t ipc_database_id, | |
| 182 const std::vector<int32_t>& observer_ids_to_remove); | |
| 183 | |
| 184 void OnGet(const IndexedDBHostMsg_DatabaseGet_Params& params); | |
| 185 void OnGetAll(const IndexedDBHostMsg_DatabaseGetAll_Params& params); | |
| 186 // OnPutWrapper starts on the IO thread so that it can grab BlobDataHandles | |
| 187 // before posting to the IDB TaskRunner for the rest of the job. | |
| 188 void OnPutWrapper(const IndexedDBHostMsg_DatabasePut_Params& params); | |
| 189 void OnPut(const IndexedDBHostMsg_DatabasePut_Params& params, | |
| 190 std::vector<storage::BlobDataHandle*> handles); | |
| 191 void OnSetIndexKeys( | |
| 192 const IndexedDBHostMsg_DatabaseSetIndexKeys_Params& params); | |
| 193 void OnSetIndexesReady(int32_t ipc_database_id, | |
| 194 int64_t transaction_id, | |
| 195 int64_t object_store_id, | |
| 196 const std::vector<int64_t>& ids); | |
| 197 void OnOpenCursor(const IndexedDBHostMsg_DatabaseOpenCursor_Params& params); | |
| 198 void OnCount(const IndexedDBHostMsg_DatabaseCount_Params& params); | |
| 199 void OnDeleteRange( | |
| 200 const IndexedDBHostMsg_DatabaseDeleteRange_Params& params); | |
| 201 void OnClear(int32_t ipc_thread_id, | |
| 202 int32_t ipc_callbacks_id, | |
| 203 int32_t ipc_database_id, | |
| 204 int64_t transaction_id, | |
| 205 int64_t object_store_id); | |
| 206 void OnCreateIndex( | |
| 207 const IndexedDBHostMsg_DatabaseCreateIndex_Params& params); | |
| 208 void OnDeleteIndex(int32_t ipc_database_id, | |
| 209 int64_t transaction_id, | |
| 210 int64_t object_store_id, | |
| 211 int64_t index_id); | |
| 212 | |
| 213 void OnAbort(int32_t ipc_database_id, int64_t transaction_id); | |
| 214 void OnCommit(int32_t ipc_database_id, int64_t transaction_id); | |
| 215 void OnGotUsageAndQuotaForCommit(int32_t ipc_database_id, | |
| 216 int64_t transaction_id, | |
| 217 storage::QuotaStatusCode status, | |
| 218 int64_t usage, | |
| 219 int64_t quota); | |
| 220 | |
| 221 IndexedDBDispatcherHost* parent_; | |
| 222 IDMap<IndexedDBConnection, IDMapOwnPointer> map_; | |
| 223 WebIDBObjectIDToOriginMap database_origin_map_; | |
| 224 TransactionIDToSizeMap transaction_size_map_; | |
| 225 TransactionIDToOriginMap transaction_origin_map_; | |
| 226 TransactionIDToDatabaseIDMap transaction_database_map_; | |
| 227 | |
| 228 // Weak pointers are used when an asynchronous quota request is made, in | |
| 229 // case the dispatcher is torn down before the response returns. | |
| 230 base::WeakPtrFactory<DatabaseDispatcherHost> weak_factory_; | |
| 231 | |
| 232 private: | |
| 233 DISALLOW_COPY_AND_ASSIGN(DatabaseDispatcherHost); | |
| 234 }; | |
| 235 | |
| 236 class CursorDispatcherHost { | |
| 237 public: | |
| 238 explicit CursorDispatcherHost(IndexedDBDispatcherHost* parent); | |
| 239 ~CursorDispatcherHost(); | |
| 240 | |
| 241 bool OnMessageReceived(const IPC::Message& message); | |
| 242 | |
| 243 void OnAdvance(int32_t ipc_object_store_id, | |
| 244 int32_t ipc_thread_id, | |
| 245 int32_t ipc_callbacks_id, | |
| 246 uint32_t count); | |
| 247 void OnContinue(int32_t ipc_object_store_id, | |
| 248 int32_t ipc_thread_id, | |
| 249 int32_t ipc_callbacks_id, | |
| 250 const IndexedDBKey& key, | |
| 251 const IndexedDBKey& primary_key); | |
| 252 void OnPrefetch(int32_t ipc_cursor_id, | |
| 253 int32_t ipc_thread_id, | |
| 254 int32_t ipc_callbacks_id, | |
| 255 int n); | |
| 256 void OnPrefetchReset(int32_t ipc_cursor_id, | |
| 257 int used_prefetches, | |
| 258 int unused_prefetches); | |
| 259 void OnDestroyed(int32_t ipc_cursor_id); | |
| 260 | |
| 261 IndexedDBDispatcherHost* parent_; | |
| 262 RefIDMap<IndexedDBCursor> map_; | |
| 263 | |
| 264 private: | |
| 265 DISALLOW_COPY_AND_ASSIGN(CursorDispatcherHost); | |
| 266 }; | |
| 267 | |
| 268 ~IndexedDBDispatcherHost() override; | |
| 269 | |
| 270 // Helper templates. | |
| 271 template <class ReturnType> | |
| 272 ReturnType* GetOrTerminateProcess(IDMap<ReturnType, IDMapOwnPointer>* map, | |
| 273 int32_t ipc_return_object_id); | |
| 274 template <class ReturnType> | |
| 275 ReturnType* GetOrTerminateProcess(RefIDMap<ReturnType>* map, | |
| 276 int32_t ipc_return_object_id); | |
| 277 | |
| 278 template <typename MapType> | |
| 279 void DestroyObject(MapType* map, int32_t ipc_object_id); | |
| 280 | |
| 281 // Message processing. Most of the work is delegated to the dispatcher hosts | |
| 282 // below. | |
| 283 void OnIDBFactoryGetDatabaseNames( | |
| 284 const IndexedDBHostMsg_FactoryGetDatabaseNames_Params& p); | |
| 285 void OnIDBFactoryOpen(const IndexedDBHostMsg_FactoryOpen_Params& p); | |
| 286 | |
| 287 void OnIDBFactoryDeleteDatabase( | |
| 288 const IndexedDBHostMsg_FactoryDeleteDatabase_Params& p); | |
| 289 | |
| 290 void OnAckReceivedBlobs(const std::vector<std::string>& uuids); | |
| 291 void OnPutHelper(const IndexedDBHostMsg_DatabasePut_Params& params, | |
| 292 std::vector<storage::BlobDataHandle*> handles); | |
| 293 | |
| 294 void ResetDispatcherHosts(); | |
| 295 void DropBlobData(const std::string& uuid); | |
| 296 | |
| 297 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 298 scoped_refptr<IndexedDBContextImpl> indexed_db_context_; | |
| 299 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; | |
| 300 | |
| 301 // Maps blob uuid to a pair (handle, ref count). Entry is added and/or count | |
| 302 // is incremented in HoldBlobData(), and count is decremented and/or entry | |
| 303 // removed in DropBlobData(). | |
| 304 std::map<std::string, | |
| 305 std::pair<std::unique_ptr<storage::BlobDataHandle>, int>> | |
| 306 blob_data_handle_map_; | |
| 307 | |
| 308 // Only access on IndexedDB thread. | |
| 309 std::unique_ptr<DatabaseDispatcherHost> database_dispatcher_host_; | |
| 310 std::unique_ptr<CursorDispatcherHost> cursor_dispatcher_host_; | |
| 311 | |
| 312 // Used to set file permissions for blob storage. | |
| 313 int ipc_process_id_; | |
| 314 | |
| 315 DISALLOW_IMPLICIT_CONSTRUCTORS(IndexedDBDispatcherHost); | |
| 316 }; | |
| 317 | |
| 318 } // namespace content | |
| 319 | |
| 320 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DISPATCHER_HOST_H_ | |
| OLD | NEW |