summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcalvinlo@chromium.org <calvinlo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-05 12:12:27 +0000
committercalvinlo@chromium.org <calvinlo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-05 12:12:27 +0000
commit96bf58a48f35aacd2dc50719a163ae875eda45a0 (patch)
tree78468861e9534f919fa24e497ea2d42e9f5236a2
parent985adebe8e754b359c3a6f4b802996d6f0265249 (diff)
downloadchromium_src-96bf58a48f35aacd2dc50719a163ae875eda45a0.zip
chromium_src-96bf58a48f35aacd2dc50719a163ae875eda45a0.tar.gz
chromium_src-96bf58a48f35aacd2dc50719a163ae875eda45a0.tar.bz2
Add Utility functions to Normalize and check for Absolute Virtual Paths.
BUG=173627 TEST= content_unittests FileSystemUtilTest.GetNormalizedFilePath content_unittests FileSystemUtilTest.IsAbsolutePath Review URL: https://chromiumcodereview.appspot.com/12088105 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@180688 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--webkit/fileapi/file_system_util.cc20
-rw-r--r--webkit/fileapi/file_system_util.h17
-rw-r--r--webkit/fileapi/file_system_util_unittest.cc25
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..cb12822 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 = ARRAYSIZE_UNSAFE(
+ static_cast<const FilePath::CharType*>(FilePath::kSeparators));
+ 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;