OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/support/simple_database_system.h" | 5 #include "webkit/support/simple_database_system.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "third_party/sqlite/sqlite3.h" | 14 #include "third_party/sqlite/sqlite3.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
17 #include "webkit/database/database_util.h" | 17 #include "webkit/database/database_util.h" |
18 #include "webkit/database/vfs_backend.h" | 18 #include "webkit/database/vfs_backend.h" |
19 | 19 |
20 using webkit_database::DatabaseTracker; | 20 using webkit_database::DatabaseTracker; |
21 using webkit_database::DatabaseUtil; | 21 using webkit_database::DatabaseUtil; |
22 using webkit_database::OriginInfo; | |
22 using webkit_database::VfsBackend; | 23 using webkit_database::VfsBackend; |
23 | 24 |
24 SimpleDatabaseSystem* SimpleDatabaseSystem::instance_ = NULL; | 25 SimpleDatabaseSystem* SimpleDatabaseSystem::instance_ = NULL; |
25 | 26 |
26 SimpleDatabaseSystem* SimpleDatabaseSystem::GetInstance() { | 27 SimpleDatabaseSystem* SimpleDatabaseSystem::GetInstance() { |
27 DCHECK(instance_); | 28 DCHECK(instance_); |
28 return instance_; | 29 return instance_; |
29 } | 30 } |
30 | 31 |
31 SimpleDatabaseSystem::SimpleDatabaseSystem() | 32 SimpleDatabaseSystem::SimpleDatabaseSystem() |
32 : db_thread_("SimpleDBThread"), | 33 : db_thread_("SimpleDBThread"), |
34 quota_per_origin_(5 * 1024 * 1024), | |
33 open_connections_(new webkit_database::DatabaseConnectionsWrapper) { | 35 open_connections_(new webkit_database::DatabaseConnectionsWrapper) { |
34 DCHECK(!instance_); | 36 DCHECK(!instance_); |
35 instance_ = this; | 37 instance_ = this; |
36 CHECK(temp_dir_.CreateUniqueTempDir()); | 38 CHECK(temp_dir_.CreateUniqueTempDir()); |
37 db_tracker_ = new DatabaseTracker(temp_dir_.path(), false, NULL, NULL, NULL); | 39 db_tracker_ = new DatabaseTracker(temp_dir_.path(), false, NULL, NULL, NULL); |
38 db_tracker_->AddObserver(this); | 40 db_tracker_->AddObserver(this); |
39 db_thread_.Start(); | 41 db_thread_.Start(); |
40 db_thread_proxy_ = db_thread_.message_loop_proxy(); | 42 db_thread_proxy_ = db_thread_.message_loop_proxy(); |
41 } | 43 } |
42 | 44 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 int64 SimpleDatabaseSystem::GetFileSize(const string16& vfs_file_name) { | 115 int64 SimpleDatabaseSystem::GetFileSize(const string16& vfs_file_name) { |
114 int64 result = 0; | 116 int64 result = 0; |
115 base::WaitableEvent done_event(false, false); | 117 base::WaitableEvent done_event(false, false); |
116 db_thread_proxy_->PostTask(FROM_HERE, | 118 db_thread_proxy_->PostTask(FROM_HERE, |
117 NewRunnableMethod(this, &SimpleDatabaseSystem::VfsGetFileSize, | 119 NewRunnableMethod(this, &SimpleDatabaseSystem::VfsGetFileSize, |
118 vfs_file_name, &result, &done_event)); | 120 vfs_file_name, &result, &done_event)); |
119 done_event.Wait(); | 121 done_event.Wait(); |
120 return result; | 122 return result; |
121 } | 123 } |
122 | 124 |
125 int64 SimpleDatabaseSystem::GetSpaceAvailable( | |
126 const string16& origin_identifier) { | |
127 int64 result = 0; | |
128 base::WaitableEvent done_event(false, false); | |
129 db_thread_proxy_->PostTask(FROM_HERE, | |
130 NewRunnableMethod(this, &SimpleDatabaseSystem::VfsGetSpaceAvailable, | |
131 origin_identifier, &result, &done_event)); | |
132 done_event.Wait(); | |
133 return result; | |
134 } | |
135 | |
123 void SimpleDatabaseSystem::ClearAllDatabases() { | 136 void SimpleDatabaseSystem::ClearAllDatabases() { |
124 open_connections_->WaitForAllDatabasesToClose(); | 137 open_connections_->WaitForAllDatabasesToClose(); |
125 db_thread_proxy_->PostTask(FROM_HERE, | 138 db_thread_proxy_->PostTask(FROM_HERE, |
126 NewRunnableMethod(this, &SimpleDatabaseSystem::ResetTracker)); | 139 NewRunnableMethod(this, &SimpleDatabaseSystem::ResetTracker)); |
127 } | 140 } |
128 | 141 |
129 void SimpleDatabaseSystem::SetDatabaseQuota(int64 quota) { | 142 void SimpleDatabaseSystem::SetDatabaseQuota(int64 quota) { |
130 if (!db_thread_proxy_->BelongsToCurrentThread()) { | 143 if (!db_thread_proxy_->BelongsToCurrentThread()) { |
131 db_thread_proxy_->PostTask(FROM_HERE, | 144 db_thread_proxy_->PostTask(FROM_HERE, |
132 NewRunnableMethod(this, &SimpleDatabaseSystem::SetDatabaseQuota, | 145 NewRunnableMethod(this, &SimpleDatabaseSystem::SetDatabaseQuota, |
133 quota)); | 146 quota)); |
134 return; | 147 return; |
135 } | 148 } |
136 db_tracker_->SetDefaultQuota(quota); | 149 quota_per_origin_ = quota; |
137 } | 150 } |
138 | 151 |
139 void SimpleDatabaseSystem::DatabaseOpened(const string16& origin_identifier, | 152 void SimpleDatabaseSystem::DatabaseOpened(const string16& origin_identifier, |
140 const string16& database_name, | 153 const string16& database_name, |
141 const string16& description, | 154 const string16& description, |
142 int64 estimated_size) { | 155 int64 estimated_size) { |
143 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); | 156 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); |
144 int64 database_size = 0; | 157 int64 database_size = 0; |
145 int64 space_available = 0; | 158 int64 space_available_not_used = 0; |
146 db_tracker_->DatabaseOpened( | 159 db_tracker_->DatabaseOpened( |
147 origin_identifier, database_name, description, | 160 origin_identifier, database_name, description, |
148 estimated_size, &database_size, &space_available); | 161 estimated_size, &database_size, &space_available_not_used); |
149 OnDatabaseSizeChanged(origin_identifier, database_name, | 162 OnDatabaseSizeChanged(origin_identifier, database_name, |
150 database_size, space_available); | 163 database_size, 0); |
151 } | 164 } |
152 | 165 |
153 void SimpleDatabaseSystem::DatabaseModified(const string16& origin_identifier, | 166 void SimpleDatabaseSystem::DatabaseModified(const string16& origin_identifier, |
154 const string16& database_name) { | 167 const string16& database_name) { |
155 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); | 168 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); |
156 db_tracker_->DatabaseModified(origin_identifier, database_name); | 169 db_tracker_->DatabaseModified(origin_identifier, database_name); |
157 } | 170 } |
158 | 171 |
159 void SimpleDatabaseSystem::DatabaseClosed(const string16& origin_identifier, | 172 void SimpleDatabaseSystem::DatabaseClosed(const string16& origin_identifier, |
160 const string16& database_name) { | 173 const string16& database_name) { |
161 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); | 174 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); |
162 db_tracker_->DatabaseClosed(origin_identifier, database_name); | 175 db_tracker_->DatabaseClosed(origin_identifier, database_name); |
163 open_connections_->RemoveOpenConnection(origin_identifier, database_name); | 176 open_connections_->RemoveOpenConnection(origin_identifier, database_name); |
164 } | 177 } |
165 | 178 |
166 void SimpleDatabaseSystem::OnDatabaseSizeChanged( | 179 void SimpleDatabaseSystem::OnDatabaseSizeChanged( |
167 const string16& origin_identifier, | 180 const string16& origin_identifier, |
168 const string16& database_name, | 181 const string16& database_name, |
169 int64 database_size, | 182 int64 database_size, |
170 int64 space_available) { | 183 int64 space_available_not_used) { |
171 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); | 184 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); |
172 // We intentionally call into webkit on our background db_thread_ | 185 // We intentionally call into webkit on our background db_thread_ |
173 // to better emulate what happens in chrome where this method is | 186 // to better emulate what happens in chrome where this method is |
174 // invoked on the background ipc thread. | 187 // invoked on the background ipc thread. |
175 WebKit::WebDatabase::updateDatabaseSize( | 188 WebKit::WebDatabase::updateDatabaseSize( |
176 origin_identifier, database_name, database_size, space_available); | 189 origin_identifier, database_name, database_size); |
177 } | 190 } |
178 | 191 |
179 void SimpleDatabaseSystem::OnDatabaseScheduledForDeletion( | 192 void SimpleDatabaseSystem::OnDatabaseScheduledForDeletion( |
180 const string16& origin_identifier, | 193 const string16& origin_identifier, |
181 const string16& database_name) { | 194 const string16& database_name) { |
182 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); | 195 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); |
183 // We intentionally call into webkit on our background db_thread_ | 196 // We intentionally call into webkit on our background db_thread_ |
184 // to better emulate what happens in chrome where this method is | 197 // to better emulate what happens in chrome where this method is |
185 // invoked on the background ipc thread. | 198 // invoked on the background ipc thread. |
186 WebKit::WebDatabase::closeDatabaseImmediately( | 199 WebKit::WebDatabase::closeDatabaseImmediately( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 } | 244 } |
232 | 245 |
233 void SimpleDatabaseSystem::VfsGetFileSize( | 246 void SimpleDatabaseSystem::VfsGetFileSize( |
234 const string16& vfs_file_name, | 247 const string16& vfs_file_name, |
235 int64* result, base::WaitableEvent* done_event) { | 248 int64* result, base::WaitableEvent* done_event) { |
236 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); | 249 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); |
237 *result = VfsBackend::GetFileSize(GetFullFilePathForVfsFile(vfs_file_name)); | 250 *result = VfsBackend::GetFileSize(GetFullFilePathForVfsFile(vfs_file_name)); |
238 done_event->Signal(); | 251 done_event->Signal(); |
239 } | 252 } |
240 | 253 |
254 void SimpleDatabaseSystem::VfsGetSpaceAvailable( | |
255 const string16& origin_identifier, | |
256 int64* result, base::WaitableEvent* done_event) { | |
257 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); | |
258 // This method isn't actually part of the "vfs" interface, but it is | |
259 // used from within webcore and handled here in the same fashion. | |
260 OriginInfo info; | |
261 if (db_tracker_->GetOriginInfo(origin_identifier, &info)) { | |
262 int64 space_available = quota_per_origin_ - info.TotalSize(); | |
263 *result = space_available < 0 ? 0 : space_available; | |
264 } else { | |
265 NOTREACHED(); | |
266 *result = 0; | |
267 } | |
268 done_event->Signal(); | |
darin (slow to review)
2011/05/24 04:56:11
are you sure it is safe to delete done_event befor
michaeln
2011/05/24 05:10:54
right... i remember that too... i'll check, the ot
| |
269 } | |
270 | |
241 FilePath SimpleDatabaseSystem::GetFullFilePathForVfsFile( | 271 FilePath SimpleDatabaseSystem::GetFullFilePathForVfsFile( |
242 const string16& vfs_file_name) { | 272 const string16& vfs_file_name) { |
243 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); | 273 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); |
244 if (vfs_file_name.empty()) // temp file, used for vacuuming | 274 if (vfs_file_name.empty()) // temp file, used for vacuuming |
245 return FilePath(); | 275 return FilePath(); |
246 return DatabaseUtil::GetFullFilePathForVfsFile( | 276 return DatabaseUtil::GetFullFilePathForVfsFile( |
247 db_tracker_.get(), vfs_file_name); | 277 db_tracker_.get(), vfs_file_name); |
248 } | 278 } |
249 | 279 |
250 void SimpleDatabaseSystem::ResetTracker() { | 280 void SimpleDatabaseSystem::ResetTracker() { |
251 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); | 281 DCHECK(db_thread_proxy_->BelongsToCurrentThread()); |
252 db_tracker_->CloseTrackerDatabaseAndClearCaches(); | 282 db_tracker_->CloseTrackerDatabaseAndClearCaches(); |
253 file_util::Delete(db_tracker_->DatabaseDirectory(), true); | 283 file_util::Delete(db_tracker_->DatabaseDirectory(), true); |
254 } | 284 } |
255 | 285 |
256 void SimpleDatabaseSystem::ThreadCleanup(base::WaitableEvent* done_event) { | 286 void SimpleDatabaseSystem::ThreadCleanup(base::WaitableEvent* done_event) { |
257 ResetTracker(); | 287 ResetTracker(); |
258 db_tracker_->RemoveObserver(this); | 288 db_tracker_->RemoveObserver(this); |
259 db_tracker_ = NULL; | 289 db_tracker_ = NULL; |
260 done_event->Signal(); | 290 done_event->Signal(); |
261 } | 291 } |
262 | 292 |
OLD | NEW |