diff options
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/fileapi/file_system_util.cc | 20 | ||||
-rw-r--r-- | webkit/fileapi/file_system_util.h | 17 | ||||
-rw-r--r-- | webkit/fileapi/file_system_util_unittest.cc | 25 |
3 files changed, 59 insertions, 3 deletions
diff --git a/webkit/fileapi/file_system_util.cc b/webkit/fileapi/file_system_util.cc index 7e017f7..9b2aae0 100644 --- a/webkit/fileapi/file_system_util.cc +++ b/webkit/fileapi/file_system_util.cc @@ -25,6 +25,9 @@ const char kIsolatedDir[] = "/isolated"; const char kExternalDir[] = "/external"; const char kTestDir[] = "/test"; +const FilePath::CharType VirtualPath::kRoot[] = FILE_PATH_LITERAL("/"); +const FilePath::CharType VirtualPath::kSeparator = FILE_PATH_LITERAL('/'); + // TODO(ericu): Consider removing support for '\', even on Windows, if possible. // There's a lot of test code that will need reworking, and we may have trouble // with base::FilePath elsewhere [e.g. DirName and other methods may also need @@ -70,6 +73,23 @@ void VirtualPath::GetComponents( std::vector<base::FilePath::StringType>(ret_val.rbegin(), ret_val.rend()); } +FilePath::StringType VirtualPath::GetNormalizedFilePath(const FilePath& path) { + FilePath::StringType normalized_path = path.value(); + const size_t num_separators = FilePath::StringType( + FilePath::kSeparators).length(); + for (size_t i = 1; i < num_separators; ++i) { + std::replace(normalized_path.begin(), normalized_path.end(), + FilePath::kSeparators[i], kSeparator); + } + + return (IsAbsolute(normalized_path)) ? + normalized_path : FilePath::StringType(kRoot) + normalized_path; +} + +bool VirtualPath::IsAbsolute(const FilePath::StringType& path) { + return path.find(kRoot) == 0; +} + GURL GetFileSystemRootURI(const GURL& origin_url, FileSystemType type) { // origin_url is based on a security origin, so http://foo.com or file:/// // instead of the corresponding filesystem URL. diff --git a/webkit/fileapi/file_system_util.h b/webkit/fileapi/file_system_util.h index be863bf..6ba6dee 100644 --- a/webkit/fileapi/file_system_util.h +++ b/webkit/fileapi/file_system_util.h @@ -27,9 +27,13 @@ extern const char kTestDir[]; class WEBKIT_STORAGE_EXPORT VirtualPath { public: - // Use this instead of base::FilePath::BaseName when operating on virtual paths. - // base::FilePath::BaseName will get confused by ':' on Windows when it looks like a - // drive letter separator; this will treat it as just another character. + static const FilePath::CharType kRoot[]; + static const FilePath::CharType kSeparator; + + // Use this instead of base::FilePath::BaseName when operating on virtual + // paths. base::FilePath::BaseName will get confused by ':' on Windows when it + // looks like a drive letter separator; this will treat it as just another + // character. static base::FilePath BaseName(const base::FilePath& virtual_path); // Likewise, use this instead of base::FilePath::GetComponents when operating on @@ -38,6 +42,13 @@ class WEBKIT_STORAGE_EXPORT VirtualPath { // not evaluate '.' or '..' components. static void GetComponents(const base::FilePath& path, std::vector<base::FilePath::StringType>* components); + + // Returns a path name ensuring that it begins with kRoot and all path + // separators are forward slashes /. + static FilePath::StringType GetNormalizedFilePath(const FilePath& path); + + // Returns true if the given path begins with kRoot. + static bool IsAbsolute(const FilePath::StringType& path); }; // Returns the root URI of the filesystem that can be specified by a pair of diff --git a/webkit/fileapi/file_system_util_unittest.cc b/webkit/fileapi/file_system_util_unittest.cc index 0fd4f0a..3bf54fb 100644 --- a/webkit/fileapi/file_system_util_unittest.cc +++ b/webkit/fileapi/file_system_util_unittest.cc @@ -54,6 +54,31 @@ TEST_F(FileSystemUtilTest, VirtualPathBaseName) { } } +TEST_F(FileSystemUtilTest, GetNormalizedFilePath) { + struct test_data { + const FilePath::StringType path; + const FilePath::StringType normalized_path; + } test_cases[] = { + { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("/") }, + { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") }, + { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("/foo/bar") }, + { FILE_PATH_LITERAL("/foo/bar"), FILE_PATH_LITERAL("/foo/bar") } + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { + FilePath input = FilePath(test_cases[i].path); + FilePath::StringType normalized_path_string = + VirtualPath::GetNormalizedFilePath(input); + EXPECT_EQ(test_cases[i].normalized_path, normalized_path_string); + } +} + +TEST_F(FileSystemUtilTest, IsAbsolutePath) { + EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/"))); + EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/foo/bar"))); + EXPECT_FALSE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL(""))); + EXPECT_FALSE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("foo/bar"))); +} + TEST_F(FileSystemUtilTest, VirtualPathGetComponents) { struct test_data { const base::FilePath::StringType path; |