diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-09 23:31:14 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-09 23:31:14 +0000 |
commit | 3200ec656f6c1712ce3a98e9f0a9fc5caa0ecd6f (patch) | |
tree | 93f89b09c124b5cd01fce39a0cfba436bd4b1c7b /webkit/support | |
parent | 5651386fa81b87a1e473c016afe5c3c4670bbb5f (diff) | |
download | chromium_src-3200ec656f6c1712ce3a98e9f0a9fc5caa0ecd6f.zip chromium_src-3200ec656f6c1712ce3a98e9f0a9fc5caa0ecd6f.tar.gz chromium_src-3200ec656f6c1712ce3a98e9f0a9fc5caa0ecd6f.tar.bz2 |
webkit: move SimpleDatabaseSystem into webkit/support/
It is used by DRT and by test_shell, so it belongs in webkit/support/.
Review URL: http://codereview.chromium.org/6657011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77550 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/support')
-rw-r--r-- | webkit/support/simple_database_system.cc | 193 | ||||
-rw-r--r-- | webkit/support/simple_database_system.h | 84 | ||||
-rw-r--r-- | webkit/support/test_webkit_client.cc | 2 | ||||
-rw-r--r-- | webkit/support/test_webkit_client.h | 2 | ||||
-rw-r--r-- | webkit/support/webkit_support.cc | 2 | ||||
-rw-r--r-- | webkit/support/webkit_support.gypi | 6 |
6 files changed, 283 insertions, 6 deletions
diff --git a/webkit/support/simple_database_system.cc b/webkit/support/simple_database_system.cc new file mode 100644 index 0000000..c1749a5 --- /dev/null +++ b/webkit/support/simple_database_system.cc @@ -0,0 +1,193 @@ +// Copyright (c) 2010 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/support/simple_database_system.h" + +#include "base/auto_reset.h" +#include "base/file_util.h" +#include "base/message_loop.h" +#include "base/threading/platform_thread.h" +#include "base/utf_string_conversions.h" +#include "third_party/sqlite/sqlite3.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" +#include "webkit/database/database_util.h" +#include "webkit/database/vfs_backend.h" + +using webkit_database::DatabaseTracker; +using webkit_database::DatabaseUtil; +using webkit_database::VfsBackend; + +SimpleDatabaseSystem* SimpleDatabaseSystem::instance_ = NULL; + +SimpleDatabaseSystem* SimpleDatabaseSystem::GetInstance() { + DCHECK(instance_); + return instance_; +} + +SimpleDatabaseSystem::SimpleDatabaseSystem() + : waiting_for_dbs_to_close_(false) { + CHECK(temp_dir_.CreateUniqueTempDir()); + db_tracker_ = new DatabaseTracker(temp_dir_.path(), false, NULL); + db_tracker_->AddObserver(this); + DCHECK(!instance_); + instance_ = this; +} + +SimpleDatabaseSystem::~SimpleDatabaseSystem() { + db_tracker_->RemoveObserver(this); + instance_ = NULL; +} + +base::PlatformFile SimpleDatabaseSystem::OpenFile( + const string16& vfs_file_name, int desired_flags) { + base::PlatformFile file_handle = base::kInvalidPlatformFileValue; + FilePath file_name = GetFullFilePathForVfsFile(vfs_file_name); + if (file_name.empty()) { + VfsBackend::OpenTempFileInDirectory( + db_tracker_->DatabaseDirectory(), desired_flags, &file_handle); + } else { + VfsBackend::OpenFile(file_name, desired_flags, &file_handle); + } + + return file_handle; +} + +int SimpleDatabaseSystem::DeleteFile( + const string16& vfs_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; + FilePath file_name = GetFullFilePathForVfsFile(vfs_file_name); + do { + error_code = VfsBackend::DeleteFile(file_name, sync_dir); + } while ((++num_retries < kNumDeleteRetries) && + (error_code == SQLITE_IOERR_DELETE) && + (base::PlatformThread::Sleep(10), 1)); + + return error_code; +} + +long SimpleDatabaseSystem::GetFileAttributes(const string16& vfs_file_name) { + return VfsBackend::GetFileAttributes( + GetFullFilePathForVfsFile(vfs_file_name)); +} + +long long SimpleDatabaseSystem::GetFileSize(const string16& vfs_file_name) { + return VfsBackend::GetFileSize(GetFullFilePathForVfsFile(vfs_file_name)); +} + +void SimpleDatabaseSystem::DatabaseOpened(const string16& origin_identifier, + const string16& database_name, + const string16& description, + int64 estimated_size) { + int64 database_size = 0; + int64 space_available = 0; + database_connections_.AddConnection(origin_identifier, database_name); + db_tracker_->DatabaseOpened(origin_identifier, database_name, description, + estimated_size, &database_size, &space_available); + SetFullFilePathsForVfsFile(origin_identifier, database_name); + + OnDatabaseSizeChanged(origin_identifier, database_name, + database_size, space_available); +} + +void SimpleDatabaseSystem::DatabaseModified(const string16& origin_identifier, + const string16& database_name) { + DCHECK(database_connections_.IsDatabaseOpened( + origin_identifier, database_name)); + db_tracker_->DatabaseModified(origin_identifier, database_name); +} + +void SimpleDatabaseSystem::DatabaseClosed(const string16& origin_identifier, + const string16& database_name) { + DCHECK(database_connections_.IsDatabaseOpened( + origin_identifier, database_name)); + db_tracker_->DatabaseClosed(origin_identifier, database_name); + database_connections_.RemoveConnection(origin_identifier, database_name); + + if (waiting_for_dbs_to_close_ && database_connections_.IsEmpty()) + MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); +} + +void SimpleDatabaseSystem::OnDatabaseSizeChanged( + const string16& origin_identifier, + const string16& database_name, + int64 database_size, + int64 space_available) { + if (database_connections_.IsOriginUsed(origin_identifier)) { + WebKit::WebDatabase::updateDatabaseSize( + origin_identifier, database_name, database_size, space_available); + } +} + +void SimpleDatabaseSystem::OnDatabaseScheduledForDeletion( + const string16& origin_identifier, + const string16& database_name) { + WebKit::WebDatabase::closeDatabaseImmediately( + origin_identifier, database_name); +} + +void SimpleDatabaseSystem::databaseOpened(const WebKit::WebDatabase& database) { + DatabaseOpened(database.securityOrigin().databaseIdentifier(), + database.name(), database.displayName(), + database.estimatedSize()); +} + +void SimpleDatabaseSystem::databaseModified( + const WebKit::WebDatabase& database) { + DatabaseModified(database.securityOrigin().databaseIdentifier(), + database.name()); +} + +void SimpleDatabaseSystem::databaseClosed(const WebKit::WebDatabase& database) { + DatabaseClosed(database.securityOrigin().databaseIdentifier(), + database.name()); +} + +void SimpleDatabaseSystem::ClearAllDatabases() { + // Wait for all databases to be closed. + if (!database_connections_.IsEmpty()) { + AutoReset<bool> waiting_for_dbs_auto_reset( + &waiting_for_dbs_to_close_, true); + MessageLoop::ScopedNestableTaskAllower nestable(MessageLoop::current()); + MessageLoop::current()->Run(); + } + + db_tracker_->CloseTrackerDatabaseAndClearCaches(); + file_util::Delete(db_tracker_->DatabaseDirectory(), true); + file_names_.clear(); +} + +void SimpleDatabaseSystem::SetDatabaseQuota(int64 quota) { + db_tracker_->SetDefaultQuota(quota); +} + +void SimpleDatabaseSystem::SetFullFilePathsForVfsFile( + const string16& origin_identifier, + const string16& database_name) { + string16 vfs_file_name = origin_identifier + ASCIIToUTF16("/") + + database_name + ASCIIToUTF16("#"); + FilePath file_name = + DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_, vfs_file_name); + + base::AutoLock file_names_auto_lock(file_names_lock_); + file_names_[vfs_file_name] = file_name; + file_names_[vfs_file_name + ASCIIToUTF16("-journal")] = + FilePath::FromWStringHack(file_name.ToWStringHack() + + ASCIIToWide("-journal")); +} + +FilePath SimpleDatabaseSystem::GetFullFilePathForVfsFile( + const string16& vfs_file_name) { + if (vfs_file_name.empty()) // temp file, used for vacuuming + return FilePath(); + + base::AutoLock file_names_auto_lock(file_names_lock_); + DCHECK(file_names_.find(vfs_file_name) != file_names_.end()); + return file_names_[vfs_file_name]; +} diff --git a/webkit/support/simple_database_system.h b/webkit/support/simple_database_system.h new file mode 100644 index 0000000..682c123 --- /dev/null +++ b/webkit/support/simple_database_system.h @@ -0,0 +1,84 @@ +// Copyright (c) 2010 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. + +#ifndef WEBKIT_SUPPORT_SIMPLE_DATABASE_SYSTEM_H_ +#define WEBKIT_SUPPORT_SIMPLE_DATABASE_SYSTEM_H_ + +#include "base/file_path.h" +#include "base/hash_tables.h" +#include "base/platform_file.h" +#include "base/ref_counted.h" +#include "base/scoped_temp_dir.h" +#include "base/string16.h" +#include "base/synchronization/lock.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabaseObserver.h" +#include "webkit/database/database_connections.h" +#include "webkit/database/database_tracker.h" + +class SimpleDatabaseSystem : public webkit_database::DatabaseTracker::Observer, + public WebKit::WebDatabaseObserver { + public: + static SimpleDatabaseSystem* GetInstance(); + SimpleDatabaseSystem(); + ~SimpleDatabaseSystem(); + + // VFS functions + base::PlatformFile OpenFile(const string16& vfs_file_name, int desired_flags); + int DeleteFile(const string16& vfs_file_name, bool sync_dir); + long GetFileAttributes(const string16& vfs_file_name); + long long GetFileSize(const string16& vfs_file_name); + + // database tracker functions + void DatabaseOpened(const string16& origin_identifier, + const string16& database_name, + const string16& description, + int64 estimated_size); + void DatabaseModified(const string16& origin_identifier, + const string16& database_name); + void DatabaseClosed(const string16& origin_identifier, + const string16& database_name); + + // DatabaseTracker::Observer implementation + virtual void OnDatabaseSizeChanged(const string16& origin_identifier, + const string16& database_name, + int64 database_size, + int64 space_available); + virtual void OnDatabaseScheduledForDeletion(const string16& origin_identifier, + const string16& database_name); + + // WebDatabaseObserver implementation + virtual void databaseOpened(const WebKit::WebDatabase& database); + virtual void databaseModified(const WebKit::WebDatabase& database); + virtual void databaseClosed(const WebKit::WebDatabase& database); + + void ClearAllDatabases(); + void SetDatabaseQuota(int64 quota); + + private: + // The calls that come from the database tracker run on the main thread. + // Therefore, we can only call DatabaseUtil::GetFullFilePathForVfsFile() + // on the main thread. However, the VFS calls run on the DB thread and + // they need to crack VFS file paths. To resolve this problem, we store + // a map of vfs_file_names to file_paths. The map is updated on the main + // thread on each DatabaseOpened() call that comes from the database + // tracker, and is read on the DB thread by each VFS call. + void SetFullFilePathsForVfsFile(const string16& origin_identifier, + const string16& database_name); + FilePath GetFullFilePathForVfsFile(const string16& vfs_file_name); + + static SimpleDatabaseSystem* instance_; + + bool waiting_for_dbs_to_close_; + + ScopedTempDir temp_dir_; + + scoped_refptr<webkit_database::DatabaseTracker> db_tracker_; + + base::Lock file_names_lock_; + base::hash_map<string16, FilePath> file_names_; + + webkit_database::DatabaseConnections database_connections_; +}; + +#endif // WEBKIT_SUPPORT_SIMPLE_DATABASE_SYSTEM_H_ diff --git a/webkit/support/test_webkit_client.cc b/webkit/support/test_webkit_client.cc index 73c8a41..7b90dea 100644 --- a/webkit/support/test_webkit_client.cc +++ b/webkit/support/test_webkit_client.cc @@ -40,10 +40,10 @@ #include "webkit/glue/webkit_glue.h" #include "webkit/glue/webkitclient_impl.h" #include "webkit/gpu/webgraphicscontext3d_in_process_impl.h" +#include "webkit/support/simple_database_system.h" #include "webkit/support/weburl_loader_mock_factory.h" #include "webkit/tools/test_shell/mock_webclipboard_impl.h" #include "webkit/tools/test_shell/simple_appcache_system.h" -#include "webkit/tools/test_shell/simple_database_system.h" #include "webkit/tools/test_shell/simple_file_system.h" #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" #include "webkit/tools/test_shell/simple_webcookiejar_impl.h" diff --git a/webkit/support/test_webkit_client.h b/webkit/support/test_webkit_client.h index efeecf6..8867864 100644 --- a/webkit/support/test_webkit_client.h +++ b/webkit/support/test_webkit_client.h @@ -7,10 +7,10 @@ #include "webkit/glue/webfileutilities_impl.h" #include "webkit/glue/webkitclient_impl.h" +#include "webkit/support/simple_database_system.h" #include "webkit/support/weburl_loader_mock_factory.h" #include "webkit/tools/test_shell/mock_webclipboard_impl.h" #include "webkit/tools/test_shell/simple_appcache_system.h" -#include "webkit/tools/test_shell/simple_database_system.h" #include "webkit/tools/test_shell/simple_file_system.h" #include "webkit/tools/test_shell/simple_webcookiejar_impl.h" #include "webkit/tools/test_shell/test_shell_webmimeregistry_impl.h" diff --git a/webkit/support/webkit_support.cc b/webkit/support/webkit_support.cc index 83fae0c..89c39cc 100644 --- a/webkit/support/webkit_support.cc +++ b/webkit/support/webkit_support.cc @@ -47,9 +47,9 @@ #include "webkit/plugins/npapi/webplugin_page_delegate.h" #include "webkit/plugins/npapi/webplugininfo.h" #include "webkit/support/platform_support.h" +#include "webkit/support/simple_database_system.h" #include "webkit/support/test_webplugin_page_delegate.h" #include "webkit/support/test_webkit_client.h" -#include "webkit/tools/test_shell/simple_database_system.h" #include "webkit/tools/test_shell/simple_file_system.h" #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" diff --git a/webkit/support/webkit_support.gypi b/webkit/support/webkit_support.gypi index 5b682c0..b62b4bb 100644 --- a/webkit/support/webkit_support.gypi +++ b/webkit/support/webkit_support.gypi @@ -83,13 +83,11 @@ '<(DEPTH)/webkit/tools/test_shell/mock_webclipboard_impl.h', '<(DEPTH)/webkit/tools/test_shell/simple_appcache_system.cc', '<(DEPTH)/webkit/tools/test_shell/simple_appcache_system.h', + '<(DEPTH)/webkit/tools/test_shell/simple_clipboard_impl.cc', '<(DEPTH)/webkit/tools/test_shell/simple_file_system.cc', '<(DEPTH)/webkit/tools/test_shell/simple_file_system.h', '<(DEPTH)/webkit/tools/test_shell/simple_file_writer.cc', '<(DEPTH)/webkit/tools/test_shell/simple_file_writer.h', - '<(DEPTH)/webkit/tools/test_shell/simple_clipboard_impl.cc', - '<(DEPTH)/webkit/tools/test_shell/simple_database_system.cc', - '<(DEPTH)/webkit/tools/test_shell/simple_database_system.h', '<(DEPTH)/webkit/tools/test_shell/simple_resource_loader_bridge.cc', '<(DEPTH)/webkit/tools/test_shell/simple_resource_loader_bridge.h', '<(DEPTH)/webkit/tools/test_shell/simple_socket_stream_bridge.cc', @@ -102,6 +100,8 @@ '<(DEPTH)/webkit/tools/test_shell/test_shell_webblobregistry_impl.h', '<(DEPTH)/webkit/tools/test_shell/test_shell_webmimeregistry_impl.cc', '<(DEPTH)/webkit/tools/test_shell/test_shell_webmimeregistry_impl.h', + 'simple_database_system.cc', + 'simple_database_system.h', ], 'conditions': [ ['inside_chromium_build==0', { |