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" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/files/file_enumerator.h" | 13 #include "base/files/file_enumerator.h" |
14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
15 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
19 #include "sql/connection.h" | 19 #include "sql/connection.h" |
20 #include "sql/meta_table.h" | 20 #include "sql/meta_table.h" |
21 #include "sql/transaction.h" | 21 #include "sql/transaction.h" |
22 #include "third_party/sqlite/sqlite3.h" | 22 #include "third_party/sqlite/sqlite3.h" |
23 #include "webkit/base/origin_url_conversions.h" | |
24 #include "webkit/browser/database/database_quota_client.h" | 23 #include "webkit/browser/database/database_quota_client.h" |
25 #include "webkit/browser/database/database_util.h" | 24 #include "webkit/browser/database/database_util.h" |
26 #include "webkit/browser/database/databases_table.h" | 25 #include "webkit/browser/database/databases_table.h" |
27 #include "webkit/browser/quota/quota_manager.h" | 26 #include "webkit/browser/quota/quota_manager.h" |
28 #include "webkit/browser/quota/special_storage_policy.h" | 27 #include "webkit/browser/quota/special_storage_policy.h" |
| 28 #include "webkit/common/database/database_identifier.h" |
29 | 29 |
30 namespace webkit_database { | 30 namespace webkit_database { |
31 | 31 |
32 const base::FilePath::CharType kDatabaseDirectoryName[] = | 32 const base::FilePath::CharType kDatabaseDirectoryName[] = |
33 FILE_PATH_LITERAL("databases"); | 33 FILE_PATH_LITERAL("databases"); |
34 const base::FilePath::CharType kIncognitoDatabaseDirectoryName[] = | 34 const base::FilePath::CharType kIncognitoDatabaseDirectoryName[] = |
35 FILE_PATH_LITERAL("databases-incognito"); | 35 FILE_PATH_LITERAL("databases-incognito"); |
36 const base::FilePath::CharType kTrackerDatabaseFileName[] = | 36 const base::FilePath::CharType kTrackerDatabaseFileName[] = |
37 FILE_PATH_LITERAL("Databases.db"); | 37 FILE_PATH_LITERAL("Databases.db"); |
38 static const int kCurrentVersion = 2; | 38 static const int kCurrentVersion = 2; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 int64 estimated_size, | 115 int64 estimated_size, |
116 int64* database_size) { | 116 int64* database_size) { |
117 if (shutting_down_ || !LazyInit()) { | 117 if (shutting_down_ || !LazyInit()) { |
118 *database_size = 0; | 118 *database_size = 0; |
119 return; | 119 return; |
120 } | 120 } |
121 | 121 |
122 if (quota_manager_proxy_.get()) | 122 if (quota_manager_proxy_.get()) |
123 quota_manager_proxy_->NotifyStorageAccessed( | 123 quota_manager_proxy_->NotifyStorageAccessed( |
124 quota::QuotaClient::kDatabase, | 124 quota::QuotaClient::kDatabase, |
125 webkit_base::GetOriginURLFromIdentifier(origin_identifier), | 125 webkit_database::GetOriginFromIdentifier(origin_identifier), |
126 quota::kStorageTypeTemporary); | 126 quota::kStorageTypeTemporary); |
127 | 127 |
128 InsertOrUpdateDatabaseDetails(origin_identifier, database_name, | 128 InsertOrUpdateDatabaseDetails(origin_identifier, database_name, |
129 database_description, estimated_size); | 129 database_description, estimated_size); |
130 if (database_connections_.AddConnection(origin_identifier, database_name)) { | 130 if (database_connections_.AddConnection(origin_identifier, database_name)) { |
131 *database_size = SeedOpenDatabaseInfo(origin_identifier, | 131 *database_size = SeedOpenDatabaseInfo(origin_identifier, |
132 database_name, | 132 database_name, |
133 database_description); | 133 database_description); |
134 return; | 134 return; |
135 } | 135 } |
(...skipping 14 matching lines...) Expand all Loading... |
150 if (database_connections_.IsEmpty()) { | 150 if (database_connections_.IsEmpty()) { |
151 DCHECK(!is_initialized_); | 151 DCHECK(!is_initialized_); |
152 return; | 152 return; |
153 } | 153 } |
154 | 154 |
155 // We call NotifiyStorageAccessed when a db is opened and also when | 155 // We call NotifiyStorageAccessed when a db is opened and also when |
156 // closed because we don't call it for read while open. | 156 // closed because we don't call it for read while open. |
157 if (quota_manager_proxy_.get()) | 157 if (quota_manager_proxy_.get()) |
158 quota_manager_proxy_->NotifyStorageAccessed( | 158 quota_manager_proxy_->NotifyStorageAccessed( |
159 quota::QuotaClient::kDatabase, | 159 quota::QuotaClient::kDatabase, |
160 webkit_base::GetOriginURLFromIdentifier(origin_identifier), | 160 webkit_database::GetOriginFromIdentifier(origin_identifier), |
161 quota::kStorageTypeTemporary); | 161 quota::kStorageTypeTemporary); |
162 | 162 |
163 UpdateOpenDatabaseSizeAndNotify(origin_identifier, database_name); | 163 UpdateOpenDatabaseSizeAndNotify(origin_identifier, database_name); |
164 if (database_connections_.RemoveConnection(origin_identifier, database_name)) | 164 if (database_connections_.RemoveConnection(origin_identifier, database_name)) |
165 DeleteDatabaseIfNeeded(origin_identifier, database_name); | 165 DeleteDatabaseIfNeeded(origin_identifier, database_name); |
166 } | 166 } |
167 | 167 |
168 void DatabaseTracker::HandleSqliteError( | 168 void DatabaseTracker::HandleSqliteError( |
169 const std::string& origin_identifier, | 169 const std::string& origin_identifier, |
170 const base::string16& database_name, | 170 const base::string16& database_name, |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 return false; | 362 return false; |
363 | 363 |
364 // Also delete any orphaned journal file. | 364 // Also delete any orphaned journal file. |
365 DCHECK(db_file.Extension().empty()); | 365 DCHECK(db_file.Extension().empty()); |
366 file_util::Delete(db_file.InsertBeforeExtensionASCII( | 366 file_util::Delete(db_file.InsertBeforeExtensionASCII( |
367 DatabaseUtil::kJournalFileSuffix), false); | 367 DatabaseUtil::kJournalFileSuffix), false); |
368 | 368 |
369 if (quota_manager_proxy_.get() && db_file_size) | 369 if (quota_manager_proxy_.get() && db_file_size) |
370 quota_manager_proxy_->NotifyStorageModified( | 370 quota_manager_proxy_->NotifyStorageModified( |
371 quota::QuotaClient::kDatabase, | 371 quota::QuotaClient::kDatabase, |
372 webkit_base::GetOriginURLFromIdentifier(origin_identifier), | 372 webkit_database::GetOriginFromIdentifier(origin_identifier), |
373 quota::kStorageTypeTemporary, | 373 quota::kStorageTypeTemporary, |
374 -db_file_size); | 374 -db_file_size); |
375 | 375 |
376 // Clean up the main database and invalidate the cached record. | 376 // Clean up the main database and invalidate the cached record. |
377 databases_table_->DeleteDatabaseDetails(origin_identifier, database_name); | 377 databases_table_->DeleteDatabaseDetails(origin_identifier, database_name); |
378 origins_info_map_.erase(origin_identifier); | 378 origins_info_map_.erase(origin_identifier); |
379 | 379 |
380 std::vector<DatabaseDetails> details; | 380 std::vector<DatabaseDetails> details; |
381 if (databases_table_->GetAllDatabaseDetailsForOriginIdentifier( | 381 if (databases_table_->GetAllDatabaseDetailsForOriginIdentifier( |
382 origin_identifier, &details) && details.empty()) { | 382 origin_identifier, &details) && details.empty()) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 file_util::Move(database, new_file); | 422 file_util::Move(database, new_file); |
423 } | 423 } |
424 file_util::Delete(origin_dir, true); | 424 file_util::Delete(origin_dir, true); |
425 file_util::Delete(new_origin_dir, true); // might fail on windows. | 425 file_util::Delete(new_origin_dir, true); // might fail on windows. |
426 | 426 |
427 databases_table_->DeleteOriginIdentifier(origin_identifier); | 427 databases_table_->DeleteOriginIdentifier(origin_identifier); |
428 | 428 |
429 if (quota_manager_proxy_.get() && deleted_size) { | 429 if (quota_manager_proxy_.get() && deleted_size) { |
430 quota_manager_proxy_->NotifyStorageModified( | 430 quota_manager_proxy_->NotifyStorageModified( |
431 quota::QuotaClient::kDatabase, | 431 quota::QuotaClient::kDatabase, |
432 webkit_base::GetOriginURLFromIdentifier(origin_identifier), | 432 webkit_database::GetOriginFromIdentifier(origin_identifier), |
433 quota::kStorageTypeTemporary, | 433 quota::kStorageTypeTemporary, |
434 -deleted_size); | 434 -deleted_size); |
435 } | 435 } |
436 | 436 |
437 return true; | 437 return true; |
438 } | 438 } |
439 | 439 |
440 bool DatabaseTracker::IsDatabaseScheduledForDeletion( | 440 bool DatabaseTracker::IsDatabaseScheduledForDeletion( |
441 const std::string& origin_identifier, | 441 const std::string& origin_identifier, |
442 const base::string16& database_name) { | 442 const base::string16& database_name) { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 CachedOriginInfo* info = MaybeGetCachedOriginInfo(origin_id, false); | 609 CachedOriginInfo* info = MaybeGetCachedOriginInfo(origin_id, false); |
610 if (info && opt_description) | 610 if (info && opt_description) |
611 info->SetDatabaseDescription(name, *opt_description); | 611 info->SetDatabaseDescription(name, *opt_description); |
612 if (old_size != new_size) { | 612 if (old_size != new_size) { |
613 database_connections_.SetOpenDatabaseSize(origin_id, name, new_size); | 613 database_connections_.SetOpenDatabaseSize(origin_id, name, new_size); |
614 if (info) | 614 if (info) |
615 info->SetDatabaseSize(name, new_size); | 615 info->SetDatabaseSize(name, new_size); |
616 if (quota_manager_proxy_.get()) | 616 if (quota_manager_proxy_.get()) |
617 quota_manager_proxy_->NotifyStorageModified( | 617 quota_manager_proxy_->NotifyStorageModified( |
618 quota::QuotaClient::kDatabase, | 618 quota::QuotaClient::kDatabase, |
619 webkit_base::GetOriginURLFromIdentifier(origin_id), | 619 webkit_database::GetOriginFromIdentifier(origin_id), |
620 quota::kStorageTypeTemporary, | 620 quota::kStorageTypeTemporary, |
621 new_size - old_size); | 621 new_size - old_size); |
622 FOR_EACH_OBSERVER(Observer, observers_, OnDatabaseSizeChanged( | 622 FOR_EACH_OBSERVER(Observer, observers_, OnDatabaseSizeChanged( |
623 origin_id, name, new_size)); | 623 origin_id, name, new_size)); |
624 } | 624 } |
625 return new_size; | 625 return new_size; |
626 } | 626 } |
627 | 627 |
628 void DatabaseTracker::ScheduleDatabaseForDeletion( | 628 void DatabaseTracker::ScheduleDatabaseForDeletion( |
629 const std::string& origin_identifier, | 629 const std::string& origin_identifier, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 | 680 |
681 std::vector<std::string> origins_identifiers; | 681 std::vector<std::string> origins_identifiers; |
682 if (!databases_table_->GetAllOriginIdentifiers(&origins_identifiers)) | 682 if (!databases_table_->GetAllOriginIdentifiers(&origins_identifiers)) |
683 return net::ERR_FAILED; | 683 return net::ERR_FAILED; |
684 int rv = net::OK; | 684 int rv = net::OK; |
685 for (std::vector<std::string>::const_iterator ori = | 685 for (std::vector<std::string>::const_iterator ori = |
686 origins_identifiers.begin(); | 686 origins_identifiers.begin(); |
687 ori != origins_identifiers.end(); ++ori) { | 687 ori != origins_identifiers.end(); ++ori) { |
688 if (special_storage_policy_.get() && | 688 if (special_storage_policy_.get() && |
689 special_storage_policy_->IsStorageProtected( | 689 special_storage_policy_->IsStorageProtected( |
690 webkit_base::GetOriginURLFromIdentifier(*ori))) { | 690 webkit_database::GetOriginFromIdentifier(*ori))) { |
691 continue; | 691 continue; |
692 } | 692 } |
693 | 693 |
694 std::vector<DatabaseDetails> details; | 694 std::vector<DatabaseDetails> details; |
695 if (!databases_table_-> | 695 if (!databases_table_-> |
696 GetAllDatabaseDetailsForOriginIdentifier(*ori, &details)) | 696 GetAllDatabaseDetailsForOriginIdentifier(*ori, &details)) |
697 rv = net::ERR_FAILED; | 697 rv = net::ERR_FAILED; |
698 for (std::vector<DatabaseDetails>::const_iterator db = details.begin(); | 698 for (std::vector<DatabaseDetails>::const_iterator db = details.begin(); |
699 db != details.end(); ++db) { | 699 db != details.end(); ++db) { |
700 base::FilePath db_file = GetFullDBFilePath(*ori, db->database_name); | 700 base::FilePath db_file = GetFullDBFilePath(*ori, db->database_name); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 | 819 |
820 if (!LazyInit()) | 820 if (!LazyInit()) |
821 return; | 821 return; |
822 | 822 |
823 std::vector<std::string> origin_identifiers; | 823 std::vector<std::string> origin_identifiers; |
824 GetAllOriginIdentifiers(&origin_identifiers); | 824 GetAllOriginIdentifiers(&origin_identifiers); |
825 | 825 |
826 for (std::vector<std::string>::iterator origin = | 826 for (std::vector<std::string>::iterator origin = |
827 origin_identifiers.begin(); | 827 origin_identifiers.begin(); |
828 origin != origin_identifiers.end(); ++origin) { | 828 origin != origin_identifiers.end(); ++origin) { |
829 GURL origin_url = webkit_base::GetOriginURLFromIdentifier(*origin); | 829 GURL origin_url = webkit_database::GetOriginFromIdentifier(*origin); |
830 if (!special_storage_policy_->IsStorageSessionOnly(origin_url)) | 830 if (!special_storage_policy_->IsStorageSessionOnly(origin_url)) |
831 continue; | 831 continue; |
832 if (special_storage_policy_->IsStorageProtected(origin_url)) | 832 if (special_storage_policy_->IsStorageProtected(origin_url)) |
833 continue; | 833 continue; |
834 webkit_database::OriginInfo origin_info; | 834 webkit_database::OriginInfo origin_info; |
835 std::vector<base::string16> databases; | 835 std::vector<base::string16> databases; |
836 GetOriginInfo(*origin, &origin_info); | 836 GetOriginInfo(*origin, &origin_info); |
837 origin_info.GetAllDatabaseNames(&databases); | 837 origin_info.GetAllDatabaseNames(&databases); |
838 | 838 |
839 for (std::vector<base::string16>::iterator database = databases.begin(); | 839 for (std::vector<base::string16>::iterator database = databases.begin(); |
(...skipping 30 matching lines...) Expand all Loading... |
870 if (!db_tracker_thread_->BelongsToCurrentThread()) { | 870 if (!db_tracker_thread_->BelongsToCurrentThread()) { |
871 db_tracker_thread_->PostTask( | 871 db_tracker_thread_->PostTask( |
872 FROM_HERE, | 872 FROM_HERE, |
873 base::Bind(&DatabaseTracker::SetForceKeepSessionState, this)); | 873 base::Bind(&DatabaseTracker::SetForceKeepSessionState, this)); |
874 return; | 874 return; |
875 } | 875 } |
876 force_keep_session_state_ = true; | 876 force_keep_session_state_ = true; |
877 } | 877 } |
878 | 878 |
879 } // namespace webkit_database | 879 } // namespace webkit_database |
OLD | NEW |