| 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/appcache/appcache_database.h" | 5 #include "webkit/browser/appcache/appcache_database.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/rand_util.h" // just for local testing, not for commit |
| 11 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 12 #include "sql/connection.h" | 14 #include "sql/connection.h" |
| 15 #include "sql/error_delegate_util.h" |
| 13 #include "sql/meta_table.h" | 16 #include "sql/meta_table.h" |
| 14 #include "sql/statement.h" | 17 #include "sql/statement.h" |
| 15 #include "sql/transaction.h" | 18 #include "sql/transaction.h" |
| 16 #include "webkit/browser/appcache/appcache_entry.h" | 19 #include "webkit/browser/appcache/appcache_entry.h" |
| 17 #include "webkit/browser/appcache/appcache_histograms.h" | 20 #include "webkit/browser/appcache/appcache_histograms.h" |
| 18 | 21 |
| 19 namespace appcache { | 22 namespace appcache { |
| 20 | 23 |
| 21 // Schema ------------------------------------------------------------------- | 24 // Schema ------------------------------------------------------------------- |
| 22 namespace { | 25 namespace { |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 196 |
| 194 AppCacheDatabase::NamespaceRecord::NamespaceRecord() | 197 AppCacheDatabase::NamespaceRecord::NamespaceRecord() |
| 195 : cache_id(0) { | 198 : cache_id(0) { |
| 196 } | 199 } |
| 197 | 200 |
| 198 AppCacheDatabase::NamespaceRecord::~NamespaceRecord() { | 201 AppCacheDatabase::NamespaceRecord::~NamespaceRecord() { |
| 199 } | 202 } |
| 200 | 203 |
| 201 | 204 |
| 202 AppCacheDatabase::AppCacheDatabase(const base::FilePath& path) | 205 AppCacheDatabase::AppCacheDatabase(const base::FilePath& path) |
| 203 : db_file_path_(path), is_disabled_(false), is_recreating_(false) { | 206 : db_file_path_(path), is_disabled_(false), |
| 207 is_recreating_(false), was_corruption_detected_(false) { |
| 204 } | 208 } |
| 205 | 209 |
| 206 AppCacheDatabase::~AppCacheDatabase() { | 210 AppCacheDatabase::~AppCacheDatabase() { |
| 207 } | 211 } |
| 208 | 212 |
| 209 void AppCacheDatabase::CloseConnection() { | 213 void AppCacheDatabase::CloseConnection() { |
| 210 // We can't close the connection for an in-memory database w/o | 214 // We can't close the connection for an in-memory database w/o |
| 211 // losing all of the data, so we don't do that. | 215 // losing all of the data, so we don't do that. |
| 212 if (!db_file_path_.empty()) | 216 if (!db_file_path_.empty()) |
| 213 ResetConnectionAndTables(); | 217 ResetConnectionAndTables(); |
| (...skipping 815 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1029 // the existing appcache data and starting with a clean slate in | 1033 // the existing appcache data and starting with a clean slate in |
| 1030 // this browser session. | 1034 // this browser session. |
| 1031 if (!use_in_memory_db && DeleteExistingAndCreateNewDatabase()) | 1035 if (!use_in_memory_db && DeleteExistingAndCreateNewDatabase()) |
| 1032 return true; | 1036 return true; |
| 1033 | 1037 |
| 1034 Disable(); | 1038 Disable(); |
| 1035 return false; | 1039 return false; |
| 1036 } | 1040 } |
| 1037 | 1041 |
| 1038 AppCacheHistograms::CountInitResult(AppCacheHistograms::INIT_OK); | 1042 AppCacheHistograms::CountInitResult(AppCacheHistograms::INIT_OK); |
| 1043 was_corruption_detected_ = false; |
| 1044 db_->set_error_callback( |
| 1045 base::Bind(&AppCacheDatabase::OnDatabaseError, base::Unretained(this))); |
| 1039 return true; | 1046 return true; |
| 1040 } | 1047 } |
| 1041 | 1048 |
| 1042 bool AppCacheDatabase::EnsureDatabaseVersion() { | 1049 bool AppCacheDatabase::EnsureDatabaseVersion() { |
| 1043 if (!sql::MetaTable::DoesTableExist(db_.get())) | 1050 if (!sql::MetaTable::DoesTableExist(db_.get())) |
| 1044 return CreateSchema(); | 1051 return CreateSchema(); |
| 1045 | 1052 |
| 1046 if (!meta_table_->Init(db_.get(), kCurrentVersion, kCompatibleVersion)) | 1053 if (!meta_table_->Init(db_.get(), kCurrentVersion, kCompatibleVersion)) |
| 1047 return false; | 1054 return false; |
| 1048 | 1055 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1188 | 1195 |
| 1189 bool AppCacheDatabase::DeleteExistingAndCreateNewDatabase() { | 1196 bool AppCacheDatabase::DeleteExistingAndCreateNewDatabase() { |
| 1190 DCHECK(!db_file_path_.empty()); | 1197 DCHECK(!db_file_path_.empty()); |
| 1191 DCHECK(base::PathExists(db_file_path_)); | 1198 DCHECK(base::PathExists(db_file_path_)); |
| 1192 VLOG(1) << "Deleting existing appcache data and starting over."; | 1199 VLOG(1) << "Deleting existing appcache data and starting over."; |
| 1193 | 1200 |
| 1194 ResetConnectionAndTables(); | 1201 ResetConnectionAndTables(); |
| 1195 | 1202 |
| 1196 // This also deletes the disk cache data. | 1203 // This also deletes the disk cache data. |
| 1197 base::FilePath directory = db_file_path_.DirName(); | 1204 base::FilePath directory = db_file_path_.DirName(); |
| 1198 if (!base::DeleteFile(directory, true) || | 1205 if (!base::DeleteFile(directory, true)) |
| 1199 !base::CreateDirectory(directory)) { | |
| 1200 return false; | 1206 return false; |
| 1201 } | |
| 1202 | 1207 |
| 1203 // Make sure the steps above actually deleted things. | 1208 // Make sure the steps above actually deleted things. |
| 1204 if (base::PathExists(db_file_path_)) | 1209 if (base::PathExists(directory)) |
| 1210 return false; |
| 1211 |
| 1212 if (!base::CreateDirectory(directory)) |
| 1205 return false; | 1213 return false; |
| 1206 | 1214 |
| 1207 // So we can't go recursive. | 1215 // So we can't go recursive. |
| 1208 if (is_recreating_) | 1216 if (is_recreating_) |
| 1209 return false; | 1217 return false; |
| 1210 | 1218 |
| 1211 base::AutoReset<bool> auto_reset(&is_recreating_, true); | 1219 base::AutoReset<bool> auto_reset(&is_recreating_, true); |
| 1212 return LazyOpen(true); | 1220 return LazyOpen(true); |
| 1213 } | 1221 } |
| 1214 | 1222 |
| 1223 void AppCacheDatabase::OnDatabaseError(int err, sql::Statement* stmt) { |
| 1224 was_corruption_detected_ |= sql::IsErrorCatastrophic(err); |
| 1225 |
| 1226 // Retain the default handling which is to assert on debug and to |
| 1227 // ignore on release. |
| 1228 if (!db_->ShouldIgnoreSqliteError(err)) |
| 1229 DLOG(ERROR) << db_->GetErrorMessage(); |
| 1230 |
| 1231 // Note to self? |
| 1232 // Maybe use non-catostrophic errors to trigger a full integrity check? |
| 1233 } |
| 1234 |
| 1235 bool AppCacheDatabase::was_corruption_dectected() { |
| 1236 // for local testing, inject spurious errors and see wha' happens |
| 1237 //uint64 kSynthesizedErrorRate = 10; // percent |
| 1238 //was_corruption_detected_ |= |
| 1239 // (base::RandGenerator(100) < kSynthesizedErrorRate); |
| 1240 return was_corruption_detected_; |
| 1241 } |
| 1242 |
| 1215 } // namespace appcache | 1243 } // namespace appcache |
| OLD | NEW |