| Index: webkit/fileapi/media/native_media_file_util.cc
|
| diff --git a/webkit/fileapi/media/native_media_file_util.cc b/webkit/fileapi/media/native_media_file_util.cc
|
| index 115278c5b7c617b72ef58d9751b86eb7ed17dfde..66320dbd62a320fd77c05f812d81aba02586105f 100644
|
| --- a/webkit/fileapi/media/native_media_file_util.cc
|
| +++ b/webkit/fileapi/media/native_media_file_util.cc
|
| @@ -4,7 +4,12 @@
|
|
|
| #include "webkit/fileapi/media/native_media_file_util.h"
|
|
|
| +#include "base/string_util.h"
|
| +#include "googleurl/src/gurl.h"
|
| +#include "net/base/mime_sniffer.h"
|
| +#include "webkit/fileapi/file_system_context.h"
|
| #include "webkit/fileapi/file_system_operation_context.h"
|
| +#include "webkit/fileapi/file_system_task_runners.h"
|
| #include "webkit/fileapi/media/media_path_filter.h"
|
| #include "webkit/fileapi/media/filtering_file_enumerator.h"
|
| #include "webkit/fileapi/native_file_util.h"
|
| @@ -13,6 +18,27 @@ using base::PlatformFile;
|
| using base::PlatformFileError;
|
| using base::PlatformFileInfo;
|
|
|
| +namespace {
|
| +
|
| +// Modelled after ScopedFILEClose.
|
| +class ScopedPlatformFileClose {
|
| + public:
|
| + void operator()(base::PlatformFile* ppf) const {
|
| + base::ClosePlatformFile(*ppf);
|
| + }
|
| +};
|
| +
|
| +typedef scoped_ptr<base::PlatformFile,
|
| + ScopedPlatformFileClose> ScopedPlatformFile;
|
| +
|
| +// Returns true if the current thread is capable of doing IO.
|
| +bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) {
|
| + return context->file_system_context()->task_runners()->
|
| + media_task_runner()->RunsTasksOnCurrentThread();
|
| +}
|
| +
|
| +}
|
| +
|
| namespace fileapi {
|
|
|
| NativeMediaFileUtil::NativeMediaFileUtil() {
|
| @@ -214,4 +240,51 @@ NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory(
|
| return base::PLATFORM_FILE_OK;
|
| }
|
|
|
| +base::PlatformFileError NativeMediaFileUtil::CreateSnapshotFile(
|
| + FileSystemOperationContext* context,
|
| + const FileSystemURL& url,
|
| + base::PlatformFileInfo* file_info,
|
| + base::FilePath* platform_path,
|
| + SnapshotFilePolicy* policy) {
|
| + DCHECK(IsOnTaskRunnerThread(context));
|
| + base::PlatformFileError error = IsolatedFileUtil::CreateSnapshotFile(
|
| + context, url, file_info, platform_path, policy);
|
| + if (error != base::PLATFORM_FILE_OK)
|
| + return error;
|
| + return IsMediaFile(*platform_path);
|
| +}
|
| +
|
| +base::PlatformFileError NativeMediaFileUtil::IsMediaFile(
|
| + const base::FilePath& path) {
|
| + base::PlatformFile file_handle;
|
| + bool created;
|
| + base::PlatformFileError error;
|
| + error = NativeFileUtil::CreateOrOpen(path,
|
| + base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, &file_handle,
|
| + &created);
|
| + if (error != base::PLATFORM_FILE_OK)
|
| + return error;
|
| + ScopedPlatformFile scoped_platform_file(new base::PlatformFile(file_handle));
|
| + char buffer[net::kMaxBytesToSniff];
|
| + int64 len;
|
| + // Read as much as SniffMimeType() will bother looking at.
|
| + len = base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff);
|
| + if (len < 0)
|
| + return base::PLATFORM_FILE_ERROR_FAILED;
|
| + if (len == 0)
|
| + return base::PLATFORM_FILE_ERROR_SECURITY;
|
| + std::string mime_type;
|
| + if (!net::SniffMimeType(buffer, len, GURL("file://" + path.value()),
|
| + "no/idea", &mime_type)) {
|
| + return base::PLATFORM_FILE_ERROR_SECURITY;
|
| + }
|
| + if (StartsWithASCII(mime_type, "image/", true) ||
|
| + StartsWithASCII(mime_type, "audio/", true) ||
|
| + StartsWithASCII(mime_type, "video/", true)) {
|
| + return base::PLATFORM_FILE_OK;
|
| + } else {
|
| + return base::PLATFORM_FILE_ERROR_SECURITY;
|
| + }
|
| +}
|
| +
|
| } // namespace fileapi
|
|
|