diff options
author | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-22 11:05:54 +0000 |
---|---|---|
committer | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-22 11:05:54 +0000 |
commit | a13832a1c24681144c0d6ec31be09d812eacd285 (patch) | |
tree | a8d2c9bf5b3957a6df5f7cfdc3bf73a4fa801d7f /webkit/fileapi | |
parent | e1943b7dda014c0f3f3304ea256e13c3d484e25d (diff) | |
download | chromium_src-a13832a1c24681144c0d6ec31be09d812eacd285.zip chromium_src-a13832a1c24681144c0d6ec31be09d812eacd285.tar.gz chromium_src-a13832a1c24681144c0d6ec31be09d812eacd285.tar.bz2 |
Add a utility method that returns a list of origins that have FileSystem storage.
The method is to be used to retrieve total amount of usage for a type, to collect all the origins at startup after initialization etc.
BUG=none
TEST=FileSystemUtilListOriginsTest
Review URL: http://codereview.chromium.org/6524038
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75582 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/fileapi')
-rw-r--r-- | webkit/fileapi/file_system_path_manager.cc | 23 | ||||
-rw-r--r-- | webkit/fileapi/file_system_path_manager.h | 25 | ||||
-rw-r--r-- | webkit/fileapi/file_system_path_manager_unittest.cc | 91 |
3 files changed, 131 insertions, 8 deletions
diff --git a/webkit/fileapi/file_system_path_manager.cc b/webkit/fileapi/file_system_path_manager.cc index 8aee5d5..8ef0631 100644 --- a/webkit/fileapi/file_system_path_manager.cc +++ b/webkit/fileapi/file_system_path_manager.cc @@ -4,12 +4,12 @@ #include "webkit/fileapi/file_system_path_manager.h" -#include "base/file_util.h" #include "base/rand_util.h" #include "base/logging.h" #include "base/message_loop.h" #include "base/message_loop_proxy.h" #include "base/scoped_callback_factory.h" +#include "base/scoped_ptr.h" #include "base/stringprintf.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" @@ -351,6 +351,27 @@ FilePath FileSystemPathManager::GetFileSystemBaseDirectoryForOriginAndType( .AppendASCII(type_string); } +FileSystemPathManager::OriginEnumerator::OriginEnumerator( + const FilePath& base_path) + : enumerator_(base_path, false /* recursive */, + file_util::FileEnumerator::DIRECTORIES) { +} + +std::string FileSystemPathManager::OriginEnumerator::Next() { + current_ = enumerator_.Next(); + return FilePathStringToASCII(current_.BaseName().value()); +} + +bool FileSystemPathManager::OriginEnumerator::HasTemporary() { + return !current_.empty() && file_util::DirectoryExists(current_.AppendASCII( + FileSystemPathManager::kTemporaryName)); +} + +bool FileSystemPathManager::OriginEnumerator::HasPersistent() { + return !current_.empty() && file_util::DirectoryExists(current_.AppendASCII( + FileSystemPathManager::kPersistentName)); +} + } // namespace fileapi COMPILE_ASSERT(int(WebFileSystem::TypeTemporary) == \ diff --git a/webkit/fileapi/file_system_path_manager.h b/webkit/fileapi/file_system_path_manager.h index be9ceff..28082aa 100644 --- a/webkit/fileapi/file_system_path_manager.h +++ b/webkit/fileapi/file_system_path_manager.h @@ -4,10 +4,11 @@ #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_PATH_MANAGER_H_ #define WEBKIT_FILEAPI_FILE_SYSTEM_PATH_MANAGER_H_ +#pragma once #include "base/callback.h" #include "base/file_path.h" -#include "base/scoped_ptr.h" +#include "base/file_util.h" #include "webkit/fileapi/file_system_types.h" class GURL; @@ -18,6 +19,9 @@ class MessageLoopProxy; namespace fileapi { +// TODO(kinuko): Probably this module must be called FileSystemPathUtil +// or something similar. + // An interface to construct or crack sandboxed filesystem paths. // Currently each sandboxed filesystem path looks like: // @@ -96,6 +100,25 @@ class FileSystemPathManager { const std::string& origin_identifier, fileapi::FileSystemType type); + // Enumerates origins under the given |base_path|. + // This must be used on the FILE thread. + class OriginEnumerator { + public: + OriginEnumerator(const FilePath& base_path); + + // Returns the next origin identifier. Returns empty if there are no + // more origins. + std::string Next(); + + bool HasTemporary(); + bool HasPersistent(); + const FilePath& path() { return current_; } + + private: + file_util::FileEnumerator enumerator_; + FilePath current_; + }; + private: class GetFileSystemRootPathTask; diff --git a/webkit/fileapi/file_system_path_manager_unittest.cc b/webkit/fileapi/file_system_path_manager_unittest.cc index e0c1d81..3d6c831 100644 --- a/webkit/fileapi/file_system_path_manager_unittest.cc +++ b/webkit/fileapi/file_system_path_manager_unittest.cc @@ -4,6 +4,9 @@ #include "webkit/fileapi/file_system_path_manager.h" +#include <set> +#include <string> + #include "base/basictypes.h" #include "base/file_util.h" #include "base/message_loop.h" @@ -164,8 +167,7 @@ class FileSystemPathManagerTest : public testing::Test { } void SetUp() { - data_dir_.reset(new ScopedTempDir); - ASSERT_TRUE(data_dir_->CreateUniqueTempDir()); + ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); root_path_callback_status_ = false; root_path_.clear(); file_system_name_.clear(); @@ -177,7 +179,7 @@ class FileSystemPathManagerTest : public testing::Test { bool allow_file_access) { return new FileSystemPathManager( base::MessageLoopProxy::CreateForCurrentThread(), - data_dir_->path(), incognito, allow_file_access); + data_dir_.path(), incognito, allow_file_access); } void OnGetRootPath(bool success, @@ -207,14 +209,14 @@ class FileSystemPathManagerTest : public testing::Test { return manager->CrackFileSystemPath(path, NULL, NULL, NULL); } - FilePath data_path() { return data_dir_->path(); } + FilePath data_path() { return data_dir_.path(); } FilePath file_system_path() { - return data_dir_->path().Append( + return data_dir_.path().Append( FileSystemPathManager::kFileSystemDirectory); } private: - scoped_ptr<ScopedTempDir> data_dir_; + ScopedTempDir data_dir_; base::ScopedCallbackFactory<FileSystemPathManagerTest> callback_factory_; bool root_path_callback_status_; @@ -423,3 +425,80 @@ TEST_F(FileSystemPathManagerTest, IsRestrictedName) { FileSystemPathManager::IsRestrictedFileName(name)); } } + +class FileSystemPathManagerOriginEnumeratorTest : public testing::Test { + public: + void SetUp() { + ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); + enumerator_.reset(new FileSystemPathManager::OriginEnumerator( + data_dir_.path())); + } + + FileSystemPathManager::OriginEnumerator* enumerator() const { + return enumerator_.get(); + } + + protected: + void CreateOriginTypeDirectory(const std::string& origin_identifier, + fileapi::FileSystemType type) { + std::string type_string = + FileSystemPathManager::GetFileSystemTypeString(type); + ASSERT_TRUE(!type_string.empty()); + FilePath target = data_dir_.path().AppendASCII(origin_identifier) + .AppendASCII(type_string); + file_util::CreateDirectory(target); + ASSERT_TRUE(file_util::DirectoryExists(target)); + } + + ScopedTempDir data_dir_; + scoped_ptr<FileSystemPathManager::OriginEnumerator> enumerator_; +}; + +TEST_F(FileSystemPathManagerOriginEnumeratorTest, Empty) { + ASSERT_TRUE(enumerator()->Next().empty()); +} + +TEST_F(FileSystemPathManagerOriginEnumeratorTest, EnumerateOrigins) { + const char* temporary_origins[] = { + "http_www.bar.com_0", + "http_www.foo.com_0", + "http_www.foo.com_80", + "http_www.example.com_8080", + "http_www.google.com_80", + }; + const char* persistent_origins[] = { + "http_www.bar.com_0", + "http_www.foo.com_8080", + "http_www.foo.com_80", + }; + size_t temporary_size = ARRAYSIZE_UNSAFE(temporary_origins); + size_t persistent_size = ARRAYSIZE_UNSAFE(persistent_origins); + std::set<std::string> temporary_set, persistent_set; + for (size_t i = 0; i < temporary_size; ++i) { + CreateOriginTypeDirectory(temporary_origins[i], + fileapi::kFileSystemTypeTemporary); + temporary_set.insert(temporary_origins[i]); + } + for (size_t i = 0; i < persistent_size; ++i) { + CreateOriginTypeDirectory(persistent_origins[i], kFileSystemTypePersistent); + persistent_set.insert(persistent_origins[i]); + } + + size_t temporary_actual_size = 0; + size_t persistent_actual_size = 0; + std::string current; + while (!(current = enumerator()->Next()).empty()) { + SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current); + if (enumerator()->HasTemporary()) { + ASSERT_TRUE(temporary_set.find(current) != temporary_set.end()); + ++temporary_actual_size; + } + if (enumerator()->HasPersistent()) { + ASSERT_TRUE(persistent_set.find(current) != persistent_set.end()); + ++persistent_actual_size; + } + } + + ASSERT_EQ(temporary_size, temporary_actual_size); + ASSERT_EQ(persistent_size, persistent_actual_size); +} |