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/memory/scoped_generic_obj.h" |
| 8 #include "base/string_util.h" |
7 #include "chrome/browser/media_galleries/fileapi/filtering_file_enumerator.h" | 9 #include "chrome/browser/media_galleries/fileapi/filtering_file_enumerator.h" |
8 #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" |
9 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" | 11 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" |
| 12 #include "googleurl/src/gurl.h" |
| 13 #include "net/base/mime_sniffer.h" |
| 14 #include "webkit/fileapi/file_system_context.h" |
10 #include "webkit/fileapi/file_system_operation_context.h" | 15 #include "webkit/fileapi/file_system_operation_context.h" |
| 16 #include "webkit/fileapi/file_system_task_runners.h" |
11 #include "webkit/fileapi/native_file_util.h" | 17 #include "webkit/fileapi/native_file_util.h" |
12 | 18 |
13 using base::PlatformFile; | 19 using base::PlatformFile; |
14 using base::PlatformFileError; | 20 using base::PlatformFileError; |
15 using base::PlatformFileInfo; | 21 using base::PlatformFileInfo; |
16 using fileapi::FileSystemOperationContext; | 22 using fileapi::FileSystemOperationContext; |
17 using fileapi::FileSystemURL; | 23 using fileapi::FileSystemURL; |
18 using fileapi::NativeFileUtil; | 24 using fileapi::NativeFileUtil; |
19 | 25 |
20 namespace chrome { | 26 namespace chrome { |
21 | 27 |
22 namespace { | 28 namespace { |
23 | 29 |
| 30 // Modelled after ScopedFILEClose. |
| 31 class ScopedPlatformFileClose { |
| 32 public: |
| 33 void operator()(base::PlatformFile file) const { |
| 34 if (file != base::kInvalidPlatformFileValue) |
| 35 base::ClosePlatformFile(file); |
| 36 } |
| 37 }; |
| 38 |
| 39 typedef ScopedGenericObj<base::PlatformFile, |
| 40 ScopedPlatformFileClose> ScopedPlatformFile; |
| 41 |
| 42 // Returns true if the current thread is capable of doing IO. |
| 43 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { |
| 44 return context->file_system_context()->task_runners()-> |
| 45 media_task_runner()->RunsTasksOnCurrentThread(); |
| 46 } |
| 47 |
24 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { | 48 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { |
25 return context->GetUserValue<MediaPathFilter*>( | 49 return context->GetUserValue<MediaPathFilter*>( |
26 MediaFileSystemMountPointProvider::kMediaPathFilterKey); | 50 MediaFileSystemMountPointProvider::kMediaPathFilterKey); |
27 } | 51 } |
28 | 52 |
29 } // namespace | 53 } // namespace |
30 | 54 |
31 NativeMediaFileUtil::NativeMediaFileUtil() { | 55 NativeMediaFileUtil::NativeMediaFileUtil() { |
32 } | 56 } |
33 | 57 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 243 |
220 if (!file_info.is_directory && | 244 if (!file_info.is_directory && |
221 !GetMediaPathFilter(context)->Match(file_path)) { | 245 !GetMediaPathFilter(context)->Match(file_path)) { |
222 return failure_error; | 246 return failure_error; |
223 } | 247 } |
224 | 248 |
225 *local_file_path = file_path; | 249 *local_file_path = file_path; |
226 return base::PLATFORM_FILE_OK; | 250 return base::PLATFORM_FILE_OK; |
227 } | 251 } |
228 | 252 |
| 253 webkit_blob::ScopedFile NativeMediaFileUtil::CreateSnapshotFile( |
| 254 fileapi::FileSystemOperationContext* context, |
| 255 const fileapi::FileSystemURL& url, |
| 256 base::PlatformFileError* error, |
| 257 base::PlatformFileInfo* file_info, |
| 258 base::FilePath* platform_path) { |
| 259 DCHECK(IsOnTaskRunnerThread(context)); |
| 260 webkit_blob::ScopedFile file; |
| 261 file = IsolatedFileUtil::CreateSnapshotFile( |
| 262 context, url, error, file_info, platform_path); |
| 263 if (*error != base::PLATFORM_FILE_OK) |
| 264 return file.Pass(); |
| 265 IsMediaFile(*platform_path, error); |
| 266 if (*error == base::PLATFORM_FILE_OK) |
| 267 return file.Pass(); |
| 268 return webkit_blob::ScopedFile(); |
| 269 } |
| 270 |
| 271 void NativeMediaFileUtil::IsMediaFile( |
| 272 const base::FilePath& path, |
| 273 base::PlatformFileError* error) { |
| 274 base::PlatformFile file_handle; |
| 275 bool created; |
| 276 *error = NativeFileUtil::CreateOrOpen(path, |
| 277 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, &file_handle, |
| 278 &created); |
| 279 if (*error != base::PLATFORM_FILE_OK) |
| 280 return; |
| 281 ScopedPlatformFile scoped_platform_file(file_handle); |
| 282 char buffer[net::kMaxBytesToSniff]; |
| 283 int64 len; |
| 284 // Read as much as IdentifyExtraMimeType() will bother looking at. |
| 285 len = base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); |
| 286 if (len < 0) { |
| 287 *error = base::PLATFORM_FILE_ERROR_FAILED; |
| 288 return; |
| 289 } |
| 290 if (len == 0) { |
| 291 *error = base::PLATFORM_FILE_ERROR_SECURITY; |
| 292 return; |
| 293 } |
| 294 std::string mime_type; |
| 295 if (!net::SniffMimeTypeFromLocalData(buffer, len, &mime_type)) { |
| 296 *error = base::PLATFORM_FILE_ERROR_SECURITY; |
| 297 return; |
| 298 } |
| 299 if (StartsWithASCII(mime_type, "image/", true) || |
| 300 StartsWithASCII(mime_type, "audio/", true) || |
| 301 StartsWithASCII(mime_type, "video/", true) || |
| 302 mime_type == "application/x-shockwave-flash") { |
| 303 *error = base::PLATFORM_FILE_OK; |
| 304 } else { |
| 305 *error = base::PLATFORM_FILE_ERROR_SECURITY; |
| 306 } |
| 307 } |
| 308 |
229 } // namespace chrome | 309 } // namespace chrome |
OLD | NEW |