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

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: 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 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/string_util.h"
8 #include "googleurl/src/gurl.h"
9 #include "net/base/mime_sniffer.h"
10 #include "webkit/fileapi/file_system_context.h"
7 #include "webkit/fileapi/file_system_operation_context.h" 11 #include "webkit/fileapi/file_system_operation_context.h"
12 #include "webkit/fileapi/file_system_task_runners.h"
8 #include "webkit/fileapi/media/media_path_filter.h" 13 #include "webkit/fileapi/media/media_path_filter.h"
9 #include "webkit/fileapi/media/filtering_file_enumerator.h" 14 #include "webkit/fileapi/media/filtering_file_enumerator.h"
10 #include "webkit/fileapi/native_file_util.h" 15 #include "webkit/fileapi/native_file_util.h"
11 16
12 using base::PlatformFile; 17 using base::PlatformFile;
13 using base::PlatformFileError; 18 using base::PlatformFileError;
14 using base::PlatformFileInfo; 19 using base::PlatformFileInfo;
15 20
21 namespace {
22
23 // Modelled after ScopedFILEClose.
24 class ScopedPlatformFileClose {
25 public:
26 void operator()(base::PlatformFile* ppf) const {
27 base::ClosePlatformFile(*ppf);
28 }
29 };
30
31 typedef scoped_ptr<base::PlatformFile,
32 ScopedPlatformFileClose> ScopedPlatformFile;
33
34 // Returns true if the current thread is IO thread.
35 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) {
36 return context->file_system_context()->task_runners()->
37 io_task_runner()->RunsTasksOnCurrentThread();
38 }
39
40 }
41
16 namespace fileapi { 42 namespace fileapi {
17 43
18 NativeMediaFileUtil::NativeMediaFileUtil() { 44 NativeMediaFileUtil::NativeMediaFileUtil() {
19 } 45 }
20 46
21 PlatformFileError NativeMediaFileUtil::CreateOrOpen( 47 PlatformFileError NativeMediaFileUtil::CreateOrOpen(
22 FileSystemOperationContext* context, 48 FileSystemOperationContext* context,
23 const FileSystemURL& url, 49 const FileSystemURL& url,
24 int file_flags, 50 int file_flags,
25 PlatformFile* file_handle, 51 PlatformFile* file_handle,
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 233
208 if (!file_info.is_directory && 234 if (!file_info.is_directory &&
209 !context->media_path_filter()->Match(file_path)) { 235 !context->media_path_filter()->Match(file_path)) {
210 return failure_error; 236 return failure_error;
211 } 237 }
212 238
213 *local_file_path = file_path; 239 *local_file_path = file_path;
214 return base::PLATFORM_FILE_OK; 240 return base::PLATFORM_FILE_OK;
215 } 241 }
216 242
243 bool NativeMediaFileUtil::IsMediaFile(const base::FilePath& path) {
244 base::PlatformFile file_handle;
245 bool created;
246 base::PlatformFileError error;
247 error = NativeFileUtil::CreateOrOpen(path,
248 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.
249 base::PLATFORM_FILE_READ,
250 &file_handle, &created);
251 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
252 return false;
253 }
254 ScopedPlatformFile scoped_platform_file(new base::PlatformFile(file_handle));
255 char buffer[net::kMaxBytesToSniff];
256 int64 len;
257 // Read as much as SniffMimeType() will bother looking at.
258 len = base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff);
259 if (len <= 0) {
vandebo (ex-Chrome) 2013/03/20 20:03:49 And here
Kevin Bailey 2013/03/21 15:53:19 Done.
260 return false;
261 }
262 std::string mime_type;
263 if (!net::SniffMimeType(buffer, len, GURL("file://" + path.value()),
264 "text/plain", &mime_type)) {
265 return false;
266 }
267 return StartsWithASCII(mime_type, "image/", true) ||
268 StartsWithASCII(mime_type, "audio/", true) ||
269 StartsWithASCII(mime_type, "video/", true);
270 }
271
272 base::PlatformFileError NativeMediaFileUtil::CreateSnapshotFile(
273 FileSystemOperationContext* context,
274 const FileSystemURL& url,
275 base::PlatformFileInfo* file_info,
276 base::FilePath* platform_path,
277 SnapshotFilePolicy* policy) {
278 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.
279 DCHECK(file_info);
280 DCHECK(IsOnTaskRunnerThread(context));
281 base::PlatformFileError error = IsolatedFileUtil::CreateSnapshotFile(
282 context, url, file_info, platform_path, policy);
283 if (error != base::PLATFORM_FILE_OK)
284 return error;
285 if (!IsMediaFile(*platform_path))
286 return base::PLATFORM_FILE_ERROR_SECURITY;
287 return base::PLATFORM_FILE_OK;
288 }
289
217 } // namespace fileapi 290 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698