1 // Copyright 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. 4 5 #include "webkit/browser/fileapi/dragged_file_util.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "base/file_util.h" 11 #include "webkit/browser/fileapi/file_system_context.h" 12 #include "webkit/browser/fileapi/file_system_operation_context.h" 13 #include "webkit/browser/fileapi/file_system_url.h" 14 #include "webkit/browser/fileapi/isolated_context.h" 15 #include "webkit/browser/fileapi/native_file_util.h" 16 #include "webkit/common/blob/shareable_file_reference.h" 17 18 using base::PlatformFileError; 19 using base::PlatformFileInfo; 20 21 namespace fileapi { 22 23 typedef IsolatedContext::MountPointInfo FileInfo; 24 25 namespace { 26 27 // Simply enumerate each path from a given fileinfo set. 28 // Used to enumerate top-level paths of an isolated filesystem. 29 class SetFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { 30 public: 31 explicit SetFileEnumerator(const std::vector<FileInfo>& files) 32 : files_(files) { 33 file_iter_ = files_.begin(); 34 } 35 virtual ~SetFileEnumerator() {} 36 37 // AbstractFileEnumerator overrides. 38 virtual base::FilePath Next() OVERRIDE { 39 if (file_iter_ == files_.end()) 40 return base::FilePath(); 41 base::FilePath platform_file = (file_iter_++)->path; 42 NativeFileUtil::GetFileInfo(platform_file, &file_info_); 43 return platform_file; 44 } 45 virtual int64 Size() OVERRIDE { return file_info_.size; } 46 virtual bool IsDirectory() OVERRIDE { return file_info_.is_directory; } 47 virtual base::Time LastModifiedTime() OVERRIDE { 48 return file_info_.last_modified; 49 } 50 51 private: 52 std::vector<FileInfo> files_; 53 std::vector<FileInfo>::const_iterator file_iter_; 54 base::PlatformFileInfo file_info_; 55 }; 56 57 } // namespace 58 59 //------------------------------------------------------------------------- 60 61 DraggedFileUtil::DraggedFileUtil() {} 62 63 PlatformFileError DraggedFileUtil::GetFileInfo( 64 FileSystemOperationContext* context, 65 const FileSystemURL& url, 66 PlatformFileInfo* file_info, 67 base::FilePath* platform_path) { 68 DCHECK(file_info); 69 std::string filesystem_id; 70 DCHECK(url.is_valid()); 71 if (url.path().empty()) { 72 // The root directory case. 73 // For now we leave three time fields (modified/accessed/creation time) 74 // NULL as it is not really clear what to be set for this virtual directory. 75 // TODO(kinuko): Maybe we want to set the time when this filesystem is 76 // created (i.e. when the files/directories are dropped). 77 file_info->is_directory = true; 78 file_info->is_symbolic_link = false; 79 file_info->size = 0; 80 return base::PLATFORM_FILE_OK; 81 } 82 base::PlatformFileError error = 83 NativeFileUtil::GetFileInfo(url.path(), file_info); 84 if (base::IsLink(url.path()) && !base::FilePath().IsParent(url.path())) { 85 // Don't follow symlinks unless it's the one that are selected by the user. 86 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 87 } 88 if (error == base::PLATFORM_FILE_OK) 89 *platform_path = url.path(); 90 return error; 91 } 92 93 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> 94 DraggedFileUtil::CreateFileEnumerator( 95 FileSystemOperationContext* context, 96 const FileSystemURL& root) { 97 DCHECK(root.is_valid()); 98 if (!root.path().empty()) 99 return LocalFileUtil::CreateFileEnumerator(context, root); 100 101 // Root path case. 102 std::vector<FileInfo> toplevels; 103 IsolatedContext::GetInstance()->GetDraggedFileInfo( 104 root.filesystem_id(), &toplevels); 105 return scoped_ptr<AbstractFileEnumerator>(new SetFileEnumerator(toplevels)); 106 } 107 108 } // namespace fileapi 109