File: file_system_backend.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (223 lines) | stat: -rw-r--r-- 9,355 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_FILEAPI_FILE_SYSTEM_BACKEND_H_
#define CHROME_BROWSER_ASH_FILEAPI_FILE_SYSTEM_BACKEND_H_

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "components/account_id/account_id.h"
#include "components/file_access/scoped_file_access_delegate.h"
#include "storage/browser/file_system/file_system_backend.h"
#include "storage/browser/file_system/task_runner_bound_observer_list.h"
#include "storage/common/file_system/file_system_types.h"
#include "url/origin.h"

class Profile;

namespace storage {
class CopyOrMoveFileValidatorFactory;
class ExternalMountPoints;
class FileSystemURL;
class WatcherManager;
}  // namespace storage

namespace ash {

class FileSystemBackendDelegate;
class FileAccessPermissions;

inline constexpr char kSystemMountNameArchive[] = "archive";
inline constexpr char kSystemMountNameRemovable[] = "removable";

// Backend Function called.  Used to control access.
enum class BackendFunction {
  kCreateFileSystemOperation,
  kCreateFileStreamReader,
  kCreateFileStreamWriter,
};

// FileSystemBackend is a Chrome OS specific implementation of
// ExternalFileSystemBackend. This class is responsible for a
// number of things, including:
//
// - Add system mount points
// - Grant/revoke/check file access permissions
// - Create FileSystemOperation per file system type
// - Create FileStreamReader/Writer per file system type
//
// Chrome OS specific static mount points:
//
// "archive" is a mount point for an archive file, such as a zip file. This
// mount point exposes contents of an archive file via cros_disks and AVFS
// <http://avf.sourceforge.net/>.
//
// "removable" is a mount point for removable media such as an SD card.
// Insertion and removal of removable media are handled by cros_disks.
//
// These mount points are placed under the "external" namespace, and file
// system URLs for these mount points look like:
//
//   filesystem:<origin>/external/<mount_name>/...
//
// Other mounts are also registered by VolumeManager for MyFiles, Drive, VMs
// (crostini, arc, etc), Android Document Providers, fileSystemProviders, etc.
class FileSystemBackend : public storage::FileSystemBackend {
 public:
  using storage::FileSystemBackend::ResolveURLCallback;

  // |system_mount_points| should outlive FileSystemBackend instance.
  FileSystemBackend(
      Profile* profile,
      std::unique_ptr<FileSystemBackendDelegate> file_system_provider_delegate,
      std::unique_ptr<FileSystemBackendDelegate> mtp_delegate,
      std::unique_ptr<FileSystemBackendDelegate> arc_content_delegate,
      std::unique_ptr<FileSystemBackendDelegate>
          arc_documents_provider_delegate,
      std::unique_ptr<FileSystemBackendDelegate> drivefs_delegate,
      std::unique_ptr<FileSystemBackendDelegate> smbfs_delegate,
      scoped_refptr<storage::ExternalMountPoints> mount_points,
      storage::ExternalMountPoints* system_mount_points);

  FileSystemBackend(const FileSystemBackend&) = delete;
  FileSystemBackend& operator=(const FileSystemBackend&) = delete;

  ~FileSystemBackend() override;

  // Gets the ChromeOS FileSystemBackend.
  static FileSystemBackend* Get(const storage::FileSystemContext& context);

  // Adds system mount points, such as "archive", and "removable". This
  // function is no-op if these mount points are already present.
  void AddSystemMountPoints();

  // Returns true if CrosMountpointProvider can handle |url|, i.e. its
  // file system type matches with what this provider supports.
  // This could be called on any threads.
  static bool CanHandleURL(const storage::FileSystemURL& url);

  // Returns true if |url| is allowed to be accessed.
  // This is supposed to perform ExternalFileSystem-specific security
  // checks.
  bool IsAccessAllowed(BackendFunction backend_function,
                       storage::OperationType operation_type,
                       const storage::FileSystemURL& url) const;

  // Returns the list of top level directories that are exposed by this
  // provider. This list is used to set appropriate child process file access
  // permissions.
  std::vector<base::FilePath> GetRootDirectories() const;

  // Grants access to |virtual_path| from |origin| URL.
  void GrantFileAccessToOrigin(const url::Origin& origin,
                               const base::FilePath& virtual_path);

  // Revokes file access from origin identified with |origin|.
  void RevokeAccessForOrigin(const url::Origin& origin);

  // Gets virtual path by known filesystem path. Returns false when filesystem
  // path is not exposed by this provider.
  bool GetVirtualPath(const base::FilePath& file_system_path,
                      base::FilePath* virtual_path) const;

  // Creates an internal File System URL for performing internal operations such
  // as confirming if a file or a directory exist before granting the final
  // permission to the entry. The path must be an absolute path.
  storage::FileSystemURL CreateInternalURL(
      storage::FileSystemContext* context,
      const base::FilePath& entry_path) const;

  // storage::FileSystemBackend overrides.
  bool CanHandleType(storage::FileSystemType type) const override;
  void Initialize(storage::FileSystemContext* context) override;
  void ResolveURL(const storage::FileSystemURL& url,
                  storage::OpenFileSystemMode mode,
                  ResolveURLCallback callback) override;
  storage::AsyncFileUtil* GetAsyncFileUtil(
      storage::FileSystemType type) override;
  storage::WatcherManager* GetWatcherManager(
      storage::FileSystemType type) override;
  storage::CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory(
      storage::FileSystemType type,
      base::File::Error* error_code) override;
  std::unique_ptr<storage::FileSystemOperation> CreateFileSystemOperation(
      storage::OperationType type,
      const storage::FileSystemURL& url,
      storage::FileSystemContext* context,
      base::File::Error* error_code) const override;
  bool SupportsStreaming(const storage::FileSystemURL& url) const override;
  bool HasInplaceCopyImplementation(
      storage::FileSystemType type) const override;
  std::unique_ptr<storage::FileStreamReader> CreateFileStreamReader(
      const storage::FileSystemURL& path,
      int64_t offset,
      int64_t max_bytes_to_read,
      const base::Time& expected_modification_time,
      storage::FileSystemContext* context,
      file_access::ScopedFileAccessDelegate::RequestFilesAccessIOCallback
          file_access) const override;
  std::unique_ptr<storage::FileStreamWriter> CreateFileStreamWriter(
      const storage::FileSystemURL& url,
      int64_t offset,
      storage::FileSystemContext* context) const override;
  storage::FileSystemQuotaUtil* GetQuotaUtil() override;
  const storage::UpdateObserverList* GetUpdateObservers(
      storage::FileSystemType type) const override;
  const storage::ChangeObserverList* GetChangeObservers(
      storage::FileSystemType type) const override;
  const storage::AccessObserverList* GetAccessObservers(
      storage::FileSystemType type) const override;

 private:
  const AccountId account_id_;

  std::unique_ptr<FileAccessPermissions> file_access_permissions_;
  std::unique_ptr<storage::AsyncFileUtil> local_file_util_;

  // The delegate instance for the provided file system related operations.
  std::unique_ptr<FileSystemBackendDelegate> file_system_provider_delegate_;

  // The delegate instance for the MTP file system related operations.
  std::unique_ptr<FileSystemBackendDelegate> mtp_delegate_;

  // The delegate instance for the ARC content file system related operations.
  std::unique_ptr<FileSystemBackendDelegate> arc_content_delegate_;

  // The delegate instance for the ARC documents provider related operations.
  std::unique_ptr<FileSystemBackendDelegate> arc_documents_provider_delegate_;

  // The delegate instance for the DriveFS file system related operations.
  std::unique_ptr<FileSystemBackendDelegate> drivefs_delegate_;

  // The delegate instance for the SmbFs file system related operations.
  std::unique_ptr<FileSystemBackendDelegate> smbfs_delegate_;

  // Mount points specific to the owning context (i.e. per-profile mount
  // points).
  //
  // It is legal to have mount points with the same name as in
  // system_mount_points_. Also, mount point paths may overlap with mount point
  // paths in system_mount_points_. In both cases mount points in
  // |mount_points_| will have a priority.
  // E.g. if |mount_points_| map 'foo1' to '/foo/foo1' and
  // |file_system_mount_points_| map 'xxx' to '/foo/foo1/xxx', |GetVirtualPaths|
  // will resolve '/foo/foo1/xxx/yyy' as 'foo1/xxx/yyy' (i.e. the mapping from
  // |mount_points_| will be used).
  scoped_refptr<storage::ExternalMountPoints> mount_points_;

  // Globally visible mount points. System MountPonts instance should outlive
  // all FileSystemBackend instances, so raw pointer is safe.
  raw_ptr<storage::ExternalMountPoints> system_mount_points_;
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_FILEAPI_FILE_SYSTEM_BACKEND_H_