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