| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/file_enumerator.h" | 12 #include "base/files/file_enumerator.h" |
| 13 #include "base/files/scoped_platform_file_closer.h" | |
| 14 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 15 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 16 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" | 15 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" |
| 17 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 18 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 19 #include "net/base/mime_sniffer.h" | 18 #include "net/base/mime_sniffer.h" |
| 20 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 21 #include "webkit/browser/fileapi/file_system_context.h" | 20 #include "webkit/browser/fileapi/file_system_context.h" |
| 22 #include "webkit/browser/fileapi/file_system_operation_context.h" | 21 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 23 #include "webkit/browser/fileapi/native_file_util.h" | 22 #include "webkit/browser/fileapi/native_file_util.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 53 : media_path_filter_(media_path_filter), | 52 : media_path_filter_(media_path_filter), |
| 54 weak_factory_(this) { | 53 weak_factory_(this) { |
| 55 } | 54 } |
| 56 | 55 |
| 57 NativeMediaFileUtil::~NativeMediaFileUtil() { | 56 NativeMediaFileUtil::~NativeMediaFileUtil() { |
| 58 } | 57 } |
| 59 | 58 |
| 60 // static | 59 // static |
| 61 base::File::Error NativeMediaFileUtil::IsMediaFile( | 60 base::File::Error NativeMediaFileUtil::IsMediaFile( |
| 62 const base::FilePath& path) { | 61 const base::FilePath& path) { |
| 63 base::PlatformFile file_handle; | 62 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 64 const int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 63 if (!file.IsValid()) |
| 65 base::File::Error error = | 64 return file.error_details(); |
| 66 fileapi::NativeFileUtil::CreateOrOpen(path, flags, &file_handle, NULL); | |
| 67 if (error != base::File::FILE_OK) | |
| 68 return error; | |
| 69 | 65 |
| 70 base::ScopedPlatformFileCloser scoped_platform_file(&file_handle); | |
| 71 char buffer[net::kMaxBytesToSniff]; | 66 char buffer[net::kMaxBytesToSniff]; |
| 72 | 67 |
| 73 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. | 68 // Read as much as net::SniffMimeTypeFromLocalData() will bother looking at. |
| 74 int64 len = | 69 int64 len = file.Read(0, buffer, net::kMaxBytesToSniff); |
| 75 base::ReadPlatformFile(file_handle, 0, buffer, net::kMaxBytesToSniff); | |
| 76 if (len < 0) | 70 if (len < 0) |
| 77 return base::File::FILE_ERROR_FAILED; | 71 return base::File::FILE_ERROR_FAILED; |
| 78 | 72 |
| 79 return IsMediaHeader(buffer, len); | 73 return IsMediaHeader(buffer, len); |
| 80 } | 74 } |
| 81 | 75 |
| 82 // static | 76 // static |
| 83 base::File::Error NativeMediaFileUtil::BufferIsMediaHeader( | 77 base::File::Error NativeMediaFileUtil::BufferIsMediaHeader( |
| 84 net::IOBuffer* buf, size_t length) { | 78 net::IOBuffer* buf, size_t length) { |
| 85 return IsMediaHeader(buf->data(), length); | 79 return IsMediaHeader(buf->data(), length); |
| (...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 return base::File::FILE_ERROR_FAILED; | 621 return base::File::FILE_ERROR_FAILED; |
| 628 | 622 |
| 629 if (!file_info.is_directory && | 623 if (!file_info.is_directory && |
| 630 !media_path_filter_->Match(file_path)) { | 624 !media_path_filter_->Match(file_path)) { |
| 631 return failure_error; | 625 return failure_error; |
| 632 } | 626 } |
| 633 | 627 |
| 634 *local_file_path = file_path; | 628 *local_file_path = file_path; |
| 635 return base::File::FILE_OK; | 629 return base::File::FILE_OK; |
| 636 } | 630 } |
| OLD | NEW |