OLD | NEW |
---|---|
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 "chrome/browser/media_galleries/fileapi/native_media_file_util.h" | 5 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" |
6 | 6 |
7 #include "base/memory/scoped_generic_obj.h" | |
8 #include "base/string_util.h" | |
7 #include "chrome/browser/media_galleries/fileapi/filtering_file_enumerator.h" | 9 #include "chrome/browser/media_galleries/fileapi/filtering_file_enumerator.h" |
8 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p rovider.h" | 10 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p rovider.h" |
9 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" | 11 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" |
12 #include "googleurl/src/gurl.h" | |
13 #include "net/base/mime_sniffer.h" | |
14 #include "webkit/fileapi/file_system_context.h" | |
10 #include "webkit/fileapi/file_system_operation_context.h" | 15 #include "webkit/fileapi/file_system_operation_context.h" |
16 #include "webkit/fileapi/file_system_task_runners.h" | |
11 #include "webkit/fileapi/native_file_util.h" | 17 #include "webkit/fileapi/native_file_util.h" |
12 | 18 |
13 using base::PlatformFile; | 19 using base::PlatformFile; |
14 using base::PlatformFileError; | 20 using base::PlatformFileError; |
15 using base::PlatformFileInfo; | 21 using base::PlatformFileInfo; |
16 using fileapi::FileSystemOperationContext; | 22 using fileapi::FileSystemOperationContext; |
17 using fileapi::FileSystemURL; | 23 using fileapi::FileSystemURL; |
18 using fileapi::NativeFileUtil; | 24 using fileapi::NativeFileUtil; |
19 | 25 |
20 namespace chrome { | 26 namespace chrome { |
21 | 27 |
22 namespace { | 28 namespace { |
23 | 29 |
30 // Modelled after ScopedFILEClose. | |
31 class ScopedPlatformFileClose { | |
32 public: | |
33 void operator()(base::PlatformFile file) const { | |
34 if (file != base::kInvalidPlatformFileValue) | |
35 base::ClosePlatformFile(file); | |
36 } | |
37 }; | |
38 | |
39 typedef ScopedGenericObj<base::PlatformFile, | |
40 ScopedPlatformFileClose> ScopedPlatformFile; | |
41 | |
42 // Returns true if the current thread is capable of doing IO. | |
43 bool IsOnTaskRunnerThread(fileapi::FileSystemOperationContext* context) { | |
44 return context->file_system_context()->task_runners()-> | |
45 media_task_runner()->RunsTasksOnCurrentThread(); | |
46 } | |
47 | |
24 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { | 48 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) { |
25 return context->GetUserValue<MediaPathFilter*>( | 49 return context->GetUserValue<MediaPathFilter*>( |
26 MediaFileSystemMountPointProvider::kMediaPathFilterKey); | 50 MediaFileSystemMountPointProvider::kMediaPathFilterKey); |
27 } | 51 } |
28 | 52 |
29 } // namespace | 53 } // namespace |
30 | 54 |
31 NativeMediaFileUtil::NativeMediaFileUtil() { | 55 NativeMediaFileUtil::NativeMediaFileUtil() { |
32 } | 56 } |
33 | 57 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 | 243 |
220 if (!file_info.is_directory && | 244 if (!file_info.is_directory && |
221 !GetMediaPathFilter(context)->Match(file_path)) { | 245 !GetMediaPathFilter(context)->Match(file_path)) { |
222 return failure_error; | 246 return failure_error; |
223 } | 247 } |
224 | 248 |
225 *local_file_path = file_path; | 249 *local_file_path = file_path; |
226 return base::PLATFORM_FILE_OK; | 250 return base::PLATFORM_FILE_OK; |
227 } | 251 } |
228 | 252 |
253 webkit_blob::ScopedFile NativeMediaFileUtil::CreateSnapshotFile( | |
254 fileapi::FileSystemOperationContext* context, | |
255 const fileapi::FileSystemURL& url, | |
256 base::PlatformFileError* error, | |
257 base::PlatformFileInfo* file_info, | |
258 base::FilePath* platform_path) { | |
259 DCHECK(IsOnTaskRunnerThread(context)); | |
260 webkit_blob::ScopedFile file; | |
261 file = IsolatedFileUtil::CreateSnapshotFile( | |
262 context, url, error, file_info, platform_path); | |
263 if (*error != base::PLATFORM_FILE_OK) | |
264 return file.Pass(); | |
265 *error = IsMediaFile(*platform_path); | |
266 if (*error == base::PLATFORM_FILE_OK) | |
267 return file.Pass(); | |
268 else | |
vandebo (ex-Chrome)
2013/04/30 21:20:36
nit: remove else, just return.
Kevin Bailey
2013/05/02 00:54:02
Done.
| |
269 return webkit_blob::ScopedFile(); | |
270 } | |
271 | |
272 base::PlatformFileError NativeMediaFileUtil::IsMediaFile( | |
273 const base::FilePath& path) { | |
274 base::PlatformFile file_handle; | |
275 bool created; | |
276 base::PlatformFileError error; | |
277 error = NativeFileUtil::CreateOrOpen(path, | |
278 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, &file_handle, | |
279 &created); | |
280 if (error != base::PLATFORM_FILE_OK) | |
281 return error; | |
282 ScopedPlatformFile scoped_platform_file(file_handle); | |
283 char buffer[net::kMaxBytesToSniff]; | |
284 int64 len; | |
285 // 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.
| |
286 len = base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); | |
287 if (len < 0) | |
288 return base::PLATFORM_FILE_ERROR_FAILED; | |
289 if (len == 0) | |
290 return base::PLATFORM_FILE_ERROR_SECURITY; | |
291 std::string mime_type; | |
292 if (!net::IdentifyExtraMimeType(buffer, len, &mime_type)) { | |
293 return base::PLATFORM_FILE_ERROR_SECURITY; | |
294 } | |
295 if (StartsWithASCII(mime_type, "image/", true) || | |
296 StartsWithASCII(mime_type, "audio/", true) || | |
297 StartsWithASCII(mime_type, "video/", true) || | |
298 mime_type == "application/x-shockwave-flash") { | |
299 return base::PLATFORM_FILE_OK; | |
300 } else { | |
301 return base::PLATFORM_FILE_ERROR_SECURITY; | |
302 } | |
303 } | |
304 | |
229 } // namespace chrome | 305 } // namespace chrome |
OLD | NEW |