| 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" |
| 13 #include "content/public/browser/browser_thread.h" |
| 12 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 13 #include "net/base/mime_sniffer.h" | 15 #include "net/base/mime_sniffer.h" |
| 14 #include "webkit/browser/fileapi/file_system_context.h" | 16 #include "webkit/browser/fileapi/file_system_context.h" |
| 15 #include "webkit/browser/fileapi/file_system_operation_context.h" | 17 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 16 #include "webkit/browser/fileapi/file_system_task_runners.h" | 18 #include "webkit/browser/fileapi/file_system_task_runners.h" |
| 17 #include "webkit/browser/fileapi/native_file_util.h" | 19 #include "webkit/browser/fileapi/native_file_util.h" |
| 18 | 20 #include "webkit/common/blob/shareable_file_reference.h" |
| 19 using base::PlatformFile; | |
| 20 using base::PlatformFileError; | |
| 21 using base::PlatformFileInfo; | |
| 22 using fileapi::FileSystemOperationContext; | |
| 23 using fileapi::FileSystemURL; | |
| 24 using fileapi::NativeFileUtil; | |
| 25 | 21 |
| 26 namespace chrome { | 22 namespace chrome { |
| 27 | 23 |
| 28 namespace { | 24 namespace { |
| 29 | 25 |
| 30 // Modelled after ScopedFILEClose. | 26 // Modelled after ScopedFILEClose. |
| 31 struct ScopedPlatformFileClose { | 27 struct ScopedPlatformFileClose { |
| 32 void operator()(base::PlatformFile* file) { | 28 void operator()(base::PlatformFile* file) { |
| 33 if (file && *file != base::kInvalidPlatformFileValue) | 29 if (file && *file != base::kInvalidPlatformFileValue) |
| 34 base::ClosePlatformFile(*file); | 30 base::ClosePlatformFile(*file); |
| 35 } | 31 } |
| 36 }; | 32 }; |
| 37 | 33 |
| 38 typedef scoped_ptr<base::PlatformFile, ScopedPlatformFileClose> | 34 typedef scoped_ptr<base::PlatformFile, ScopedPlatformFileClose> |
| 39 ScopedPlatformFile; | 35 ScopedPlatformFile; |
| 40 | 36 |
| 37 // Used to skip the hidden folders and files. Returns true if the file specified |
| 38 // by |path| should be skipped. |
| 39 bool ShouldSkip(const base::FilePath& path) { |
| 40 const base::FilePath::StringType base_name = path.BaseName().value(); |
| 41 if (base_name.empty()) |
| 42 return false; |
| 43 |
| 44 // Dot files (aka hidden files) |
| 45 if (base_name[0] == '.') |
| 46 return true; |
| 47 |
| 48 // Mac OS X file. |
| 49 if (base_name == FILE_PATH_LITERAL("__MACOSX")) |
| 50 return true; |
| 51 |
| 52 #if defined(OS_WIN) |
| 53 DWORD file_attributes = ::GetFileAttributes(path.value().c_str()); |
| 54 if ((file_attributes != INVALID_FILE_ATTRIBUTES) && |
| 55 ((file_attributes & FILE_ATTRIBUTE_HIDDEN) != 0)) |
| 56 return true; |
| 57 #else |
| 58 // Windows always creates a recycle bin folder in the attached device to store |
| 59 // all the deleted contents. On non-windows operating systems, there is no way |
| 60 // to get the hidden attribute of windows recycle bin folders that are present |
| 61 // on the attached device. Therefore, compare the file path name to the |
| 62 // recycle bin name and exclude those folders. For more details, please refer |
| 63 // to http://support.microsoft.com/kb/171694. |
| 64 const char win_98_recycle_bin_name[] = "RECYCLED"; |
| 65 const char win_xp_recycle_bin_name[] = "RECYCLER"; |
| 66 const char win_vista_recycle_bin_name[] = "$Recycle.bin"; |
| 67 if ((base::strncasecmp(base_name.c_str(), |
| 68 win_98_recycle_bin_name, |
| 69 strlen(win_98_recycle_bin_name)) == 0) || |
| 70 (base::strncasecmp(base_name.c_str(), |
| 71 win_xp_recycle_bin_name, |
| 72 strlen(win_xp_recycle_bin_name)) == 0) || |
| 73 (base::strncasecmp(base_name.c_str(), |
| 74 win_vista_recycle_bin_name, |
| 75 strlen(win_vista_recycle_bin_name)) == 0)) |
| 76 return true; |
| 77 #endif |
| 78 return false; |
| 79 } |
| 80 |
| 41 // Returns true if the current thread is capable of doing IO. | 81 // Returns true if the current thread is capable of doing IO. |
| 42 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { | 82 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { |
| 43 return context->file_system_context()->task_runners()-> | 83 return context->file_system_context()->task_runners()-> |
| 44 media_task_runner()->RunsTasksOnCurrentThread(); | 84 media_task_runner()->RunsTasksOnCurrentThread(); |
| 45 } | 85 } |
| 46 | 86 |
| 47 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { | 87 MediaPathFilter* GetMediaPathFilter( |
| 88 fileapi::FileSystemOperationContext* context) { |
| 48 return context->GetUserValue<MediaPathFilter*>( | 89 return context->GetUserValue<MediaPathFilter*>( |
| 49 MediaFileSystemMountPointProvider::kMediaPathFilterKey); | 90 MediaFileSystemMountPointProvider::kMediaPathFilterKey); |
| 50 } | 91 } |
| 51 | 92 |
| 52 } // namespace | 93 } // namespace |
| 53 | 94 |
| 54 NativeMediaFileUtil::NativeMediaFileUtil() { | 95 NativeMediaFileUtil::NativeMediaFileUtil() : weak_factory_(this) { |
| 55 } | 96 } |
| 56 | 97 |
| 57 PlatformFileError NativeMediaFileUtil::CreateOrOpen( | 98 NativeMediaFileUtil::~NativeMediaFileUtil() { |
| 58 FileSystemOperationContext* context, | 99 } |
| 59 const FileSystemURL& url, | 100 |
| 101 bool NativeMediaFileUtil::CreateOrOpen( |
| 102 fileapi::FileSystemOperationContext* context, |
| 103 const fileapi::FileSystemURL& url, |
| 60 int file_flags, | 104 int file_flags, |
| 61 PlatformFile* file_handle, | 105 const CreateOrOpenCallback& callback) { |
| 62 bool* created) { | 106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 63 // Only called by NaCl, which should not have access to media file systems. | 107 // Only called by NaCl, which should not have access to media file systems. |
| 64 return base::PLATFORM_FILE_ERROR_SECURITY; | 108 base::PlatformFile invalid_file(base::kInvalidPlatformFileValue); |
| 65 } | 109 if (!callback.is_null()) { |
| 66 | 110 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, |
| 67 PlatformFileError NativeMediaFileUtil::EnsureFileExists( | 111 base::PassPlatformFile(&invalid_file), |
| 68 FileSystemOperationContext* context, | 112 false); |
| 69 const FileSystemURL& url, bool* created) { | 113 } |
| 114 return true; |
| 115 } |
| 116 |
| 117 bool NativeMediaFileUtil::EnsureFileExists( |
| 118 fileapi::FileSystemOperationContext* context, |
| 119 const fileapi::FileSystemURL& url, |
| 120 const EnsureFileExistsCallback& callback) { |
| 121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 122 if (!callback.is_null()) |
| 123 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, false); |
| 124 return true; |
| 125 } |
| 126 |
| 127 bool NativeMediaFileUtil::CreateDirectory( |
| 128 fileapi::FileSystemOperationContext* context, |
| 129 const fileapi::FileSystemURL& url, |
| 130 bool exclusive, |
| 131 bool recursive, |
| 132 const StatusCallback& callback) { |
| 133 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 134 return context->task_runner()->PostTask( |
| 135 FROM_HERE, |
| 136 base::Bind(&NativeMediaFileUtil::CreateDirectoryOnTaskRunnerThread, |
| 137 weak_factory_.GetWeakPtr(), context, url, exclusive, |
| 138 recursive, callback)); |
| 139 } |
| 140 |
| 141 bool NativeMediaFileUtil::GetFileInfo( |
| 142 fileapi::FileSystemOperationContext* context, |
| 143 const fileapi::FileSystemURL& url, |
| 144 const GetFileInfoCallback& callback) { |
| 145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 146 return context->task_runner()->PostTask( |
| 147 FROM_HERE, |
| 148 base::Bind(&NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread, |
| 149 weak_factory_.GetWeakPtr(), context, url, callback)); |
| 150 } |
| 151 |
| 152 bool NativeMediaFileUtil::ReadDirectory( |
| 153 fileapi::FileSystemOperationContext* context, |
| 154 const fileapi::FileSystemURL& url, |
| 155 const ReadDirectoryCallback& callback) { |
| 156 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 157 return context->task_runner()->PostTask( |
| 158 FROM_HERE, |
| 159 base::Bind(&NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread, |
| 160 weak_factory_.GetWeakPtr(), context, url, callback)); |
| 161 } |
| 162 |
| 163 bool NativeMediaFileUtil::Touch( |
| 164 fileapi::FileSystemOperationContext* context, |
| 165 const fileapi::FileSystemURL& url, |
| 166 const base::Time& last_access_time, |
| 167 const base::Time& last_modified_time, |
| 168 const StatusCallback& callback) { |
| 169 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 170 if (!callback.is_null()) |
| 171 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 172 return true; |
| 173 } |
| 174 |
| 175 bool NativeMediaFileUtil::Truncate( |
| 176 fileapi::FileSystemOperationContext* context, |
| 177 const fileapi::FileSystemURL& url, |
| 178 int64 length, |
| 179 const StatusCallback& callback) { |
| 180 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 181 if (!callback.is_null()) |
| 182 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 183 return true; |
| 184 } |
| 185 |
| 186 bool NativeMediaFileUtil::CopyFileLocal( |
| 187 fileapi::FileSystemOperationContext* context, |
| 188 const fileapi::FileSystemURL& src_url, |
| 189 const fileapi::FileSystemURL& dest_url, |
| 190 const StatusCallback& callback) { |
| 191 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 192 return context->task_runner()->PostTask( |
| 193 FROM_HERE, |
| 194 base::Bind(&NativeMediaFileUtil::CopyOrMoveFileLocalOnTaskRunnerThread, |
| 195 weak_factory_.GetWeakPtr(), context, src_url, dest_url, |
| 196 true /* copy */, callback)); |
| 197 } |
| 198 |
| 199 bool NativeMediaFileUtil::MoveFileLocal( |
| 200 fileapi::FileSystemOperationContext* context, |
| 201 const fileapi::FileSystemURL& src_url, |
| 202 const fileapi::FileSystemURL& dest_url, |
| 203 const StatusCallback& callback) { |
| 204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 205 return context->task_runner()->PostTask( |
| 206 FROM_HERE, |
| 207 base::Bind(&NativeMediaFileUtil::CopyOrMoveFileLocalOnTaskRunnerThread, |
| 208 weak_factory_.GetWeakPtr(), context, src_url, dest_url, |
| 209 false /* copy */, callback)); |
| 210 } |
| 211 |
| 212 bool NativeMediaFileUtil::CopyInForeignFile( |
| 213 fileapi::FileSystemOperationContext* context, |
| 214 const base::FilePath& src_file_path, |
| 215 const fileapi::FileSystemURL& dest_url, |
| 216 const StatusCallback& callback) { |
| 217 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 218 return context->task_runner()->PostTask( |
| 219 FROM_HERE, |
| 220 base::Bind(&NativeMediaFileUtil::CopyInForeignFileOnTaskRunnerThread, |
| 221 weak_factory_.GetWeakPtr(), context, src_file_path, dest_url, |
| 222 callback)); |
| 223 } |
| 224 |
| 225 bool NativeMediaFileUtil::DeleteFile( |
| 226 fileapi::FileSystemOperationContext* context, |
| 227 const fileapi::FileSystemURL& url, |
| 228 const StatusCallback& callback) { |
| 229 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 230 if (!callback.is_null()) |
| 231 callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
| 232 return true; |
| 233 } |
| 234 |
| 235 // This is needed to support Copy and Move. |
| 236 bool NativeMediaFileUtil::DeleteDirectory( |
| 237 fileapi::FileSystemOperationContext* context, |
| 238 const fileapi::FileSystemURL& url, |
| 239 const StatusCallback& callback) { |
| 240 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 241 return context->task_runner()->PostTask( |
| 242 FROM_HERE, |
| 243 base::Bind(&NativeMediaFileUtil::DeleteDirectoryOnTaskRunnerThread, |
| 244 weak_factory_.GetWeakPtr(), context, url, callback)); |
| 245 } |
| 246 |
| 247 bool NativeMediaFileUtil::CreateSnapshotFile( |
| 248 fileapi::FileSystemOperationContext* context, |
| 249 const fileapi::FileSystemURL& url, |
| 250 const CreateSnapshotFileCallback& callback) { |
| 251 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 252 return context->task_runner()->PostTask( |
| 253 FROM_HERE, |
| 254 base::Bind(&NativeMediaFileUtil::CreateSnapshotFileOnTaskRunnerThread, |
| 255 weak_factory_.GetWeakPtr(), context, url, callback)); |
| 256 } |
| 257 |
| 258 base::PlatformFileError NativeMediaFileUtil::CreateDirectorySync( |
| 259 fileapi::FileSystemOperationContext* context, |
| 260 const fileapi::FileSystemURL& url, |
| 261 bool exclusive, |
| 262 bool recursive) { |
| 70 base::FilePath file_path; | 263 base::FilePath file_path; |
| 71 PlatformFileError error = GetFilteredLocalFilePath(context, url, &file_path); | 264 base::PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
| 72 if (error != base::PLATFORM_FILE_OK) | 265 if (error != base::PLATFORM_FILE_OK) |
| 73 return error; | 266 return error; |
| 74 return NativeFileUtil::EnsureFileExists(file_path, created); | 267 return fileapi::NativeFileUtil::CreateDirectory(file_path, exclusive, |
| 75 } | 268 recursive); |
| 76 | 269 } |
| 77 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> | 270 |
| 78 NativeMediaFileUtil::CreateFileEnumerator( | 271 base::PlatformFileError NativeMediaFileUtil::GetLocalFilePath( |
| 79 FileSystemOperationContext* context, | 272 fileapi::FileSystemOperationContext* context, |
| 80 const FileSystemURL& root_url) { | 273 const fileapi::FileSystemURL& url, |
| 81 DCHECK(context); | 274 base::FilePath* local_file_path) { |
| 82 return make_scoped_ptr(new FilteringFileEnumerator( | 275 DCHECK(local_file_path); |
| 83 IsolatedFileUtil::CreateFileEnumerator(context, root_url), | 276 DCHECK(url.is_valid()); |
| 84 GetMediaPathFilter(context))) | 277 if (url.path().empty()) { |
| 85 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | 278 // Root direcory case, which should not be accessed. |
| 86 } | 279 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; |
| 87 | 280 } |
| 88 PlatformFileError NativeMediaFileUtil::Touch( | 281 *local_file_path = url.path(); |
| 89 FileSystemOperationContext* context, | 282 return base::PLATFORM_FILE_OK; |
| 90 const FileSystemURL& url, | 283 } |
| 91 const base::Time& last_access_time, | 284 |
| 92 const base::Time& last_modified_time) { | 285 base::PlatformFileError NativeMediaFileUtil::ReadDirectorySync( |
| 93 base::FilePath file_path; | 286 fileapi::FileSystemOperationContext* context, |
| 94 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( | 287 const fileapi::FileSystemURL& url, |
| 95 context, | 288 EntryList* file_list) { |
| 96 url, | 289 DCHECK(IsOnTaskRunnerThread(context)); |
| 97 // Touch fails for non-existent paths and filtered paths. | 290 DCHECK(file_list); |
| 98 base::PLATFORM_FILE_ERROR_FAILED, | 291 DCHECK(file_list->empty()); |
| 99 &file_path); | 292 base::PlatformFileInfo file_info; |
| 293 base::FilePath platform_path; |
| 294 base::PlatformFileError error = GetFileInfoSync(context, url, &file_info, |
| 295 &platform_path); |
| 296 |
| 100 if (error != base::PLATFORM_FILE_OK) | 297 if (error != base::PLATFORM_FILE_OK) |
| 101 return error; | 298 return error; |
| 102 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time); | 299 |
| 103 } | 300 if (!file_info.is_directory) |
| 104 | 301 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
| 105 PlatformFileError NativeMediaFileUtil::Truncate( | 302 |
| 106 FileSystemOperationContext* context, | 303 file_util::FileEnumerator file_enum( |
| 107 const FileSystemURL& url, | 304 platform_path, |
| 108 int64 length) { | 305 false /* recursive */, |
| 109 base::FilePath file_path; | 306 file_util::FileEnumerator::FILES | |
| 110 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( | 307 file_util::FileEnumerator::DIRECTORIES); |
| 111 context, | 308 file_util::FileEnumerator::FindInfo file_util_info; |
| 112 url, | 309 #if defined(OS_WIN) |
| 113 // Cannot truncate paths that do not exist, or are filtered. | 310 memset(&file_util_info, 0, sizeof(file_util_info)); |
| 114 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 311 #endif // defined(OS_WIN) |
| 115 &file_path); | 312 |
| 116 if (error != base::PLATFORM_FILE_OK) | 313 for (base::FilePath platform_path = file_enum.Next(); |
| 117 return error; | 314 !platform_path.empty(); |
| 118 return NativeFileUtil::Truncate(file_path, length); | 315 platform_path = file_enum.Next()) { |
| 119 } | 316 // Skip symlinks. |
| 120 | 317 if (file_util::IsLink(platform_path)) |
| 121 PlatformFileError NativeMediaFileUtil::CopyOrMoveFile( | 318 continue; |
| 122 FileSystemOperationContext* context, | 319 |
| 123 const FileSystemURL& src_url, | 320 file_enum.GetFindInfo(&file_util_info); |
| 124 const FileSystemURL& dest_url, | 321 |
| 322 // NativeMediaFileUtil skip criteria. |
| 323 if (ShouldSkip(platform_path)) |
| 324 continue; |
| 325 if (!file_util::FileEnumerator::IsDirectory(file_util_info) && |
| 326 !GetMediaPathFilter(context)->Match(platform_path)) |
| 327 continue; |
| 328 |
| 329 fileapi::DirectoryEntry entry; |
| 330 entry.is_directory = file_util::FileEnumerator::IsDirectory(file_util_info); |
| 331 entry.name = platform_path.BaseName().value(); |
| 332 entry.size = file_util::FileEnumerator::GetFilesize(file_util_info); |
| 333 entry.last_modified_time = |
| 334 file_util::FileEnumerator::GetLastModifiedTime(file_util_info); |
| 335 |
| 336 file_list->push_back(entry); |
| 337 } |
| 338 |
| 339 return base::PLATFORM_FILE_OK; |
| 340 } |
| 341 |
| 342 base::PlatformFileError NativeMediaFileUtil::CopyOrMoveFileSync( |
| 343 fileapi::FileSystemOperationContext* context, |
| 344 const fileapi::FileSystemURL& src_url, |
| 345 const fileapi::FileSystemURL& dest_url, |
| 125 bool copy) { | 346 bool copy) { |
| 347 DCHECK(IsOnTaskRunnerThread(context)); |
| 126 base::FilePath src_file_path; | 348 base::FilePath src_file_path; |
| 127 PlatformFileError error = | 349 base::PlatformFileError error = |
| 128 GetFilteredLocalFilePathForExistingFileOrDirectory( | 350 GetFilteredLocalFilePathForExistingFileOrDirectory( |
| 129 context, src_url, | 351 context, src_url, |
| 130 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 352 base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 131 &src_file_path); | 353 &src_file_path); |
| 132 if (error != base::PLATFORM_FILE_OK) | 354 if (error != base::PLATFORM_FILE_OK) |
| 133 return error; | 355 return error; |
| 134 if (NativeFileUtil::DirectoryExists(src_file_path)) | 356 if (fileapi::NativeFileUtil::DirectoryExists(src_file_path)) |
| 135 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | 357 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
| 136 | 358 |
| 137 base::FilePath dest_file_path; | 359 base::FilePath dest_file_path; |
| 138 error = GetLocalFilePath(context, dest_url, &dest_file_path); | 360 error = GetLocalFilePath(context, dest_url, &dest_file_path); |
| 139 if (error != base::PLATFORM_FILE_OK) | 361 if (error != base::PLATFORM_FILE_OK) |
| 140 return error; | 362 return error; |
| 141 PlatformFileInfo file_info; | 363 base::PlatformFileInfo file_info; |
| 142 error = NativeFileUtil::GetFileInfo(dest_file_path, &file_info); | 364 error = fileapi::NativeFileUtil::GetFileInfo(dest_file_path, &file_info); |
| 143 if (error != base::PLATFORM_FILE_OK && | 365 if (error != base::PLATFORM_FILE_OK && |
| 144 error != base::PLATFORM_FILE_ERROR_NOT_FOUND) | 366 error != base::PLATFORM_FILE_ERROR_NOT_FOUND) |
| 145 return error; | 367 return error; |
| 146 if (error == base::PLATFORM_FILE_OK && file_info.is_directory) | 368 if (error == base::PLATFORM_FILE_OK && file_info.is_directory) |
| 147 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 369 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| 148 if (!GetMediaPathFilter(context)->Match(dest_file_path)) | 370 if (!GetMediaPathFilter(context)->Match(dest_file_path)) |
| 149 return base::PLATFORM_FILE_ERROR_SECURITY; | 371 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 150 | 372 |
| 151 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy); | 373 return fileapi::NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, |
| 374 copy); |
| 152 } | 375 } |
| 153 | 376 |
| 154 PlatformFileError NativeMediaFileUtil::CopyInForeignFile( | 377 base::PlatformFileError NativeMediaFileUtil::CopyInForeignFileSync( |
| 155 FileSystemOperationContext* context, | 378 fileapi::FileSystemOperationContext* context, |
| 156 const base::FilePath& src_file_path, | 379 const base::FilePath& src_file_path, |
| 157 const FileSystemURL& dest_url) { | 380 const fileapi::FileSystemURL& dest_url) { |
| 381 DCHECK(IsOnTaskRunnerThread(context)); |
| 158 if (src_file_path.empty()) | 382 if (src_file_path.empty()) |
| 159 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 383 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| 160 | 384 |
| 161 base::FilePath dest_file_path; | 385 base::FilePath dest_file_path; |
| 162 PlatformFileError error = | 386 base::PlatformFileError error = |
| 163 GetFilteredLocalFilePath(context, dest_url, &dest_file_path); | 387 GetFilteredLocalFilePath(context, dest_url, &dest_file_path); |
| 164 if (error != base::PLATFORM_FILE_OK) | 388 if (error != base::PLATFORM_FILE_OK) |
| 165 return error; | 389 return error; |
| 166 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true); | 390 return fileapi::NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, |
| 391 true); |
| 167 } | 392 } |
| 168 | 393 |
| 169 PlatformFileError NativeMediaFileUtil::DeleteFile( | 394 base::PlatformFileError NativeMediaFileUtil::GetFilteredLocalFilePath( |
| 170 FileSystemOperationContext* context, | 395 fileapi::FileSystemOperationContext* context, |
| 171 const FileSystemURL& url) { | 396 const fileapi::FileSystemURL& file_system_url, |
| 397 base::FilePath* local_file_path) { |
| 398 DCHECK(IsOnTaskRunnerThread(context)); |
| 172 base::FilePath file_path; | 399 base::FilePath file_path; |
| 173 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | |
| 174 if (error != base::PLATFORM_FILE_OK) | |
| 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 } | |
| 186 | |
| 187 PlatformFileError NativeMediaFileUtil::GetFileInfo( | |
| 188 FileSystemOperationContext* context, | |
| 189 const FileSystemURL& url, | |
| 190 PlatformFileInfo* file_info, | |
| 191 base::FilePath* platform_path) { | |
| 192 DCHECK(context); | |
| 193 DCHECK(GetMediaPathFilter(context)); | |
| 194 DCHECK(file_info); | |
| 195 DCHECK(platform_path); | |
| 196 | |
| 197 base::PlatformFileError error = | 400 base::PlatformFileError error = |
| 198 IsolatedFileUtil::GetFileInfo(context, url, file_info, platform_path); | 401 GetLocalFilePath(context, file_system_url, &file_path); |
| 199 if (error != base::PLATFORM_FILE_OK) | |
| 200 return error; | |
| 201 | |
| 202 if (file_info->is_directory || | |
| 203 GetMediaPathFilter(context)->Match(*platform_path)) { | |
| 204 return base::PLATFORM_FILE_OK; | |
| 205 } | |
| 206 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 207 } | |
| 208 | |
| 209 PlatformFileError NativeMediaFileUtil::GetFilteredLocalFilePath( | |
| 210 FileSystemOperationContext* context, | |
| 211 const FileSystemURL& file_system_url, | |
| 212 base::FilePath* local_file_path) { | |
| 213 base::FilePath file_path; | |
| 214 PlatformFileError error = | |
| 215 IsolatedFileUtil::GetLocalFilePath(context, file_system_url, &file_path); | |
| 216 if (error != base::PLATFORM_FILE_OK) | 402 if (error != base::PLATFORM_FILE_OK) |
| 217 return error; | 403 return error; |
| 218 if (!GetMediaPathFilter(context)->Match(file_path)) | 404 if (!GetMediaPathFilter(context)->Match(file_path)) |
| 219 return base::PLATFORM_FILE_ERROR_SECURITY; | 405 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 220 | 406 |
| 221 *local_file_path = file_path; | 407 *local_file_path = file_path; |
| 222 return base::PLATFORM_FILE_OK; | 408 return base::PLATFORM_FILE_OK; |
| 223 } | 409 } |
| 224 | 410 |
| 225 PlatformFileError | 411 base::PlatformFileError NativeMediaFileUtil::GetFileInfoSync( |
| 412 fileapi::FileSystemOperationContext* context, |
| 413 const fileapi::FileSystemURL& url, |
| 414 base::PlatformFileInfo* file_info, |
| 415 base::FilePath* platform_path) { |
| 416 DCHECK(IsOnTaskRunnerThread(context)); |
| 417 DCHECK(context); |
| 418 DCHECK(file_info); |
| 419 DCHECK(GetMediaPathFilter(context)); |
| 420 |
| 421 base::FilePath file_path; |
| 422 base::PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
| 423 if (error != base::PLATFORM_FILE_OK) |
| 424 return error; |
| 425 if (file_util::IsLink(file_path)) |
| 426 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 427 error = fileapi::NativeFileUtil::GetFileInfo(file_path, file_info); |
| 428 if (error != base::PLATFORM_FILE_OK) |
| 429 return error; |
| 430 |
| 431 if (platform_path) |
| 432 *platform_path = file_path; |
| 433 if (file_info->is_directory || |
| 434 GetMediaPathFilter(context)->Match(file_path)) { |
| 435 return base::PLATFORM_FILE_OK; |
| 436 } |
| 437 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 438 } |
| 439 |
| 440 base::PlatformFileError |
| 226 NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory( | 441 NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory( |
| 227 FileSystemOperationContext* context, | 442 fileapi::FileSystemOperationContext* context, |
| 228 const FileSystemURL& file_system_url, | 443 const fileapi::FileSystemURL& file_system_url, |
| 229 PlatformFileError failure_error, | 444 base::PlatformFileError failure_error, |
| 230 base::FilePath* local_file_path) { | 445 base::FilePath* local_file_path) { |
| 446 DCHECK(IsOnTaskRunnerThread(context)); |
| 231 base::FilePath file_path; | 447 base::FilePath file_path; |
| 232 PlatformFileError error = | 448 base::PlatformFileError error = |
| 233 GetLocalFilePath(context, file_system_url, &file_path); | 449 GetLocalFilePath(context, file_system_url, &file_path); |
| 234 if (error != base::PLATFORM_FILE_OK) | 450 if (error != base::PLATFORM_FILE_OK) |
| 235 return error; | 451 return error; |
| 236 | 452 |
| 237 if (!file_util::PathExists(file_path)) | 453 if (!file_util::PathExists(file_path)) |
| 238 return failure_error; | 454 return failure_error; |
| 239 PlatformFileInfo file_info; | 455 base::PlatformFileInfo file_info; |
| 240 if (!file_util::GetFileInfo(file_path, &file_info)) | 456 if (!file_util::GetFileInfo(file_path, &file_info)) |
| 241 return base::PLATFORM_FILE_ERROR_FAILED; | 457 return base::PLATFORM_FILE_ERROR_FAILED; |
| 242 | 458 |
| 243 if (!file_info.is_directory && | 459 if (!file_info.is_directory && |
| 244 !GetMediaPathFilter(context)->Match(file_path)) { | 460 !GetMediaPathFilter(context)->Match(file_path)) { |
| 245 return failure_error; | 461 return failure_error; |
| 246 } | 462 } |
| 247 | 463 |
| 248 *local_file_path = file_path; | 464 *local_file_path = file_path; |
| 249 return base::PLATFORM_FILE_OK; | 465 return base::PLATFORM_FILE_OK; |
| 250 } | 466 } |
| 251 | 467 |
| 252 webkit_blob::ScopedFile NativeMediaFileUtil::CreateSnapshotFile( | 468 base::PlatformFileError NativeMediaFileUtil::DeleteDirectorySync( |
| 469 fileapi::FileSystemOperationContext* context, |
| 470 const fileapi::FileSystemURL& url) { |
| 471 DCHECK(IsOnTaskRunnerThread(context)); |
| 472 base::FilePath file_path; |
| 473 base::PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
| 474 if (error != base::PLATFORM_FILE_OK) |
| 475 return error; |
| 476 return fileapi::NativeFileUtil::DeleteDirectory(file_path); |
| 477 } |
| 478 |
| 479 base::PlatformFileError NativeMediaFileUtil::CreateSnapshotFileSync( |
| 253 fileapi::FileSystemOperationContext* context, | 480 fileapi::FileSystemOperationContext* context, |
| 254 const fileapi::FileSystemURL& url, | 481 const fileapi::FileSystemURL& url, |
| 255 base::PlatformFileError* error, | |
| 256 base::PlatformFileInfo* file_info, | 482 base::PlatformFileInfo* file_info, |
| 257 base::FilePath* platform_path) { | 483 base::FilePath* platform_path, |
| 484 scoped_refptr<webkit_blob::ShareableFileReference>* file_ref) { |
| 258 DCHECK(IsOnTaskRunnerThread(context)); | 485 DCHECK(IsOnTaskRunnerThread(context)); |
| 259 webkit_blob::ScopedFile file; | 486 base::PlatformFileError error = |
| 260 file = IsolatedFileUtil::CreateSnapshotFile( | 487 GetFileInfoSync(context, url, file_info, platform_path); |
| 261 context, url, error, file_info, platform_path); | 488 if (error == base::PLATFORM_FILE_OK && file_info->is_directory) |
| 262 if (*error != base::PLATFORM_FILE_OK) | 489 error = base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
| 263 return file.Pass(); | 490 if (error == base::PLATFORM_FILE_OK) |
| 264 *error = IsMediaFile(*platform_path); | 491 error = NativeMediaFileUtil::IsMediaFile(*platform_path); |
| 265 if (*error == base::PLATFORM_FILE_OK) | 492 |
| 266 return file.Pass(); | 493 // We're just returning the local file information. |
| 267 return webkit_blob::ScopedFile(); | 494 *file_ref = scoped_refptr<webkit_blob::ShareableFileReference>(); |
| 495 |
| 496 return error; |
| 268 } | 497 } |
| 269 | 498 |
| 270 // static | 499 // static |
| 271 base::PlatformFileError NativeMediaFileUtil::IsMediaFile( | 500 base::PlatformFileError NativeMediaFileUtil::IsMediaFile( |
| 272 const base::FilePath& path) { | 501 const base::FilePath& path) { |
| 273 base::PlatformFile file_handle; | 502 base::PlatformFile file_handle; |
| 274 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 503 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
| 275 base::PlatformFileError error = | 504 base::PlatformFileError error = |
| 276 NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); | 505 fileapi::NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); |
| 277 if (error != base::PLATFORM_FILE_OK) | 506 if (error != base::PLATFORM_FILE_OK) |
| 278 return error; | 507 return error; |
| 279 | 508 |
| 280 ScopedPlatformFile scoped_platform_file(&file_handle); | 509 ScopedPlatformFile scoped_platform_file(&file_handle); |
| 281 char buffer[net::kMaxBytesToSniff]; | 510 char buffer[net::kMaxBytesToSniff]; |
| 282 | 511 |
| 283 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. | 512 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. |
| 284 int64 len = | 513 int64 len = |
| 285 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); | 514 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); |
| 286 if (len < 0) | 515 if (len < 0) |
| 287 return base::PLATFORM_FILE_ERROR_FAILED; | 516 return base::PLATFORM_FILE_ERROR_FAILED; |
| 288 if (len == 0) | 517 if (len == 0) |
| 289 return base::PLATFORM_FILE_ERROR_SECURITY; | 518 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 290 | 519 |
| 291 std::string mime_type; | 520 std::string mime_type; |
| 292 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) | 521 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) |
| 293 return base::PLATFORM_FILE_ERROR_SECURITY; | 522 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 294 | 523 |
| 295 if (StartsWithASCII(mime_type, "image/", true) || | 524 if (StartsWithASCII(mime_type, "image/", true) || |
| 296 StartsWithASCII(mime_type, "audio/", true) || | 525 StartsWithASCII(mime_type, "audio/", true) || |
| 297 StartsWithASCII(mime_type, "video/", true) || | 526 StartsWithASCII(mime_type, "video/", true) || |
| 298 mime_type == "application/x-shockwave-flash") { | 527 mime_type == "application/x-shockwave-flash") { |
| 299 return base::PLATFORM_FILE_OK; | 528 return base::PLATFORM_FILE_OK; |
| 300 } | 529 } |
| 301 return base::PLATFORM_FILE_ERROR_SECURITY; | 530 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 302 } | 531 } |
| 303 | 532 |
| 533 void NativeMediaFileUtil::CreateDirectoryOnTaskRunnerThread( |
| 534 fileapi::FileSystemOperationContext* context, |
| 535 const fileapi::FileSystemURL& url, |
| 536 bool exclusive, |
| 537 bool recursive, |
| 538 const StatusCallback& callback) { |
| 539 DCHECK(IsOnTaskRunnerThread(context)); |
| 540 base::PlatformFileError error = |
| 541 CreateDirectorySync(context, url, exclusive, recursive); |
| 542 if (callback.is_null()) |
| 543 return; |
| 544 content::BrowserThread::PostTask( |
| 545 content::BrowserThread::IO, |
| 546 FROM_HERE, |
| 547 base::Bind(callback, error)); |
| 548 } |
| 549 |
| 550 void NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread( |
| 551 fileapi::FileSystemOperationContext* context, |
| 552 const fileapi::FileSystemURL& url, |
| 553 const GetFileInfoCallback& callback) { |
| 554 DCHECK(IsOnTaskRunnerThread(context)); |
| 555 base::PlatformFileInfo file_info; |
| 556 base::FilePath platform_path; |
| 557 base::PlatformFileError error = |
| 558 GetFileInfoSync(context, url, &file_info, &platform_path); |
| 559 if (callback.is_null()) |
| 560 return; |
| 561 content::BrowserThread::PostTask( |
| 562 content::BrowserThread::IO, |
| 563 FROM_HERE, |
| 564 base::Bind(callback, error, file_info, platform_path)); |
| 565 } |
| 566 |
| 567 void NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread( |
| 568 fileapi::FileSystemOperationContext* context, |
| 569 const fileapi::FileSystemURL& url, |
| 570 const ReadDirectoryCallback& callback) { |
| 571 DCHECK(IsOnTaskRunnerThread(context)); |
| 572 EntryList entry_list; |
| 573 base::PlatformFileError error = |
| 574 ReadDirectorySync(context, url, &entry_list); |
| 575 if (callback.is_null()) |
| 576 return; |
| 577 content::BrowserThread::PostTask( |
| 578 content::BrowserThread::IO, |
| 579 FROM_HERE, |
| 580 base::Bind(callback, error, entry_list, false /* has_more */)); |
| 581 } |
| 582 |
| 583 void NativeMediaFileUtil::CopyOrMoveFileLocalOnTaskRunnerThread( |
| 584 fileapi::FileSystemOperationContext* context, |
| 585 const fileapi::FileSystemURL& src_url, |
| 586 const fileapi::FileSystemURL& dest_url, |
| 587 bool copy, |
| 588 const StatusCallback& callback) { |
| 589 DCHECK(IsOnTaskRunnerThread(context)); |
| 590 base::PlatformFileError error = |
| 591 CopyOrMoveFileSync(context, src_url, dest_url, copy); |
| 592 if (callback.is_null()) |
| 593 return; |
| 594 content::BrowserThread::PostTask( |
| 595 content::BrowserThread::IO, |
| 596 FROM_HERE, |
| 597 base::Bind(callback, error)); |
| 598 } |
| 599 |
| 600 void NativeMediaFileUtil::CopyInForeignFileOnTaskRunnerThread( |
| 601 fileapi::FileSystemOperationContext* context, |
| 602 const base::FilePath& src_file_path, |
| 603 const fileapi::FileSystemURL& dest_url, |
| 604 const StatusCallback& callback) { |
| 605 DCHECK(IsOnTaskRunnerThread(context)); |
| 606 base::PlatformFileError error = |
| 607 CopyInForeignFileSync(context, src_file_path, dest_url); |
| 608 if (callback.is_null()) |
| 609 return; |
| 610 content::BrowserThread::PostTask( |
| 611 content::BrowserThread::IO, |
| 612 FROM_HERE, |
| 613 base::Bind(callback, error)); |
| 614 } |
| 615 |
| 616 void NativeMediaFileUtil::DeleteDirectoryOnTaskRunnerThread( |
| 617 fileapi::FileSystemOperationContext* context, |
| 618 const fileapi::FileSystemURL& url, |
| 619 const StatusCallback& callback) { |
| 620 DCHECK(IsOnTaskRunnerThread(context)); |
| 621 base::PlatformFileError error = DeleteDirectorySync(context, url); |
| 622 if (callback.is_null()) |
| 623 return; |
| 624 content::BrowserThread::PostTask( |
| 625 content::BrowserThread::IO, |
| 626 FROM_HERE, |
| 627 base::Bind(callback, error)); |
| 628 } |
| 629 |
| 630 void NativeMediaFileUtil::CreateSnapshotFileOnTaskRunnerThread( |
| 631 fileapi::FileSystemOperationContext* context, |
| 632 const fileapi::FileSystemURL& url, |
| 633 const CreateSnapshotFileCallback& callback) { |
| 634 DCHECK(IsOnTaskRunnerThread(context)); |
| 635 base::PlatformFileInfo file_info; |
| 636 base::FilePath platform_path; |
| 637 scoped_refptr<webkit_blob::ShareableFileReference> file_ref; |
| 638 base::PlatformFileError error = |
| 639 CreateSnapshotFileSync(context, url, &file_info, &platform_path, |
| 640 &file_ref); |
| 641 if (callback.is_null()) |
| 642 return; |
| 643 content::BrowserThread::PostTask( |
| 644 content::BrowserThread::IO, |
| 645 FROM_HERE, |
| 646 base::Bind(callback, error, file_info, platform_path, file_ref)); |
| 647 } |
| 648 |
| 304 } // namespace chrome | 649 } // namespace chrome |
| OLD | NEW |