Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Yeunjoo Choi | ef8a4a4 | 2022-11-08 04:21:05 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_ASH_FILEAPI_FILE_SYSTEM_BACKEND_H_ |
| 6 | #define CHROME_BROWSER_ASH_FILEAPI_FILE_SYSTEM_BACKEND_H_ |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 7 | |
avi | 8a07d5389 | 2015-12-24 22:13:53 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 11 | #include <string> |
[email protected] | b777b33 | 2011-04-16 04:01:08 | [diff] [blame] | 12 | #include <vector> |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 13 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
Arthur Sonzogni | 3939693 | 2023-04-24 09:41:33 | [diff] [blame] | 15 | #include "base/memory/raw_ptr.h" |
mtomasz | a6775be | 2015-03-18 06:45:27 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
David Black | 8f8c1eb | 2020-12-09 04:02:23 | [diff] [blame] | 17 | #include "components/account_id/account_id.h" |
Daniel Brinkers | 9dacf9e1 | 2023-03-09 07:39:05 | [diff] [blame] | 18 | #include "components/file_access/scoped_file_access_delegate.h" |
DongJun Kim | febb3c2 | 2019-10-21 02:08:06 | [diff] [blame] | 19 | #include "storage/browser/file_system/file_system_backend.h" |
| 20 | #include "storage/browser/file_system/task_runner_bound_observer_list.h" |
DongJun Kim | d6930ea | 2019-10-24 08:49:25 | [diff] [blame] | 21 | #include "storage/common/file_system/file_system_types.h" |
Bo Majewski | 762d913a | 2021-06-30 03:54:25 | [diff] [blame] | 22 | #include "url/origin.h" |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 23 | |
David Black | 8f8c1eb | 2020-12-09 04:02:23 | [diff] [blame] | 24 | class Profile; |
| 25 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 26 | namespace storage { |
[email protected] | 98407ee | 2013-04-04 08:52:17 | [diff] [blame] | 27 | class CopyOrMoveFileValidatorFactory; |
[email protected] | 9d5d698 | 2013-01-18 03:12:54 | [diff] [blame] | 28 | class ExternalMountPoints; |
[email protected] | 199263e | 2012-12-14 07:14:20 | [diff] [blame] | 29 | class FileSystemURL; |
mtomasz | b75244fd | 2014-08-28 05:37:34 | [diff] [blame] | 30 | class WatcherManager; |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 31 | } // namespace storage |
[email protected] | ccb14ed | 2011-07-06 10:50:50 | [diff] [blame] | 32 | |
Yeunjoo Choi | 3d9ed38a | 2022-11-10 02:51:24 | [diff] [blame] | 33 | namespace ash { |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 34 | |
[email protected] | f19bbf6 | 2013-07-09 01:22:32 | [diff] [blame] | 35 | class FileSystemBackendDelegate; |
[email protected] | b777b33 | 2011-04-16 04:01:08 | [diff] [blame] | 36 | class FileAccessPermissions; |
| 37 | |
Lei Zhang | 37d2e451 | 2025-05-06 19:12:01 | [diff] [blame] | 38 | inline constexpr char kSystemMountNameArchive[] = "archive"; |
| 39 | inline constexpr char kSystemMountNameRemovable[] = "removable"; |
Joel Hockey | 09ff61be | 2018-11-01 05:54:36 | [diff] [blame] | 40 | |
Joel Hockey | 9d202494 | 2023-08-09 03:23:56 | [diff] [blame] | 41 | // Backend Function called. Used to control access. |
| 42 | enum class BackendFunction { |
| 43 | kCreateFileSystemOperation, |
| 44 | kCreateFileStreamReader, |
| 45 | kCreateFileStreamWriter, |
Joel Hockey | 9d202494 | 2023-08-09 03:23:56 | [diff] [blame] | 46 | }; |
| 47 | |
[email protected] | f19bbf6 | 2013-07-09 01:22:32 | [diff] [blame] | 48 | // FileSystemBackend is a Chrome OS specific implementation of |
| 49 | // ExternalFileSystemBackend. This class is responsible for a |
[email protected] | 2f87443 | 2013-06-03 04:41:29 | [diff] [blame] | 50 | // number of things, including: |
| 51 | // |
[email protected] | 3c3621b | 2013-06-07 06:12:34 | [diff] [blame] | 52 | // - Add system mount points |
[email protected] | 2f87443 | 2013-06-03 04:41:29 | [diff] [blame] | 53 | // - Grant/revoke/check file access permissions |
| 54 | // - Create FileSystemOperation per file system type |
| 55 | // - Create FileStreamReader/Writer per file system type |
| 56 | // |
Joel Hockey | a9e2d2ed | 2023-08-01 00:34:12 | [diff] [blame] | 57 | // Chrome OS specific static mount points: |
[email protected] | 2f87443 | 2013-06-03 04:41:29 | [diff] [blame] | 58 | // |
| 59 | // "archive" is a mount point for an archive file, such as a zip file. This |
| 60 | // mount point exposes contents of an archive file via cros_disks and AVFS |
| 61 | // <http://avf.sourceforge.net/>. |
| 62 | // |
| 63 | // "removable" is a mount point for removable media such as an SD card. |
| 64 | // Insertion and removal of removable media are handled by cros_disks. |
| 65 | // |
[email protected] | 2f87443 | 2013-06-03 04:41:29 | [diff] [blame] | 66 | // These mount points are placed under the "external" namespace, and file |
| 67 | // system URLs for these mount points look like: |
| 68 | // |
| 69 | // filesystem:<origin>/external/<mount_name>/... |
| 70 | // |
Joel Hockey | a9e2d2ed | 2023-08-01 00:34:12 | [diff] [blame] | 71 | // Other mounts are also registered by VolumeManager for MyFiles, Drive, VMs |
| 72 | // (crostini, arc, etc), Android Document Providers, fileSystemProviders, etc. |
Joel Hockey | d096e54 | 2023-08-07 06:01:24 | [diff] [blame] | 73 | class FileSystemBackend : public storage::FileSystemBackend { |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 74 | public: |
Nathan Eliason | bca1174c | 2022-05-12 16:00:57 | [diff] [blame] | 75 | using storage::FileSystemBackend::ResolveURLCallback; |
[email protected] | d5e0855 | 2012-08-02 21:43:40 | [diff] [blame] | 76 | |
hashimoto | a53e7e8 | 2016-10-26 06:30:47 | [diff] [blame] | 77 | // |system_mount_points| should outlive FileSystemBackend instance. |
[email protected] | f19bbf6 | 2013-07-09 01:22:32 | [diff] [blame] | 78 | FileSystemBackend( |
David Black | 8f8c1eb | 2020-12-09 04:02:23 | [diff] [blame] | 79 | Profile* profile, |
hashimoto | a53e7e8 | 2016-10-26 06:30:47 | [diff] [blame] | 80 | std::unique_ptr<FileSystemBackendDelegate> file_system_provider_delegate, |
| 81 | std::unique_ptr<FileSystemBackendDelegate> mtp_delegate, |
| 82 | std::unique_ptr<FileSystemBackendDelegate> arc_content_delegate, |
nya | f5df1e3 | 2016-12-14 04:36:17 | [diff] [blame] | 83 | std::unique_ptr<FileSystemBackendDelegate> |
| 84 | arc_documents_provider_delegate, |
Sam McNally | d8b7d82 | 2018-08-21 03:18:18 | [diff] [blame] | 85 | std::unique_ptr<FileSystemBackendDelegate> drivefs_delegate, |
Anand K. Mistry | 7694c36 | 2020-03-17 23:33:34 | [diff] [blame] | 86 | std::unique_ptr<FileSystemBackendDelegate> smbfs_delegate, |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 87 | scoped_refptr<storage::ExternalMountPoints> mount_points, |
| 88 | storage::ExternalMountPoints* system_mount_points); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 89 | |
| 90 | FileSystemBackend(const FileSystemBackend&) = delete; |
| 91 | FileSystemBackend& operator=(const FileSystemBackend&) = delete; |
| 92 | |
dcheng | ac231f29 | 2015-01-20 19:56:17 | [diff] [blame] | 93 | ~FileSystemBackend() override; |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 94 | |
Joel Hockey | d096e54 | 2023-08-07 06:01:24 | [diff] [blame] | 95 | // Gets the ChromeOS FileSystemBackend. |
| 96 | static FileSystemBackend* Get(const storage::FileSystemContext& context); |
| 97 | |
[email protected] | 3c3621b | 2013-06-07 06:12:34 | [diff] [blame] | 98 | // Adds system mount points, such as "archive", and "removable". This |
| 99 | // function is no-op if these mount points are already present. |
| 100 | void AddSystemMountPoints(); |
| 101 | |
[email protected] | 9fb9294a | 2012-08-21 14:55:37 | [diff] [blame] | 102 | // Returns true if CrosMountpointProvider can handle |url|, i.e. its |
| 103 | // file system type matches with what this provider supports. |
| 104 | // This could be called on any threads. |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 105 | static bool CanHandleURL(const storage::FileSystemURL& url); |
[email protected] | 9fb9294a | 2012-08-21 14:55:37 | [diff] [blame] | 106 | |
Joel Hockey | d096e54 | 2023-08-07 06:01:24 | [diff] [blame] | 107 | // Returns true if |url| is allowed to be accessed. |
| 108 | // This is supposed to perform ExternalFileSystem-specific security |
| 109 | // checks. |
Joel Hockey | 9d202494 | 2023-08-09 03:23:56 | [diff] [blame] | 110 | bool IsAccessAllowed(BackendFunction backend_function, |
| 111 | storage::OperationType operation_type, |
| 112 | const storage::FileSystemURL& url) const; |
Joel Hockey | d096e54 | 2023-08-07 06:01:24 | [diff] [blame] | 113 | |
| 114 | // Returns the list of top level directories that are exposed by this |
| 115 | // provider. This list is used to set appropriate child process file access |
| 116 | // permissions. |
| 117 | std::vector<base::FilePath> GetRootDirectories() const; |
| 118 | |
| 119 | // Grants access to |virtual_path| from |origin| URL. |
| 120 | void GrantFileAccessToOrigin(const url::Origin& origin, |
| 121 | const base::FilePath& virtual_path); |
| 122 | |
| 123 | // Revokes file access from origin identified with |origin|. |
| 124 | void RevokeAccessForOrigin(const url::Origin& origin); |
| 125 | |
| 126 | // Gets virtual path by known filesystem path. Returns false when filesystem |
| 127 | // path is not exposed by this provider. |
| 128 | bool GetVirtualPath(const base::FilePath& file_system_path, |
| 129 | base::FilePath* virtual_path) const; |
| 130 | |
Joel Hockey | d096e54 | 2023-08-07 06:01:24 | [diff] [blame] | 131 | // Creates an internal File System URL for performing internal operations such |
| 132 | // as confirming if a file or a directory exist before granting the final |
| 133 | // permission to the entry. The path must be an absolute path. |
| 134 | storage::FileSystemURL CreateInternalURL( |
| 135 | storage::FileSystemContext* context, |
| 136 | const base::FilePath& entry_path) const; |
| 137 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 138 | // storage::FileSystemBackend overrides. |
dcheng | ac231f29 | 2015-01-20 19:56:17 | [diff] [blame] | 139 | bool CanHandleType(storage::FileSystemType type) const override; |
| 140 | void Initialize(storage::FileSystemContext* context) override; |
| 141 | void ResolveURL(const storage::FileSystemURL& url, |
| 142 | storage::OpenFileSystemMode mode, |
Nathan Eliason | bca1174c | 2022-05-12 16:00:57 | [diff] [blame] | 143 | ResolveURLCallback callback) override; |
dcheng | ac231f29 | 2015-01-20 19:56:17 | [diff] [blame] | 144 | storage::AsyncFileUtil* GetAsyncFileUtil( |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 145 | storage::FileSystemType type) override; |
dcheng | ac231f29 | 2015-01-20 19:56:17 | [diff] [blame] | 146 | storage::WatcherManager* GetWatcherManager( |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 147 | storage::FileSystemType type) override; |
dcheng | ac231f29 | 2015-01-20 19:56:17 | [diff] [blame] | 148 | storage::CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory( |
| 149 | storage::FileSystemType type, |
| 150 | base::File::Error* error_code) override; |
Victor Costan | c49feb7 | 2021-08-16 19:19:20 | [diff] [blame] | 151 | std::unique_ptr<storage::FileSystemOperation> CreateFileSystemOperation( |
Joel Hockey | 75a1058 | 2023-08-09 02:58:29 | [diff] [blame] | 152 | storage::OperationType type, |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 153 | const storage::FileSystemURL& url, |
| 154 | storage::FileSystemContext* context, |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 155 | base::File::Error* error_code) const override; |
dcheng | ac231f29 | 2015-01-20 19:56:17 | [diff] [blame] | 156 | bool SupportsStreaming(const storage::FileSystemURL& url) const override; |
| 157 | bool HasInplaceCopyImplementation( |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 158 | storage::FileSystemType type) const override; |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 159 | std::unique_ptr<storage::FileStreamReader> CreateFileStreamReader( |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 160 | const storage::FileSystemURL& path, |
avi | 8a07d5389 | 2015-12-24 22:13:53 | [diff] [blame] | 161 | int64_t offset, |
| 162 | int64_t max_bytes_to_read, |
[email protected] | a105783 | 2012-10-15 13:28:06 | [diff] [blame] | 163 | const base::Time& expected_modification_time, |
Daniel Brinkers | 9dacf9e1 | 2023-03-09 07:39:05 | [diff] [blame] | 164 | storage::FileSystemContext* context, |
| 165 | file_access::ScopedFileAccessDelegate::RequestFilesAccessIOCallback |
| 166 | file_access) const override; |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 167 | std::unique_ptr<storage::FileStreamWriter> CreateFileStreamWriter( |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 168 | const storage::FileSystemURL& url, |
avi | 8a07d5389 | 2015-12-24 22:13:53 | [diff] [blame] | 169 | int64_t offset, |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 170 | storage::FileSystemContext* context) const override; |
dcheng | ac231f29 | 2015-01-20 19:56:17 | [diff] [blame] | 171 | storage::FileSystemQuotaUtil* GetQuotaUtil() override; |
| 172 | const storage::UpdateObserverList* GetUpdateObservers( |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 173 | storage::FileSystemType type) const override; |
dcheng | ac231f29 | 2015-01-20 19:56:17 | [diff] [blame] | 174 | const storage::ChangeObserverList* GetChangeObservers( |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 175 | storage::FileSystemType type) const override; |
dcheng | ac231f29 | 2015-01-20 19:56:17 | [diff] [blame] | 176 | const storage::AccessObserverList* GetAccessObservers( |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 177 | storage::FileSystemType type) const override; |
[email protected] | b777b33 | 2011-04-16 04:01:08 | [diff] [blame] | 178 | |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 179 | private: |
David Black | 8f8c1eb | 2020-12-09 04:02:23 | [diff] [blame] | 180 | const AccountId account_id_; |
| 181 | |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 182 | std::unique_ptr<FileAccessPermissions> file_access_permissions_; |
| 183 | std::unique_ptr<storage::AsyncFileUtil> local_file_util_; |
[email protected] | 9d5d698 | 2013-01-18 03:12:54 | [diff] [blame] | 184 | |
[email protected] | 43420a1 | 2014-04-21 03:20:48 | [diff] [blame] | 185 | // The delegate instance for the provided file system related operations. |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 186 | std::unique_ptr<FileSystemBackendDelegate> file_system_provider_delegate_; |
[email protected] | 43420a1 | 2014-04-21 03:20:48 | [diff] [blame] | 187 | |
[email protected] | bee0df31 | 2014-04-28 06:59:58 | [diff] [blame] | 188 | // The delegate instance for the MTP file system related operations. |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 189 | std::unique_ptr<FileSystemBackendDelegate> mtp_delegate_; |
[email protected] | bee0df31 | 2014-04-28 06:59:58 | [diff] [blame] | 190 | |
hashimoto | a53e7e8 | 2016-10-26 06:30:47 | [diff] [blame] | 191 | // The delegate instance for the ARC content file system related operations. |
| 192 | std::unique_ptr<FileSystemBackendDelegate> arc_content_delegate_; |
| 193 | |
nya | f5df1e3 | 2016-12-14 04:36:17 | [diff] [blame] | 194 | // The delegate instance for the ARC documents provider related operations. |
| 195 | std::unique_ptr<FileSystemBackendDelegate> arc_documents_provider_delegate_; |
| 196 | |
Sam McNally | d8b7d82 | 2018-08-21 03:18:18 | [diff] [blame] | 197 | // The delegate instance for the DriveFS file system related operations. |
| 198 | std::unique_ptr<FileSystemBackendDelegate> drivefs_delegate_; |
| 199 | |
Anand K. Mistry | 7694c36 | 2020-03-17 23:33:34 | [diff] [blame] | 200 | // The delegate instance for the SmbFs file system related operations. |
| 201 | std::unique_ptr<FileSystemBackendDelegate> smbfs_delegate_; |
| 202 | |
[email protected] | 3c3621b | 2013-06-07 06:12:34 | [diff] [blame] | 203 | // Mount points specific to the owning context (i.e. per-profile mount |
| 204 | // points). |
[email protected] | 9d5d698 | 2013-01-18 03:12:54 | [diff] [blame] | 205 | // |
| 206 | // It is legal to have mount points with the same name as in |
| 207 | // system_mount_points_. Also, mount point paths may overlap with mount point |
| 208 | // paths in system_mount_points_. In both cases mount points in |
| 209 | // |mount_points_| will have a priority. |
| 210 | // E.g. if |mount_points_| map 'foo1' to '/foo/foo1' and |
| 211 | // |file_system_mount_points_| map 'xxx' to '/foo/foo1/xxx', |GetVirtualPaths| |
| 212 | // will resolve '/foo/foo1/xxx/yyy' as 'foo1/xxx/yyy' (i.e. the mapping from |
| 213 | // |mount_points_| will be used). |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 214 | scoped_refptr<storage::ExternalMountPoints> mount_points_; |
[email protected] | 9d5d698 | 2013-01-18 03:12:54 | [diff] [blame] | 215 | |
| 216 | // Globally visible mount points. System MountPonts instance should outlive |
[email protected] | f19bbf6 | 2013-07-09 01:22:32 | [diff] [blame] | 217 | // all FileSystemBackend instances, so raw pointer is safe. |
Bartek Nowierski | 8f57934 | 2024-01-08 06:45:15 | [diff] [blame] | 218 | raw_ptr<storage::ExternalMountPoints> system_mount_points_; |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 219 | }; |
| 220 | |
Yeunjoo Choi | 3d9ed38a | 2022-11-10 02:51:24 | [diff] [blame] | 221 | } // namespace ash |
[email protected] | 55d9bed | 2011-03-25 20:37:59 | [diff] [blame] | 222 | |
Yeunjoo Choi | ef8a4a4 | 2022-11-08 04:21:05 | [diff] [blame] | 223 | #endif // CHROME_BROWSER_ASH_FILEAPI_FILE_SYSTEM_BACKEND_H_ |