| 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/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/memory/scoped_generic_obj.h" | |
| 9 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 10 #include "chrome/browser/media_galleries/fileapi/filtering_file_enumerator.h" | 9 #include "chrome/browser/media_galleries/fileapi/filtering_file_enumerator.h" |
| 11 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" | 10 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" |
| 12 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" | 11 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" |
| 13 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
| 14 #include "net/base/mime_sniffer.h" | 13 #include "net/base/mime_sniffer.h" |
| 15 #include "webkit/browser/fileapi/file_system_context.h" | 14 #include "webkit/browser/fileapi/file_system_context.h" |
| 16 #include "webkit/browser/fileapi/file_system_operation_context.h" | 15 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 17 #include "webkit/browser/fileapi/file_system_task_runners.h" | 16 #include "webkit/browser/fileapi/file_system_task_runners.h" |
| 18 #include "webkit/browser/fileapi/native_file_util.h" | 17 #include "webkit/browser/fileapi/native_file_util.h" |
| 19 | 18 |
| 20 using base::PlatformFile; | 19 using base::PlatformFile; |
| 21 using base::PlatformFileError; | 20 using base::PlatformFileError; |
| 22 using base::PlatformFileInfo; | 21 using base::PlatformFileInfo; |
| 23 using fileapi::FileSystemOperationContext; | 22 using fileapi::FileSystemOperationContext; |
| 24 using fileapi::FileSystemURL; | 23 using fileapi::FileSystemURL; |
| 25 using fileapi::NativeFileUtil; | 24 using fileapi::NativeFileUtil; |
| 26 | 25 |
| 27 namespace chrome { | 26 namespace chrome { |
| 28 | 27 |
| 29 namespace { | 28 namespace { |
| 30 | 29 |
| 31 // Modelled after ScopedFILEClose. | 30 // Modelled after ScopedFILEClose. |
| 32 class ScopedPlatformFileClose { | 31 struct ScopedPlatformFileClose { |
| 33 public: | 32 void operator()(base::PlatformFile* file) { |
| 34 void operator()(base::PlatformFile file) const { | 33 if (file && *file != base::kInvalidPlatformFileValue) |
| 35 if (file != base::kInvalidPlatformFileValue) | 34 base::ClosePlatformFile(*file); |
| 36 base::ClosePlatformFile(file); | |
| 37 } | 35 } |
| 38 }; | 36 }; |
| 39 | 37 |
| 40 typedef ScopedGenericObj<base::PlatformFile, | 38 typedef scoped_ptr<base::PlatformFile, ScopedPlatformFileClose> |
| 41 ScopedPlatformFileClose> ScopedPlatformFile; | 39 ScopedPlatformFile; |
| 42 | 40 |
| 43 // Returns true if the current thread is capable of doing IO. | 41 // Returns true if the current thread is capable of doing IO. |
| 44 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { | 42 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { |
| 45 return context->file_system_context()->task_runners()-> | 43 return context->file_system_context()->task_runners()-> |
| 46 media_task_runner()->RunsTasksOnCurrentThread(); | 44 media_task_runner()->RunsTasksOnCurrentThread(); |
| 47 } | 45 } |
| 48 | 46 |
| 49 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { | 47 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { |
| 50 return context->GetUserValue<MediaPathFilter*>( | 48 return context->GetUserValue<MediaPathFilter*>( |
| 51 MediaFileSystemMountPointProvider::kMediaPathFilterKey); | 49 MediaFileSystemMountPointProvider::kMediaPathFilterKey); |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 // static | 270 // static |
| 273 base::PlatformFileError NativeMediaFileUtil::IsMediaFile( | 271 base::PlatformFileError NativeMediaFileUtil::IsMediaFile( |
| 274 const base::FilePath& path) { | 272 const base::FilePath& path) { |
| 275 base::PlatformFile file_handle; | 273 base::PlatformFile file_handle; |
| 276 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 274 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
| 277 base::PlatformFileError error = | 275 base::PlatformFileError error = |
| 278 NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); | 276 NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); |
| 279 if (error != base::PLATFORM_FILE_OK) | 277 if (error != base::PLATFORM_FILE_OK) |
| 280 return error; | 278 return error; |
| 281 | 279 |
| 282 ScopedPlatformFile scoped_platform_file(file_handle); | 280 ScopedPlatformFile scoped_platform_file(&file_handle); |
| 283 char buffer[net::kMaxBytesToSniff]; | 281 char buffer[net::kMaxBytesToSniff]; |
| 284 | 282 |
| 285 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. | 283 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. |
| 286 int64 len = | 284 int64 len = |
| 287 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); | 285 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); |
| 288 if (len < 0) | 286 if (len < 0) |
| 289 return base::PLATFORM_FILE_ERROR_FAILED; | 287 return base::PLATFORM_FILE_ERROR_FAILED; |
| 290 if (len == 0) | 288 if (len == 0) |
| 291 return base::PLATFORM_FILE_ERROR_SECURITY; | 289 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 292 | 290 |
| 293 std::string mime_type; | 291 std::string mime_type; |
| 294 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) | 292 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) |
| 295 return base::PLATFORM_FILE_ERROR_SECURITY; | 293 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 296 | 294 |
| 297 if (StartsWithASCII(mime_type, "image/", true) || | 295 if (StartsWithASCII(mime_type, "image/", true) || |
| 298 StartsWithASCII(mime_type, "audio/", true) || | 296 StartsWithASCII(mime_type, "audio/", true) || |
| 299 StartsWithASCII(mime_type, "video/", true) || | 297 StartsWithASCII(mime_type, "video/", true) || |
| 300 mime_type == "application/x-shockwave-flash") { | 298 mime_type == "application/x-shockwave-flash") { |
| 301 return base::PLATFORM_FILE_OK; | 299 return base::PLATFORM_FILE_OK; |
| 302 } | 300 } |
| 303 return base::PLATFORM_FILE_ERROR_SECURITY; | 301 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 304 } | 302 } |
| 305 | 303 |
| 306 } // namespace chrome | 304 } // namespace chrome |
| OLD | NEW |