Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/media/webrtc_identity_store_backend.h" | 5 #include "content/browser/media/webrtc_identity_store_backend.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/strings/string_util.h" | |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 10 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 11 #include "sql/error_delegate_util.h" | 12 #include "sql/error_delegate_util.h" |
| 12 #include "sql/statement.h" | 13 #include "sql/statement.h" |
| 13 #include "sql/transaction.h" | 14 #include "sql/transaction.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 #include "webkit/browser/quota/special_storage_policy.h" | 16 #include "webkit/browser/quota/special_storage_policy.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 | 19 |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 } | 337 } |
| 337 | 338 |
| 338 WebRTCIdentityStoreBackend::~WebRTCIdentityStoreBackend() {} | 339 WebRTCIdentityStoreBackend::~WebRTCIdentityStoreBackend() {} |
| 339 | 340 |
| 340 void WebRTCIdentityStoreBackend::OnLoaded(scoped_ptr<IdentityMap> out_map) { | 341 void WebRTCIdentityStoreBackend::OnLoaded(scoped_ptr<IdentityMap> out_map) { |
| 341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 342 | 343 |
| 343 if (state_ != LOADING) | 344 if (state_ != LOADING) |
| 344 return; | 345 return; |
| 345 | 346 |
| 346 DVLOG(2) << "WebRTC identity store has loaded."; | 347 DVLOG(3) << "WebRTC identity store has loaded."; |
| 347 | 348 |
| 348 state_ = LOADED; | 349 state_ = LOADED; |
| 349 identities_.swap(*out_map); | 350 identities_.swap(*out_map); |
| 350 | 351 |
| 351 for (size_t i = 0; i < pending_find_requests_.size(); ++i) { | 352 for (size_t i = 0; i < pending_find_requests_.size(); ++i) { |
| 352 FindIdentity(pending_find_requests_[i]->origin, | 353 FindIdentity(pending_find_requests_[i]->origin, |
| 353 pending_find_requests_[i]->identity_name, | 354 pending_find_requests_[i]->identity_name, |
| 354 pending_find_requests_[i]->common_name, | 355 pending_find_requests_[i]->common_name, |
| 355 pending_find_requests_[i]->callback); | 356 pending_find_requests_[i]->callback); |
| 356 delete pending_find_requests_[i]; | 357 delete pending_find_requests_[i]; |
| 357 } | 358 } |
| 358 pending_find_requests_.clear(); | 359 pending_find_requests_.clear(); |
| 359 } | 360 } |
| 360 | 361 |
| 361 // | 362 // |
| 362 // Implementation of SqlLiteStorage. | 363 // Implementation of SqlLiteStorage. |
| 363 // | 364 // |
| 364 | 365 |
| 365 void WebRTCIdentityStoreBackend::SqlLiteStorage::Load(IdentityMap* out_map) { | 366 void WebRTCIdentityStoreBackend::SqlLiteStorage::Load(IdentityMap* out_map) { |
| 366 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 367 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 367 DCHECK(!db_.get()); | 368 DCHECK(!db_.get()); |
| 368 | 369 |
| 369 // Ensure the parent directory for storing certs is created before reading | 370 // Ensure the parent directory for storing certs is created before reading |
| 370 // from it. | 371 // from it. |
| 371 const base::FilePath dir = path_.DirName(); | 372 const base::FilePath dir = path_.DirName(); |
| 372 if (!base::PathExists(dir) && !base::CreateDirectory(dir)) { | 373 if (!base::PathExists(dir) && !base::CreateDirectory(dir)) { |
| 373 DLOG(ERROR) << "Unable to open DB file path."; | 374 DVLOG(2) << "Unable to open DB file path."; |
| 374 return; | 375 return; |
| 375 } | 376 } |
| 376 | 377 |
| 377 db_.reset(new sql::Connection()); | 378 db_.reset(new sql::Connection()); |
| 378 | 379 |
| 379 db_->set_error_callback(base::Bind(&SqlLiteStorage::OnDatabaseError, this)); | 380 db_->set_error_callback(base::Bind(&SqlLiteStorage::OnDatabaseError, this)); |
| 380 | 381 |
| 381 if (!db_->Open(path_)) { | 382 if (!db_->Open(path_)) { |
| 382 DLOG(ERROR) << "Unable to open DB."; | 383 DVLOG(2) << "Unable to open DB."; |
|
Scott Hess - ex-Googler
2014/05/24 05:06:41
For all of these log lines, please verify that you
jiayl
2014/05/27 18:18:31
I don't think it's redundant with the sql/ logs, w
| |
| 383 db_.reset(); | 384 db_.reset(); |
| 384 return; | 385 return; |
| 385 } | 386 } |
| 386 | 387 |
| 387 if (!InitDB(db_.get())) { | 388 if (!InitDB(db_.get())) { |
| 388 DLOG(ERROR) << "Unable to init DB."; | 389 DVLOG(2) << "Unable to init DB."; |
| 389 db_.reset(); | 390 db_.reset(); |
| 390 return; | 391 return; |
| 391 } | 392 } |
| 392 | 393 |
| 393 db_->Preload(); | 394 db_->Preload(); |
| 394 | 395 |
| 395 // Delete expired identities. | 396 // Delete expired identities. |
| 396 DeleteBetween(base::Time(), base::Time::Now() - validity_period_); | 397 DeleteBetween(base::Time(), base::Time::Now() - validity_period_); |
| 397 | 398 |
| 398 // Slurp all the identities into the out_map. | 399 // Slurp all the identities into the out_map. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 463 SQL_FROM_HERE, | 464 SQL_FROM_HERE, |
| 464 "DELETE FROM webrtc_identity_store" | 465 "DELETE FROM webrtc_identity_store" |
| 465 " WHERE creation_time >= ? AND creation_time <= ?")); | 466 " WHERE creation_time >= ? AND creation_time <= ?")); |
| 466 CHECK(del_stmt.is_valid()); | 467 CHECK(del_stmt.is_valid()); |
| 467 | 468 |
| 468 del_stmt.BindInt64(0, delete_begin.ToInternalValue()); | 469 del_stmt.BindInt64(0, delete_begin.ToInternalValue()); |
| 469 del_stmt.BindInt64(1, delete_end.ToInternalValue()); | 470 del_stmt.BindInt64(1, delete_end.ToInternalValue()); |
| 470 | 471 |
| 471 sql::Transaction transaction(db_.get()); | 472 sql::Transaction transaction(db_.get()); |
| 472 if (!transaction.Begin()) { | 473 if (!transaction.Begin()) { |
| 473 DLOG(ERROR) << "Failed to begin the transaction."; | 474 DVLOG(2) << "Failed to begin the transaction."; |
| 474 return; | 475 return; |
| 475 } | 476 } |
| 476 | 477 |
| 477 CHECK(del_stmt.Run()); | 478 if (!del_stmt.Run()) { |
| 478 transaction.Commit(); | 479 DVLOG(2) << "Failed to run the delete statement."; |
| 480 return; | |
| 481 } | |
| 482 | |
| 483 if (!transaction.Commit()) | |
| 484 DVLOG(2) << "Failed to commit the transaction."; | |
| 479 } | 485 } |
| 480 | 486 |
| 481 void WebRTCIdentityStoreBackend::SqlLiteStorage::OnDatabaseError( | 487 void WebRTCIdentityStoreBackend::SqlLiteStorage::OnDatabaseError( |
| 482 int error, | 488 int error, |
| 483 sql::Statement* stmt) { | 489 sql::Statement* stmt) { |
| 484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 485 if (!sql::IsErrorCatastrophic(error)) | 491 |
| 492 std::string statement(stmt->GetSQLStatement()); | |
| 493 | |
| 494 // "DELETE" failures are always non-recoverable, because we don't want to keep | |
| 495 // unwanted identities (e.g. expired, or user requested to delete) in the | |
| 496 // database. | |
| 497 if (!sql::IsErrorCatastrophic(error) && | |
| 498 !StartsWithASCII(statement, "DELETE", false)) { | |
|
Ami GONE FROM CHROMIUM
2014/05/23 23:30:30
ಠ_ಠ
What about "dElEte"?
(should you ToLower() t
jiayl
2014/05/23 23:34:54
The comparison is case insensitive.
DB corruption
Ami GONE FROM CHROMIUM
2014/05/23 23:47:01
So it is!
(I missed the "false")
jiayl
2014/05/23 23:53:25
The expectations of this class and not expressed i
Scott Hess - ex-Googler
2014/05/24 05:06:41
If you're going to use this kind of assumption to
jiayl
2014/05/27 18:18:31
Now all DB errors will trigger RazeAndClose. A tes
| |
| 486 return; | 499 return; |
| 500 } | |
| 501 | |
| 502 DVLOG(2) << "Database error " << error << " for statement " << statement; | |
| 487 db_->RazeAndClose(); | 503 db_->RazeAndClose(); |
| 504 db_.reset(); | |
| 488 } | 505 } |
| 489 | 506 |
| 490 void WebRTCIdentityStoreBackend::SqlLiteStorage::BatchOperation( | 507 void WebRTCIdentityStoreBackend::SqlLiteStorage::BatchOperation( |
| 491 OperationType type, | 508 OperationType type, |
| 492 const GURL& origin, | 509 const GURL& origin, |
| 493 const std::string& identity_name, | 510 const std::string& identity_name, |
| 494 const Identity& identity) { | 511 const Identity& identity) { |
| 495 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 512 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 496 // Commit every 30 seconds. | 513 // Commit every 30 seconds. |
| 497 static const base::TimeDelta kCommitInterval( | 514 static const base::TimeDelta kCommitInterval( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 535 CHECK(add_stmt.is_valid()); | 552 CHECK(add_stmt.is_valid()); |
| 536 | 553 |
| 537 sql::Statement del_stmt(db_->GetCachedStatement( | 554 sql::Statement del_stmt(db_->GetCachedStatement( |
| 538 SQL_FROM_HERE, | 555 SQL_FROM_HERE, |
| 539 "DELETE FROM webrtc_identity_store WHERE origin=? AND identity_name=?")); | 556 "DELETE FROM webrtc_identity_store WHERE origin=? AND identity_name=?")); |
| 540 | 557 |
| 541 CHECK(del_stmt.is_valid()); | 558 CHECK(del_stmt.is_valid()); |
| 542 | 559 |
| 543 sql::Transaction transaction(db_.get()); | 560 sql::Transaction transaction(db_.get()); |
| 544 if (!transaction.Begin()) { | 561 if (!transaction.Begin()) { |
| 545 DLOG(ERROR) << "Failed to begin the transaction."; | 562 DVLOG(2) << "Failed to begin the transaction."; |
| 546 return; | 563 return; |
| 547 } | 564 } |
| 548 | 565 |
| 549 for (PendingOperationList::iterator it = pending_operations_.begin(); | 566 for (PendingOperationList::iterator it = pending_operations_.begin(); |
| 550 it != pending_operations_.end(); | 567 it != pending_operations_.end(); |
| 551 ++it) { | 568 ++it) { |
| 552 scoped_ptr<PendingOperation> po(*it); | 569 scoped_ptr<PendingOperation> po(*it); |
| 553 switch (po->type) { | 570 switch (po->type) { |
| 554 case ADD_IDENTITY: { | 571 case ADD_IDENTITY: { |
| 555 add_stmt.Reset(true); | 572 add_stmt.Reset(true); |
| 556 add_stmt.BindString(0, po->origin.spec()); | 573 add_stmt.BindString(0, po->origin.spec()); |
| 557 add_stmt.BindString(1, po->identity_name); | 574 add_stmt.BindString(1, po->identity_name); |
| 558 add_stmt.BindString(2, po->identity.common_name); | 575 add_stmt.BindString(2, po->identity.common_name); |
| 559 const std::string& cert = po->identity.certificate; | 576 const std::string& cert = po->identity.certificate; |
| 560 add_stmt.BindBlob(3, cert.data(), cert.size()); | 577 add_stmt.BindBlob(3, cert.data(), cert.size()); |
| 561 const std::string& private_key = po->identity.private_key; | 578 const std::string& private_key = po->identity.private_key; |
| 562 add_stmt.BindBlob(4, private_key.data(), private_key.size()); | 579 add_stmt.BindBlob(4, private_key.data(), private_key.size()); |
| 563 add_stmt.BindInt64(5, po->identity.creation_time); | 580 add_stmt.BindInt64(5, po->identity.creation_time); |
| 564 CHECK(add_stmt.Run()); | 581 if (!add_stmt.Run()) { |
| 582 DVLOG(2) << "Failed to add the identity to DB."; | |
| 583 return; | |
| 584 } | |
| 565 break; | 585 break; |
| 566 } | 586 } |
| 567 case DELETE_IDENTITY: | 587 case DELETE_IDENTITY: |
| 568 del_stmt.Reset(true); | 588 del_stmt.Reset(true); |
| 569 del_stmt.BindString(0, po->origin.spec()); | 589 del_stmt.BindString(0, po->origin.spec()); |
| 570 del_stmt.BindString(1, po->identity_name); | 590 del_stmt.BindString(1, po->identity_name); |
| 571 CHECK(del_stmt.Run()); | 591 if (!del_stmt.Run()) { |
| 592 DVLOG(2) << "Failed to delete the identity from DB."; | |
| 593 return; | |
| 594 } | |
| 572 break; | 595 break; |
| 573 | 596 |
| 574 default: | 597 default: |
| 575 NOTREACHED(); | 598 NOTREACHED(); |
| 576 break; | 599 break; |
| 577 } | 600 } |
| 578 } | 601 } |
| 579 transaction.Commit(); | 602 |
| 603 if (!transaction.Commit()) { | |
| 604 DVLOG(2) << "Failed to commit the transaction."; | |
| 605 return; | |
| 606 } | |
| 607 | |
| 580 pending_operations_.clear(); | 608 pending_operations_.clear(); |
| 581 } | 609 } |
| 582 | 610 |
| 583 } // namespace content | 611 } // namespace content |
| OLD | NEW |