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..56d82edd358bbc16fea1c2355ad8016a954c4da5 100644 |
--- a/webkit/fileapi/media/native_media_file_util.cc |
+++ b/webkit/fileapi/media/native_media_file_util.cc |
@@ -4,15 +4,42 @@ |
#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" |
+#include <fstream> |
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 +241,54 @@ 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)); |
+ LOG(WARNING) << "NativeMediaFileUtil::CreateSnapshotFile"; |
+ std::ofstream mylog("/tmp/krblog", std::ios::app); |
+ mylog << "NativeMediaFileUtil::CreateSnapshotFile" << std::endl; |
+ 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 |