OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 WEBKIT_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ | |
6 #define WEBKIT_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/synchronization/lock.h" | |
14 #include "webkit/fileapi/file_system_types.h" | |
15 #include "webkit/fileapi/mount_points.h" | |
16 #include "webkit/storage/webkit_storage_export.h" | |
17 | |
18 class FilePath; | |
19 | |
20 namespace fileapi { | |
21 class RemoteFileSystemProxyInterface; | |
22 } | |
23 | |
24 namespace fileapi { | |
25 | |
26 // Manages external filesystem namespaces that are identified by 'mount name' | |
27 // and are persisted until RevokeFileSystem is called. | |
28 // Files in an external filesystem are identified by a filesystem URL like: | |
29 // | |
30 // filesystem:<origin>/external/<mount_name>/relative/path | |
31 // | |
32 class WEBKIT_STORAGE_EXPORT ExternalMountPoints | |
33 : public base::RefCountedThreadSafe<ExternalMountPoints>, | |
34 public MountPoints { | |
35 public: | |
36 static ExternalMountPoints* GetSystemInstance(); | |
37 static scoped_refptr<ExternalMountPoints> CreateRefCounted(); | |
38 | |
39 // Registers a new named external filesystem. | |
40 // The |path| is registered as the root path of the mount point which | |
41 // is identified by a URL "filesystem:.../external/mount_name". | |
42 // | |
43 // For example, if the path "/media/removable" is registered with | |
44 // the mount_name "removable", a filesystem URL like | |
45 // "filesystem:.../external/removable/a/b" will be resolved as | |
46 // "/media/removable/a/b". | |
47 // | |
48 // The |mount_name| should NOT contain a path separator '/'. | |
49 // Returns false if the given name is already registered. | |
50 // | |
51 // Overlapping mount points are not allowed. Adding mount point whose path | |
kinuko
2013/01/08 12:22:43
nit: "Overlapping mount points in a single MountPo
tbarzic
2013/01/09 01:26:34
Done.
| |
52 // overlaps with an existing mount point will fail. | |
53 // | |
54 // If not empty, |path| must be absolute. It is allowed for the path to be | |
55 // empty, but |GetVirtualPath| will not work for those mount points. | |
56 // | |
57 // An external file system registered by this method can be revoked | |
58 // by calling RevokeFileSystem with |mount_name|. | |
59 bool RegisterFileSystem(const std::string& mount_name, | |
60 FileSystemType type, | |
61 const FilePath& path); | |
62 | |
63 // Same as |RegisterExternalFileSystem|, but registers a remote file system | |
64 // proxy for the file system. | |
65 bool RegisterRemoteFileSystem(const std::string& mount_name, | |
66 FileSystemType type, | |
67 RemoteFileSystemProxyInterface* remote_proxy, | |
68 const FilePath& path); | |
69 | |
70 // Retrieves the remote file system proxy for the registered file system. | |
71 // Returns NULL if there is no file system with given name, or if the file | |
72 // system does not have a remote file system proxy. | |
73 RemoteFileSystemProxyInterface* GetRemoteFileSystemProxy( | |
74 const std::string& mount_name) const; | |
75 | |
76 // Returns a list of registered MountPointInfos (of <mount_name, path>). | |
77 void AppendMountPoints(std::vector<MountPointInfo>* mount_points) const; | |
78 | |
79 // MountPoints overrides. | |
80 virtual bool CanCrackMountType(FileSystemType type) const OVERRIDE; | |
81 virtual bool GetRegisteredPath(const std::string& mount_name, | |
82 FilePath* path) const OVERRIDE; | |
83 virtual bool RevokeFileSystem(const std::string& mount_name) OVERRIDE; | |
84 | |
85 // Converts a path on a registered file system to virtual path relative to the | |
86 // file system root. E.g. if 'Downloads' file system is mapped to | |
87 // '/usr/local/home/Downloads', and |absolute| path is set to | |
88 // 'usr/local/home/Downloads/foo', the method will set |virtual_path| to | |
89 // 'Downloads/foo'. | |
90 bool GetVirtualPath(const FilePath& absolute_path, FilePath* virtual_path); | |
91 | |
92 // Cracks the given |virtual_path| (which is the path part of a filesystem URL | |
93 // without /'/external' prefix part) and populates the | |
94 // |mount_name|, |type|, and |path| if the <mount_name> part embedded in | |
95 // the |virtual_path| (i.e. the first component of the |virtual_path|) is a | |
96 // valid registered filesystem ID or mount name for an external filesystem. | |
97 // | |
98 // Returns false if the given virtual_path or the cracked mount_name | |
99 // is not valid. | |
100 bool CrackExternalPath(const FilePath& virtual_path, | |
101 std::string* mount_name, | |
102 FileSystemType* type, | |
103 FilePath* path) const; | |
104 | |
105 // Returns the virtual root path that looks like /<mount_name>. | |
106 FilePath CreateVirtualRootPath(const std::string& mount_name) const; | |
107 | |
108 private: | |
109 friend class base::RefCountedThreadSafe<ExternalMountPoints>; | |
110 | |
111 // Represents each file system instance (defined in the .cc). | |
112 class Instance; | |
113 | |
114 typedef std::map<std::string, Instance*> NameToInstance; | |
115 | |
116 // Reverse map from registered path to its corresponding mount name. | |
117 typedef std::map<FilePath, std::string> PathToName; | |
118 | |
119 // Use |GetSystemInstance| of |CreateRefCounted| to get an instance. | |
120 explicit ExternalMountPoints(bool is_system_instance); | |
121 virtual ~ExternalMountPoints(); | |
122 | |
123 // Performs sanity checks on the new mount point. | |
124 // Checks following: | |
125 // - there is no registered mount point with mount_name | |
126 // - path does not reference parent | |
127 // - path is absolute | |
128 // - path does not overlap with an existing mount point path. | |
129 // | |
130 // |lock_| should be taken before calling this method. | |
131 bool ValidateNewMountPoint(const std::string& mount_name, | |
132 const FilePath& path); | |
133 | |
134 // This lock needs to be obtained when accessing the instance_map_. | |
135 mutable base::Lock lock_; | |
136 | |
137 NameToInstance instance_map_; | |
138 PathToName path_to_name_map_; | |
139 | |
140 DISALLOW_COPY_AND_ASSIGN(ExternalMountPoints); | |
141 }; | |
142 | |
143 // Registers a scoped external filesystem which gets revoked when it scopes out. | |
144 class WEBKIT_STORAGE_EXPORT ScopedExternalFileSystem { | |
145 public: | |
146 ScopedExternalFileSystem(const std::string& mount_name, | |
147 FileSystemType type, | |
148 const FilePath& path); | |
149 ~ScopedExternalFileSystem(); | |
150 | |
151 FilePath GetVirtualRootPath() const; | |
152 | |
153 private: | |
154 const std::string mount_name_; | |
155 }; | |
156 | |
157 } // namespace fileapi | |
158 | |
159 #endif // WEBKIT_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ | |
OLD | NEW |