Chromium Code Reviews| Index: webkit/fileapi/isolated_context.cc |
| diff --git a/webkit/fileapi/isolated_context.cc b/webkit/fileapi/isolated_context.cc |
| index 16f7a89b8ddc386d64f5e34c5be771c8ec5a74a6..d3b2352f22b05716ab320b2b827b3ad1ffca5b73 100644 |
| --- a/webkit/fileapi/isolated_context.cc |
| +++ b/webkit/fileapi/isolated_context.cc |
| @@ -4,6 +4,7 @@ |
| #include "webkit/fileapi/isolated_context.h" |
| +#include "base/file_path.h" |
| #include "base/basictypes.h" |
| #include "base/logging.h" |
| #include "base/rand_util.h" |
| @@ -15,31 +16,55 @@ namespace fileapi { |
| static base::LazyInstance<IsolatedContext>::Leaky g_isolated_context = |
| LAZY_INSTANCE_INITIALIZER; |
| +IsolatedContext::FileInfo::FileInfo() {} |
| +IsolatedContext::FileInfo::FileInfo( |
| + const std::string& name, const FilePath& path) |
| + : name(name), path(path) {} |
| + |
| // static |
| IsolatedContext* IsolatedContext::GetInstance() { |
| return g_isolated_context.Pointer(); |
| } |
| -std::string IsolatedContext::RegisterIsolatedFileSystem( |
| - const std::set<FilePath>& files) { |
| +std::string IsolatedContext::GetNameForPath(const FilePath& path) { |
| + // If it's not a root path simply return a base name. |
| + if (path.DirName() != path) |
| + return path.BaseName().AsUTF8Unsafe(); |
| + |
| +#if defined(FILE_PATH_USES_DRIVE_LETTERS) |
| + FilePath::StringType name; |
| + for (size_t i = 0; |
| + i < path.value().size() && !FilePath::IsSeparator(path.value()[i]); |
| + ++i) { |
| + if (path.value()[i] == L':') { |
| + name.append(L"_drive"); |
| + break; |
| + } |
| + name.append(1, path.value()[i]); |
| + } |
| + return FilePath(name).AsUTF8Unsafe(); |
| +#else |
| + return "<root>"; |
| +#endif |
| +} |
| + |
| +std::string IsolatedContext::RegisterFileSystem( |
| + const std::vector<FileInfo>& files) { |
| base::AutoLock locker(lock_); |
| std::string filesystem_id = GetNewFileSystemId(); |
| - // Stores basename to fullpath map, as we store the basenames as |
| + // Stores name to fullpath map, as we store the name as a key in |
| // the filesystem's toplevel entries. |
| - PathMap toplevels; |
| - for (std::set<FilePath>::const_iterator iter = files.begin(); |
| - iter != files.end(); ++iter) { |
| + FileMap toplevels; |
| + for (size_t i = 0; i < files.size(); ++i) { |
| + const FileInfo& info = files[i]; |
| // The given path should not contain any '..' and should be absolute. |
| - if (iter->ReferencesParent() || !iter->IsAbsolute()) |
| + if (info.path.ReferencesParent() || !info.path.IsAbsolute()) |
| continue; |
| // Register the basename -> fullpath map. (We only expose the basename |
| // part to the user scripts) |
| - FilePath fullpath = iter->NormalizePathSeparators(); |
| - FilePath basename = iter->BaseName(); |
| - // TODO(kinuko): Append a suffix or something if we have multiple pathnames |
| - // with the same basename. For now we only register the first one. |
| - toplevels.insert(std::make_pair(basename, fullpath)); |
| + FilePath fullpath = info.path.NormalizePathSeparators(); |
| + toplevels.insert(std::make_pair(info.name, FileInfo(info.name, fullpath))); |
| } |
| // TODO(kinuko): we may not want to register the file system if there're |
| @@ -53,8 +78,15 @@ std::string IsolatedContext::RegisterIsolatedFileSystem( |
| return filesystem_id; |
| } |
| -void IsolatedContext::RevokeIsolatedFileSystem( |
| - const std::string& filesystem_id) { |
| +std::string IsolatedContext::RegisterFileSystemForFile( |
| + const std::string& display_name, |
| + const FilePath& path) { |
| + std::vector<FileInfo> files; |
| + files.push_back(FileInfo(display_name, path)); |
| + return RegisterFileSystem(files); |
| +} |
| + |
| +void IsolatedContext::RevokeFileSystem(const std::string& filesystem_id) { |
| base::AutoLock locker(lock_); |
| RevokeWithoutLocking(filesystem_id); |
| } |
| @@ -68,7 +100,7 @@ void IsolatedContext::AddReference(const std::string& filesystem_id) { |
| void IsolatedContext::RemoveReference(const std::string& filesystem_id) { |
| base::AutoLock locker(lock_); |
| // This could get called for non-existent filesystem if it has been |
| - // already deleted by RevokeIsolatedFileSystem. |
| + // already deleted by RevokeFileSystem. |
| if (ref_counts_.find(filesystem_id) == ref_counts_.end()) |
| return; |
| DCHECK(ref_counts_[filesystem_id] > 0); |
| @@ -78,7 +110,7 @@ void IsolatedContext::RemoveReference(const std::string& filesystem_id) { |
| bool IsolatedContext::CrackIsolatedPath(const FilePath& virtual_path, |
| std::string* filesystem_id, |
| - FilePath* root_path, |
| + FileInfo* root_info, |
| FilePath* platform_path) const { |
| DCHECK(filesystem_id); |
| DCHECK(platform_path); |
| @@ -97,7 +129,7 @@ bool IsolatedContext::CrackIsolatedPath(const FilePath& virtual_path, |
| std::string fsid = FilePath(components[0]).MaybeAsASCII(); |
| if (fsid.empty()) |
| return false; |
| - IDToPathMap::const_iterator found_toplevels = toplevel_map_.find(fsid); |
| + IDToFileMap::const_iterator found_toplevels = toplevel_map_.find(fsid); |
| if (found_toplevels == toplevel_map_.end()) |
| return false; |
| *filesystem_id = fsid; |
| @@ -105,34 +137,33 @@ bool IsolatedContext::CrackIsolatedPath(const FilePath& virtual_path, |
| platform_path->clear(); |
| return true; |
| } |
| - // components[1] should be a toplevel path of the dropped paths. |
| - PathMap::const_iterator found = found_toplevels->second.find( |
| - FilePath(components[1])); |
| + // components[1] should be a display name of the dropped paths. |
| + FileMap::const_iterator found = found_toplevels->second.find( |
| + FilePath(components[1]).AsUTF8Unsafe()); |
| if (found == found_toplevels->second.end()) |
| return false; |
| - FilePath path = found->second; |
| - if (root_path) |
| - *root_path = path; |
| - for (size_t i = 2; i < components.size(); ++i) { |
| + if (root_info) |
| + *root_info = found->second; |
| + FilePath path = found->second.path; |
| + for (size_t i = 2; i < components.size(); ++i) |
| path = path.Append(components[i]); |
| - } |
| *platform_path = path; |
| return true; |
| } |
| -bool IsolatedContext::GetTopLevelPaths(const std::string& filesystem_id, |
| - std::vector<FilePath>* paths) const { |
| - DCHECK(paths); |
| +bool IsolatedContext::GetRegisteredFileInfo( |
| + const std::string& filesystem_id, std::vector<FileInfo>* files) const { |
| + DCHECK(files); |
| base::AutoLock locker(lock_); |
| - IDToPathMap::const_iterator found = toplevel_map_.find(filesystem_id); |
| + IDToFileMap::const_iterator found = toplevel_map_.find(filesystem_id); |
| if (found == toplevel_map_.end()) |
| return false; |
| - paths->clear(); |
| - PathMap toplevels = found->second; |
| - for (PathMap::const_iterator iter = toplevels.begin(); |
| - iter != toplevels.end(); ++iter) { |
| - // Each path map entry holds a map of a toplevel name to its full path. |
| - paths->push_back(iter->second); |
| + files->clear(); |
| + files->reserve(found->second.size()); |
| + for (FileMap::const_iterator iter = found->second.begin(); |
| + iter != found->second.end(); |
| + iter++) { |
| + files->push_back(iter->second); |
| } |
|
tzik
2012/06/29 02:56:35
We can use std::vector::assign if we use std::set
kinuko
2012/06/29 08:31:22
Done.
|
| return true; |
| } |
| @@ -154,13 +185,9 @@ bool IsolatedContext::IsWritable(const std::string& filesystem_id) const { |
| return (writable_ids_.find(filesystem_id) != writable_ids_.end()); |
| } |
| -FilePath IsolatedContext::CreateVirtualPath( |
| - const std::string& filesystem_id, const FilePath& relative_path) const { |
| - FilePath full_path; |
| - full_path = full_path.AppendASCII(filesystem_id); |
| - if (relative_path.value() != FILE_PATH_LITERAL("/")) |
| - full_path = full_path.Append(relative_path); |
| - return full_path; |
| +FilePath IsolatedContext::CreateVirtualRootPath( |
| + const std::string& filesystem_id) const { |
| + return FilePath().AppendASCII(filesystem_id); |
| } |
| IsolatedContext::IsolatedContext() { |