blob: 5d1593dd4b62dffbe01b50d54be162c2f08e9ed0 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2013 The Chromium Authors
[email protected]55d9bed2011-03-25 20:37:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Yeunjoo Choief8a4a42022-11-08 04:21:055#ifndef CHROME_BROWSER_ASH_FILEAPI_FILE_SYSTEM_BACKEND_H_
6#define CHROME_BROWSER_ASH_FILEAPI_FILE_SYSTEM_BACKEND_H_
[email protected]55d9bed2011-03-25 20:37:597
avi8a07d53892015-12-24 22:13:538#include <stdint.h>
9
dcheng24002d02016-04-08 02:42:4010#include <memory>
[email protected]55d9bed2011-03-25 20:37:5911#include <string>
[email protected]b777b332011-04-16 04:01:0812#include <vector>
[email protected]55d9bed2011-03-25 20:37:5913
[email protected]57999812013-02-24 05:40:5214#include "base/files/file_path.h"
Arthur Sonzogni39396932023-04-24 09:41:3315#include "base/memory/raw_ptr.h"
mtomasza6775be2015-03-18 06:45:2716#include "base/memory/ref_counted.h"
David Black8f8c1eb2020-12-09 04:02:2317#include "components/account_id/account_id.h"
Daniel Brinkers9dacf9e12023-03-09 07:39:0518#include "components/file_access/scoped_file_access_delegate.h"
DongJun Kimfebb3c22019-10-21 02:08:0619#include "storage/browser/file_system/file_system_backend.h"
20#include "storage/browser/file_system/task_runner_bound_observer_list.h"
DongJun Kimd6930ea2019-10-24 08:49:2521#include "storage/common/file_system/file_system_types.h"
Bo Majewski762d913a2021-06-30 03:54:2522#include "url/origin.h"
[email protected]55d9bed2011-03-25 20:37:5923
David Black8f8c1eb2020-12-09 04:02:2324class Profile;
25
[email protected]cd501a72014-08-22 19:58:3126namespace storage {
[email protected]98407ee2013-04-04 08:52:1727class CopyOrMoveFileValidatorFactory;
[email protected]9d5d6982013-01-18 03:12:5428class ExternalMountPoints;
[email protected]199263e2012-12-14 07:14:2029class FileSystemURL;
mtomaszb75244fd2014-08-28 05:37:3430class WatcherManager;
[email protected]cd501a72014-08-22 19:58:3131} // namespace storage
[email protected]ccb14ed2011-07-06 10:50:5032
Yeunjoo Choi3d9ed38a2022-11-10 02:51:2433namespace ash {
[email protected]55d9bed2011-03-25 20:37:5934
[email protected]f19bbf62013-07-09 01:22:3235class FileSystemBackendDelegate;
[email protected]b777b332011-04-16 04:01:0836class FileAccessPermissions;
37
Lei Zhang37d2e4512025-05-06 19:12:0138inline constexpr char kSystemMountNameArchive[] = "archive";
39inline constexpr char kSystemMountNameRemovable[] = "removable";
Joel Hockey09ff61be2018-11-01 05:54:3640
Joel Hockey9d2024942023-08-09 03:23:5641// Backend Function called. Used to control access.
42enum class BackendFunction {
43 kCreateFileSystemOperation,
44 kCreateFileStreamReader,
45 kCreateFileStreamWriter,
Joel Hockey9d2024942023-08-09 03:23:5646};
47
[email protected]f19bbf62013-07-09 01:22:3248// FileSystemBackend is a Chrome OS specific implementation of
49// ExternalFileSystemBackend. This class is responsible for a
[email protected]2f874432013-06-03 04:41:2950// number of things, including:
51//
[email protected]3c3621b2013-06-07 06:12:3452// - Add system mount points
[email protected]2f874432013-06-03 04:41:2953// - Grant/revoke/check file access permissions
54// - Create FileSystemOperation per file system type
55// - Create FileStreamReader/Writer per file system type
56//
Joel Hockeya9e2d2ed2023-08-01 00:34:1257// Chrome OS specific static mount points:
[email protected]2f874432013-06-03 04:41:2958//
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]2f874432013-06-03 04:41:2966// 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 Hockeya9e2d2ed2023-08-01 00:34:1271// Other mounts are also registered by VolumeManager for MyFiles, Drive, VMs
72// (crostini, arc, etc), Android Document Providers, fileSystemProviders, etc.
Joel Hockeyd096e542023-08-07 06:01:2473class FileSystemBackend : public storage::FileSystemBackend {
[email protected]55d9bed2011-03-25 20:37:5974 public:
Nathan Eliasonbca1174c2022-05-12 16:00:5775 using storage::FileSystemBackend::ResolveURLCallback;
[email protected]d5e08552012-08-02 21:43:4076
hashimotoa53e7e82016-10-26 06:30:4777 // |system_mount_points| should outlive FileSystemBackend instance.
[email protected]f19bbf62013-07-09 01:22:3278 FileSystemBackend(
David Black8f8c1eb2020-12-09 04:02:2379 Profile* profile,
hashimotoa53e7e82016-10-26 06:30:4780 std::unique_ptr<FileSystemBackendDelegate> file_system_provider_delegate,
81 std::unique_ptr<FileSystemBackendDelegate> mtp_delegate,
82 std::unique_ptr<FileSystemBackendDelegate> arc_content_delegate,
nyaf5df1e32016-12-14 04:36:1783 std::unique_ptr<FileSystemBackendDelegate>
84 arc_documents_provider_delegate,
Sam McNallyd8b7d822018-08-21 03:18:1885 std::unique_ptr<FileSystemBackendDelegate> drivefs_delegate,
Anand K. Mistry7694c362020-03-17 23:33:3486 std::unique_ptr<FileSystemBackendDelegate> smbfs_delegate,
[email protected]cd501a72014-08-22 19:58:3187 scoped_refptr<storage::ExternalMountPoints> mount_points,
88 storage::ExternalMountPoints* system_mount_points);
Peter Boström53c6c5952021-09-17 09:41:2689
90 FileSystemBackend(const FileSystemBackend&) = delete;
91 FileSystemBackend& operator=(const FileSystemBackend&) = delete;
92
dchengac231f292015-01-20 19:56:1793 ~FileSystemBackend() override;
[email protected]55d9bed2011-03-25 20:37:5994
Joel Hockeyd096e542023-08-07 06:01:2495 // Gets the ChromeOS FileSystemBackend.
96 static FileSystemBackend* Get(const storage::FileSystemContext& context);
97
[email protected]3c3621b2013-06-07 06:12:3498 // 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]9fb9294a2012-08-21 14:55:37102 // 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]cd501a72014-08-22 19:58:31105 static bool CanHandleURL(const storage::FileSystemURL& url);
[email protected]9fb9294a2012-08-21 14:55:37106
Joel Hockeyd096e542023-08-07 06:01:24107 // Returns true if |url| is allowed to be accessed.
108 // This is supposed to perform ExternalFileSystem-specific security
109 // checks.
Joel Hockey9d2024942023-08-09 03:23:56110 bool IsAccessAllowed(BackendFunction backend_function,
111 storage::OperationType operation_type,
112 const storage::FileSystemURL& url) const;
Joel Hockeyd096e542023-08-07 06:01:24113
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 Hockeyd096e542023-08-07 06:01:24131 // 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]cd501a72014-08-22 19:58:31138 // storage::FileSystemBackend overrides.
dchengac231f292015-01-20 19:56:17139 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 Eliasonbca1174c2022-05-12 16:00:57143 ResolveURLCallback callback) override;
dchengac231f292015-01-20 19:56:17144 storage::AsyncFileUtil* GetAsyncFileUtil(
mostynb9108efb92014-10-03 23:46:15145 storage::FileSystemType type) override;
dchengac231f292015-01-20 19:56:17146 storage::WatcherManager* GetWatcherManager(
mostynb9108efb92014-10-03 23:46:15147 storage::FileSystemType type) override;
dchengac231f292015-01-20 19:56:17148 storage::CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory(
149 storage::FileSystemType type,
150 base::File::Error* error_code) override;
Victor Costanc49feb72021-08-16 19:19:20151 std::unique_ptr<storage::FileSystemOperation> CreateFileSystemOperation(
Joel Hockey75a10582023-08-09 02:58:29152 storage::OperationType type,
[email protected]cd501a72014-08-22 19:58:31153 const storage::FileSystemURL& url,
154 storage::FileSystemContext* context,
mostynb9108efb92014-10-03 23:46:15155 base::File::Error* error_code) const override;
dchengac231f292015-01-20 19:56:17156 bool SupportsStreaming(const storage::FileSystemURL& url) const override;
157 bool HasInplaceCopyImplementation(
mostynb9108efb92014-10-03 23:46:15158 storage::FileSystemType type) const override;
dcheng24002d02016-04-08 02:42:40159 std::unique_ptr<storage::FileStreamReader> CreateFileStreamReader(
[email protected]cd501a72014-08-22 19:58:31160 const storage::FileSystemURL& path,
avi8a07d53892015-12-24 22:13:53161 int64_t offset,
162 int64_t max_bytes_to_read,
[email protected]a1057832012-10-15 13:28:06163 const base::Time& expected_modification_time,
Daniel Brinkers9dacf9e12023-03-09 07:39:05164 storage::FileSystemContext* context,
165 file_access::ScopedFileAccessDelegate::RequestFilesAccessIOCallback
166 file_access) const override;
dcheng24002d02016-04-08 02:42:40167 std::unique_ptr<storage::FileStreamWriter> CreateFileStreamWriter(
[email protected]cd501a72014-08-22 19:58:31168 const storage::FileSystemURL& url,
avi8a07d53892015-12-24 22:13:53169 int64_t offset,
mostynb9108efb92014-10-03 23:46:15170 storage::FileSystemContext* context) const override;
dchengac231f292015-01-20 19:56:17171 storage::FileSystemQuotaUtil* GetQuotaUtil() override;
172 const storage::UpdateObserverList* GetUpdateObservers(
mostynb9108efb92014-10-03 23:46:15173 storage::FileSystemType type) const override;
dchengac231f292015-01-20 19:56:17174 const storage::ChangeObserverList* GetChangeObservers(
mostynb9108efb92014-10-03 23:46:15175 storage::FileSystemType type) const override;
dchengac231f292015-01-20 19:56:17176 const storage::AccessObserverList* GetAccessObservers(
mostynb9108efb92014-10-03 23:46:15177 storage::FileSystemType type) const override;
[email protected]b777b332011-04-16 04:01:08178
[email protected]55d9bed2011-03-25 20:37:59179 private:
David Black8f8c1eb2020-12-09 04:02:23180 const AccountId account_id_;
181
dcheng24002d02016-04-08 02:42:40182 std::unique_ptr<FileAccessPermissions> file_access_permissions_;
183 std::unique_ptr<storage::AsyncFileUtil> local_file_util_;
[email protected]9d5d6982013-01-18 03:12:54184
[email protected]43420a12014-04-21 03:20:48185 // The delegate instance for the provided file system related operations.
dcheng24002d02016-04-08 02:42:40186 std::unique_ptr<FileSystemBackendDelegate> file_system_provider_delegate_;
[email protected]43420a12014-04-21 03:20:48187
[email protected]bee0df312014-04-28 06:59:58188 // The delegate instance for the MTP file system related operations.
dcheng24002d02016-04-08 02:42:40189 std::unique_ptr<FileSystemBackendDelegate> mtp_delegate_;
[email protected]bee0df312014-04-28 06:59:58190
hashimotoa53e7e82016-10-26 06:30:47191 // The delegate instance for the ARC content file system related operations.
192 std::unique_ptr<FileSystemBackendDelegate> arc_content_delegate_;
193
nyaf5df1e32016-12-14 04:36:17194 // The delegate instance for the ARC documents provider related operations.
195 std::unique_ptr<FileSystemBackendDelegate> arc_documents_provider_delegate_;
196
Sam McNallyd8b7d822018-08-21 03:18:18197 // The delegate instance for the DriveFS file system related operations.
198 std::unique_ptr<FileSystemBackendDelegate> drivefs_delegate_;
199
Anand K. Mistry7694c362020-03-17 23:33:34200 // The delegate instance for the SmbFs file system related operations.
201 std::unique_ptr<FileSystemBackendDelegate> smbfs_delegate_;
202
[email protected]3c3621b2013-06-07 06:12:34203 // Mount points specific to the owning context (i.e. per-profile mount
204 // points).
[email protected]9d5d6982013-01-18 03:12:54205 //
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]cd501a72014-08-22 19:58:31214 scoped_refptr<storage::ExternalMountPoints> mount_points_;
[email protected]9d5d6982013-01-18 03:12:54215
216 // Globally visible mount points. System MountPonts instance should outlive
[email protected]f19bbf62013-07-09 01:22:32217 // all FileSystemBackend instances, so raw pointer is safe.
Bartek Nowierski8f579342024-01-08 06:45:15218 raw_ptr<storage::ExternalMountPoints> system_mount_points_;
[email protected]55d9bed2011-03-25 20:37:59219};
220
Yeunjoo Choi3d9ed38a2022-11-10 02:51:24221} // namespace ash
[email protected]55d9bed2011-03-25 20:37:59222
Yeunjoo Choief8a4a42022-11-08 04:21:05223#endif // CHROME_BROWSER_ASH_FILEAPI_FILE_SYSTEM_BACKEND_H_