OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. |
| 4 |
| 5 #include "webkit/tools/test_shell/simple_database_system.h" |
| 6 |
| 7 #if defined(USE_SYSTEM_SQLITE) |
| 8 #include <sqlite3.h> |
| 9 #else |
| 10 #include "third_party/sqlite/preprocessed/sqlite3.h" |
| 11 #endif |
| 12 |
| 13 #include "base/file_util.h" |
| 14 #include "base/platform_thread.h" |
| 15 #include "base/process_util.h" |
| 16 #include "webkit/database/vfs_backend.h" |
| 17 #include "webkit/glue/webkit_glue.h" |
| 18 |
| 19 using webkit_database::VfsBackend; |
| 20 |
| 21 SimpleDatabaseSystem* SimpleDatabaseSystem::instance_ = NULL; |
| 22 |
| 23 SimpleDatabaseSystem* SimpleDatabaseSystem::GetInstance() { |
| 24 DCHECK(instance_); |
| 25 return instance_; |
| 26 } |
| 27 |
| 28 SimpleDatabaseSystem::SimpleDatabaseSystem() |
| 29 : hack_main_db_handle_(base::kInvalidPlatformFileValue) { |
| 30 temp_dir_.CreateUniqueTempDir(); |
| 31 DCHECK(!instance_); |
| 32 instance_ = this; |
| 33 } |
| 34 |
| 35 SimpleDatabaseSystem::~SimpleDatabaseSystem() { |
| 36 base::ClosePlatformFile(hack_main_db_handle_); |
| 37 instance_ = NULL; |
| 38 } |
| 39 |
| 40 base::PlatformFile SimpleDatabaseSystem::OpenFile( |
| 41 const FilePath& file_name, int desired_flags, |
| 42 base::PlatformFile* dir_handle) { |
| 43 base::PlatformFile file_handle = base::kInvalidPlatformFileValue; |
| 44 VfsBackend::OpenFile(GetDBFileFullPath(file_name), GetDBDir(), desired_flags, |
| 45 base::GetCurrentProcessHandle(), &file_handle, |
| 46 dir_handle); |
| 47 |
| 48 // HACK: Currently, the DB object that keeps track of the main database |
| 49 // (DatabaseTracker) is a singleton that is declared as a static variable |
| 50 // in a function, so it gets destroyed at the very end of the program. |
| 51 // Because of that, we have a handle opened to the main DB file until the |
| 52 // very end of the program, which prevents temp_dir_'s destructor from |
| 53 // deleting the database directory. |
| 54 // |
| 55 // We will properly solve this problem when we reimplement DatabaseTracker. |
| 56 // For now, however, we are going to take advantage of the fact that in order |
| 57 // to do anything related to DBs, we have to call openDatabase() first, which |
| 58 // opens a handle to the main DB before opening handles to any other DB files. |
| 59 // We are going to cache the first file handle we get, and we are going to |
| 60 // manually close it in the destructor. |
| 61 if (hack_main_db_handle_ == base::kInvalidPlatformFileValue) { |
| 62 hack_main_db_handle_ = file_handle; |
| 63 } |
| 64 |
| 65 return file_handle; |
| 66 } |
| 67 |
| 68 int SimpleDatabaseSystem::DeleteFile( |
| 69 const FilePath& file_name, bool sync_dir) { |
| 70 // We try to delete the file multiple times, because that's what the default |
| 71 // VFS does (apparently deleting a file can sometimes fail on Windows). |
| 72 // We sleep for 10ms between retries for the same reason. |
| 73 const int kNumDeleteRetries = 3; |
| 74 int num_retries = 0; |
| 75 int error_code = SQLITE_OK; |
| 76 do { |
| 77 error_code = VfsBackend::DeleteFile( |
| 78 GetDBFileFullPath(file_name), GetDBDir(), sync_dir); |
| 79 } while ((++num_retries < kNumDeleteRetries) && |
| 80 (error_code == SQLITE_IOERR_DELETE) && |
| 81 (PlatformThread::Sleep(10), 1)); |
| 82 |
| 83 return error_code; |
| 84 } |
| 85 |
| 86 long SimpleDatabaseSystem::GetFileAttributes( |
| 87 const FilePath& file_name) { |
| 88 return VfsBackend::GetFileAttributes(GetDBFileFullPath(file_name)); |
| 89 } |
| 90 |
| 91 long long SimpleDatabaseSystem::GetFileSize( |
| 92 const FilePath& file_name) { |
| 93 return VfsBackend::GetFileSize(GetDBFileFullPath(file_name)); |
| 94 } |
| 95 |
| 96 void SimpleDatabaseSystem::ClearAllDatabases() { |
| 97 // TODO(dumi): implement this once we refactor DatabaseTracker |
| 98 //file_util::Delete(GetDBDir(), true); |
| 99 } |
| 100 |
| 101 FilePath SimpleDatabaseSystem::GetDBDir() { |
| 102 return temp_dir_.path().Append(FILE_PATH_LITERAL("databases")); |
| 103 } |
| 104 |
| 105 FilePath SimpleDatabaseSystem::GetDBFileFullPath(const FilePath& file_name) { |
| 106 return GetDBDir().Append(file_name); |
| 107 } |
OLD | NEW |