| 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 "webkit/browser/database/database_tracker.h" | 5 #include "webkit/browser/database/database_tracker.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 void DatabaseTracker::DatabaseOpened(const base::string16& origin_identifier, | 113 void DatabaseTracker::DatabaseOpened(const base::string16& origin_identifier, |
| 114 const base::string16& database_name, | 114 const base::string16& database_name, |
| 115 const base::string16& database_description, | 115 const base::string16& database_description, |
| 116 int64 estimated_size, | 116 int64 estimated_size, |
| 117 int64* database_size) { | 117 int64* database_size) { |
| 118 if (shutting_down_ || !LazyInit()) { | 118 if (shutting_down_ || !LazyInit()) { |
| 119 *database_size = 0; | 119 *database_size = 0; |
| 120 return; | 120 return; |
| 121 } | 121 } |
| 122 | 122 |
| 123 if (quota_manager_proxy_) | 123 if (quota_manager_proxy_.get()) |
| 124 quota_manager_proxy_->NotifyStorageAccessed( | 124 quota_manager_proxy_->NotifyStorageAccessed( |
| 125 quota::QuotaClient::kDatabase, | 125 quota::QuotaClient::kDatabase, |
| 126 webkit_base::GetOriginURLFromIdentifier(origin_identifier), | 126 webkit_base::GetOriginURLFromIdentifier(origin_identifier), |
| 127 quota::kStorageTypeTemporary); | 127 quota::kStorageTypeTemporary); |
| 128 | 128 |
| 129 InsertOrUpdateDatabaseDetails(origin_identifier, database_name, | 129 InsertOrUpdateDatabaseDetails(origin_identifier, database_name, |
| 130 database_description, estimated_size); | 130 database_description, estimated_size); |
| 131 if (database_connections_.AddConnection(origin_identifier, database_name)) { | 131 if (database_connections_.AddConnection(origin_identifier, database_name)) { |
| 132 *database_size = SeedOpenDatabaseInfo(origin_identifier, | 132 *database_size = SeedOpenDatabaseInfo(origin_identifier, |
| 133 database_name, | 133 database_name, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 148 | 148 |
| 149 void DatabaseTracker::DatabaseClosed(const base::string16& origin_identifier, | 149 void DatabaseTracker::DatabaseClosed(const base::string16& origin_identifier, |
| 150 const base::string16& database_name) { | 150 const base::string16& database_name) { |
| 151 if (database_connections_.IsEmpty()) { | 151 if (database_connections_.IsEmpty()) { |
| 152 DCHECK(!is_initialized_); | 152 DCHECK(!is_initialized_); |
| 153 return; | 153 return; |
| 154 } | 154 } |
| 155 | 155 |
| 156 // We call NotifiyStorageAccessed when a db is opened and also when | 156 // We call NotifiyStorageAccessed when a db is opened and also when |
| 157 // closed because we don't call it for read while open. | 157 // closed because we don't call it for read while open. |
| 158 if (quota_manager_proxy_) | 158 if (quota_manager_proxy_.get()) |
| 159 quota_manager_proxy_->NotifyStorageAccessed( | 159 quota_manager_proxy_->NotifyStorageAccessed( |
| 160 quota::QuotaClient::kDatabase, | 160 quota::QuotaClient::kDatabase, |
| 161 webkit_base::GetOriginURLFromIdentifier(origin_identifier), | 161 webkit_base::GetOriginURLFromIdentifier(origin_identifier), |
| 162 quota::kStorageTypeTemporary); | 162 quota::kStorageTypeTemporary); |
| 163 | 163 |
| 164 UpdateOpenDatabaseSizeAndNotify(origin_identifier, database_name); | 164 UpdateOpenDatabaseSizeAndNotify(origin_identifier, database_name); |
| 165 if (database_connections_.RemoveConnection(origin_identifier, database_name)) | 165 if (database_connections_.RemoveConnection(origin_identifier, database_name)) |
| 166 DeleteDatabaseIfNeeded(origin_identifier, database_name); | 166 DeleteDatabaseIfNeeded(origin_identifier, database_name); |
| 167 } | 167 } |
| 168 | 168 |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 bool DatabaseTracker::DeleteClosedDatabase( | 346 bool DatabaseTracker::DeleteClosedDatabase( |
| 347 const base::string16& origin_identifier, | 347 const base::string16& origin_identifier, |
| 348 const base::string16& database_name) { | 348 const base::string16& database_name) { |
| 349 if (!LazyInit()) | 349 if (!LazyInit()) |
| 350 return false; | 350 return false; |
| 351 | 351 |
| 352 // Check if the database is opened by any renderer. | 352 // Check if the database is opened by any renderer. |
| 353 if (database_connections_.IsDatabaseOpened(origin_identifier, database_name)) | 353 if (database_connections_.IsDatabaseOpened(origin_identifier, database_name)) |
| 354 return false; | 354 return false; |
| 355 | 355 |
| 356 int64 db_file_size = quota_manager_proxy_ ? | 356 int64 db_file_size = quota_manager_proxy_.get() |
| 357 GetDBFileSize(origin_identifier, database_name) : 0; | 357 ? GetDBFileSize(origin_identifier, database_name) |
| 358 : 0; |
| 358 | 359 |
| 359 // Try to delete the file on the hard drive. | 360 // Try to delete the file on the hard drive. |
| 360 base::FilePath db_file = GetFullDBFilePath(origin_identifier, database_name); | 361 base::FilePath db_file = GetFullDBFilePath(origin_identifier, database_name); |
| 361 if (file_util::PathExists(db_file) && !file_util::Delete(db_file, false)) | 362 if (file_util::PathExists(db_file) && !file_util::Delete(db_file, false)) |
| 362 return false; | 363 return false; |
| 363 | 364 |
| 364 // Also delete any orphaned journal file. | 365 // Also delete any orphaned journal file. |
| 365 DCHECK(db_file.Extension().empty()); | 366 DCHECK(db_file.Extension().empty()); |
| 366 file_util::Delete(db_file.InsertBeforeExtensionASCII( | 367 file_util::Delete(db_file.InsertBeforeExtensionASCII( |
| 367 DatabaseUtil::kJournalFileSuffix), false); | 368 DatabaseUtil::kJournalFileSuffix), false); |
| 368 | 369 |
| 369 if (quota_manager_proxy_ && db_file_size) | 370 if (quota_manager_proxy_.get() && db_file_size) |
| 370 quota_manager_proxy_->NotifyStorageModified( | 371 quota_manager_proxy_->NotifyStorageModified( |
| 371 quota::QuotaClient::kDatabase, | 372 quota::QuotaClient::kDatabase, |
| 372 webkit_base::GetOriginURLFromIdentifier(origin_identifier), | 373 webkit_base::GetOriginURLFromIdentifier(origin_identifier), |
| 373 quota::kStorageTypeTemporary, | 374 quota::kStorageTypeTemporary, |
| 374 -db_file_size); | 375 -db_file_size); |
| 375 | 376 |
| 376 // Clean up the main database and invalidate the cached record. | 377 // Clean up the main database and invalidate the cached record. |
| 377 databases_table_->DeleteDatabaseDetails(origin_identifier, database_name); | 378 databases_table_->DeleteDatabaseDetails(origin_identifier, database_name); |
| 378 origins_info_map_.erase(origin_identifier); | 379 origins_info_map_.erase(origin_identifier); |
| 379 | 380 |
| 380 std::vector<DatabaseDetails> details; | 381 std::vector<DatabaseDetails> details; |
| 381 if (databases_table_->GetAllDatabaseDetailsForOrigin( | 382 if (databases_table_->GetAllDatabaseDetailsForOrigin( |
| 382 origin_identifier, &details) && details.empty()) { | 383 origin_identifier, &details) && details.empty()) { |
| 383 // Try to delete the origin in case this was the last database. | 384 // Try to delete the origin in case this was the last database. |
| 384 DeleteOrigin(origin_identifier, false); | 385 DeleteOrigin(origin_identifier, false); |
| 385 } | 386 } |
| 386 return true; | 387 return true; |
| 387 } | 388 } |
| 388 | 389 |
| 389 bool DatabaseTracker::DeleteOrigin(const base::string16& origin_identifier, | 390 bool DatabaseTracker::DeleteOrigin(const base::string16& origin_identifier, |
| 390 bool force) { | 391 bool force) { |
| 391 if (!LazyInit()) | 392 if (!LazyInit()) |
| 392 return false; | 393 return false; |
| 393 | 394 |
| 394 // Check if any database in this origin is opened by any renderer. | 395 // Check if any database in this origin is opened by any renderer. |
| 395 if (database_connections_.IsOriginUsed(origin_identifier) && !force) | 396 if (database_connections_.IsOriginUsed(origin_identifier) && !force) |
| 396 return false; | 397 return false; |
| 397 | 398 |
| 398 int64 deleted_size = 0; | 399 int64 deleted_size = 0; |
| 399 if (quota_manager_proxy_) { | 400 if (quota_manager_proxy_.get()) { |
| 400 CachedOriginInfo* origin_info = GetCachedOriginInfo(origin_identifier); | 401 CachedOriginInfo* origin_info = GetCachedOriginInfo(origin_identifier); |
| 401 if (origin_info) | 402 if (origin_info) |
| 402 deleted_size = origin_info->TotalSize(); | 403 deleted_size = origin_info->TotalSize(); |
| 403 } | 404 } |
| 404 | 405 |
| 405 origins_info_map_.erase(origin_identifier); | 406 origins_info_map_.erase(origin_identifier); |
| 406 base::FilePath origin_dir = db_dir_.Append(base::FilePath::FromWStringHack( | 407 base::FilePath origin_dir = db_dir_.Append(base::FilePath::FromWStringHack( |
| 407 UTF16ToWide(origin_identifier))); | 408 UTF16ToWide(origin_identifier))); |
| 408 | 409 |
| 409 // Create a temporary directory to move possibly still existing databases to, | 410 // Create a temporary directory to move possibly still existing databases to, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 420 for (base::FilePath database = databases.Next(); !database.empty(); | 421 for (base::FilePath database = databases.Next(); !database.empty(); |
| 421 database = databases.Next()) { | 422 database = databases.Next()) { |
| 422 base::FilePath new_file = new_origin_dir.Append(database.BaseName()); | 423 base::FilePath new_file = new_origin_dir.Append(database.BaseName()); |
| 423 file_util::Move(database, new_file); | 424 file_util::Move(database, new_file); |
| 424 } | 425 } |
| 425 file_util::Delete(origin_dir, true); | 426 file_util::Delete(origin_dir, true); |
| 426 file_util::Delete(new_origin_dir, true); // might fail on windows. | 427 file_util::Delete(new_origin_dir, true); // might fail on windows. |
| 427 | 428 |
| 428 databases_table_->DeleteOrigin(origin_identifier); | 429 databases_table_->DeleteOrigin(origin_identifier); |
| 429 | 430 |
| 430 if (quota_manager_proxy_ && deleted_size) { | 431 if (quota_manager_proxy_.get() && deleted_size) { |
| 431 quota_manager_proxy_->NotifyStorageModified( | 432 quota_manager_proxy_->NotifyStorageModified( |
| 432 quota::QuotaClient::kDatabase, | 433 quota::QuotaClient::kDatabase, |
| 433 webkit_base::GetOriginURLFromIdentifier(origin_identifier), | 434 webkit_base::GetOriginURLFromIdentifier(origin_identifier), |
| 434 quota::kStorageTypeTemporary, | 435 quota::kStorageTypeTemporary, |
| 435 -deleted_size); | 436 -deleted_size); |
| 436 } | 437 } |
| 437 | 438 |
| 438 return true; | 439 return true; |
| 439 } | 440 } |
| 440 | 441 |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 DCHECK(database_connections_.IsDatabaseOpened(origin_id, name)); | 608 DCHECK(database_connections_.IsDatabaseOpened(origin_id, name)); |
| 608 int64 new_size = GetDBFileSize(origin_id, name); | 609 int64 new_size = GetDBFileSize(origin_id, name); |
| 609 int64 old_size = database_connections_.GetOpenDatabaseSize(origin_id, name); | 610 int64 old_size = database_connections_.GetOpenDatabaseSize(origin_id, name); |
| 610 CachedOriginInfo* info = MaybeGetCachedOriginInfo(origin_id, false); | 611 CachedOriginInfo* info = MaybeGetCachedOriginInfo(origin_id, false); |
| 611 if (info && opt_description) | 612 if (info && opt_description) |
| 612 info->SetDatabaseDescription(name, *opt_description); | 613 info->SetDatabaseDescription(name, *opt_description); |
| 613 if (old_size != new_size) { | 614 if (old_size != new_size) { |
| 614 database_connections_.SetOpenDatabaseSize(origin_id, name, new_size); | 615 database_connections_.SetOpenDatabaseSize(origin_id, name, new_size); |
| 615 if (info) | 616 if (info) |
| 616 info->SetDatabaseSize(name, new_size); | 617 info->SetDatabaseSize(name, new_size); |
| 617 if (quota_manager_proxy_) | 618 if (quota_manager_proxy_.get()) |
| 618 quota_manager_proxy_->NotifyStorageModified( | 619 quota_manager_proxy_->NotifyStorageModified( |
| 619 quota::QuotaClient::kDatabase, | 620 quota::QuotaClient::kDatabase, |
| 620 webkit_base::GetOriginURLFromIdentifier(origin_id), | 621 webkit_base::GetOriginURLFromIdentifier(origin_id), |
| 621 quota::kStorageTypeTemporary, | 622 quota::kStorageTypeTemporary, |
| 622 new_size - old_size); | 623 new_size - old_size); |
| 623 FOR_EACH_OBSERVER(Observer, observers_, OnDatabaseSizeChanged( | 624 FOR_EACH_OBSERVER(Observer, observers_, OnDatabaseSizeChanged( |
| 624 origin_id, name, new_size)); | 625 origin_id, name, new_size)); |
| 625 } | 626 } |
| 626 return new_size; | 627 return new_size; |
| 627 } | 628 } |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 869 if (!db_tracker_thread_->BelongsToCurrentThread()) { | 870 if (!db_tracker_thread_->BelongsToCurrentThread()) { |
| 870 db_tracker_thread_->PostTask( | 871 db_tracker_thread_->PostTask( |
| 871 FROM_HERE, | 872 FROM_HERE, |
| 872 base::Bind(&DatabaseTracker::SetForceKeepSessionState, this)); | 873 base::Bind(&DatabaseTracker::SetForceKeepSessionState, this)); |
| 873 return; | 874 return; |
| 874 } | 875 } |
| 875 force_keep_session_state_ = true; | 876 force_keep_session_state_ = true; |
| 876 } | 877 } |
| 877 | 878 |
| 878 } // namespace webkit_database | 879 } // namespace webkit_database |
| OLD | NEW |