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

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

Issue 14895013: Move webkit/fileapi/file_system_mount_point_provider.h to webkit/browser/fileapi (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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/isolated_context.h"
6
7 #include "base/basictypes.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/rand_util.h"
11 #include "base/stl_util.h"
12 #include "base/string_util.h"
13 #include "base/stringprintf.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "webkit/fileapi/file_system_url.h"
16
17 namespace fileapi {
18
19 namespace {
20
21 base::FilePath::StringType GetRegisterNameForPath(const base::FilePath& path) {
22 // If it's not a root path simply return a base name.
23 if (path.DirName() != path)
24 return path.BaseName().value();
25
26 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
27 base::FilePath::StringType name;
28 for (size_t i = 0;
29 i < path.value().size() && !base::FilePath::IsSeparator(path.value()[i]) ;
30 ++i) {
31 if (path.value()[i] == L':') {
32 name.append(L"_drive");
33 break;
34 }
35 name.append(1, path.value()[i]);
36 }
37 return name;
38 #else
39 return FILE_PATH_LITERAL("<root>");
40 #endif
41 }
42
43 bool IsSinglePathIsolatedFileSystem(FileSystemType type) {
44 switch (type) {
45 // As of writing dragged file system is the only filesystem
46 // which could have multiple top-level paths.
47 case kFileSystemTypeDragged:
48 return false;
49
50 case kFileSystemTypeUnknown:
51 NOTREACHED();
52 return true;
53
54 default:
55 return true;
56 }
57 NOTREACHED();
58 return true;
59 }
60
61 static base::LazyInstance<IsolatedContext>::Leaky g_isolated_context =
62 LAZY_INSTANCE_INITIALIZER;
63
64 } // namespace
65
66 IsolatedContext::FileInfoSet::FileInfoSet() {}
67 IsolatedContext::FileInfoSet::~FileInfoSet() {}
68
69 bool IsolatedContext::FileInfoSet::AddPath(
70 const base::FilePath& path, std::string* registered_name) {
71 // The given path should not contain any '..' and should be absolute.
72 if (path.ReferencesParent() || !path.IsAbsolute())
73 return false;
74 base::FilePath::StringType name = GetRegisterNameForPath(path);
75 std::string utf8name = base::FilePath(name).AsUTF8Unsafe();
76 base::FilePath normalized_path = path.NormalizePathSeparators();
77 bool inserted =
78 fileset_.insert(MountPointInfo(utf8name, normalized_path)).second;
79 if (!inserted) {
80 int suffix = 1;
81 std::string basepart = base::FilePath(name).RemoveExtension().AsUTF8Unsafe() ;
82 std::string ext = base::FilePath(base::FilePath(name).Extension()).AsUTF8Uns afe();
83 while (!inserted) {
84 utf8name = base::StringPrintf("%s (%d)", basepart.c_str(), suffix++);
85 if (!ext.empty())
86 utf8name.append(ext);
87 inserted =
88 fileset_.insert(MountPointInfo(utf8name, normalized_path)).second;
89 }
90 }
91 if (registered_name)
92 *registered_name = utf8name;
93 return true;
94 }
95
96 bool IsolatedContext::FileInfoSet::AddPathWithName(
97 const base::FilePath& path, const std::string& name) {
98 // The given path should not contain any '..' and should be absolute.
99 if (path.ReferencesParent() || !path.IsAbsolute())
100 return false;
101 return fileset_.insert(
102 MountPointInfo(name, path.NormalizePathSeparators())).second;
103 }
104
105 //--------------------------------------------------------------------------
106
107 class IsolatedContext::Instance {
108 public:
109 // For a single-path isolated file system, which could be registered by
110 // IsolatedContext::RegisterFileSystemForPath().
111 // Most of isolated file system contexts should be of this type.
112 Instance(FileSystemType type, const MountPointInfo& file_info);
113
114 // For a multi-paths isolated file system. As of writing only file system
115 // type which could have multi-paths is Dragged file system, and
116 // could be registered by IsolatedContext::RegisterDraggedFileSystem().
117 Instance(FileSystemType type, const std::set<MountPointInfo>& files);
118
119 ~Instance();
120
121 FileSystemType type() const { return type_; }
122 const MountPointInfo& file_info() const { return file_info_; }
123 const std::set<MountPointInfo>& files() const { return files_; }
124 int ref_counts() const { return ref_counts_; }
125
126 void AddRef() { ++ref_counts_; }
127 void RemoveRef() { --ref_counts_; }
128
129 bool ResolvePathForName(const std::string& name, base::FilePath* path) const;
130
131 // Returns true if the instance is a single-path instance.
132 bool IsSinglePathInstance() const;
133
134 private:
135 const FileSystemType type_;
136
137 // For single-path instance.
138 const MountPointInfo file_info_;
139
140 // For multiple-path instance (e.g. dragged file system).
141 const std::set<MountPointInfo> files_;
142
143 // Reference counts. Note that an isolated filesystem is created with ref==0
144 // and will get deleted when the ref count reaches <=0.
145 int ref_counts_;
146
147 DISALLOW_COPY_AND_ASSIGN(Instance);
148 };
149
150 IsolatedContext::Instance::Instance(FileSystemType type,
151 const MountPointInfo& file_info)
152 : type_(type),
153 file_info_(file_info),
154 ref_counts_(0) {
155 DCHECK(IsSinglePathIsolatedFileSystem(type_));
156 }
157
158 IsolatedContext::Instance::Instance(FileSystemType type,
159 const std::set<MountPointInfo>& files)
160 : type_(type),
161 files_(files),
162 ref_counts_(0) {
163 DCHECK(!IsSinglePathIsolatedFileSystem(type_));
164 }
165
166 IsolatedContext::Instance::~Instance() {}
167
168 bool IsolatedContext::Instance::ResolvePathForName(const std::string& name,
169 base::FilePath* path) const {
170 if (IsSinglePathIsolatedFileSystem(type_)) {
171 *path = file_info_.path;
172 return file_info_.name == name;
173 }
174 std::set<MountPointInfo>::const_iterator found = files_.find(
175 MountPointInfo(name, base::FilePath()));
176 if (found == files_.end())
177 return false;
178 *path = found->path;
179 return true;
180 }
181
182 bool IsolatedContext::Instance::IsSinglePathInstance() const {
183 return IsSinglePathIsolatedFileSystem(type_);
184 }
185
186 //--------------------------------------------------------------------------
187
188 // static
189 IsolatedContext* IsolatedContext::GetInstance() {
190 return g_isolated_context.Pointer();
191 }
192
193 // static
194 bool IsolatedContext::IsIsolatedType(FileSystemType type) {
195 return type == kFileSystemTypeIsolated || type == kFileSystemTypeExternal;
196 }
197
198 std::string IsolatedContext::RegisterDraggedFileSystem(
199 const FileInfoSet& files) {
200 base::AutoLock locker(lock_);
201 std::string filesystem_id = GetNewFileSystemId();
202 instance_map_[filesystem_id] = new Instance(
203 kFileSystemTypeDragged, files.fileset());
204 return filesystem_id;
205 }
206
207 std::string IsolatedContext::RegisterFileSystemForPath(
208 FileSystemType type,
209 const base::FilePath& path_in,
210 std::string* register_name) {
211 base::FilePath path(path_in.NormalizePathSeparators());
212 if (path.ReferencesParent() || !path.IsAbsolute())
213 return std::string();
214 std::string name;
215 if (register_name && !register_name->empty()) {
216 name = *register_name;
217 } else {
218 name = base::FilePath(GetRegisterNameForPath(path)).AsUTF8Unsafe();
219 if (register_name)
220 register_name->assign(name);
221 }
222
223 base::AutoLock locker(lock_);
224 std::string filesystem_id = GetNewFileSystemId();
225 instance_map_[filesystem_id] = new Instance(type, MountPointInfo(name, path));
226 path_to_id_map_[path].insert(filesystem_id);
227 return filesystem_id;
228 }
229
230 bool IsolatedContext::HandlesFileSystemMountType(FileSystemType type) const {
231 return type == kFileSystemTypeIsolated;
232 }
233
234 bool IsolatedContext::RevokeFileSystem(const std::string& filesystem_id) {
235 base::AutoLock locker(lock_);
236 return UnregisterFileSystem(filesystem_id);
237 }
238
239 bool IsolatedContext::GetRegisteredPath(
240 const std::string& filesystem_id, base::FilePath* path) const {
241 DCHECK(path);
242 base::AutoLock locker(lock_);
243 IDToInstance::const_iterator found = instance_map_.find(filesystem_id);
244 if (found == instance_map_.end() || !found->second->IsSinglePathInstance())
245 return false;
246 *path = found->second->file_info().path;
247 return true;
248 }
249
250 bool IsolatedContext::CrackVirtualPath(const base::FilePath& virtual_path,
251 std::string* id_or_name,
252 FileSystemType* type,
253 base::FilePath* path) const {
254 DCHECK(id_or_name);
255 DCHECK(path);
256
257 // This should not contain any '..' references.
258 if (virtual_path.ReferencesParent())
259 return false;
260
261 // The virtual_path should comprise <id_or_name> and <relative_path> parts.
262 std::vector<base::FilePath::StringType> components;
263 virtual_path.GetComponents(&components);
264 if (components.size() < 1)
265 return false;
266 std::vector<base::FilePath::StringType>::iterator component_iter =
267 components.begin();
268 std::string fsid = base::FilePath(*component_iter++).MaybeAsASCII();
269 if (fsid.empty())
270 return false;
271
272 base::FilePath cracked_path;
273 {
274 base::AutoLock locker(lock_);
275 IDToInstance::const_iterator found_instance = instance_map_.find(fsid);
276 if (found_instance == instance_map_.end())
277 return false;
278 *id_or_name = fsid;
279 const Instance* instance = found_instance->second;
280 if (type)
281 *type = instance->type();
282
283 if (component_iter == components.end()) {
284 // The virtual root case.
285 path->clear();
286 return true;
287 }
288
289 // *component_iter should be a name of the registered path.
290 std::string name = base::FilePath(*component_iter++).AsUTF8Unsafe();
291 if (!instance->ResolvePathForName(name, &cracked_path))
292 return false;
293 }
294
295 for (; component_iter != components.end(); ++component_iter)
296 cracked_path = cracked_path.Append(*component_iter);
297 *path = cracked_path;
298 return true;
299 }
300
301 FileSystemURL IsolatedContext::CrackURL(const GURL& url) const {
302 FileSystemURL filesystem_url = FileSystemURL(url);
303 if (!filesystem_url.is_valid())
304 return FileSystemURL();
305 return CrackFileSystemURL(filesystem_url);
306 }
307
308 FileSystemURL IsolatedContext::CreateCrackedFileSystemURL(
309 const GURL& origin,
310 FileSystemType type,
311 const base::FilePath& path) const {
312 return CrackFileSystemURL(FileSystemURL(origin, type, path));
313 }
314
315 void IsolatedContext::RevokeFileSystemByPath(const base::FilePath& path_in) {
316 base::AutoLock locker(lock_);
317 base::FilePath path(path_in.NormalizePathSeparators());
318 PathToID::iterator ids_iter = path_to_id_map_.find(path);
319 if (ids_iter == path_to_id_map_.end())
320 return;
321 std::set<std::string>& ids = ids_iter->second;
322 for (std::set<std::string>::iterator iter = ids.begin();
323 iter != ids.end(); ++iter) {
324 IDToInstance::iterator found = instance_map_.find(*iter);
325 if (found != instance_map_.end()) {
326 delete found->second;
327 instance_map_.erase(found);
328 }
329 }
330 path_to_id_map_.erase(ids_iter);
331 }
332
333 void IsolatedContext::AddReference(const std::string& filesystem_id) {
334 base::AutoLock locker(lock_);
335 DCHECK(instance_map_.find(filesystem_id) != instance_map_.end());
336 instance_map_[filesystem_id]->AddRef();
337 }
338
339 void IsolatedContext::RemoveReference(const std::string& filesystem_id) {
340 base::AutoLock locker(lock_);
341 // This could get called for non-existent filesystem if it has been
342 // already deleted by RevokeFileSystemByPath.
343 IDToInstance::iterator found = instance_map_.find(filesystem_id);
344 if (found == instance_map_.end())
345 return;
346 Instance* instance = found->second;
347 DCHECK_GT(instance->ref_counts(), 0);
348 instance->RemoveRef();
349 if (instance->ref_counts() == 0) {
350 bool deleted = UnregisterFileSystem(filesystem_id);
351 DCHECK(deleted);
352 }
353 }
354
355 bool IsolatedContext::GetDraggedFileInfo(
356 const std::string& filesystem_id,
357 std::vector<MountPointInfo>* files) const {
358 DCHECK(files);
359 base::AutoLock locker(lock_);
360 IDToInstance::const_iterator found = instance_map_.find(filesystem_id);
361 if (found == instance_map_.end() ||
362 found->second->type() != kFileSystemTypeDragged)
363 return false;
364 files->assign(found->second->files().begin(),
365 found->second->files().end());
366 return true;
367 }
368
369 base::FilePath IsolatedContext::CreateVirtualRootPath(
370 const std::string& filesystem_id) const {
371 return base::FilePath().AppendASCII(filesystem_id);
372 }
373
374 IsolatedContext::IsolatedContext() {
375 }
376
377 IsolatedContext::~IsolatedContext() {
378 STLDeleteContainerPairSecondPointers(instance_map_.begin(),
379 instance_map_.end());
380 }
381
382 FileSystemURL IsolatedContext::CrackFileSystemURL(
383 const FileSystemURL& url) const {
384 if (!HandlesFileSystemMountType(url.type()))
385 return FileSystemURL();
386
387 std::string mount_name;
388 FileSystemType cracked_type;
389 base::FilePath cracked_path;
390 if (!CrackVirtualPath(url.path(), &mount_name, &cracked_type, &cracked_path))
391 return FileSystemURL();
392
393 return FileSystemURL(
394 url.origin(), url.mount_type(), url.virtual_path(),
395 !url.filesystem_id().empty() ? url.filesystem_id() : mount_name,
396 cracked_type, cracked_path, mount_name);
397 }
398
399 bool IsolatedContext::UnregisterFileSystem(const std::string& filesystem_id) {
400 lock_.AssertAcquired();
401 IDToInstance::iterator found = instance_map_.find(filesystem_id);
402 if (found == instance_map_.end())
403 return false;
404 Instance* instance = found->second;
405 if (instance->IsSinglePathInstance()) {
406 PathToID::iterator ids_iter = path_to_id_map_.find(
407 instance->file_info().path);
408 DCHECK(ids_iter != path_to_id_map_.end());
409 ids_iter->second.erase(filesystem_id);
410 if (ids_iter->second.empty())
411 path_to_id_map_.erase(ids_iter);
412 }
413 delete found->second;
414 instance_map_.erase(found);
415 return true;
416 }
417
418 std::string IsolatedContext::GetNewFileSystemId() const {
419 // Returns an arbitrary random string which must be unique in the map.
420 lock_.AssertAcquired();
421 uint32 random_data[4];
422 std::string id;
423 do {
424 base::RandBytes(random_data, sizeof(random_data));
425 id = base::HexEncode(random_data, sizeof(random_data));
426 } while (instance_map_.find(id) != instance_map_.end());
427 return id;
428 }
429
430 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698