Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Unified Diff: webkit/fileapi/media/native_media_file_util.cc

Issue 12703012: Have media gallery (through native media file util) use MIME sniffer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed ScopedPlatformFile. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 7c586d1bef5ef87c641db218e91c90203a3cff3c..097531bd6bd29c08610fb9a04827820d73f02519 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/media_path_filter.h"
#include "webkit/fileapi/media/filtering_file_enumerator.h"
#include "webkit/fileapi/native_file_util.h"
@@ -13,6 +19,27 @@ using base::PlatformFile;
using base::PlatformFileError;
using base::PlatformFileInfo;
+namespace {
+
+// Modelled after ScopedFILEClose.
+class ScopedPlatformFileClose {
+ public:
+ void operator()(base::PlatformFile file) const {
+ base::ClosePlatformFile(file);
vandebo (ex-Chrome) 2013/04/12 20:57:40 I think you need to check that the file handle is
Kevin Bailey 2013/04/12 22:58:35 I can of course make that tiny change, but realize
vandebo (ex-Chrome) 2013/04/15 20:01:47 It looks like you're current use of it is ok. The
+ }
+};
+
+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 {
NativeMediaFileUtil::NativeMediaFileUtil() {
@@ -213,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(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
« no previous file with comments | « webkit/fileapi/media/native_media_file_util.h ('k') | webkit/fileapi/media/native_media_file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698