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

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: Code review response. Created 7 years, 9 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 115278c5b7c617b72ef58d9751b86eb7ed17dfde..ef5bfe0e7391b7925235e3d6eb040399e0cbc8da 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 IO thread.
+bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) {
+ return context->file_system_context()->task_runners()->
+ io_task_runner()->RunsTasksOnCurrentThread();
+}
+
+}
+
namespace fileapi {
NativeMediaFileUtil::NativeMediaFileUtil() {
@@ -214,4 +240,51 @@ NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory(
return base::PLATFORM_FILE_OK;
}
+bool NativeMediaFileUtil::IsMediaFile(const base::FilePath& path) {
+ base::PlatformFile file_handle;
+ bool created;
+ base::PlatformFileError error;
+ error = NativeFileUtil::CreateOrOpen(path,
+ base::PLATFORM_FILE_OPEN |
vandebo (ex-Chrome) 2013/03/20 20:03:49 nit: technically line 248 and 249 are a single arg
Kevin Bailey 2013/03/21 15:53:19 Done.
+ base::PLATFORM_FILE_READ,
+ &file_handle, &created);
+ if (error != base::PLATFORM_FILE_OK) {
vandebo (ex-Chrome) 2013/03/20 20:03:49 nit: Single line if's with single line bodies may
vandebo (ex-Chrome) 2013/03/20 20:03:49 Hmm, I guess this function can fail for a reason o
Kevin Bailey 2013/03/21 15:53:19 I glanced up to line 234 to see what style the fil
Kevin Bailey 2013/03/21 15:53:19 Reason #27 why exceptions are better than return c
+ return false;
+ }
+ 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) {
vandebo (ex-Chrome) 2013/03/20 20:03:49 And here
Kevin Bailey 2013/03/21 15:53:19 Done.
+ return false;
+ }
+ std::string mime_type;
+ if (!net::SniffMimeType(buffer, len, GURL("file://" + path.value()),
+ "text/plain", &mime_type)) {
+ return false;
+ }
+ return StartsWithASCII(mime_type, "image/", true) ||
+ StartsWithASCII(mime_type, "audio/", true) ||
+ StartsWithASCII(mime_type, "video/", true);
+}
+
+base::PlatformFileError NativeMediaFileUtil::CreateSnapshotFile(
+ FileSystemOperationContext* context,
+ const FileSystemURL& url,
+ base::PlatformFileInfo* file_info,
+ base::FilePath* platform_path,
+ SnapshotFilePolicy* policy) {
+ DCHECK(policy);
vandebo (ex-Chrome) 2013/03/20 20:03:49 nit: this method doesn't actually care if policy f
Kevin Bailey 2013/03/21 15:53:19 Done.
+ DCHECK(file_info);
+ DCHECK(IsOnTaskRunnerThread(context));
+ base::PlatformFileError error = IsolatedFileUtil::CreateSnapshotFile(
+ context, url, file_info, platform_path, policy);
+ if (error != base::PLATFORM_FILE_OK)
+ return error;
+ if (!IsMediaFile(*platform_path))
+ return base::PLATFORM_FILE_ERROR_SECURITY;
+ return base::PLATFORM_FILE_OK;
+}
+
} // namespace fileapi

Powered by Google App Engine
This is Rietveld 408576698