summaryrefslogtreecommitdiffstats
path: root/webkit/tools/test_shell/simple_database_system.cc
diff options
context:
space:
mode:
Diffstat (limited to 'webkit/tools/test_shell/simple_database_system.cc')
-rw-r--r--webkit/tools/test_shell/simple_database_system.cc107
1 files changed, 107 insertions, 0 deletions
diff --git a/webkit/tools/test_shell/simple_database_system.cc b/webkit/tools/test_shell/simple_database_system.cc
new file mode 100644
index 0000000..ab98e1a
--- /dev/null
+++ b/webkit/tools/test_shell/simple_database_system.cc
@@ -0,0 +1,107 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
+// source code is governed by a BSD-style license that can be found in the
+// LICENSE file.
+
+#include "webkit/tools/test_shell/simple_database_system.h"
+
+#if defined(USE_SYSTEM_SQLITE)
+#include <sqlite3.h>
+#else
+#include "third_party/sqlite/preprocessed/sqlite3.h"
+#endif
+
+#include "base/file_util.h"
+#include "base/platform_thread.h"
+#include "base/process_util.h"
+#include "webkit/database/vfs_backend.h"
+#include "webkit/glue/webkit_glue.h"
+
+using webkit_database::VfsBackend;
+
+SimpleDatabaseSystem* SimpleDatabaseSystem::instance_ = NULL;
+
+SimpleDatabaseSystem* SimpleDatabaseSystem::GetInstance() {
+ DCHECK(instance_);
+ return instance_;
+}
+
+SimpleDatabaseSystem::SimpleDatabaseSystem()
+ : hack_main_db_handle_(base::kInvalidPlatformFileValue) {
+ temp_dir_.CreateUniqueTempDir();
+ DCHECK(!instance_);
+ instance_ = this;
+}
+
+SimpleDatabaseSystem::~SimpleDatabaseSystem() {
+ base::ClosePlatformFile(hack_main_db_handle_);
+ instance_ = NULL;
+}
+
+base::PlatformFile SimpleDatabaseSystem::OpenFile(
+ const FilePath& file_name, int desired_flags,
+ base::PlatformFile* dir_handle) {
+ base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
+ VfsBackend::OpenFile(GetDBFileFullPath(file_name), GetDBDir(), desired_flags,
+ base::GetCurrentProcessHandle(), &file_handle,
+ dir_handle);
+
+ // HACK: Currently, the DB object that keeps track of the main database
+ // (DatabaseTracker) is a singleton that is declared as a static variable
+ // in a function, so it gets destroyed at the very end of the program.
+ // Because of that, we have a handle opened to the main DB file until the
+ // very end of the program, which prevents temp_dir_'s destructor from
+ // deleting the database directory.
+ //
+ // We will properly solve this problem when we reimplement DatabaseTracker.
+ // For now, however, we are going to take advantage of the fact that in order
+ // to do anything related to DBs, we have to call openDatabase() first, which
+ // opens a handle to the main DB before opening handles to any other DB files.
+ // We are going to cache the first file handle we get, and we are going to
+ // manually close it in the destructor.
+ if (hack_main_db_handle_ == base::kInvalidPlatformFileValue) {
+ hack_main_db_handle_ = file_handle;
+ }
+
+ return file_handle;
+}
+
+int SimpleDatabaseSystem::DeleteFile(
+ const FilePath& file_name, bool sync_dir) {
+ // We try to delete the file multiple times, because that's what the default
+ // VFS does (apparently deleting a file can sometimes fail on Windows).
+ // We sleep for 10ms between retries for the same reason.
+ const int kNumDeleteRetries = 3;
+ int num_retries = 0;
+ int error_code = SQLITE_OK;
+ do {
+ error_code = VfsBackend::DeleteFile(
+ GetDBFileFullPath(file_name), GetDBDir(), sync_dir);
+ } while ((++num_retries < kNumDeleteRetries) &&
+ (error_code == SQLITE_IOERR_DELETE) &&
+ (PlatformThread::Sleep(10), 1));
+
+ return error_code;
+}
+
+long SimpleDatabaseSystem::GetFileAttributes(
+ const FilePath& file_name) {
+ return VfsBackend::GetFileAttributes(GetDBFileFullPath(file_name));
+}
+
+long long SimpleDatabaseSystem::GetFileSize(
+ const FilePath& file_name) {
+ return VfsBackend::GetFileSize(GetDBFileFullPath(file_name));
+}
+
+void SimpleDatabaseSystem::ClearAllDatabases() {
+ // TODO(dumi): implement this once we refactor DatabaseTracker
+ //file_util::Delete(GetDBDir(), true);
+}
+
+FilePath SimpleDatabaseSystem::GetDBDir() {
+ return temp_dir_.path().Append(FILE_PATH_LITERAL("databases"));
+}
+
+FilePath SimpleDatabaseSystem::GetDBFileFullPath(const FilePath& file_name) {
+ return GetDBDir().Append(file_name);
+}