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

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: 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
« no previous file with comments | « webkit/fileapi/media/native_media_file_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 IsOnIOThread(fileapi::FileSystemOperationContext* context) {
vandebo (ex-Chrome) 2013/03/20 00:18:27 nit: There's a thread called the IO thread, so thi
Kevin Bailey 2013/03/20 13:53:23 And that's what I get for plagiarizing. This was c
vandebo (ex-Chrome) 2013/03/20 20:03:48 Hmm, I'm not completely familiar with the differen
Kevin Bailey 2013/03/21 15:53:19 It's identical to what's in device_media_async_fil
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 if (NativeFileUtil::CreateOrOpen(path,
247 base::PLATFORM_FILE_OPEN |
248 base::PLATFORM_FILE_READ,
249 &file_handle, &created)
250 != base::PLATFORM_FILE_OK) {
vandebo (ex-Chrome) 2013/03/20 00:18:27 To improve formatting, assign the result of Create
Kevin Bailey 2013/03/20 13:53:23 Done.
251 return false;
252 }
253 ScopedPlatformFile scoped_platform_file(
254 new base::PlatformFile(file_handle));
vandebo (ex-Chrome) 2013/03/20 00:18:27 nit: will fit on the previous line.
Kevin Bailey 2013/03/20 13:53:23 Ok, but one stray rename...
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 // Let the SniffMimeType() function decide what to do
vandebo (ex-Chrome) 2013/03/20 00:18:27 Actually, I think we can pretty safely say that a
Kevin Bailey 2013/03/20 13:53:23 Fair enough, 1) I wanted the policy decisions in o
vandebo (ex-Chrome) 2013/03/20 20:03:48 I don't feel strongly, if you'd prefer to leave it
Kevin Bailey 2013/03/21 15:53:19 Ack.
260 // with a 0 length file.
261 if (len < 0) {
262 return false;
263 }
264 std::string result;
vandebo (ex-Chrome) 2013/03/20 00:18:27 nit: result -> mime_type ?
Kevin Bailey 2013/03/20 13:53:23 Done.
265 if (!net::SniffMimeType(buffer, net::kMaxBytesToSniff,
vandebo (ex-Chrome) 2013/03/20 00:18:27 I think you should pass len here.
Kevin Bailey 2013/03/20 13:53:23 Good catch.
266 GURL(path.value()),
vandebo (ex-Chrome) 2013/03/20 00:18:27 nit: This will fit on the previous line.
vandebo (ex-Chrome) 2013/03/20 00:18:27 Should we prefix "file://" to the path first ?
Kevin Bailey 2013/03/20 13:53:23 It doesn't seem to need it, but I'm all for saniti
Kevin Bailey 2013/03/20 13:53:23 Done.
267 "text/plain", &result)) {
268 return false;
269 }
270 return StartsWithASCII(result, "image/", true) ||
271 StartsWithASCII(result, "audio/", true) ||
272 StartsWithASCII(result, "video/", true);
273 }
274
275 base::PlatformFileError NativeMediaFileUtil::CreateSnapshotFile(
276 FileSystemOperationContext* context,
277 const FileSystemURL& url,
278 base::PlatformFileInfo* file_info,
279 base::FilePath* platform_path,
280 SnapshotFilePolicy* policy) {
281 DCHECK(policy);
vandebo (ex-Chrome) 2013/03/20 00:18:27 Can we change lines 281-288 to just IsolatedFileUt
Kevin Bailey 2013/03/20 13:53:23 Done. Moved the DCHECK too, since I imagine we wan
282 DCHECK(file_info);
283 // We're just returning the local file information.
284 *policy = kSnapshotFileLocal;
285 base::PlatformFileError error =
286 GetFileInfo(context, url, file_info, platform_path);
287 if (error == base::PLATFORM_FILE_OK && file_info->is_directory)
288 return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
289 DCHECK(IsOnIOThread(context));
290 if (!IsMediaFile(*platform_path))
291 return base::PLATFORM_FILE_ERROR_SECURITY;
292 return error;
293 }
294
217 } // namespace fileapi 295 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/media/native_media_file_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698