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