Chromium Code Reviews| 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 IO thread. | |
| 35 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { | |
| 36 return context->file_system_context()->task_runners()-> | |
| 37 io_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::IsMediaFile( | |
| 244 const base::FilePath& path) { | |
| 245 base::PlatformFile file_handle; | |
| 246 bool created; | |
| 247 base::PlatformFileError error; | |
| 248 error = NativeFileUtil::CreateOrOpen(path, | |
| 249 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, &file_handle, | |
| 250 &created); | |
| 251 if (error != base::PLATFORM_FILE_OK) | |
| 252 return error; | |
| 253 ScopedPlatformFile scoped_platform_file(new base::PlatformFile(file_handle)); | |
| 254 char buffer[net::kMaxBytesToSniff]; | |
| 255 int64 len; | |
| 256 // Read as much as SniffMimeType() will bother looking at. | |
| 257 len = base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); | |
| 258 if (len < 0) | |
| 259 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 260 if (len == 0) | |
| 261 return base::PLATFORM_FILE_ERROR_SECURITY; | |
| 262 std::string mime_type; | |
| 263 if (!net::SniffMimeType(buffer, len, GURL("file://" + path.value()), | |
|
vandebo (ex-Chrome)
2013/03/25 19:02:11
nit: this is a multi-line if, so it should use bra
Kevin Bailey
2013/04/03 18:05:07
Done.
| |
| 264 "text/plain", &mime_type)) | |
| 265 return base::PLATFORM_FILE_ERROR_SECURITY; | |
| 266 if (StartsWithASCII(mime_type, "image/", true) || | |
| 267 StartsWithASCII(mime_type, "audio/", true) || | |
| 268 StartsWithASCII(mime_type, "video/", true)) { | |
| 269 return base::PLATFORM_FILE_OK; | |
| 270 } else { | |
| 271 return base::PLATFORM_FILE_ERROR_SECURITY; | |
| 272 } | |
| 273 } | |
| 274 | |
| 275 base::PlatformFileError NativeMediaFileUtil::CreateSnapshotFile( | |
| 276 FileSystemOperationContext* context, | |
| 277 const FileSystemURL& url, | |
| 278 base::PlatformFileInfo* file_info, | |
| 279 base::FilePath* platform_path, | |
| 280 SnapshotFilePolicy* policy) { | |
| 281 DCHECK(IsOnTaskRunnerThread(context)); | |
| 282 base::PlatformFileError error = IsolatedFileUtil::CreateSnapshotFile( | |
| 283 context, url, file_info, platform_path, policy); | |
| 284 if (error != base::PLATFORM_FILE_OK) | |
| 285 return error; | |
| 286 return IsMediaFile(*platform_path); | |
| 287 } | |
| 288 | |
| 217 } // namespace fileapi | 289 } // namespace fileapi |
| OLD | NEW |