1 // Copyright 2013 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 CHROME_BROWSER_CHROMEOS_FILEAPI_FILE_SYSTEM_BACKEND_H_ 6 #define CHROME_BROWSER_CHROMEOS_FILEAPI_FILE_SYSTEM_BACKEND_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/compiler_specific.h" 13 #include "base/files/file_path.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/synchronization/lock.h" 16 #include "webkit/browser/fileapi/file_system_backend.h" 17 #include "webkit/browser/quota/special_storage_policy.h" 18 #include "webkit/browser/webkit_storage_browser_export.h" 19 #include "webkit/common/fileapi/file_system_types.h" 20 21 namespace fileapi { 22 class AsyncFileUtilAdapter; 23 class CopyOrMoveFileValidatorFactory; 24 class ExternalMountPoints; 25 class FileSystemFileUtil; 26 class FileSystemURL; 27 class IsolatedContext; 28 } 29 30 namespace chromeos { 31 32 class FileSystemBackendDelegate; 33 class FileAccessPermissions; 34 35 // FileSystemBackend is a Chrome OS specific implementation of 36 // ExternalFileSystemBackend. This class is responsible for a 37 // number of things, including: 38 // 39 // - Add system mount points 40 // - Grant/revoke/check file access permissions 41 // - Create FileSystemOperation per file system type 42 // - Create FileStreamReader/Writer per file system type 43 // 44 // Chrome OS specific mount points: 45 // 46 // "Downloads" is a mount point for user's Downloads directory on the local 47 // disk, where downloaded files are stored by default. 48 // 49 // "archive" is a mount point for an archive file, such as a zip file. This 50 // mount point exposes contents of an archive file via cros_disks and AVFS 51 // <http://avf.sourceforge.net/>. 52 // 53 // "removable" is a mount point for removable media such as an SD card. 54 // Insertion and removal of removable media are handled by cros_disks. 55 // 56 // "oem" is a read-only mount point for a directory containing OEM data. 57 // 58 // "drive" is a mount point for Google Drive. Drive is integrated with the 59 // FileSystem API layer via drive::FileSystemProxy. This mount point is added 60 // by drive::DriveIntegrationService. 61 // 62 // These mount points are placed under the "external" namespace, and file 63 // system URLs for these mount points look like: 64 // 65 // filesystem:<origin>/external/<mount_name>/... 66 // 67 class FileSystemBackend : public fileapi::ExternalFileSystemBackend { 68 public: 69 using fileapi::FileSystemBackend::OpenFileSystemCallback; 70 71 // FileSystemBackend will take an ownership of a |mount_points| 72 // reference. On the other hand, |system_mount_points| will be kept as a raw 73 // pointer and it should outlive FileSystemBackend instance. 74 // The ownership of |drive_delegate| is also taken. 75 FileSystemBackend( 76 FileSystemBackendDelegate* drive_delegate, 77 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy, 78 scoped_refptr<fileapi::ExternalMountPoints> mount_points, 79 fileapi::ExternalMountPoints* system_mount_points); 80 virtual ~FileSystemBackend(); 81 82 // Adds system mount points, such as "archive", and "removable". This 83 // function is no-op if these mount points are already present. 84 void AddSystemMountPoints(); 85 86 // Returns true if CrosMountpointProvider can handle |url|, i.e. its 87 // file system type matches with what this provider supports. 88 // This could be called on any threads. 89 static bool CanHandleURL(const fileapi::FileSystemURL& url); 90 91 // fileapi::FileSystemBackend overrides. 92 virtual bool CanHandleType(fileapi::FileSystemType type) const OVERRIDE; 93 virtual void Initialize(fileapi::FileSystemContext* context) OVERRIDE; 94 virtual void OpenFileSystem( 95 const GURL& origin_url, 96 fileapi::FileSystemType type, 97 fileapi::OpenFileSystemMode mode, 98 const OpenFileSystemCallback& callback) OVERRIDE; 99 virtual fileapi::FileSystemFileUtil* GetFileUtil( 100 fileapi::FileSystemType type) OVERRIDE; 101 virtual fileapi::AsyncFileUtil* GetAsyncFileUtil( 102 fileapi::FileSystemType type) OVERRIDE; 103 virtual fileapi::CopyOrMoveFileValidatorFactory* 104 GetCopyOrMoveFileValidatorFactory( 105 fileapi::FileSystemType type, 106 base::PlatformFileError* error_code) OVERRIDE; 107 virtual fileapi::FileSystemOperation* CreateFileSystemOperation( 108 const fileapi::FileSystemURL& url, 109 fileapi::FileSystemContext* context, 110 base::PlatformFileError* error_code) const OVERRIDE; 111 virtual scoped_ptr<webkit_blob::FileStreamReader> CreateFileStreamReader( 112 const fileapi::FileSystemURL& path, 113 int64 offset, 114 const base::Time& expected_modification_time, 115 fileapi::FileSystemContext* context) const OVERRIDE; 116 virtual scoped_ptr<fileapi::FileStreamWriter> CreateFileStreamWriter( 117 const fileapi::FileSystemURL& url, 118 int64 offset, 119 fileapi::FileSystemContext* context) const OVERRIDE; 120 virtual fileapi::FileSystemQuotaUtil* GetQuotaUtil() OVERRIDE; 121 122 // fileapi::ExternalFileSystemBackend overrides. 123 virtual bool IsAccessAllowed(const fileapi::FileSystemURL& url) 124 const OVERRIDE; 125 virtual std::vector<base::FilePath> GetRootDirectories() const OVERRIDE; 126 virtual void GrantFullAccessToExtension( 127 const std::string& extension_id) OVERRIDE; 128 virtual void GrantFileAccessToExtension( 129 const std::string& extension_id, 130 const base::FilePath& virtual_path) OVERRIDE; 131 virtual void RevokeAccessForExtension( 132 const std::string& extension_id) OVERRIDE; 133 virtual bool GetVirtualPath(const base::FilePath& filesystem_path, 134 base::FilePath* virtual_path) OVERRIDE; 135 136 private: 137 base::FilePath GetFileSystemRootPath(const fileapi::FileSystemURL& url) const; 138 139 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; 140 scoped_ptr<FileAccessPermissions> file_access_permissions_; 141 scoped_ptr<fileapi::AsyncFileUtilAdapter> local_file_util_; 142 143 // The Delegate instance for the drive file system related operation. 144 scoped_ptr<FileSystemBackendDelegate> drive_delegate_; 145 146 // Mount points specific to the owning context (i.e. per-profile mount 147 // points). 148 // 149 // It is legal to have mount points with the same name as in 150 // system_mount_points_. Also, mount point paths may overlap with mount point 151 // paths in system_mount_points_. In both cases mount points in 152 // |mount_points_| will have a priority. 153 // E.g. if |mount_points_| map 'foo1' to '/foo/foo1' and 154 // |file_system_mount_points_| map 'xxx' to '/foo/foo1/xxx', |GetVirtualPaths| 155 // will resolve '/foo/foo1/xxx/yyy' as 'foo1/xxx/yyy' (i.e. the mapping from 156 // |mount_points_| will be used). 157 scoped_refptr<fileapi::ExternalMountPoints> mount_points_; 158 159 // Globally visible mount points. System MountPonts instance should outlive 160 // all FileSystemBackend instances, so raw pointer is safe. 161 fileapi::ExternalMountPoints* system_mount_points_; 162 163 DISALLOW_COPY_AND_ASSIGN(FileSystemBackend); 164 }; 165 166 } // namespace chromeos 167 168 #endif // CHROME_BROWSER_CHROMEOS_FILEAPI_FILE_SYSTEM_BACKEND_H_ 169