Home | History | Annotate | Download | only in fileapi
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef WEBKIT_COMMON_FILEAPI_FILE_SYSTEM_UTIL_H_
      6 #define WEBKIT_COMMON_FILEAPI_FILE_SYSTEM_UTIL_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/files/file_path.h"
     12 #include "base/platform_file.h"
     13 #include "third_party/WebKit/public/platform/WebFileError.h"
     14 #include "third_party/WebKit/public/platform/WebFileSystemType.h"
     15 #include "webkit/common/fileapi/file_system_info.h"
     16 #include "webkit/common/fileapi/file_system_types.h"
     17 #include "webkit/common/quota/quota_types.h"
     18 #include "webkit/common/webkit_storage_common_export.h"
     19 
     20 class GURL;
     21 
     22 namespace fileapi {
     23 
     24 WEBKIT_STORAGE_COMMON_EXPORT extern const char kPersistentDir[];
     25 WEBKIT_STORAGE_COMMON_EXPORT extern const char kTemporaryDir[];
     26 WEBKIT_STORAGE_COMMON_EXPORT extern const char kExternalDir[];
     27 WEBKIT_STORAGE_COMMON_EXPORT extern const char kIsolatedDir[];
     28 WEBKIT_STORAGE_COMMON_EXPORT extern const char kTestDir[];
     29 
     30 class WEBKIT_STORAGE_COMMON_EXPORT VirtualPath {
     31  public:
     32   static const base::FilePath::CharType kRoot[];
     33   static const base::FilePath::CharType kSeparator;
     34 
     35   // Use this instead of base::FilePath::BaseName when operating on virtual
     36   // paths. FilePath::BaseName will get confused by ':' on Windows when it
     37   // looks like a drive letter separator; this will treat it as just another
     38   // character.
     39   static base::FilePath BaseName(const base::FilePath& virtual_path);
     40 
     41   // Use this instead of base::FilePath::DirName when operating on virtual
     42   // paths.
     43   static base::FilePath DirName(const base::FilePath& virtual_path);
     44 
     45   // Likewise, use this instead of base::FilePath::GetComponents when
     46   // operating on virtual paths.
     47   // Note that this assumes very clean input, with no leading slash, and
     48   // it will not evaluate '..' components.
     49   static void GetComponents(
     50       const base::FilePath& path,
     51       std::vector<base::FilePath::StringType>* components);
     52 
     53   static void GetComponentsUTF8Unsafe(
     54       const base::FilePath& path, std::vector<std::string>* components);
     55 
     56   // Returns a path name ensuring that it begins with kRoot and all path
     57   // separators are forward slashes /.
     58   static base::FilePath::StringType GetNormalizedFilePath(
     59       const base::FilePath& path);
     60 
     61   // Returns true if the given path begins with kRoot.
     62   static bool IsAbsolute(const base::FilePath::StringType& path);
     63 
     64   // Returns true if the given path points to the root.
     65   static bool IsRootPath(const base::FilePath& path);
     66 };
     67 
     68 // Returns the root URI of the filesystem that can be specified by a pair of
     69 // |origin_url| and |type|.  The returned URI can be used as a root path
     70 // of the filesystem (e.g. <returned_URI> + "/relative/path" will compose
     71 // a path pointing to the entry "/relative/path" in the filesystem).
     72 //
     73 // For Isolated filesystem this returns the 'common' root part, e.g.
     74 // returns URL without the filesystem ID.
     75 //
     76 // |type| needs to be public type as the returned URI is given to the renderer.
     77 WEBKIT_STORAGE_COMMON_EXPORT GURL GetFileSystemRootURI(const GURL& origin_url,
     78                                                        FileSystemType type);
     79 
     80 // Returns the name for the filesystem that is specified by a pair of
     81 // |origin_url| and |type|.
     82 // (The name itself is neither really significant nor a formal identifier
     83 // but can be read as the .name field of the returned FileSystem object
     84 // as a user-friendly name in the javascript layer).
     85 //
     86 // |type| needs to be public type as the returned name is given to the renderer.
     87 //
     88 // Example:
     89 //   The name for a TEMPORARY filesystem of "http://www.example.com:80/"
     90 //   should look like: "http_www.example.host_80:temporary"
     91 WEBKIT_STORAGE_COMMON_EXPORT std::string
     92 GetFileSystemName(const GURL& origin_url, FileSystemType type);
     93 
     94 // Converts FileSystemType |type| to/from the StorageType |storage_type| that
     95 // is used for the unified quota system.
     96 // (Basically this naively maps TEMPORARY storage type to TEMPORARY filesystem
     97 // type, PERSISTENT storage type to PERSISTENT filesystem type and vice versa.)
     98 WEBKIT_STORAGE_COMMON_EXPORT FileSystemType
     99 QuotaStorageTypeToFileSystemType(quota::StorageType storage_type);
    100 
    101 WEBKIT_STORAGE_COMMON_EXPORT quota::StorageType
    102 FileSystemTypeToQuotaStorageType(FileSystemType type);
    103 
    104 // Returns the string representation of the given filesystem |type|.
    105 // Returns an empty string if the |type| is invalid.
    106 WEBKIT_STORAGE_COMMON_EXPORT std::string
    107 GetFileSystemTypeString(FileSystemType type);
    108 
    109 // Sets type to FileSystemType enum that corresponds to the string name.
    110 // Returns false if the |type_string| is invalid.
    111 WEBKIT_STORAGE_COMMON_EXPORT bool GetFileSystemPublicType(
    112     std::string type_string,
    113     blink::WebFileSystemType* type);
    114 
    115 // Encodes |file_path| to a string.
    116 // Following conditions should be held:
    117 //  - StringToFilePath(FilePathToString(path)) == path
    118 //  - StringToFilePath(FilePathToString(path) + "/" + "SubDirectory") ==
    119 //    path.AppendASCII("SubDirectory");
    120 //
    121 // TODO(tzik): Replace CreateFilePath and FilePathToString in
    122 // third_party/leveldatabase/env_chromium.cc with them.
    123 WEBKIT_STORAGE_COMMON_EXPORT std::string FilePathToString(
    124     const base::FilePath& file_path);
    125 
    126 // Decode a file path from |file_path_string|.
    127 WEBKIT_STORAGE_COMMON_EXPORT base::FilePath StringToFilePath(
    128     const std::string& file_path_string);
    129 
    130 // File error conversion
    131 WEBKIT_STORAGE_COMMON_EXPORT blink::WebFileError
    132 PlatformFileErrorToWebFileError(base::PlatformFileError error_code);
    133 
    134 // Generate a file system name for the given arguments. Should only be used by
    135 // platform apps.
    136 WEBKIT_STORAGE_COMMON_EXPORT std::string GetIsolatedFileSystemName(
    137     const GURL& origin_url,
    138     const std::string& filesystem_id);
    139 
    140 // Find the file system id from |filesystem_name|. Should only be used by
    141 // platform apps. This function will return false if the file system name is
    142 // not of the form {origin}:Isolated_{id}, and will also check that there is an
    143 // origin and id present. It will not check that the origin or id are valid.
    144 WEBKIT_STORAGE_COMMON_EXPORT bool CrackIsolatedFileSystemName(
    145     const std::string& filesystem_name,
    146     std::string* filesystem_id);
    147 
    148 // Validates the given isolated file system id.
    149 WEBKIT_STORAGE_COMMON_EXPORT bool ValidateIsolatedFileSystemId(
    150     const std::string& filesystem_id);
    151 
    152 // Returns the root URI for an isolated filesystem for origin |origin_url|
    153 // and |filesystem_id|. If the |optional_root_name| is given the resulting
    154 // root URI will point to the subfolder within the isolated filesystem.
    155 WEBKIT_STORAGE_COMMON_EXPORT std::string GetIsolatedFileSystemRootURIString(
    156     const GURL& origin_url,
    157     const std::string& filesystem_id,
    158     const std::string& optional_root_name);
    159 
    160 // Returns the root URI for an external filesystem for origin |origin_url|
    161 // and |mount_name|.
    162 WEBKIT_STORAGE_COMMON_EXPORT std::string GetExternalFileSystemRootURIString(
    163     const GURL& origin_url,
    164     const std::string& mount_name);
    165 
    166 // Translates the net::Error to base::PlatformFileError.
    167 WEBKIT_STORAGE_COMMON_EXPORT base::PlatformFileError
    168 NetErrorToPlatformFileError(int error);
    169 
    170 #if defined(OS_CHROMEOS)
    171 // Returns the filesystem info that can be specified by |origin_url|.
    172 // TODO(nhiroki): This should be deprecated and use
    173 // GetExternalFileSystemRootURIString() to get separate file systems for each
    174 // mount type. http://crbug.com/284963.
    175 WEBKIT_STORAGE_COMMON_EXPORT FileSystemInfo
    176 GetFileSystemInfoForChromeOS(const GURL& origin_url);
    177 #endif
    178 
    179 }  // namespace fileapi
    180 
    181 #endif  // WEBKIT_COMMON_FILEAPI_FILE_SYSTEM_UTIL_H_
    182