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/string_util.h" |
| 8 #include "googleurl/src/gurl.h" |
| 9 #include "net/base/mime_sniffer.h" |
| 10 #include "webkit/fileapi/file_system_context.h" |
7 #include "webkit/fileapi/file_system_operation_context.h" | 11 #include "webkit/fileapi/file_system_operation_context.h" |
| 12 #include "webkit/fileapi/file_system_task_runners.h" |
8 #include "webkit/fileapi/media/media_path_filter.h" | 13 #include "webkit/fileapi/media/media_path_filter.h" |
9 #include "webkit/fileapi/media/filtering_file_enumerator.h" | 14 #include "webkit/fileapi/media/filtering_file_enumerator.h" |
10 #include "webkit/fileapi/native_file_util.h" | 15 #include "webkit/fileapi/native_file_util.h" |
11 | 16 |
12 using base::PlatformFile; | 17 using base::PlatformFile; |
13 using base::PlatformFileError; | 18 using base::PlatformFileError; |
14 using base::PlatformFileInfo; | 19 using base::PlatformFileInfo; |
15 | 20 |
| 21 namespace { |
| 22 |
| 23 // Modelled after ScopedFILEClose. |
| 24 class ScopedPlatformFileClose { |
| 25 public: |
| 26 void operator()(base::PlatformFile* ppf) const { |
| 27 base::ClosePlatformFile(*ppf); |
| 28 } |
| 29 }; |
| 30 |
| 31 typedef scoped_ptr<base::PlatformFile, |
| 32 ScopedPlatformFileClose> ScopedPlatformFile; |
| 33 |
| 34 // Returns true if the current thread is capable of doing IO. |
| 35 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { |
| 36 return context->file_system_context()->task_runners()-> |
| 37 media_task_runner()->RunsTasksOnCurrentThread(); |
| 38 } |
| 39 |
| 40 } |
| 41 |
16 namespace fileapi { | 42 namespace fileapi { |
17 | 43 |
18 NativeMediaFileUtil::NativeMediaFileUtil() { | 44 NativeMediaFileUtil::NativeMediaFileUtil() { |
19 } | 45 } |
20 | 46 |
21 PlatformFileError NativeMediaFileUtil::CreateOrOpen( | 47 PlatformFileError NativeMediaFileUtil::CreateOrOpen( |
22 FileSystemOperationContext* context, | 48 FileSystemOperationContext* context, |
23 const FileSystemURL& url, | 49 const FileSystemURL& url, |
24 int file_flags, | 50 int file_flags, |
25 PlatformFile* file_handle, | 51 PlatformFile* file_handle, |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 | 233 |
208 if (!file_info.is_directory && | 234 if (!file_info.is_directory && |
209 !context->media_path_filter()->Match(file_path)) { | 235 !context->media_path_filter()->Match(file_path)) { |
210 return failure_error; | 236 return failure_error; |
211 } | 237 } |
212 | 238 |
213 *local_file_path = file_path; | 239 *local_file_path = file_path; |
214 return base::PLATFORM_FILE_OK; | 240 return base::PLATFORM_FILE_OK; |
215 } | 241 } |
216 | 242 |
| 243 base::PlatformFileError NativeMediaFileUtil::CreateSnapshotFile( |
| 244 FileSystemOperationContext* context, |
| 245 const FileSystemURL& url, |
| 246 base::PlatformFileInfo* file_info, |
| 247 base::FilePath* platform_path, |
| 248 SnapshotFilePolicy* policy) { |
| 249 DCHECK(IsOnTaskRunnerThread(context)); |
| 250 base::PlatformFileError error = IsolatedFileUtil::CreateSnapshotFile( |
| 251 context, url, file_info, platform_path, policy); |
| 252 if (error != base::PLATFORM_FILE_OK) |
| 253 return error; |
| 254 return IsMediaFile(*platform_path); |
| 255 } |
| 256 |
| 257 base::PlatformFileError NativeMediaFileUtil::IsMediaFile( |
| 258 const base::FilePath& path) { |
| 259 base::PlatformFile file_handle; |
| 260 bool created; |
| 261 base::PlatformFileError error; |
| 262 error = NativeFileUtil::CreateOrOpen(path, |
| 263 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, &file_handle, |
| 264 &created); |
| 265 if (error != base::PLATFORM_FILE_OK) |
| 266 return error; |
| 267 ScopedPlatformFile scoped_platform_file(new base::PlatformFile(file_handle)); |
| 268 char buffer[net::kMaxBytesToSniff]; |
| 269 int64 len; |
| 270 // Read as much as SniffMimeType() will bother looking at. |
| 271 len = base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); |
| 272 if (len < 0) |
| 273 return base::PLATFORM_FILE_ERROR_FAILED; |
| 274 if (len == 0) |
| 275 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 276 std::string mime_type; |
| 277 if (!net::SniffMimeType(buffer, len, GURL("file://" + path.value()), |
| 278 "no/idea", &mime_type)) { |
| 279 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 280 } |
| 281 if (StartsWithASCII(mime_type, "image/", true) || |
| 282 StartsWithASCII(mime_type, "audio/", true) || |
| 283 StartsWithASCII(mime_type, "video/", true)) { |
| 284 return base::PLATFORM_FILE_OK; |
| 285 } else { |
| 286 return base::PLATFORM_FILE_ERROR_SECURITY; |
| 287 } |
| 288 } |
| 289 |
217 } // namespace fileapi | 290 } // namespace fileapi |
OLD | NEW |