| 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 fa5087d6712595036f6d345a96e84aab694164bb..0cb1cb00ec293040eb55d5b10105fa41820c10ce 100644
|
| --- a/webkit/fileapi/media/native_media_file_util.cc
|
| +++ b/webkit/fileapi/media/native_media_file_util.cc
|
| @@ -4,7 +4,13 @@
|
|
|
| #include "webkit/fileapi/media/native_media_file_util.h"
|
|
|
| +#include "base/memory/scoped_generic_obj.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/filtering_file_enumerator.h"
|
| #include "webkit/fileapi/media/media_file_system_mount_point_provider.h"
|
| #include "webkit/fileapi/media/media_path_filter.h"
|
| @@ -14,6 +20,28 @@ using base::PlatformFile;
|
| using base::PlatformFileError;
|
| using base::PlatformFileInfo;
|
|
|
| +namespace {
|
| +
|
| +// Modelled after ScopedFILEClose.
|
| +class ScopedPlatformFileClose {
|
| + public:
|
| + void operator()(base::PlatformFile file) const {
|
| + if (file != base::kInvalidPlatformFileValue)
|
| + base::ClosePlatformFile(file);
|
| + }
|
| +};
|
| +
|
| +typedef ScopedGenericObj<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 {
|
|
|
| namespace {
|
| @@ -223,4 +251,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(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::IdentifyExtraMimeType(buffer, len, &mime_type)) {
|
| + return base::PLATFORM_FILE_ERROR_SECURITY;
|
| + }
|
| + if (StartsWithASCII(mime_type, "image/", true) ||
|
| + StartsWithASCII(mime_type, "audio/", true) ||
|
| + StartsWithASCII(mime_type, "video/", true) ||
|
| + mime_type == "application/x-shockwave-flash") {
|
| + return base::PLATFORM_FILE_OK;
|
| + } else {
|
| + return base::PLATFORM_FILE_ERROR_SECURITY;
|
| + }
|
| +}
|
| +
|
| } // namespace fileapi
|
|
|