1 // Copyright (c) 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.
5 #ifndef WEBKIT_BROWSER_FILEAPI_EXTERNAL_MOUNT_POINTS_H_
6 #define WEBKIT_BROWSER_FILEAPI_EXTERNAL_MOUNT_POINTS_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/synchronization/lock.h"
14 #include "webkit/browser/fileapi/mount_points.h"
15 #include "webkit/browser/webkit_storage_browser_export.h"
16 #include "webkit/common/fileapi/file_system_mount_option.h"
17 #include "webkit/common/fileapi/file_system_types.h"
27 // Manages external filesystem namespaces that are identified by 'mount name'
28 // and are persisted until RevokeFileSystem is called.
29 // Files in an external filesystem are identified by a filesystem URL like:
31 // filesystem:<origin>/external/<mount_name>/relative/path
33 class WEBKIT_STORAGE_BROWSER_EXPORT ExternalMountPoints
34 : public base::RefCountedThreadSafe
<ExternalMountPoints
>,
37 static ExternalMountPoints
* GetSystemInstance();
38 static scoped_refptr
<ExternalMountPoints
> CreateRefCounted();
40 // Registers a new named external filesystem.
41 // The |path| is registered as the root path of the mount point which
42 // is identified by a URL "filesystem:.../external/mount_name".
44 // For example, if the path "/media/removable" is registered with
45 // the mount_name "removable", a filesystem URL like
46 // "filesystem:.../external/removable/a/b" will be resolved as
47 // "/media/removable/a/b".
49 // The |mount_name| should NOT contain a path separator '/'.
50 // Returns false if the given name is already registered.
52 // Overlapping mount points in a single MountPoints instance are not allowed.
53 // Adding mount point whose path overlaps with an existing mount point will
54 // fail except for media galleries, which do not count toward registered
55 // paths for overlap calculation.
57 // If not empty, |path| must be absolute. It is allowed for the path to be
58 // empty, but |GetVirtualPath| will not work for those mount points.
60 // An external file system registered by this method can be revoked
61 // by calling RevokeFileSystem with |mount_name|.
62 bool RegisterFileSystem(const std::string
& mount_name
,
64 const FileSystemMountOption
& mount_option
,
65 const base::FilePath
& path
);
67 // MountPoints overrides.
68 virtual bool HandlesFileSystemMountType(FileSystemType type
) const OVERRIDE
;
69 virtual bool RevokeFileSystem(const std::string
& mount_name
) OVERRIDE
;
70 virtual bool GetRegisteredPath(const std::string
& mount_name
,
71 base::FilePath
* path
) const OVERRIDE
;
72 virtual bool CrackVirtualPath(
73 const base::FilePath
& virtual_path
,
74 std::string
* mount_name
,
76 std::string
* cracked_id
,
78 FileSystemMountOption
* mount_option
) const OVERRIDE
;
79 virtual FileSystemURL
CrackURL(const GURL
& url
) const OVERRIDE
;
80 virtual FileSystemURL
CreateCrackedFileSystemURL(
83 const base::FilePath
& path
) const OVERRIDE
;
85 // Returns a list of registered MountPointInfos (of <mount_name, path>).
86 void AddMountPointInfosTo(std::vector
<MountPointInfo
>* mount_points
) const;
88 // Converts a path on a registered file system to virtual path relative to the
89 // file system root. E.g. if 'Downloads' file system is mapped to
90 // '/usr/local/home/Downloads', and |absolute| path is set to
91 // '/usr/local/home/Downloads/foo', the method will set |virtual_path| to
93 // Returns false if the path cannot be resolved (e.g. if the path is not
94 // part of any registered filesystem).
96 // Media gallery type file systems do not count for this calculation. i.e.
97 // if only a media gallery is registered for the path, false will be returned.
98 // If a media gallery and another file system are registered for related
99 // paths, only the other registration is taken into account.
101 // Returned virtual_path will have normalized path separators.
102 bool GetVirtualPath(const base::FilePath
& absolute_path
,
103 base::FilePath
* virtual_path
) const;
105 // Returns the virtual root path that looks like /<mount_name>.
106 base::FilePath
CreateVirtualRootPath(const std::string
& mount_name
) const;
108 FileSystemURL
CreateExternalFileSystemURL(
110 const std::string
& mount_name
,
111 const base::FilePath
& path
) const;
113 // Revoke all registered filesystems. Used only by testing (for clean-ups).
114 void RevokeAllFileSystems();
117 friend class base::RefCountedThreadSafe
<ExternalMountPoints
>;
119 // Represents each file system instance (defined in the .cc).
122 typedef std::map
<std::string
, Instance
*> NameToInstance
;
124 // Reverse map from registered path to its corresponding mount name.
125 typedef std::map
<base::FilePath
, std::string
> PathToName
;
127 // Use |GetSystemInstance| of |CreateRefCounted| to get an instance.
128 ExternalMountPoints();
129 virtual ~ExternalMountPoints();
131 // MountPoint overrides.
132 virtual FileSystemURL
CrackFileSystemURL(
133 const FileSystemURL
& url
) const OVERRIDE
;
135 // Performs sanity checks on the new mount point.
136 // Checks the following:
137 // - there is no registered mount point with mount_name
138 // - path does not contain a reference to a parent
139 // - path is absolute
140 // - path does not overlap with an existing mount point path unless it is a
141 // media gallery type.
143 // |lock_| should be taken before calling this method.
144 bool ValidateNewMountPoint(const std::string
& mount_name
,
146 const base::FilePath
& path
);
148 // This lock needs to be obtained when accessing the instance_map_.
149 mutable base::Lock lock_
;
151 NameToInstance instance_map_
;
152 PathToName path_to_name_map_
;
154 DISALLOW_COPY_AND_ASSIGN(ExternalMountPoints
);
157 } // namespace fileapi
159 #endif // WEBKIT_BROWSER_FILEAPI_EXTERNAL_MOUNT_POINTS_H_