| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" | 5 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 9 #include "chrome/browser/media_galleries/fileapi/filtering_file_enumerator.h" | 10 #include "base/task_runner_util.h" |
| 10 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" | 11 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" |
| 11 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" | 12 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" |
| 12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 13 #include "net/base/mime_sniffer.h" | 14 #include "net/base/mime_sniffer.h" |
| 14 #include "webkit/browser/fileapi/file_system_context.h" | 15 #include "webkit/browser/fileapi/file_system_context.h" |
| 15 #include "webkit/browser/fileapi/file_system_operation_context.h" | 16 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 16 #include "webkit/browser/fileapi/file_system_task_runners.h" | 17 #include "webkit/browser/fileapi/file_system_task_runners.h" |
| 17 #include "webkit/browser/fileapi/native_file_util.h" | 18 #include "webkit/browser/fileapi/native_file_util.h" |
| 19 #include "webkit/common/blob/shareable_file_reference.h" |
| 20 #include "webkit/common/fileapi/file_system_util.h" |
| 18 | 21 |
| 22 using base::Bind; |
| 23 using base::Owned; |
| 19 using base::PlatformFile; | 24 using base::PlatformFile; |
| 20 using base::PlatformFileError; | 25 using base::PlatformFileError; |
| 21 using base::PlatformFileInfo; | 26 using base::PlatformFileInfo; |
| 27 using base::Unretained; |
| 22 using fileapi::FileSystemOperationContext; | 28 using fileapi::FileSystemOperationContext; |
| 23 using fileapi::FileSystemURL; | 29 using fileapi::FileSystemURL; |
| 24 using fileapi::NativeFileUtil; | 30 using fileapi::NativeFileUtil; |
| 25 | 31 |
| 26 namespace chrome { | 32 namespace chrome { |
| 27 | 33 |
| 28 namespace { | 34 namespace { |
| 29 | 35 |
| 30 // Modelled after ScopedFILEClose. | 36 // Modelled after ScopedFILEClose. |
| 31 struct ScopedPlatformFileClose { | 37 struct ScopedPlatformFileClose { |
| 32 void operator()(base::PlatformFile* file) { | 38 void operator()(base::PlatformFile* file) { |
| 33 if (file && *file != base::kInvalidPlatformFileValue) | 39 if (file && *file != base::kInvalidPlatformFileValue) |
| 34 base::ClosePlatformFile(*file); | 40 base::ClosePlatformFile(*file); |
| 35 } | 41 } |
| 36 }; | 42 }; |
| 37 | 43 |
| 38 typedef scoped_ptr<base::PlatformFile, ScopedPlatformFileClose> | 44 typedef scoped_ptr<base::PlatformFile, ScopedPlatformFileClose> |
| 39 ScopedPlatformFile; | 45 ScopedPlatformFile; |
| 40 | 46 |
| 47 // Used to skip the hidden folders and files. Returns true if the file specified |
| 48 // by |path| should be skipped. |
| 49 bool ShouldSkip(const base::FilePath& path) { |
| 50 const base::FilePath::StringType base_name = path.BaseName().value(); |
| 51 if (base_name.empty()) |
| 52 return false; |
| 53 |
| 54 // Dot files (aka hidden files) |
| 55 if (base_name[0] == '.') |
| 56 return true; |
| 57 |
| 58 // Mac OS X file. |
| 59 if (base_name == FILE_PATH_LITERAL("__MACOSX")) |
| 60 return true; |
| 61 |
| 62 #if defined(OS_WIN) |
| 63 DWORD file_attributes = ::GetFileAttributes(path.value().c_str()); |
| 64 if ((file_attributes != INVALID_FILE_ATTRIBUTES) && |
| 65 ((file_attributes & FILE_ATTRIBUTE_HIDDEN) != 0)) |
| 66 return true; |
| 67 #else |
| 68 // Windows always creates a recycle bin folder in the attached device to store |
| 69 // all the deleted contents. On non-windows operating systems, there is no way |
| 70 // to get the hidden attribute of windows recycle bin folders that are present |
| 71 // on the attached device. Therefore, compare the file path name to the |
| 72 // recycle bin name and exclude those folders. For more details, please refer |
| 73 // to http://support.microsoft.com/kb/171694. |
| 74 const char win_98_recycle_bin_name[] = "RECYCLED"; |
| 75 const char win_xp_recycle_bin_name[] = "RECYCLER"; |
| 76 const char win_vista_recycle_bin_name[] = "$Recycle.bin"; |
| 77 if ((base::strncasecmp(base_name.c_str(), |
| 78 win_98_recycle_bin_name, |
| 79 strlen(win_98_recycle_bin_name)) == 0) || |
| 80 (base::strncasecmp(base_name.c_str(), |
| 81 win_xp_recycle_bin_name, |
| 82 strlen(win_xp_recycle_bin_name)) == 0) || |
| 83 (base::strncasecmp(base_name.c_str(), |
| 84 win_vista_recycle_bin_name, |
| 85 strlen(win_vista_recycle_bin_name)) == 0)) |
| 86 return true; |
| 87 #endif |
| 88 return false; |
| 89 } |
| 90 |
| 41 // Returns true if the current thread is capable of doing IO. | 91 // Returns true if the current thread is capable of doing IO. |
| 42 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { | 92 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { |
| 43 return context->file_system_context()->task_runners()-> | 93 return context->file_system_context()->task_runners()-> |
| 44 media_task_runner()->RunsTasksOnCurrentThread(); | 94 media_task_runner()->RunsTasksOnCurrentThread(); |
| 45 } | 95 } |
| 46 | 96 |
| 47 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { | 97 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { |
| 48 return context->GetUserValue<MediaPathFilter*>( | 98 return context->GetUserValue<MediaPathFilter*>( |
| 49 MediaFileSystemMountPointProvider::kMediaPathFilterKey); | 99 MediaFileSystemMountPointProvider::kMediaPathFilterKey); |
| 50 } | 100 } |
| 51 | 101 |
| 102 void FinishGetFileInfo( |
| 103 const NativeMediaFileUtil::GetFileInfoCallback& callback, |
| 104 PlatformFileInfo* file_info, |
| 105 base::FilePath* platform_path, |
| 106 const PlatformFileError& error) { |
| 107 callback.Run(error, *file_info, *platform_path); |
| 108 } |
| 109 |
| 110 void FinishReadDirectory( |
| 111 const NativeMediaFileUtil::ReadDirectoryCallback& callback, |
| 112 NativeMediaFileUtil::EntryList* file_list, |
| 113 const base::PlatformFileError& error) { |
| 114 callback.Run(error, *file_list, false /* has_more */); |
| 115 } |
| 116 |
| 117 void FinishCreateSnapshotFile( |
| 118 const NativeMediaFileUtil::CreateSnapshotFileCallback& callback, |
| 119 PlatformFileInfo* file_info, |
| 120 base::FilePath* platform_path, |
| 121 scoped_refptr<webkit_blob::ShareableFileReference>* file_ref, |
| 122 PlatformFileError error) { |
| 123 callback.Run(error, *file_info, *platform_path, *file_ref); |
| 124 } |
| 125 |
| 52 } // namespace | 126 } // namespace |
| 53 | 127 |
| 54 NativeMediaFileUtil::NativeMediaFileUtil() { | 128 NativeMediaFileUtil::NativeMediaFileUtil() { |
| 55 } | 129 } |
| 56 | 130 |
| 57 PlatformFileError NativeMediaFileUtil::CreateOrOpen( | 131 bool NativeMediaFileUtil::CreateOrOpen( |
| 58 FileSystemOperationContext* context, | 132 FileSystemOperationContext* context, |
| 59 const FileSystemURL& url, | 133 const FileSystemURL& url, |
| 60 int file_flags, | 134 int file_flags, |
| 61 PlatformFile* file_handle, | 135 const CreateOrOpenCallback& callback) { |
| 62 bool* created) { | |
| 63 // Only called by NaCl, which should not have access to media file systems. | 136 // Only called by NaCl, which should not have access to media file systems. |
| 64 return base::PLATFORM_FILE_ERROR_SECURITY; | 137 base::PlatformFile invalid_file(base::kInvalidPlatformFileValue); |
| 138 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, |
| 139 base::PassPlatformFile(&invalid_file), |
| 140 false); |
| 141 return true; |
| 65 } | 142 } |
| 66 | 143 |
| 67 PlatformFileError NativeMediaFileUtil::EnsureFileExists( | 144 bool NativeMediaFileUtil::EnsureFileExists( |
| 68 FileSystemOperationContext* context, | 145 FileSystemOperationContext* context, |
| 69 const FileSystemURL& url, bool* created) { | 146 const FileSystemURL& url, |
| 70 base::FilePath file_path; | 147 const EnsureFileExistsCallback& callback) { |
| 71 PlatformFileError error = GetFilteredLocalFilePath(context, url, &file_path); | 148 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, false); |
| 72 if (error != base::PLATFORM_FILE_OK) | 149 return true; |
| 73 return error; | |
| 74 return NativeFileUtil::EnsureFileExists(file_path, created); | |
| 75 } | 150 } |
| 76 | 151 |
| 77 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> | 152 bool NativeMediaFileUtil::CreateDirectory( |
| 78 NativeMediaFileUtil::CreateFileEnumerator( | |
| 79 FileSystemOperationContext* context, | 153 FileSystemOperationContext* context, |
| 80 const FileSystemURL& root_url) { | 154 const FileSystemURL& url, |
| 81 DCHECK(context); | 155 bool exclusive, |
| 82 return make_scoped_ptr(new FilteringFileEnumerator( | 156 bool recursive, |
| 83 IsolatedFileUtil::CreateFileEnumerator(context, root_url), | 157 const StatusCallback& callback) { |
| 84 GetMediaPathFilter(context))) | 158 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 85 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | 159 return true; |
| 86 } | 160 } |
| 87 | 161 |
| 88 PlatformFileError NativeMediaFileUtil::Touch( | 162 bool NativeMediaFileUtil::GetFileInfo( |
| 163 FileSystemOperationContext* context, |
| 164 const FileSystemURL& url, |
| 165 const GetFileInfoCallback& callback) { |
| 166 PlatformFileInfo* file_info = new PlatformFileInfo(); |
| 167 base::FilePath* platform_path = new base::FilePath(); |
| 168 return base::PostTaskAndReplyWithResult( |
| 169 context->task_runner(), |
| 170 FROM_HERE, |
| 171 Bind(&NativeMediaFileUtil::GetFileInfoSync, Unretained(this), |
| 172 context, url, file_info, platform_path), |
| 173 Bind(&FinishGetFileInfo, callback, Owned(file_info), |
| 174 Owned(platform_path))); |
| 175 } |
| 176 |
| 177 bool NativeMediaFileUtil::ReadDirectory( |
| 178 FileSystemOperationContext* context, |
| 179 const FileSystemURL& url, |
| 180 const ReadDirectoryCallback& callback) { |
| 181 EntryList* entry_list = new EntryList(); |
| 182 return base::PostTaskAndReplyWithResult( |
| 183 context->task_runner(), |
| 184 FROM_HERE, |
| 185 Bind(&NativeMediaFileUtil::ReadDirectorySync, Unretained(this), |
| 186 context, url, entry_list), |
| 187 Bind(&FinishReadDirectory, callback, Owned(entry_list))); |
| 188 } |
| 189 |
| 190 bool NativeMediaFileUtil::Touch( |
| 89 FileSystemOperationContext* context, | 191 FileSystemOperationContext* context, |
| 90 const FileSystemURL& url, | 192 const FileSystemURL& url, |
| 91 const base::Time& last_access_time, | 193 const base::Time& last_access_time, |
| 92 const base::Time& last_modified_time) { | 194 const base::Time& last_modified_time, |
| 93 base::FilePath file_path; | 195 const StatusCallback& callback) { |
| 94 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( | 196 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 95 context, | 197 return true; |
| 96 url, | |
| 97 // Touch fails for non-existent paths and filtered paths. | |
| 98 base::PLATFORM_FILE_ERROR_FAILED, | |
| 99 &file_path); | |
| 100 if (error != base::PLATFORM_FILE_OK) | |
| 101 return error; | |
| 102 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time); | |
| 103 } | 198 } |
| 104 | 199 |
| 105 PlatformFileError NativeMediaFileUtil::Truncate( | 200 bool NativeMediaFileUtil::Truncate( |
| 106 FileSystemOperationContext* context, | 201 FileSystemOperationContext* context, |
| 107 const FileSystemURL& url, | 202 const FileSystemURL& url, |
| 108 int64 length) { | 203 int64 length, |
| 109 base::FilePath file_path; | 204 const StatusCallback& callback) { |
| 110 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( | 205 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 111 context, | 206 return true; |
| 112 url, | |
| 113 // Cannot truncate paths that do not exist, or are filtered. | |
| 114 base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
| 115 &file_path); | |
| 116 if (error != base::PLATFORM_FILE_OK) | |
| 117 return error; | |
| 118 return NativeFileUtil::Truncate(file_path, length); | |
| 119 } | 207 } |
| 120 | 208 |
| 121 PlatformFileError NativeMediaFileUtil::CopyOrMoveFile( | 209 bool NativeMediaFileUtil::CopyFileLocal( |
| 122 FileSystemOperationContext* context, | 210 FileSystemOperationContext* context, |
| 123 const FileSystemURL& src_url, | 211 const FileSystemURL& src_url, |
| 124 const FileSystemURL& dest_url, | 212 const FileSystemURL& dest_url, |
| 125 bool copy) { | 213 const StatusCallback& callback) { |
| 126 base::FilePath src_file_path; | 214 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 127 PlatformFileError error = | 215 return true; |
| 128 GetFilteredLocalFilePathForExistingFileOrDirectory( | |
| 129 context, src_url, | |
| 130 base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
| 131 &src_file_path); | |
| 132 if (error != base::PLATFORM_FILE_OK) | |
| 133 return error; | |
| 134 if (NativeFileUtil::DirectoryExists(src_file_path)) | |
| 135 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
| 136 | |
| 137 base::FilePath dest_file_path; | |
| 138 error = GetLocalFilePath(context, dest_url, &dest_file_path); | |
| 139 if (error != base::PLATFORM_FILE_OK) | |
| 140 return error; | |
| 141 PlatformFileInfo file_info; | |
| 142 error = NativeFileUtil::GetFileInfo(dest_file_path, &file_info); | |
| 143 if (error != base::PLATFORM_FILE_OK && | |
| 144 error != base::PLATFORM_FILE_ERROR_NOT_FOUND) | |
| 145 return error; | |
| 146 if (error == base::PLATFORM_FILE_OK && file_info.is_directory) | |
| 147 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 148 if (!GetMediaPathFilter(context)->Match(dest_file_path)) | |
| 149 return base::PLATFORM_FILE_ERROR_SECURITY; | |
| 150 | |
| 151 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy); | |
| 152 } | 216 } |
| 153 | 217 |
| 154 PlatformFileError NativeMediaFileUtil::CopyInForeignFile( | 218 bool NativeMediaFileUtil::MoveFileLocal( |
| 219 FileSystemOperationContext* context, |
| 220 const FileSystemURL& src_url, |
| 221 const FileSystemURL& dest_url, |
| 222 const StatusCallback& callback) { |
| 223 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 224 return true; |
| 225 } |
| 226 |
| 227 bool NativeMediaFileUtil::CopyInForeignFile( |
| 155 FileSystemOperationContext* context, | 228 FileSystemOperationContext* context, |
| 156 const base::FilePath& src_file_path, | 229 const base::FilePath& src_file_path, |
| 157 const FileSystemURL& dest_url) { | 230 const FileSystemURL& dest_url, |
| 158 if (src_file_path.empty()) | 231 const StatusCallback& callback) { |
| 159 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 232 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 160 | 233 return true; |
| 161 base::FilePath dest_file_path; | |
| 162 PlatformFileError error = | |
| 163 GetFilteredLocalFilePath(context, dest_url, &dest_file_path); | |
| 164 if (error != base::PLATFORM_FILE_OK) | |
| 165 return error; | |
| 166 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true); | |
| 167 } | 234 } |
| 168 | 235 |
| 169 PlatformFileError NativeMediaFileUtil::DeleteFile( | 236 bool NativeMediaFileUtil::DeleteFile( |
| 170 FileSystemOperationContext* context, | 237 FileSystemOperationContext* context, |
| 171 const FileSystemURL& url) { | 238 const FileSystemURL& url, |
| 172 base::FilePath file_path; | 239 const StatusCallback& callback) { |
| 173 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 240 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 174 if (error != base::PLATFORM_FILE_OK) | 241 return true; |
| 175 return error; | |
| 176 PlatformFileInfo file_info; | |
| 177 error = NativeFileUtil::GetFileInfo(file_path, &file_info); | |
| 178 if (error != base::PLATFORM_FILE_OK) | |
| 179 return error; | |
| 180 if (file_info.is_directory) | |
| 181 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | |
| 182 if (!GetMediaPathFilter(context)->Match(file_path)) | |
| 183 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 184 return NativeFileUtil::DeleteFile(file_path); | |
| 185 } | 242 } |
| 186 | 243 |
| 187 PlatformFileError NativeMediaFileUtil::GetFileInfo( | 244 bool NativeMediaFileUtil::DeleteDirectory( |
| 245 FileSystemOperationContext* context, |
| 246 const FileSystemURL& url, |
| 247 const StatusCallback& callback) { |
| 248 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 249 return true; |
| 250 } |
| 251 |
| 252 bool NativeMediaFileUtil::CreateSnapshotFile( |
| 253 FileSystemOperationContext* context, |
| 254 const FileSystemURL& url, |
| 255 const CreateSnapshotFileCallback& callback) { |
| 256 PlatformFileInfo* file_info = new PlatformFileInfo(); |
| 257 base::FilePath* platform_path = new base::FilePath(); |
| 258 scoped_refptr<webkit_blob::ShareableFileReference>* file_ref = |
| 259 new scoped_refptr<webkit_blob::ShareableFileReference>(); |
| 260 return base::PostTaskAndReplyWithResult( |
| 261 context->task_runner(), |
| 262 FROM_HERE, |
| 263 Bind(&NativeMediaFileUtil::CreateSnapshotFileSync, Unretained(this), |
| 264 context, url, file_info, platform_path, file_ref), |
| 265 Bind(&FinishCreateSnapshotFile, callback, Owned(file_info), |
| 266 Owned(platform_path), Owned(file_ref))); |
| 267 } |
| 268 |
| 269 PlatformFileError NativeMediaFileUtil::GetFileInfoSync( |
| 188 FileSystemOperationContext* context, | 270 FileSystemOperationContext* context, |
| 189 const FileSystemURL& url, | 271 const FileSystemURL& url, |
| 190 PlatformFileInfo* file_info, | 272 PlatformFileInfo* file_info, |
| 191 base::FilePath* platform_path) { | 273 base::FilePath* platform_path) { |
| 192 DCHECK(context); | 274 DCHECK(context); |
| 193 DCHECK(GetMediaPathFilter(context)); | |
| 194 DCHECK(file_info); | 275 DCHECK(file_info); |
| 195 DCHECK(platform_path); | 276 DCHECK(platform_path); |
| 277 DCHECK(GetMediaPathFilter(context)); |
| 196 | 278 |
| 197 base::PlatformFileError error = | 279 base::FilePath file_path; |
| 198 IsolatedFileUtil::GetFileInfo(context, url, file_info, platform_path); | 280 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
| 281 if (error != base::PLATFORM_FILE_OK) |
| 282 return error; |
| 283 if (file_util::IsLink(file_path)) |
| 284 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 285 error = NativeFileUtil::GetFileInfo(file_path, file_info); |
| 199 if (error != base::PLATFORM_FILE_OK) | 286 if (error != base::PLATFORM_FILE_OK) |
| 200 return error; | 287 return error; |
| 201 | 288 |
| 289 *platform_path = file_path; |
| 202 if (file_info->is_directory || | 290 if (file_info->is_directory || |
| 203 GetMediaPathFilter(context)->Match(*platform_path)) { | 291 GetMediaPathFilter(context)->Match(*platform_path)) { |
| 204 return base::PLATFORM_FILE_OK; | 292 return base::PLATFORM_FILE_OK; |
| 205 } | 293 } |
| 206 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 294 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 207 } | 295 } |
| 208 | 296 |
| 209 PlatformFileError NativeMediaFileUtil::GetFilteredLocalFilePath( | 297 PlatformFileError NativeMediaFileUtil::GetLocalFilePath( |
| 210 FileSystemOperationContext* context, | 298 FileSystemOperationContext* context, |
| 211 const FileSystemURL& file_system_url, | 299 const FileSystemURL& url, |
| 212 base::FilePath* local_file_path) { | 300 base::FilePath* local_file_path) { |
| 213 base::FilePath file_path; | 301 DCHECK(local_file_path); |
| 214 PlatformFileError error = | 302 DCHECK(url.is_valid()); |
| 215 IsolatedFileUtil::GetLocalFilePath(context, file_system_url, &file_path); | 303 if (url.path().empty()) { |
| 216 if (error != base::PLATFORM_FILE_OK) | 304 // Root direcory case, which should not be accessed. |
| 217 return error; | 305 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; |
| 218 if (!GetMediaPathFilter(context)->Match(file_path)) | 306 } |
| 219 return base::PLATFORM_FILE_ERROR_SECURITY; | 307 *local_file_path = url.path(); |
| 220 | |
| 221 *local_file_path = file_path; | |
| 222 return base::PLATFORM_FILE_OK; | 308 return base::PLATFORM_FILE_OK; |
| 223 } | 309 } |
| 224 | 310 |
| 225 PlatformFileError | 311 PlatformFileError NativeMediaFileUtil::ReadDirectorySync( |
| 226 NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory( | 312 FileSystemOperationContext* context, |
| 227 FileSystemOperationContext* context, | 313 const FileSystemURL& url, |
| 228 const FileSystemURL& file_system_url, | 314 EntryList* file_list) { |
| 229 PlatformFileError failure_error, | 315 DCHECK(file_list); |
| 230 base::FilePath* local_file_path) { | 316 DCHECK(file_list->empty()); |
| 231 base::FilePath file_path; | 317 base::PlatformFileInfo file_info; |
| 232 PlatformFileError error = | 318 base::FilePath platform_path; |
| 233 GetLocalFilePath(context, file_system_url, &file_path); | 319 PlatformFileError error = GetFileInfoSync(context, url, &file_info, |
| 320 &platform_path); |
| 321 |
| 322 if (error == base::PLATFORM_FILE_OK && !file_info.is_directory) |
| 323 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
| 324 |
| 234 if (error != base::PLATFORM_FILE_OK) | 325 if (error != base::PLATFORM_FILE_OK) |
| 235 return error; | 326 return error; |
| 236 | 327 |
| 237 if (!file_util::PathExists(file_path)) | 328 file_util::FileEnumerator file_enum( |
| 238 return failure_error; | 329 platform_path, |
| 239 PlatformFileInfo file_info; | 330 false /* recursive */, |
| 240 if (!file_util::GetFileInfo(file_path, &file_info)) | 331 file_util::FileEnumerator::FILES | |
| 241 return base::PLATFORM_FILE_ERROR_FAILED; | 332 file_util::FileEnumerator::DIRECTORIES); |
| 333 file_util::FileEnumerator::FindInfo file_util_info; |
| 334 #if defined(OS_WIN) |
| 335 memset(&file_util_info, 0, sizeof(file_util_info)); |
| 336 #endif // defined(OS_WIN) |
| 242 | 337 |
| 243 if (!file_info.is_directory && | 338 for (base::FilePath platform_path = file_enum.Next(); |
| 244 !GetMediaPathFilter(context)->Match(file_path)) { | 339 !platform_path.empty(); |
| 245 return failure_error; | 340 platform_path = file_enum.Next()) { |
| 341 // Skip symlinks. |
| 342 if (file_util::IsLink(platform_path)) |
| 343 continue; |
| 344 |
| 345 file_enum.GetFindInfo(&file_util_info); |
| 346 |
| 347 // NativeMediaFileUtil skip criteria. |
| 348 if (ShouldSkip(platform_path)) |
| 349 continue; |
| 350 if (!file_util::FileEnumerator::IsDirectory(file_util_info) && |
| 351 !GetMediaPathFilter(context)->Match(platform_path)) |
| 352 continue; |
| 353 |
| 354 fileapi::DirectoryEntry entry; |
| 355 entry.is_directory = file_util::FileEnumerator::IsDirectory(file_util_info); |
| 356 entry.name = platform_path.BaseName().value(); |
| 357 entry.size = file_util::FileEnumerator::GetFilesize(file_util_info); |
| 358 entry.last_modified_time = |
| 359 file_util::FileEnumerator::GetLastModifiedTime(file_util_info); |
| 360 |
| 361 file_list->push_back(entry); |
| 246 } | 362 } |
| 247 | 363 |
| 248 *local_file_path = file_path; | |
| 249 return base::PLATFORM_FILE_OK; | 364 return base::PLATFORM_FILE_OK; |
| 250 } | 365 } |
| 251 | 366 |
| 252 webkit_blob::ScopedFile NativeMediaFileUtil::CreateSnapshotFile( | 367 PlatformFileError NativeMediaFileUtil::CreateSnapshotFileSync( |
| 253 fileapi::FileSystemOperationContext* context, | 368 FileSystemOperationContext* context, |
| 254 const fileapi::FileSystemURL& url, | 369 const fileapi::FileSystemURL& url, |
| 255 base::PlatformFileError* error, | 370 PlatformFileInfo* file_info, |
| 256 base::PlatformFileInfo* file_info, | 371 base::FilePath* platform_path, |
| 257 base::FilePath* platform_path) { | 372 scoped_refptr<webkit_blob::ShareableFileReference>* file_ref) { |
| 258 DCHECK(IsOnTaskRunnerThread(context)); | 373 PlatformFileError error = |
| 259 webkit_blob::ScopedFile file; | 374 GetFileInfoSync(context, url, file_info, platform_path); |
| 260 file = IsolatedFileUtil::CreateSnapshotFile( | 375 if (error == base::PLATFORM_FILE_OK && file_info->is_directory) |
| 261 context, url, error, file_info, platform_path); | 376 error = base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
| 262 if (*error != base::PLATFORM_FILE_OK) | 377 if (error == base::PLATFORM_FILE_OK) |
| 263 return file.Pass(); | 378 error = NativeMediaFileUtil::IsMediaFile(*platform_path); |
| 264 *error = IsMediaFile(*platform_path); | 379 |
| 265 if (*error == base::PLATFORM_FILE_OK) | 380 // We're just returning the local file information. |
| 266 return file.Pass(); | 381 *file_ref = scoped_refptr<webkit_blob::ShareableFileReference>(); |
| 267 return webkit_blob::ScopedFile(); | 382 |
| 383 return error; |
| 268 } | 384 } |
| 269 | 385 |
| 270 // static | 386 // static |
| 271 base::PlatformFileError NativeMediaFileUtil::IsMediaFile( | 387 PlatformFileError NativeMediaFileUtil::IsMediaFile( |
| 272 const base::FilePath& path) { | 388 const base::FilePath& path) { |
| 273 base::PlatformFile file_handle; | 389 base::PlatformFile file_handle; |
| 274 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 390 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
| 275 base::PlatformFileError error = | 391 PlatformFileError error = |
| 276 NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); | 392 NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); |
| 277 if (error != base::PLATFORM_FILE_OK) | 393 if (error != base::PLATFORM_FILE_OK) |
| 278 return error; | 394 return error; |
| 279 | 395 |
| 280 ScopedPlatformFile scoped_platform_file(&file_handle); | 396 ScopedPlatformFile scoped_platform_file(&file_handle); |
| 281 char buffer[net::kMaxBytesToSniff]; | 397 char buffer[net::kMaxBytesToSniff]; |
| 282 | 398 |
| 283 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. | 399 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. |
| 284 int64 len = | 400 int64 len = |
| 285 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); | 401 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); |
| 286 if (len < 0) | 402 if (len < 0) |
| 287 return base::PLATFORM_FILE_ERROR_FAILED; | 403 return base::PLATFORM_FILE_ERROR_FAILED; |
| 288 if (len == 0) | 404 if (len == 0) |
| 289 return base::PLATFORM_FILE_ERROR_SECURITY; | 405 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 290 | 406 |
| 291 std::string mime_type; | 407 std::string mime_type; |
| 292 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) | 408 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) |
| 293 return base::PLATFORM_FILE_ERROR_SECURITY; | 409 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 294 | 410 |
| 295 if (StartsWithASCII(mime_type, "image/", true) || | 411 if (StartsWithASCII(mime_type, "image/", true) || |
| 296 StartsWithASCII(mime_type, "audio/", true) || | 412 StartsWithASCII(mime_type, "audio/", true) || |
| 297 StartsWithASCII(mime_type, "video/", true) || | 413 StartsWithASCII(mime_type, "video/", true) || |
| 298 mime_type == "application/x-shockwave-flash") { | 414 mime_type == "application/x-shockwave-flash") { |
| 299 return base::PLATFORM_FILE_OK; | 415 return base::PLATFORM_FILE_OK; |
| 300 } | 416 } |
| 301 return base::PLATFORM_FILE_ERROR_SECURITY; | 417 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 302 } | 418 } |
| 303 | 419 |
| 304 } // namespace chrome | 420 } // namespace chrome |
| OLD | NEW |