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