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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "webkit/fileapi/media/native_media_file_util.h" 5 #include "webkit/fileapi/media/native_media_file_util.h"
6 6
7 #include "base/memory/scoped_generic_obj.h"
8 #include "base/string_util.h"
9 #include "googleurl/src/gurl.h"
10 #include "net/base/mime_sniffer.h"
11 #include "webkit/fileapi/file_system_context.h"
7 #include "webkit/fileapi/file_system_operation_context.h" 12 #include "webkit/fileapi/file_system_operation_context.h"
13 #include "webkit/fileapi/file_system_task_runners.h"
8 #include "webkit/fileapi/media/media_path_filter.h" 14 #include "webkit/fileapi/media/media_path_filter.h"
9 #include "webkit/fileapi/media/filtering_file_enumerator.h" 15 #include "webkit/fileapi/media/filtering_file_enumerator.h"
10 #include "webkit/fileapi/native_file_util.h" 16 #include "webkit/fileapi/native_file_util.h"
11 17
12 using base::PlatformFile; 18 using base::PlatformFile;
13 using base::PlatformFileError; 19 using base::PlatformFileError;
14 using base::PlatformFileInfo; 20 using base::PlatformFileInfo;
15 21
22 namespace {
23
24 // Modelled after ScopedFILEClose.
25 class ScopedPlatformFileClose {
26 public:
27 void operator()(base::PlatformFile file) const {
28 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
29 }
30 };
31
32 typedef ScopedGenericObj<base::PlatformFile,
33 ScopedPlatformFileClose> ScopedPlatformFile;
34
35 // Returns true if the current thread is capable of doing IO.
36 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) {
37 return context->file_system_context()->task_runners()->
38 media_task_runner()->RunsTasksOnCurrentThread();
39 }
40
41 }
42
16 namespace fileapi { 43 namespace fileapi {
17 44
18 NativeMediaFileUtil::NativeMediaFileUtil() { 45 NativeMediaFileUtil::NativeMediaFileUtil() {
19 } 46 }
20 47
21 PlatformFileError NativeMediaFileUtil::CreateOrOpen( 48 PlatformFileError NativeMediaFileUtil::CreateOrOpen(
22 FileSystemOperationContext* context, 49 FileSystemOperationContext* context,
23 const FileSystemURL& url, 50 const FileSystemURL& url,
24 int file_flags, 51 int file_flags,
25 PlatformFile* file_handle, 52 PlatformFile* file_handle,
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 233
207 if (!file_info.is_directory && 234 if (!file_info.is_directory &&
208 !context->media_path_filter()->Match(file_path)) { 235 !context->media_path_filter()->Match(file_path)) {
209 return failure_error; 236 return failure_error;
210 } 237 }
211 238
212 *local_file_path = file_path; 239 *local_file_path = file_path;
213 return base::PLATFORM_FILE_OK; 240 return base::PLATFORM_FILE_OK;
214 } 241 }
215 242
243 base::PlatformFileError NativeMediaFileUtil::CreateSnapshotFile(
244 FileSystemOperationContext* context,
245 const FileSystemURL& url,
246 base::PlatformFileInfo* file_info,
247 base::FilePath* platform_path,
248 SnapshotFilePolicy* policy) {
249 DCHECK(IsOnTaskRunnerThread(context));
250 base::PlatformFileError error = IsolatedFileUtil::CreateSnapshotFile(
251 context, url, file_info, platform_path, policy);
252 if (error != base::PLATFORM_FILE_OK)
253 return error;
254 return IsMediaFile(*platform_path);
255 }
256
257 base::PlatformFileError NativeMediaFileUtil::IsMediaFile(
258 const base::FilePath& path) {
259 base::PlatformFile file_handle;
260 bool created;
261 base::PlatformFileError error;
262 error = NativeFileUtil::CreateOrOpen(path,
263 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, &file_handle,
264 &created);
265 if (error != base::PLATFORM_FILE_OK)
266 return error;
267 ScopedPlatformFile scoped_platform_file(file_handle);
268 char buffer[net::kMaxBytesToSniff];
269 int64 len;
270 // Read as much as SniffMimeType() will bother looking at.
271 len = base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff);
272 if (len < 0)
273 return base::PLATFORM_FILE_ERROR_FAILED;
274 if (len == 0)
275 return base::PLATFORM_FILE_ERROR_SECURITY;
276 std::string mime_type;
277 if (!net::SniffMimeType(buffer, len, GURL("file://" + path.value()),
278 "no/idea", &mime_type)) {
279 return base::PLATFORM_FILE_ERROR_SECURITY;
280 }
281 if (StartsWithASCII(mime_type, "image/", true) ||
282 StartsWithASCII(mime_type, "audio/", true) ||
283 StartsWithASCII(mime_type, "video/", true)) {
284 return base::PLATFORM_FILE_OK;
285 } else {
286 return base::PLATFORM_FILE_ERROR_SECURITY;
287 }
288 }
289
216 } // namespace fileapi 290 } // namespace fileapi
OLDNEW
« 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