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