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

Unified Diff: chrome/browser/media_galleries/fileapi/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: Addressed comments about MIME sniffer. 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: chrome/browser/media_galleries/fileapi/native_media_file_util.cc
diff --git a/chrome/browser/media_galleries/fileapi/native_media_file_util.cc b/chrome/browser/media_galleries/fileapi/native_media_file_util.cc
index 19724f94a1bb5a7d2b40abc90653bdbc78da510c..05499e4f9d78ffa25685f3ddcb0a2172233cfdfd 100644
--- a/chrome/browser/media_galleries/fileapi/native_media_file_util.cc
+++ b/chrome/browser/media_galleries/fileapi/native_media_file_util.cc
@@ -4,10 +4,16 @@
#include "chrome/browser/media_galleries/fileapi/native_media_file_util.h"
+#include "base/memory/scoped_generic_obj.h"
+#include "base/string_util.h"
#include "chrome/browser/media_galleries/fileapi/filtering_file_enumerator.h"
#include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_provider.h"
#include "chrome/browser/media_galleries/fileapi/media_path_filter.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/native_file_util.h"
using base::PlatformFile;
@@ -21,6 +27,24 @@ namespace chrome {
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();
+}
+
MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) {
return context->GetUserValue<MediaPathFilter*>(
MediaFileSystemMountPointProvider::kMediaPathFilterKey);
@@ -226,4 +250,56 @@ NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory(
return base::PLATFORM_FILE_OK;
}
+webkit_blob::ScopedFile NativeMediaFileUtil::CreateSnapshotFile(
+ fileapi::FileSystemOperationContext* context,
+ const fileapi::FileSystemURL& url,
+ base::PlatformFileError* error,
+ base::PlatformFileInfo* file_info,
+ base::FilePath* platform_path) {
+ DCHECK(IsOnTaskRunnerThread(context));
+ webkit_blob::ScopedFile file;
+ file = IsolatedFileUtil::CreateSnapshotFile(
+ context, url, error, file_info, platform_path);
+ if (*error != base::PLATFORM_FILE_OK)
+ return file.Pass();
+ *error = IsMediaFile(*platform_path);
+ if (*error == base::PLATFORM_FILE_OK)
+ return file.Pass();
+ else
vandebo (ex-Chrome) 2013/04/30 21:20:36 nit: remove else, just return.
Kevin Bailey 2013/05/02 00:54:02 Done.
+ return webkit_blob::ScopedFile();
+}
+
+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.
vandebo (ex-Chrome) 2013/04/30 21:20:36 nit: update comment.
Kevin Bailey 2013/05/02 00:54:02 Done.
+ 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 chrome

Powered by Google App Engine
This is Rietveld 408576698