| 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 #include "content/browser/indexed_db/indexed_db_dispatcher_host.h" | 5 #include "content/browser/indexed_db/indexed_db_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/user_metrics.h" | 31 #include "content/public/browser/user_metrics.h" |
| 32 #include "content/public/common/content_switches.h" | 32 #include "content/public/common/content_switches.h" |
| 33 #include "content/public/common/result_codes.h" | 33 #include "content/public/common/result_codes.h" |
| 34 #include "storage/browser/blob/blob_data_builder.h" | 34 #include "storage/browser/blob/blob_data_builder.h" |
| 35 #include "storage/browser/blob/blob_storage_context.h" | 35 #include "storage/browser/blob/blob_storage_context.h" |
| 36 #include "storage/browser/database/database_util.h" | 36 #include "storage/browser/database/database_util.h" |
| 37 #include "storage/browser/quota/quota_manager_proxy.h" | 37 #include "storage/browser/quota/quota_manager_proxy.h" |
| 38 #include "storage/common/database/database_identifier.h" | 38 #include "storage/common/database/database_identifier.h" |
| 39 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc
eption.h" | 39 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc
eption.h" |
| 40 #include "url/gurl.h" | 40 #include "url/origin.h" |
| 41 | 41 |
| 42 using storage::DatabaseUtil; | 42 using storage::DatabaseUtil; |
| 43 using blink::WebIDBKey; | 43 using blink::WebIDBKey; |
| 44 | 44 |
| 45 namespace content { | 45 namespace content { |
| 46 | 46 |
| 47 namespace { | 47 namespace { |
| 48 | 48 |
| 49 bool IsValidOrigin(const url::Origin& origin) { | 49 bool IsValidOrigin(const url::Origin& origin) { |
| 50 return !origin.unique(); | 50 return !origin.unique(); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 174 |
| 175 int32_t IndexedDBDispatcherHost::Add(IndexedDBCursor* cursor) { | 175 int32_t IndexedDBDispatcherHost::Add(IndexedDBCursor* cursor) { |
| 176 if (!cursor_dispatcher_host_) { | 176 if (!cursor_dispatcher_host_) { |
| 177 return 0; | 177 return 0; |
| 178 } | 178 } |
| 179 return cursor_dispatcher_host_->map_.Add(cursor); | 179 return cursor_dispatcher_host_->map_.Add(cursor); |
| 180 } | 180 } |
| 181 | 181 |
| 182 int32_t IndexedDBDispatcherHost::Add(IndexedDBConnection* connection, | 182 int32_t IndexedDBDispatcherHost::Add(IndexedDBConnection* connection, |
| 183 int32_t ipc_thread_id, | 183 int32_t ipc_thread_id, |
| 184 const GURL& origin_url) { | 184 const url::Origin& origin) { |
| 185 if (!database_dispatcher_host_) { | 185 if (!database_dispatcher_host_) { |
| 186 connection->Close(); | 186 connection->Close(); |
| 187 delete connection; | 187 delete connection; |
| 188 return -1; | 188 return -1; |
| 189 } | 189 } |
| 190 int32_t ipc_database_id = database_dispatcher_host_->map_.Add(connection); | 190 int32_t ipc_database_id = database_dispatcher_host_->map_.Add(connection); |
| 191 context()->ConnectionOpened(origin_url, connection); | 191 context()->ConnectionOpened(origin, connection); |
| 192 database_dispatcher_host_->database_url_map_[ipc_database_id] = origin_url; | 192 database_dispatcher_host_->database_origin_map_[ipc_database_id] = origin; |
| 193 return ipc_database_id; | 193 return ipc_database_id; |
| 194 } | 194 } |
| 195 | 195 |
| 196 void IndexedDBDispatcherHost::RegisterTransactionId(int64_t host_transaction_id, | 196 void IndexedDBDispatcherHost::RegisterTransactionId(int64_t host_transaction_id, |
| 197 const GURL& url) { | 197 const url::Origin& origin) { |
| 198 if (!database_dispatcher_host_) | 198 if (!database_dispatcher_host_) |
| 199 return; | 199 return; |
| 200 database_dispatcher_host_->transaction_size_map_[host_transaction_id] = 0; | 200 database_dispatcher_host_->transaction_size_map_[host_transaction_id] = 0; |
| 201 database_dispatcher_host_->transaction_url_map_[host_transaction_id] = url; | 201 database_dispatcher_host_->transaction_origin_map_[host_transaction_id] = |
| 202 origin; |
| 202 } | 203 } |
| 203 | 204 |
| 204 int64_t IndexedDBDispatcherHost::HostTransactionId(int64_t transaction_id) { | 205 int64_t IndexedDBDispatcherHost::HostTransactionId(int64_t transaction_id) { |
| 205 // Inject the renderer process id into the transaction id, to | 206 // Inject the renderer process id into the transaction id, to |
| 206 // uniquely identify this transaction, and effectively bind it to | 207 // uniquely identify this transaction, and effectively bind it to |
| 207 // the renderer that initiated it. The lower 32 bits of | 208 // the renderer that initiated it. The lower 32 bits of |
| 208 // transaction_id are guaranteed to be unique within that renderer. | 209 // transaction_id are guaranteed to be unique within that renderer. |
| 209 base::ProcessId pid = peer_pid(); | 210 base::ProcessId pid = peer_pid(); |
| 210 DCHECK(!(transaction_id >> 32)) << "Transaction ids can only be 32 bits"; | 211 DCHECK(!(transaction_id >> 32)) << "Transaction ids can only be 32 bits"; |
| 211 static_assert(sizeof(base::ProcessId) <= sizeof(int32_t), | 212 static_assert(sizeof(base::ProcessId) <= sizeof(int32_t), |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 | 344 |
| 344 base::TimeTicks begin_time = base::TimeTicks::Now(); | 345 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 345 base::FilePath indexed_db_path = indexed_db_context_->data_path(); | 346 base::FilePath indexed_db_path = indexed_db_context_->data_path(); |
| 346 | 347 |
| 347 int64_t host_transaction_id = HostTransactionId(params.transaction_id); | 348 int64_t host_transaction_id = HostTransactionId(params.transaction_id); |
| 348 | 349 |
| 349 // TODO(dgrogan): Don't let a non-existing database be opened (and therefore | 350 // TODO(dgrogan): Don't let a non-existing database be opened (and therefore |
| 350 // created) if this origin is already over quota. | 351 // created) if this origin is already over quota. |
| 351 scoped_refptr<IndexedDBCallbacks> callbacks = new IndexedDBCallbacks( | 352 scoped_refptr<IndexedDBCallbacks> callbacks = new IndexedDBCallbacks( |
| 352 this, params.ipc_thread_id, params.ipc_callbacks_id, | 353 this, params.ipc_thread_id, params.ipc_callbacks_id, |
| 353 params.ipc_database_callbacks_id, host_transaction_id, | 354 params.ipc_database_callbacks_id, host_transaction_id, params.origin); |
| 354 GURL(params.origin.Serialize())); | |
| 355 callbacks->SetConnectionOpenStartTime(begin_time); | 355 callbacks->SetConnectionOpenStartTime(begin_time); |
| 356 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks = | 356 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks = |
| 357 new IndexedDBDatabaseCallbacks( | 357 new IndexedDBDatabaseCallbacks( |
| 358 this, params.ipc_thread_id, params.ipc_database_callbacks_id); | 358 this, params.ipc_thread_id, params.ipc_database_callbacks_id); |
| 359 IndexedDBPendingConnection connection(callbacks, | 359 IndexedDBPendingConnection connection(callbacks, |
| 360 database_callbacks, | 360 database_callbacks, |
| 361 ipc_process_id_, | 361 ipc_process_id_, |
| 362 host_transaction_id, | 362 host_transaction_id, |
| 363 params.version); | 363 params.version); |
| 364 DCHECK(request_context_); | 364 DCHECK(request_context_); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 397 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 398 for (const auto& uuid : uuids) | 398 for (const auto& uuid : uuids) |
| 399 DropBlobData(uuid); | 399 DropBlobData(uuid); |
| 400 } | 400 } |
| 401 | 401 |
| 402 void IndexedDBDispatcherHost::FinishTransaction(int64_t host_transaction_id, | 402 void IndexedDBDispatcherHost::FinishTransaction(int64_t host_transaction_id, |
| 403 bool committed) { | 403 bool committed) { |
| 404 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); | 404 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); |
| 405 if (!database_dispatcher_host_) | 405 if (!database_dispatcher_host_) |
| 406 return; | 406 return; |
| 407 TransactionIDToURLMap& transaction_url_map = | 407 TransactionIDToOriginMap& transaction_origin_map = |
| 408 database_dispatcher_host_->transaction_url_map_; | 408 database_dispatcher_host_->transaction_origin_map_; |
| 409 TransactionIDToSizeMap& transaction_size_map = | 409 TransactionIDToSizeMap& transaction_size_map = |
| 410 database_dispatcher_host_->transaction_size_map_; | 410 database_dispatcher_host_->transaction_size_map_; |
| 411 TransactionIDToDatabaseIDMap& transaction_database_map = | 411 TransactionIDToDatabaseIDMap& transaction_database_map = |
| 412 database_dispatcher_host_->transaction_database_map_; | 412 database_dispatcher_host_->transaction_database_map_; |
| 413 if (committed) | 413 if (committed) |
| 414 context()->TransactionComplete(transaction_url_map[host_transaction_id]); | 414 context()->TransactionComplete(transaction_origin_map[host_transaction_id]); |
| 415 transaction_url_map.erase(host_transaction_id); | 415 transaction_origin_map.erase(host_transaction_id); |
| 416 transaction_size_map.erase(host_transaction_id); | 416 transaction_size_map.erase(host_transaction_id); |
| 417 transaction_database_map.erase(host_transaction_id); | 417 transaction_database_map.erase(host_transaction_id); |
| 418 } | 418 } |
| 419 | 419 |
| 420 ////////////////////////////////////////////////////////////////////// | 420 ////////////////////////////////////////////////////////////////////// |
| 421 // Helper templates. | 421 // Helper templates. |
| 422 // | 422 // |
| 423 | 423 |
| 424 template <typename ObjectType> | 424 template <typename ObjectType> |
| 425 ObjectType* IndexedDBDispatcherHost::GetOrTerminateProcess( | 425 ObjectType* IndexedDBDispatcherHost::GetOrTerminateProcess( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 | 462 |
| 463 IndexedDBDispatcherHost::DatabaseDispatcherHost::DatabaseDispatcherHost( | 463 IndexedDBDispatcherHost::DatabaseDispatcherHost::DatabaseDispatcherHost( |
| 464 IndexedDBDispatcherHost* parent) | 464 IndexedDBDispatcherHost* parent) |
| 465 : parent_(parent), weak_factory_(this) { | 465 : parent_(parent), weak_factory_(this) { |
| 466 map_.set_check_on_null_data(true); | 466 map_.set_check_on_null_data(true); |
| 467 } | 467 } |
| 468 | 468 |
| 469 IndexedDBDispatcherHost::DatabaseDispatcherHost::~DatabaseDispatcherHost() { | 469 IndexedDBDispatcherHost::DatabaseDispatcherHost::~DatabaseDispatcherHost() { |
| 470 // TODO(alecflett): uncomment these when we find the source of these leaks. | 470 // TODO(alecflett): uncomment these when we find the source of these leaks. |
| 471 // DCHECK(transaction_size_map_.empty()); | 471 // DCHECK(transaction_size_map_.empty()); |
| 472 // DCHECK(transaction_url_map_.empty()); | 472 // DCHECK(transaction_origin_map_.empty()); |
| 473 } | 473 } |
| 474 | 474 |
| 475 void IndexedDBDispatcherHost::DatabaseDispatcherHost::CloseAll() { | 475 void IndexedDBDispatcherHost::DatabaseDispatcherHost::CloseAll() { |
| 476 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); | 476 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); |
| 477 // Abort outstanding transactions started by connections in the associated | 477 // Abort outstanding transactions started by connections in the associated |
| 478 // front-end to unblock later transactions. This should only occur on unclean | 478 // front-end to unblock later transactions. This should only occur on unclean |
| 479 // (crash) or abrupt (process-kill) shutdowns. | 479 // (crash) or abrupt (process-kill) shutdowns. |
| 480 for (TransactionIDToDatabaseIDMap::iterator iter = | 480 for (TransactionIDToDatabaseIDMap::iterator iter = |
| 481 transaction_database_map_.begin(); | 481 transaction_database_map_.begin(); |
| 482 iter != transaction_database_map_.end();) { | 482 iter != transaction_database_map_.end();) { |
| 483 int64_t transaction_id = iter->first; | 483 int64_t transaction_id = iter->first; |
| 484 int32_t ipc_database_id = iter->second; | 484 int32_t ipc_database_id = iter->second; |
| 485 ++iter; | 485 ++iter; |
| 486 IndexedDBConnection* connection = map_.Lookup(ipc_database_id); | 486 IndexedDBConnection* connection = map_.Lookup(ipc_database_id); |
| 487 if (connection && connection->IsConnected()) { | 487 if (connection && connection->IsConnected()) { |
| 488 connection->database()->Abort( | 488 connection->database()->Abort( |
| 489 transaction_id, | 489 transaction_id, |
| 490 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError)); | 490 IndexedDBDatabaseError(blink::WebIDBDatabaseExceptionUnknownError)); |
| 491 } | 491 } |
| 492 } | 492 } |
| 493 DCHECK(transaction_database_map_.empty()); | 493 DCHECK(transaction_database_map_.empty()); |
| 494 | 494 |
| 495 for (const auto& iter : database_url_map_) { | 495 for (const auto& iter : database_origin_map_) { |
| 496 IndexedDBConnection* connection = map_.Lookup(iter.first); | 496 IndexedDBConnection* connection = map_.Lookup(iter.first); |
| 497 if (connection && connection->IsConnected()) { | 497 if (connection && connection->IsConnected()) { |
| 498 connection->Close(); | 498 connection->Close(); |
| 499 parent_->context()->ConnectionClosed(iter.second, connection); | 499 parent_->context()->ConnectionClosed(iter.second, connection); |
| 500 } | 500 } |
| 501 } | 501 } |
| 502 } | 502 } |
| 503 | 503 |
| 504 bool IndexedDBDispatcherHost::DatabaseDispatcherHost::OnMessageReceived( | 504 bool IndexedDBDispatcherHost::DatabaseDispatcherHost::OnMessageReceived( |
| 505 const IPC::Message& message) { | 505 const IPC::Message& message) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 | 584 |
| 585 if (ContainsKey(transaction_database_map_, host_transaction_id)) { | 585 if (ContainsKey(transaction_database_map_, host_transaction_id)) { |
| 586 DLOG(ERROR) << "Duplicate host_transaction_id."; | 586 DLOG(ERROR) << "Duplicate host_transaction_id."; |
| 587 return; | 587 return; |
| 588 } | 588 } |
| 589 | 589 |
| 590 connection->database()->CreateTransaction( | 590 connection->database()->CreateTransaction( |
| 591 host_transaction_id, connection, params.object_store_ids, params.mode); | 591 host_transaction_id, connection, params.object_store_ids, params.mode); |
| 592 transaction_database_map_[host_transaction_id] = params.ipc_database_id; | 592 transaction_database_map_[host_transaction_id] = params.ipc_database_id; |
| 593 parent_->RegisterTransactionId(host_transaction_id, | 593 parent_->RegisterTransactionId(host_transaction_id, |
| 594 database_url_map_[params.ipc_database_id]); | 594 database_origin_map_[params.ipc_database_id]); |
| 595 } | 595 } |
| 596 | 596 |
| 597 void IndexedDBDispatcherHost::DatabaseDispatcherHost::OnClose( | 597 void IndexedDBDispatcherHost::DatabaseDispatcherHost::OnClose( |
| 598 int32_t ipc_database_id) { | 598 int32_t ipc_database_id) { |
| 599 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); | 599 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); |
| 600 IndexedDBConnection* connection = | 600 IndexedDBConnection* connection = |
| 601 parent_->GetOrTerminateProcess(&map_, ipc_database_id); | 601 parent_->GetOrTerminateProcess(&map_, ipc_database_id); |
| 602 if (!connection || !connection->IsConnected()) | 602 if (!connection || !connection->IsConnected()) |
| 603 return; | 603 return; |
| 604 connection->Close(); | 604 connection->Close(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 616 | 616 |
| 617 void IndexedDBDispatcherHost::DatabaseDispatcherHost::OnDestroyed( | 617 void IndexedDBDispatcherHost::DatabaseDispatcherHost::OnDestroyed( |
| 618 int32_t ipc_object_id) { | 618 int32_t ipc_object_id) { |
| 619 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); | 619 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); |
| 620 IndexedDBConnection* connection = | 620 IndexedDBConnection* connection = |
| 621 parent_->GetOrTerminateProcess(&map_, ipc_object_id); | 621 parent_->GetOrTerminateProcess(&map_, ipc_object_id); |
| 622 if (!connection) | 622 if (!connection) |
| 623 return; | 623 return; |
| 624 if (connection->IsConnected()) | 624 if (connection->IsConnected()) |
| 625 connection->Close(); | 625 connection->Close(); |
| 626 parent_->context()->ConnectionClosed(database_url_map_[ipc_object_id], | 626 parent_->context()->ConnectionClosed(database_origin_map_[ipc_object_id], |
| 627 connection); | 627 connection); |
| 628 database_url_map_.erase(ipc_object_id); | 628 database_origin_map_.erase(ipc_object_id); |
| 629 parent_->DestroyObject(&map_, ipc_object_id); | 629 parent_->DestroyObject(&map_, ipc_object_id); |
| 630 } | 630 } |
| 631 | 631 |
| 632 void IndexedDBDispatcherHost::DatabaseDispatcherHost::OnGet( | 632 void IndexedDBDispatcherHost::DatabaseDispatcherHost::OnGet( |
| 633 const IndexedDBHostMsg_DatabaseGet_Params& params) { | 633 const IndexedDBHostMsg_DatabaseGet_Params& params) { |
| 634 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); | 634 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); |
| 635 IndexedDBConnection* connection = | 635 IndexedDBConnection* connection = |
| 636 parent_->GetOrTerminateProcess(&map_, params.ipc_database_id); | 636 parent_->GetOrTerminateProcess(&map_, params.ipc_database_id); |
| 637 if (!connection || !connection->IsConnected()) | 637 if (!connection || !connection->IsConnected()) |
| 638 return; | 638 return; |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 864 int64_t transaction_size = transaction_size_map_[host_transaction_id]; | 864 int64_t transaction_size = transaction_size_map_[host_transaction_id]; |
| 865 | 865 |
| 866 // Always allow empty or delete-only transactions. | 866 // Always allow empty or delete-only transactions. |
| 867 if (!transaction_size) { | 867 if (!transaction_size) { |
| 868 connection->database()->Commit(host_transaction_id); | 868 connection->database()->Commit(host_transaction_id); |
| 869 return; | 869 return; |
| 870 } | 870 } |
| 871 | 871 |
| 872 parent_->context()->quota_manager_proxy()->GetUsageAndQuota( | 872 parent_->context()->quota_manager_proxy()->GetUsageAndQuota( |
| 873 parent_->context()->TaskRunner(), | 873 parent_->context()->TaskRunner(), |
| 874 transaction_url_map_[host_transaction_id], storage::kStorageTypeTemporary, | 874 GURL(transaction_origin_map_[host_transaction_id].Serialize()), |
| 875 storage::kStorageTypeTemporary, |
| 875 base::Bind(&IndexedDBDispatcherHost::DatabaseDispatcherHost:: | 876 base::Bind(&IndexedDBDispatcherHost::DatabaseDispatcherHost:: |
| 876 OnGotUsageAndQuotaForCommit, | 877 OnGotUsageAndQuotaForCommit, |
| 877 weak_factory_.GetWeakPtr(), ipc_database_id, transaction_id)); | 878 weak_factory_.GetWeakPtr(), ipc_database_id, transaction_id)); |
| 878 } | 879 } |
| 879 | 880 |
| 880 void IndexedDBDispatcherHost::DatabaseDispatcherHost:: | 881 void IndexedDBDispatcherHost::DatabaseDispatcherHost:: |
| 881 OnGotUsageAndQuotaForCommit(int32_t ipc_database_id, | 882 OnGotUsageAndQuotaForCommit(int32_t ipc_database_id, |
| 882 int64_t transaction_id, | 883 int64_t transaction_id, |
| 883 storage::QuotaStatusCode status, | 884 storage::QuotaStatusCode status, |
| 884 int64_t usage, | 885 int64_t usage, |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1041 DLOG(ERROR) << "Unable to reset prefetch"; | 1042 DLOG(ERROR) << "Unable to reset prefetch"; |
| 1042 } | 1043 } |
| 1043 | 1044 |
| 1044 void IndexedDBDispatcherHost::CursorDispatcherHost::OnDestroyed( | 1045 void IndexedDBDispatcherHost::CursorDispatcherHost::OnDestroyed( |
| 1045 int32_t ipc_object_id) { | 1046 int32_t ipc_object_id) { |
| 1046 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); | 1047 DCHECK(parent_->context()->TaskRunner()->RunsTasksOnCurrentThread()); |
| 1047 parent_->DestroyObject(&map_, ipc_object_id); | 1048 parent_->DestroyObject(&map_, ipc_object_id); |
| 1048 } | 1049 } |
| 1049 | 1050 |
| 1050 } // namespace content | 1051 } // namespace content |
| OLD | NEW |