| 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/appcache/appcache_database.h" | 5 #include "webkit/appcache/appcache_database.h" |
| 6 | 6 |
| 7 #include "app/sql/connection.h" | 7 #include "app/sql/connection.h" |
| 8 #include "app/sql/diagnostic_error_delegate.h" | 8 #include "app/sql/diagnostic_error_delegate.h" |
| 9 #include "app/sql/meta_table.h" | 9 #include "app/sql/meta_table.h" |
| 10 #include "app/sql/statement.h" | 10 #include "app/sql/statement.h" |
| 11 #include "app/sql/transaction.h" | 11 #include "app/sql/transaction.h" |
| 12 #include "base/auto_reset.h" | 12 #include "base/auto_reset.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "webkit/appcache/appcache_entry.h" | 16 #include "webkit/appcache/appcache_entry.h" |
| 17 #include "webkit/appcache/appcache_histograms.h" | 17 #include "webkit/appcache/appcache_histograms.h" |
| 18 #include "webkit/database/quota_table.h" | |
| 19 | 18 |
| 20 // Schema ------------------------------------------------------------------- | 19 // Schema ------------------------------------------------------------------- |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 const int kCurrentVersion = 3; | 22 const int kCurrentVersion = 3; |
| 24 const int kCompatibleVersion = 3; | 23 const int kCompatibleVersion = 3; |
| 25 | 24 |
| 26 const char* kGroupsTable = "Groups"; | 25 const char* kGroupsTable = "Groups"; |
| 27 const char* kCachesTable = "Caches"; | 26 const char* kCachesTable = "Caches"; |
| 28 const char* kEntriesTable = "Entries"; | 27 const char* kEntriesTable = "Entries"; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 188 |
| 190 int64 origin_usage = 0; | 189 int64 origin_usage = 0; |
| 191 std::vector<CacheRecord>::const_iterator iter = records.begin(); | 190 std::vector<CacheRecord>::const_iterator iter = records.begin(); |
| 192 while (iter != records.end()) { | 191 while (iter != records.end()) { |
| 193 origin_usage += iter->cache_size; | 192 origin_usage += iter->cache_size; |
| 194 ++iter; | 193 ++iter; |
| 195 } | 194 } |
| 196 return origin_usage; | 195 return origin_usage; |
| 197 } | 196 } |
| 198 | 197 |
| 199 int64 AppCacheDatabase::GetOriginQuota(const GURL& origin) { | 198 bool AppCacheDatabase::GetAllOriginUsage(std::map<GURL, int64>* usage_map) { |
| 200 if (!LazyOpen(false)) | 199 std::set<GURL> origins; |
| 201 return GetDefaultOriginQuota(); | 200 if (!FindOriginsWithGroups(&origins)) |
| 202 int64 quota = quota_table_->GetOriginQuota( | 201 return false; |
| 203 UTF8ToUTF16(origin.spec().c_str())); | 202 for (std::set<GURL>::const_iterator origin = origins.begin(); |
| 204 return (quota >= 0) ? quota : GetDefaultOriginQuota(); | 203 origin != origins.end(); ++origin) { |
| 204 (*usage_map)[*origin] = GetOriginUsage(*origin); |
| 205 } |
| 206 return true; |
| 205 } | 207 } |
| 206 | 208 |
| 207 bool AppCacheDatabase::FindOriginsWithGroups(std::set<GURL>* origins) { | 209 bool AppCacheDatabase::FindOriginsWithGroups(std::set<GURL>* origins) { |
| 208 DCHECK(origins && origins->empty()); | 210 DCHECK(origins && origins->empty()); |
| 209 if (!LazyOpen(false)) | 211 if (!LazyOpen(false)) |
| 210 return false; | 212 return false; |
| 211 | 213 |
| 212 const char* kSql = | 214 const char* kSql = |
| 213 "SELECT DISTINCT(origin) FROM Groups"; | 215 "SELECT DISTINCT(origin) FROM Groups"; |
| 214 | 216 |
| (...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 | 999 |
| 998 // Avoid creating a database at all if we can. | 1000 // Avoid creating a database at all if we can. |
| 999 bool use_in_memory_db = db_file_path_.empty(); | 1001 bool use_in_memory_db = db_file_path_.empty(); |
| 1000 if (!create_if_needed && | 1002 if (!create_if_needed && |
| 1001 (use_in_memory_db || !file_util::PathExists(db_file_path_))) { | 1003 (use_in_memory_db || !file_util::PathExists(db_file_path_))) { |
| 1002 return false; | 1004 return false; |
| 1003 } | 1005 } |
| 1004 | 1006 |
| 1005 db_.reset(new sql::Connection); | 1007 db_.reset(new sql::Connection); |
| 1006 meta_table_.reset(new sql::MetaTable); | 1008 meta_table_.reset(new sql::MetaTable); |
| 1007 quota_table_.reset(new webkit_database::QuotaTable(db_.get())); | |
| 1008 | 1009 |
| 1009 db_->set_error_delegate(GetErrorHandlerForAppCacheDb()); | 1010 db_->set_error_delegate(GetErrorHandlerForAppCacheDb()); |
| 1010 | 1011 |
| 1011 bool opened = false; | 1012 bool opened = false; |
| 1012 if (use_in_memory_db) { | 1013 if (use_in_memory_db) { |
| 1013 opened = db_->OpenInMemory(); | 1014 opened = db_->OpenInMemory(); |
| 1014 } else if (!file_util::CreateDirectory(db_file_path_.DirName())) { | 1015 } else if (!file_util::CreateDirectory(db_file_path_.DirName())) { |
| 1015 LOG(ERROR) << "Failed to create appcache directory."; | 1016 LOG(ERROR) << "Failed to create appcache directory."; |
| 1016 } else { | 1017 } else { |
| 1017 opened = db_->Open(db_file_path_); | 1018 opened = db_->Open(db_file_path_); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1062 #endif | 1063 #endif |
| 1063 | 1064 |
| 1064 return true; | 1065 return true; |
| 1065 } | 1066 } |
| 1066 | 1067 |
| 1067 bool AppCacheDatabase::CreateSchema() { | 1068 bool AppCacheDatabase::CreateSchema() { |
| 1068 sql::Transaction transaction(db_.get()); | 1069 sql::Transaction transaction(db_.get()); |
| 1069 if (!transaction.Begin()) | 1070 if (!transaction.Begin()) |
| 1070 return false; | 1071 return false; |
| 1071 | 1072 |
| 1072 if (!meta_table_->Init(db_.get(), kCurrentVersion, kCompatibleVersion) || | 1073 if (!meta_table_->Init(db_.get(), kCurrentVersion, kCompatibleVersion)) |
| 1073 !quota_table_->Init()) { | |
| 1074 return false; | 1074 return false; |
| 1075 } | |
| 1076 | 1075 |
| 1077 for (int i = 0; i < kTableCount; ++i) { | 1076 for (int i = 0; i < kTableCount; ++i) { |
| 1078 std::string sql("CREATE TABLE "); | 1077 std::string sql("CREATE TABLE "); |
| 1079 sql += kTables[i].table_name; | 1078 sql += kTables[i].table_name; |
| 1080 sql += kTables[i].columns; | 1079 sql += kTables[i].columns; |
| 1081 if (!db_->Execute(sql.c_str())) | 1080 if (!db_->Execute(sql.c_str())) |
| 1082 return false; | 1081 return false; |
| 1083 } | 1082 } |
| 1084 | 1083 |
| 1085 for (int i = 0; i < kIndexCount; ++i) { | 1084 for (int i = 0; i < kIndexCount; ++i) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1101 | 1100 |
| 1102 bool AppCacheDatabase::UpgradeSchema() { | 1101 bool AppCacheDatabase::UpgradeSchema() { |
| 1103 // Upgrade logic goes here | 1102 // Upgrade logic goes here |
| 1104 | 1103 |
| 1105 // If there is no upgrade path for the version on disk to the current | 1104 // If there is no upgrade path for the version on disk to the current |
| 1106 // version, nuke everything and start over. | 1105 // version, nuke everything and start over. |
| 1107 return DeleteExistingAndCreateNewDatabase(); | 1106 return DeleteExistingAndCreateNewDatabase(); |
| 1108 } | 1107 } |
| 1109 | 1108 |
| 1110 void AppCacheDatabase::ResetConnectionAndTables() { | 1109 void AppCacheDatabase::ResetConnectionAndTables() { |
| 1111 quota_table_.reset(); | |
| 1112 meta_table_.reset(); | 1110 meta_table_.reset(); |
| 1113 db_.reset(); | 1111 db_.reset(); |
| 1114 } | 1112 } |
| 1115 | 1113 |
| 1116 bool AppCacheDatabase::DeleteExistingAndCreateNewDatabase() { | 1114 bool AppCacheDatabase::DeleteExistingAndCreateNewDatabase() { |
| 1117 DCHECK(!db_file_path_.empty()); | 1115 DCHECK(!db_file_path_.empty()); |
| 1118 DCHECK(file_util::PathExists(db_file_path_)); | 1116 DCHECK(file_util::PathExists(db_file_path_)); |
| 1119 VLOG(1) << "Deleting existing appcache data and starting over."; | 1117 VLOG(1) << "Deleting existing appcache data and starting over."; |
| 1120 | 1118 |
| 1121 ResetConnectionAndTables(); | 1119 ResetConnectionAndTables(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1133 | 1131 |
| 1134 // So we can't go recursive. | 1132 // So we can't go recursive. |
| 1135 if (is_recreating_) | 1133 if (is_recreating_) |
| 1136 return false; | 1134 return false; |
| 1137 | 1135 |
| 1138 AutoReset<bool> auto_reset(&is_recreating_, true); | 1136 AutoReset<bool> auto_reset(&is_recreating_, true); |
| 1139 return LazyOpen(true); | 1137 return LazyOpen(true); |
| 1140 } | 1138 } |
| 1141 | 1139 |
| 1142 } // namespace appcache | 1140 } // namespace appcache |
| OLD | NEW |