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