15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef WEBKIT_COMMON_FILEAPI_FILE_SYSTEM_UTIL_H_
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define WEBKIT_COMMON_FILEAPI_FILE_SYSTEM_UTIL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/platform_file.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebFileError.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebFileSystemType.h"
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/common/fileapi/file_system_types.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "webkit/common/quota/quota_types.h"
177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "webkit/common/webkit_storage_common_export.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GURL;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace fileapi {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT extern const char kPersistentDir[];
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT extern const char kTemporaryDir[];
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT extern const char kExternalDir[];
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT extern const char kIsolatedDir[];
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT extern const char kTestDir[];
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class WEBKIT_STORAGE_COMMON_EXPORT VirtualPath {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const base::FilePath::CharType kRoot[];
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const base::FilePath::CharType kSeparator;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Use this instead of base::FilePath::BaseName when operating on virtual
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // paths. FilePath::BaseName will get confused by ':' on Windows when it
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // looks like a drive letter separator; this will treat it as just another
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // character.
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static base::FilePath BaseName(const base::FilePath& virtual_path);
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Use this instead of base::FilePath::DirName when operating on virtual
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // paths.
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static base::FilePath DirName(const base::FilePath& virtual_path);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Likewise, use this instead of base::FilePath::GetComponents when
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // operating on virtual paths.
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note that this assumes very clean input, with no leading slash, and
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it will not evaluate '..' components.
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void GetComponents(
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath& path,
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::vector<base::FilePath::StringType>* components);
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static void GetComponentsUTF8Unsafe(
537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const base::FilePath& path, std::vector<std::string>* components);
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a path name ensuring that it begins with kRoot and all path
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // separators are forward slashes /.
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static base::FilePath::StringType GetNormalizedFilePath(
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath& path);
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the given path begins with kRoot.
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static bool IsAbsolute(const base::FilePath::StringType& path);
62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Returns true if the given path points to the root.
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  static bool IsRootPath(const base::FilePath& path);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the root URI of the filesystem that can be specified by a pair of
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |origin_url| and |type|.  The returned URI can be used as a root path
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the filesystem (e.g. <returned_URI> + "/relative/path" will compose
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a path pointing to the entry "/relative/path" in the filesystem).
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For Isolated filesystem this returns the 'common' root part, e.g.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// returns URL without the filesystem ID.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |type| needs to be public type as the returned URI is given to the renderer.
767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT GURL GetFileSystemRootURI(const GURL& origin_url,
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         FileSystemType type);
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the name for the filesystem that is specified by a pair of
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |origin_url| and |type|.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (The name itself is neither really significant nor a formal identifier
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// but can be read as the .name field of the returned FileSystem object
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as a user-friendly name in the javascript layer).
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |type| needs to be public type as the returned name is given to the renderer.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example:
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   The name for a TEMPORARY filesystem of "http://www.example.com:80/"
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   should look like: "http_www.example.host_80:temporary"
907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT std::string
917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)GetFileSystemName(const GURL& origin_url, FileSystemType type);
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Converts FileSystemType |type| to/from the StorageType |storage_type| that
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is used for the unified quota system.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (Basically this naively maps TEMPORARY storage type to TEMPORARY filesystem
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// type, PERSISTENT storage type to PERSISTENT filesystem type and vice versa.)
977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT FileSystemType
987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)QuotaStorageTypeToFileSystemType(quota::StorageType storage_type);
997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT quota::StorageType
1017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)FileSystemTypeToQuotaStorageType(FileSystemType type);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the string representation of the given filesystem |type|.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns an empty string if the |type| is invalid.
1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT std::string
1067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)GetFileSystemTypeString(FileSystemType type);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Sets type to FileSystemType enum that corresponds to the string name.
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns false if the |type_string| is invalid.
1107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT bool GetFileSystemPublicType(
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::string type_string,
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    WebKit::WebFileSystemType* type);
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Encodes |file_path| to a string.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Following conditions should be held:
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  - StringToFilePath(FilePathToString(path)) == path
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  - StringToFilePath(FilePathToString(path) + "/" + "SubDirectory") ==
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    path.AppendASCII("SubDirectory");
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(tzik): Replace CreateFilePath and FilePathToString in
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// third_party/leveldatabase/env_chromium.cc with them.
1227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT std::string FilePathToString(
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const base::FilePath& file_path);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Decode a file path from |file_path_string|.
1267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT base::FilePath StringToFilePath(
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& file_path_string);
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// File error conversion
1307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT WebKit::WebFileError
1317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)PlatformFileErrorToWebFileError(base::PlatformFileError error_code);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Generate a file system name for the given arguments. Should only be used by
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platform apps.
1357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT std::string GetIsolatedFileSystemName(
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& origin_url,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& filesystem_id);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Find the file system id from |filesystem_name|. Should only be used by
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platform apps. This function will return false if the file system name is
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not of the form {origin}:Isolated_{id}, and will also check that there is an
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// origin and id present. It will not check that the origin or id are valid.
1437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT bool CrackIsolatedFileSystemName(
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& filesystem_name,
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* filesystem_id);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns the root URI for an isolated filesystem for origin |origin_url|
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// and |filesystem_id|. If the |optional_root_name| is given the resulting
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// root URI will point to the subfolder within the isolated filesystem.
1507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT std::string GetIsolatedFileSystemRootURIString(
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& origin_url,
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& filesystem_id,
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& optional_root_name);
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns the root URI for an external filesystem for origin |origin_url|
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// and |mount_name|.
1577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WEBKIT_STORAGE_COMMON_EXPORT std::string GetExternalFileSystemRootURIString(
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const GURL& origin_url,
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const std::string& mount_name);
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace fileapi
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif  // WEBKIT_COMMON_FILEAPI_FILE_SYSTEM_UTIL_H_
164