summaryrefslogtreecommitdiffstats
path: root/webkit/tools/test_shell/simple_database_system.cc
blob: ab98e1a43ded09bdd387d0b01f38819c48d45eb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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);
}