Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: webkit/fileapi/external_mount_points.cc

Issue 11648027: Extract external file systems handling from isolated context. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: few nits Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "webkit/fileapi/external_mount_points.h"
6
7 #include "base/file_path.h"
8 #include "base/lazy_instance.h"
9 #include "base/path_service.h"
10 #include "base/stl_util.h"
11 #include "webkit/fileapi/remote_file_system_proxy.h"
12
13 namespace {
14
15 // Wrapper around ref-counted ExternalMountPoints that will be used to lazily
16 // create and initialize LazyInstance system ExternalMountPoints.
17 class SystemMountPointsLazyWrapper {
18 public:
19 SystemMountPointsLazyWrapper()
20 : system_mount_points_(fileapi::ExternalMountPoints::CreateRefCounted()) {
21 RegisterDefaultMountPoints();
22 }
23
24 ~SystemMountPointsLazyWrapper() {}
25
26 fileapi::ExternalMountPoints* get() {
27 return system_mount_points_.get();
28 }
29
30 private:
31 void RegisterDefaultMountPoints() {
32 #if defined(OS_CHROMEOS)
33 // Add default system mount points.
34 system_mount_points_->RegisterFileSystem(
35 "archive",
36 fileapi::kFileSystemTypeNativeLocal,
37 FilePath(FILE_PATH_LITERAL("/media/archive")));
38 system_mount_points_->RegisterFileSystem(
39 "removable",
40 fileapi::kFileSystemTypeNativeLocal,
41 FilePath(FILE_PATH_LITERAL("/media/removable")));
42 system_mount_points_->RegisterFileSystem(
43 "oem",
44 fileapi::kFileSystemTypeRestrictedNativeLocal,
45 FilePath(FILE_PATH_LITERAL("/usr/share/oem")));
46
47 // TODO(tbarzic): Move this out of here.
48 FilePath home_path;
49 if (PathService::Get(base::DIR_HOME, &home_path)) {
50 system_mount_points_->RegisterFileSystem(
51 "Downloads",
52 fileapi::kFileSystemTypeNativeLocal,
53 home_path.AppendASCII("Downloads"));
54 }
55 #endif // defined(OS_CHROMEOS)
56 }
57
58 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points_;
59 };
60
61 base::LazyInstance<SystemMountPointsLazyWrapper>::Leaky
62 g_external_mount_points = LAZY_INSTANCE_INITIALIZER;
63
64 } // namespace
65
66 namespace fileapi {
67
68 class ExternalMountPoints::Instance {
69 public:
70 // |remote_proxy| is used only by drive file system.
71 Instance(FileSystemType type,
72 const FilePath& path,
73 RemoteFileSystemProxyInterface* remote_proxy);
74
75 ~Instance();
76
77 FileSystemType type() const { return type_; }
78 const FilePath& path() const { return path_; }
79 RemoteFileSystemProxyInterface* remote_proxy() const {
80 return remote_proxy_.get();
81 }
82
83 private:
84 const FileSystemType type_;
85 const FilePath path_;
86
87 // For file systems that have a remote file system proxy.
88 scoped_refptr<RemoteFileSystemProxyInterface> remote_proxy_;
89
90 DISALLOW_COPY_AND_ASSIGN(Instance);
91 };
92
93 ExternalMountPoints::Instance::Instance(FileSystemType type,
94 const FilePath& path,
95 RemoteFileSystemProxyInterface* proxy)
96 : type_(type),
97 path_(path),
98 remote_proxy_(proxy) {
99 DCHECK(!proxy || (kFileSystemTypeDrive == type_));
100 }
101
102 ExternalMountPoints::Instance::~Instance() {}
103
104 //--------------------------------------------------------------------------
105
106 // static
107 ExternalMountPoints* ExternalMountPoints::GetSystemInstance() {
108 return g_external_mount_points.Pointer()->get();
109 }
110
111 // static
112 scoped_refptr<ExternalMountPoints> ExternalMountPoints::CreateRefCounted() {
113 return new ExternalMountPoints();
114 }
115
116 bool ExternalMountPoints::RegisterFileSystem(
117 const std::string& mount_name,
118 FileSystemType type,
119 const FilePath& path) {
120 return RegisterRemoteFileSystem(mount_name, type, NULL, path);
121 }
122
123 bool ExternalMountPoints::RegisterRemoteFileSystem(
124 const std::string& mount_name,
125 FileSystemType type,
126 RemoteFileSystemProxyInterface* remote_proxy,
127 const FilePath& path) {
128 base::AutoLock locker(lock_);
129
130 if (!ValidateNewMountPoint(mount_name, path))
131 return false;
132
133 instance_map_[mount_name] = new Instance(type, path, remote_proxy);
134 if (!path.empty())
135 path_to_name_map_.insert(std::make_pair(path, mount_name));
136 return true;
137 }
138
139 RemoteFileSystemProxyInterface* ExternalMountPoints::GetRemoteFileSystemProxy(
140 const std::string& mount_name) const {
141 base::AutoLock locker(lock_);
142 NameToInstance::const_iterator found = instance_map_.find(mount_name);
143 if (found == instance_map_.end())
144 return NULL;
145 return found->second->remote_proxy();
146 }
147
148 void ExternalMountPoints::AddMountPointInfosTo(
149 std::vector<MountPointInfo>* mount_points) const {
150 base::AutoLock locker(lock_);
151 DCHECK(mount_points);
152 for (NameToInstance::const_iterator iter = instance_map_.begin();
153 iter != instance_map_.end(); ++iter) {
154 mount_points->push_back(MountPointInfo(iter->first, iter->second->path()));
155 }
156 }
157
158 bool ExternalMountPoints::GetRegisteredPath(
159 const std::string& filesystem_id, FilePath* path) const {
160 DCHECK(path);
161 base::AutoLock locker(lock_);
162 NameToInstance::const_iterator found = instance_map_.find(filesystem_id);
163 if (found == instance_map_.end())
164 return false;
165 *path = found->second->path();
166 return true;
167 }
168
169 bool ExternalMountPoints::RevokeFileSystem(const std::string& mount_name) {
170 base::AutoLock locker(lock_);
171 NameToInstance::iterator found = instance_map_.find(mount_name);
172 if (found == instance_map_.end())
173 return false;
174 Instance* instance = found->second;
175 path_to_name_map_.erase(instance->path());
176 delete found->second;
177 instance_map_.erase(found);
178 return true;
179 }
180
181 FilePath ExternalMountPoints::CreateVirtualRootPath(
182 const std::string& mount_name) const {
183 return FilePath().AppendASCII(mount_name);
184 }
185
186 ExternalMountPoints::ExternalMountPoints() { }
187
188 ExternalMountPoints::~ExternalMountPoints() {
189 STLDeleteContainerPairSecondPointers(instance_map_.begin(),
190 instance_map_.end());
191 }
192
193 bool ExternalMountPoints::ValidateNewMountPoint(const std::string& mount_name,
194 const FilePath& path) {
195 // Mount name must not be empty.
196 if (mount_name.empty())
197 return false;
198
199 // Verify there is no registered mount point with the same name.
200 NameToInstance::iterator found = instance_map_.find(mount_name);
201 if (found != instance_map_.end())
202 return false;
203
204 // Allow empty paths.
205 if (path.empty())
206 return true;
207
208 // Verify path is legal.
209 if (path.ReferencesParent() || !path.IsAbsolute())
210 return false;
211
212 // Check there the new path does not overlap with one of the existing ones.
213 std::map<FilePath, std::string>::reverse_iterator potential_parent(
214 path_to_name_map_.upper_bound(path));
215 if (potential_parent != path_to_name_map_.rend()) {
216 if (potential_parent->first == path ||
217 potential_parent->first.IsParent(path)) {
218 return false;
219 }
220 }
221
222 std::map<FilePath, std::string>::iterator potential_child =
223 path_to_name_map_.upper_bound(path);
224 if (potential_child == path_to_name_map_.end())
225 return true;
226 return !(potential_child->first == path) &&
227 !path.IsParent(potential_child->first);
228 }
229
230 bool ExternalMountPoints::GetVirtualPath(const FilePath& absolute_path,
231 FilePath* virtual_path) {
232 DCHECK(virtual_path);
233 base::AutoLock locker(lock_);
234 std::map<FilePath, std::string>::reverse_iterator iter(
235 path_to_name_map_.upper_bound(absolute_path));
236 if (iter == path_to_name_map_.rend())
237 return false;
238
239 *virtual_path = CreateVirtualRootPath(iter->second);
240 if (iter->first == absolute_path)
241 return true;
242 return iter->first.AppendRelativePath(absolute_path, virtual_path);
243 }
244
245 // TODO(tbarzic): Reorder methods to match .h before committing!!!!!!
kinuko 2013/01/10 05:47:31 yes please ;)
tonibarzic 2013/01/11 01:50:04 Done.
246 bool ExternalMountPoints::CrackVirtualPath(const FilePath& virtual_path,
247 std::string* mount_name,
248 FileSystemType* type,
249 FilePath* path) const {
250 DCHECK(mount_name);
251 DCHECK(path);
252
253 // This should not contain any '..' references.
254 if (virtual_path.ReferencesParent())
255 return false;
256
257 // The virtual_path should comprise <mount_name> and <relative_path> parts.
258 std::vector<FilePath::StringType> components;
259 virtual_path.GetComponents(&components);
260 if (components.size() < 1)
261 return false;
262
263 std::vector<FilePath::StringType>::iterator component_iter =
264 components.begin();
265 std::string maybe_mount_name = FilePath(*component_iter++).MaybeAsASCII();
266 if (maybe_mount_name.empty())
267 return false;
268
269 FilePath cracked_path;
270 {
271 base::AutoLock locker(lock_);
272 NameToInstance::const_iterator found_instance =
273 instance_map_.find(maybe_mount_name);
274 if (found_instance == instance_map_.end())
275 return false;
276
277 *mount_name = maybe_mount_name;
278 const Instance* instance = found_instance->second;
279 if (type)
280 *type = instance->type();
281 cracked_path = instance->path();
282 }
283
284 for (; component_iter != components.end(); ++component_iter)
285 cracked_path = cracked_path.Append(*component_iter);
286 *path = cracked_path;
287 return true;
288 }
289
290 ScopedExternalFileSystem::ScopedExternalFileSystem(
291 const std::string& mount_name,
292 FileSystemType type,
293 const FilePath& path)
294 : mount_name_(mount_name) {
295 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
296 mount_name, type, path);
297 }
298
299 FilePath ScopedExternalFileSystem::GetVirtualRootPath() const {
300 return ExternalMountPoints::GetSystemInstance()->
301 CreateVirtualRootPath(mount_name_);
302 }
303
304 ScopedExternalFileSystem::~ScopedExternalFileSystem() {
305 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(mount_name_);
306 }
307
308 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698