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