| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/database/database_tracker.h" | 5 #include "webkit/database/database_tracker.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "app/sql/connection.h" | 9 #include "app/sql/connection.h" |
| 10 #include "app/sql/meta_table.h" | 10 #include "app/sql/meta_table.h" |
| 11 #include "app/sql/statement.h" | 11 #include "app/sql/statement.h" |
| 12 #include "app/sql/transaction.h" | 12 #include "app/sql/transaction.h" |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
| 15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "webkit/database/databases_table.h" | 18 #include "webkit/database/databases_table.h" |
| 19 #include "webkit/database/quota_table.h" | 19 #include "webkit/database/quota_table.h" |
| 20 #include "webkit/glue/webkit_glue.h" | |
| 21 | 20 |
| 22 namespace webkit_database { | 21 namespace webkit_database { |
| 23 | 22 |
| 24 const FilePath::CharType kDatabaseDirectoryName[] = | 23 const FilePath::CharType kDatabaseDirectoryName[] = |
| 25 FILE_PATH_LITERAL("databases"); | 24 FILE_PATH_LITERAL("databases"); |
| 26 const FilePath::CharType kTrackerDatabaseFileName[] = | 25 const FilePath::CharType kTrackerDatabaseFileName[] = |
| 27 FILE_PATH_LITERAL("Databases.db"); | 26 FILE_PATH_LITERAL("Databases.db"); |
| 28 const int kCurrentVersion = 2; | 27 const int kCurrentVersion = 2; |
| 29 const int kCompatibleVersion = 1; | 28 const int kCompatibleVersion = 1; |
| 30 const int64 kDefaultExtensionQuota = 1024 * 1024 * 1024; | |
| 31 const char* kExtensionOriginIdentifierPrefix = "chrome-extension_"; | 29 const char* kExtensionOriginIdentifierPrefix = "chrome-extension_"; |
| 32 | 30 |
| 33 DatabaseTracker::DatabaseTracker(const FilePath& profile_path) | 31 DatabaseTracker::DatabaseTracker(const FilePath& profile_path) |
| 34 : initialized_(false), | 32 : initialized_(false), |
| 35 db_dir_(profile_path.Append(FilePath(kDatabaseDirectoryName))), | 33 db_dir_(profile_path.Append(FilePath(kDatabaseDirectoryName))), |
| 36 db_(new sql::Connection()), | 34 db_(new sql::Connection()), |
| 37 databases_table_(NULL), | 35 databases_table_(NULL), |
| 38 meta_table_(NULL), | 36 meta_table_(NULL), |
| 39 default_quota_(5 * 1024 * 1024) { | 37 default_quota_(5 * 1024 * 1024) { |
| 40 } | 38 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 quota_table_.reset(NULL); | 149 quota_table_.reset(NULL); |
| 152 db_->Close(); | 150 db_->Close(); |
| 153 initialized_ = false; | 151 initialized_ = false; |
| 154 } | 152 } |
| 155 | 153 |
| 156 FilePath DatabaseTracker::GetFullDBFilePath( | 154 FilePath DatabaseTracker::GetFullDBFilePath( |
| 157 const string16& origin_identifier, | 155 const string16& origin_identifier, |
| 158 const string16& database_name) const { | 156 const string16& database_name) const { |
| 159 DCHECK(!origin_identifier.empty()); | 157 DCHECK(!origin_identifier.empty()); |
| 160 DCHECK(!database_name.empty()); | 158 DCHECK(!database_name.empty()); |
| 159 |
| 161 int64 id = databases_table_->GetDatabaseID( | 160 int64 id = databases_table_->GetDatabaseID( |
| 162 origin_identifier, database_name); | 161 origin_identifier, database_name); |
| 163 if (id < 0) | 162 if (id < 0) |
| 164 return FilePath(); | 163 return FilePath(); |
| 165 | 164 |
| 166 FilePath file_name = FilePath::FromWStringHack(Int64ToWString(id)); | 165 FilePath file_name = FilePath::FromWStringHack(Int64ToWString(id)); |
| 167 return db_dir_.Append(FilePath::FromWStringHack( | 166 return db_dir_.Append(FilePath::FromWStringHack( |
| 168 UTF16ToWide(origin_identifier))).Append(file_name); | 167 UTF16ToWide(origin_identifier))).Append(file_name); |
| 169 } | 168 } |
| 170 | 169 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 187 return false; | 186 return false; |
| 188 } | 187 } |
| 189 origins_info->push_back(OriginInfo(*origin_info)); | 188 origins_info->push_back(OriginInfo(*origin_info)); |
| 190 } | 189 } |
| 191 | 190 |
| 192 return true; | 191 return true; |
| 193 } | 192 } |
| 194 | 193 |
| 195 void DatabaseTracker::SetOriginQuota(const string16& origin_identifier, | 194 void DatabaseTracker::SetOriginQuota(const string16& origin_identifier, |
| 196 int64 new_quota) { | 195 int64 new_quota) { |
| 196 if (!LazyInit()) |
| 197 return; |
| 198 |
| 197 if (quota_table_->SetOriginQuota(origin_identifier, new_quota) && | 199 if (quota_table_->SetOriginQuota(origin_identifier, new_quota) && |
| 198 (origins_info_map_.find(origin_identifier) != origins_info_map_.end())) { | 200 (origins_info_map_.find(origin_identifier) != origins_info_map_.end())) { |
| 199 origins_info_map_[origin_identifier].SetQuota(new_quota); | 201 origins_info_map_[origin_identifier].SetQuota(new_quota); |
| 200 } | 202 } |
| 201 } | 203 } |
| 202 | 204 |
| 205 void DatabaseTracker::SetOriginQuotaInMemory(const string16& origin_identifier, |
| 206 int64 new_quota) { |
| 207 DCHECK(new_quota >= 0); |
| 208 in_memory_quotas_[origin_identifier] = new_quota; |
| 209 } |
| 210 |
| 203 bool DatabaseTracker::DeleteClosedDatabase(const string16& origin_identifier, | 211 bool DatabaseTracker::DeleteClosedDatabase(const string16& origin_identifier, |
| 204 const string16& database_name) { | 212 const string16& database_name) { |
| 213 if (!LazyInit()) |
| 214 return false; |
| 215 |
| 205 // Check if the database is opened by any renderer. | 216 // Check if the database is opened by any renderer. |
| 206 if (database_connections_.IsDatabaseOpened(origin_identifier, database_name)) | 217 if (database_connections_.IsDatabaseOpened(origin_identifier, database_name)) |
| 207 return false; | 218 return false; |
| 208 | 219 |
| 209 // Try to delete the file on the hard drive. | 220 // Try to delete the file on the hard drive. |
| 210 // TODO(jochen): Delete journal files associated with this database. | 221 // TODO(jochen): Delete journal files associated with this database. |
| 211 FilePath db_file = GetFullDBFilePath(origin_identifier, database_name); | 222 FilePath db_file = GetFullDBFilePath(origin_identifier, database_name); |
| 212 if (file_util::PathExists(db_file) && !file_util::Delete(db_file, false)) | 223 if (file_util::PathExists(db_file) && !file_util::Delete(db_file, false)) |
| 213 return false; | 224 return false; |
| 214 | 225 |
| 215 // Clean up the main database and invalidate the cached record. | 226 // Clean up the main database and invalidate the cached record. |
| 216 databases_table_->DeleteDatabaseDetails(origin_identifier, database_name); | 227 databases_table_->DeleteDatabaseDetails(origin_identifier, database_name); |
| 217 origins_info_map_.erase(origin_identifier); | 228 origins_info_map_.erase(origin_identifier); |
| 218 | 229 |
| 219 // Try to delete the origin in case this was the last database. | 230 // Try to delete the origin in case this was the last database. |
| 220 std::vector<DatabaseDetails> details; | 231 std::vector<DatabaseDetails> details; |
| 221 if (databases_table_->GetAllDatabaseDetailsForOrigin( | 232 if (databases_table_->GetAllDatabaseDetailsForOrigin( |
| 222 origin_identifier, &details) && details.empty()) | 233 origin_identifier, &details) && details.empty()) |
| 223 DeleteOrigin(origin_identifier); | 234 DeleteOrigin(origin_identifier); |
| 224 return true; | 235 return true; |
| 225 } | 236 } |
| 226 | 237 |
| 227 bool DatabaseTracker::DeleteOrigin(const string16& origin_identifier) { | 238 bool DatabaseTracker::DeleteOrigin(const string16& origin_identifier) { |
| 239 if (!LazyInit()) |
| 240 return false; |
| 241 |
| 228 // Check if any database in this origin is opened by any renderer. | 242 // Check if any database in this origin is opened by any renderer. |
| 229 if (database_connections_.IsOriginUsed(origin_identifier)) | 243 if (database_connections_.IsOriginUsed(origin_identifier)) |
| 230 return false; | 244 return false; |
| 231 | 245 |
| 232 // We need to invalidate the cached record whether file_util::Delete() | 246 // We need to invalidate the cached record whether file_util::Delete() |
| 233 // succeeds or not, because even if it fails, it might still delete some | 247 // succeeds or not, because even if it fails, it might still delete some |
| 234 // DB files on the hard drive. | 248 // DB files on the hard drive. |
| 235 origins_info_map_.erase(origin_identifier); | 249 origins_info_map_.erase(origin_identifier); |
| 236 FilePath origin_dir = db_dir_.Append(FilePath::FromWStringHack( | 250 FilePath origin_dir = db_dir_.Append(FilePath::FromWStringHack( |
| 237 UTF16ToWide(origin_identifier))); | 251 UTF16ToWide(origin_identifier))); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 CachedOriginInfo& origin_info = origins_info_map_[origin_identifier]; | 361 CachedOriginInfo& origin_info = origins_info_map_[origin_identifier]; |
| 348 origin_info.SetOrigin(origin_identifier); | 362 origin_info.SetOrigin(origin_identifier); |
| 349 for (std::vector<DatabaseDetails>::const_iterator it = details.begin(); | 363 for (std::vector<DatabaseDetails>::const_iterator it = details.begin(); |
| 350 it != details.end(); it++) { | 364 it != details.end(); it++) { |
| 351 int64 db_file_size = | 365 int64 db_file_size = |
| 352 GetDBFileSize(origin_identifier, it->database_name); | 366 GetDBFileSize(origin_identifier, it->database_name); |
| 353 origin_info.SetDatabaseSize(it->database_name, db_file_size); | 367 origin_info.SetDatabaseSize(it->database_name, db_file_size); |
| 354 origin_info.SetDatabaseDescription(it->database_name, it->description); | 368 origin_info.SetDatabaseDescription(it->database_name, it->description); |
| 355 } | 369 } |
| 356 | 370 |
| 357 int64 origin_quota = quota_table_->GetOriginQuota(origin_identifier); | 371 if (in_memory_quotas_.find(origin_identifier) != in_memory_quotas_.end()) { |
| 358 if (origin_quota > 0) { | 372 origin_info.SetQuota(in_memory_quotas_[origin_identifier]); |
| 359 origin_info.SetQuota(origin_quota); | |
| 360 } else if (StartsWith(origin_identifier, | |
| 361 ASCIIToUTF16(kExtensionOriginIdentifierPrefix), | |
| 362 true)) { | |
| 363 origin_info.SetQuota(kDefaultExtensionQuota); | |
| 364 } else { | 373 } else { |
| 365 origin_info.SetQuota(default_quota_); | 374 int64 origin_quota = quota_table_->GetOriginQuota(origin_identifier); |
| 375 if (origin_quota > 0) |
| 376 origin_info.SetQuota(origin_quota); |
| 377 else |
| 378 origin_info.SetQuota(default_quota_); |
| 366 } | 379 } |
| 367 } | 380 } |
| 368 | 381 |
| 369 return &origins_info_map_[origin_identifier]; | 382 return &origins_info_map_[origin_identifier]; |
| 370 } | 383 } |
| 371 | 384 |
| 372 int64 DatabaseTracker::GetDBFileSize(const string16& origin_identifier, | 385 int64 DatabaseTracker::GetDBFileSize(const string16& origin_identifier, |
| 373 const string16& database_name) const { | 386 const string16& database_name) const { |
| 374 FilePath db_file_name = GetFullDBFilePath(origin_identifier, database_name); | 387 FilePath db_file_name = GetFullDBFilePath(origin_identifier, database_name); |
| 375 int64 db_file_size = 0; | 388 int64 db_file_size = 0; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 file_path = file_enumerator.Next()) { | 522 file_path = file_enumerator.Next()) { |
| 510 if (file_path.BaseName() != FilePath(kTrackerDatabaseFileName)) { | 523 if (file_path.BaseName() != FilePath(kTrackerDatabaseFileName)) { |
| 511 if (!StartsWith(file_path.BaseName().ToWStringHack(), | 524 if (!StartsWith(file_path.BaseName().ToWStringHack(), |
| 512 ASCIIToWide(kExtensionOriginIdentifierPrefix), true)) | 525 ASCIIToWide(kExtensionOriginIdentifierPrefix), true)) |
| 513 file_util::Delete(file_path, true); | 526 file_util::Delete(file_path, true); |
| 514 } | 527 } |
| 515 } | 528 } |
| 516 } | 529 } |
| 517 | 530 |
| 518 } // namespace webkit_database | 531 } // namespace webkit_database |
| OLD | NEW |