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