| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/api/file_handlers/mime_util.h" | 5 #include "extensions/browser/api/file_handlers/mime_util.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "net/base/filename_util.h" | 13 #include "net/base/filename_util.h" |
| 14 #include "net/base/mime_sniffer.h" | 14 #include "net/base/mime_sniffer.h" |
| 15 #include "net/base/mime_util.h" | 15 #include "net/base/mime_util.h" |
| 16 #include "storage/browser/fileapi/file_system_url.h" | 16 #include "storage/browser/fileapi/file_system_url.h" |
| 17 | 17 |
| 18 #if defined(OS_CHROMEOS) | 18 #if defined(OS_CHROMEOS) |
| 19 #include "extensions/browser/api/extensions_api_client.h" | 19 #include "extensions/browser/api/extensions_api_client.h" |
| 20 #include "extensions/browser/api/file_handlers/non_native_file_system_delegate.h
" | 20 #include "extensions/browser/api/file_handlers/non_native_file_system_delegate.h
" |
| 21 #endif | 21 #endif |
| (...skipping 12 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 // Detects MIME type by reading initial bytes from the file. If found, then | 35 // Detects MIME type by reading initial bytes from the file. If found, then |
| 36 // writes the MIME type to |result|. | 36 // writes the MIME type to |result|. |
| 37 void SniffMimeType(const base::FilePath& local_path, std::string* result) { | 37 void SniffMimeType(const base::FilePath& local_path, std::string* result) { |
| 38 std::vector<char> content(net::kMaxBytesToSniff); | 38 std::vector<char> content(net::kMaxBytesToSniff); |
| 39 | 39 |
| 40 const int bytes_read = | 40 const int bytes_read = |
| 41 base::ReadFile(local_path, &content[0], content.size()); | 41 base::ReadFile(local_path, &content[0], content.size()); |
| 42 | 42 |
| 43 if (bytes_read >= 0) { | 43 if (bytes_read >= 0) { |
| 44 net::SniffMimeType(&content[0], | 44 net::SniffMimeType(&content[0], bytes_read, |
| 45 bytes_read, | |
| 46 net::FilePathToFileURL(local_path), | 45 net::FilePathToFileURL(local_path), |
| 47 std::string(), // type_hint (passes no hint) | 46 std::string(), // type_hint (passes no hint) |
| 48 result); | 47 result); |
| 49 } | 48 } |
| 50 } | 49 } |
| 51 | 50 |
| 52 #if defined(OS_CHROMEOS) | 51 #if defined(OS_CHROMEOS) |
| 53 // Converts a result passed as a scoped pointer to a dereferenced value passed | 52 // Converts a result passed as a scoped pointer to a dereferenced value passed |
| 54 // to |callback|. | 53 // to |callback|. |
| 55 void OnGetMimeTypeFromFileForNonNativeLocalPathCompleted( | 54 void OnGetMimeTypeFromFileForNonNativeLocalPathCompleted( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 70 callback.Run(mime_type); | 69 callback.Run(mime_type); |
| 71 return; | 70 return; |
| 72 } | 71 } |
| 73 | 72 |
| 74 // MIME type not available with metadata, hence try to guess it from the | 73 // MIME type not available with metadata, hence try to guess it from the |
| 75 // file's extension. | 74 // file's extension. |
| 76 std::unique_ptr<std::string> mime_type_from_extension(new std::string); | 75 std::unique_ptr<std::string> mime_type_from_extension(new std::string); |
| 77 std::string* const mime_type_from_extension_ptr = | 76 std::string* const mime_type_from_extension_ptr = |
| 78 mime_type_from_extension.get(); | 77 mime_type_from_extension.get(); |
| 79 BrowserThread::PostBlockingPoolTaskAndReply( | 78 BrowserThread::PostBlockingPoolTaskAndReply( |
| 80 FROM_HERE, | 79 FROM_HERE, base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), |
| 81 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), | 80 local_path, mime_type_from_extension_ptr), |
| 82 local_path, | |
| 83 mime_type_from_extension_ptr), | |
| 84 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, | 81 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, |
| 85 base::Passed(&mime_type_from_extension), | 82 base::Passed(&mime_type_from_extension), callback)); |
| 86 callback)); | |
| 87 } | 83 } |
| 88 #endif | 84 #endif |
| 89 | 85 |
| 90 // Called when sniffing for MIME type in the native local file is completed. | 86 // Called when sniffing for MIME type in the native local file is completed. |
| 91 void OnSniffMimeTypeForNativeLocalPathCompleted( | 87 void OnSniffMimeTypeForNativeLocalPathCompleted( |
| 92 std::unique_ptr<std::string> mime_type, | 88 std::unique_ptr<std::string> mime_type, |
| 93 const base::Callback<void(const std::string&)>& callback) { | 89 const base::Callback<void(const std::string&)>& callback) { |
| 94 // Do not return application/zip as sniffed result. If the file has .zip | 90 // Do not return application/zip as sniffed result. If the file has .zip |
| 95 // extension, it should be already returned as application/zip. If the file | 91 // extension, it should be already returned as application/zip. If the file |
| 96 // does not have .zip extension and couldn't find mime type from the | 92 // does not have .zip extension and couldn't find mime type from the |
| (...skipping 17 matching lines...) Expand all Loading... |
| 114 const base::Callback<void(const std::string&)>& callback) { | 110 const base::Callback<void(const std::string&)>& callback) { |
| 115 if (!mime_type->empty()) { | 111 if (!mime_type->empty()) { |
| 116 callback.Run(*mime_type); | 112 callback.Run(*mime_type); |
| 117 return; | 113 return; |
| 118 } | 114 } |
| 119 | 115 |
| 120 std::unique_ptr<std::string> sniffed_mime_type( | 116 std::unique_ptr<std::string> sniffed_mime_type( |
| 121 new std::string(kMimeTypeApplicationOctetStream)); | 117 new std::string(kMimeTypeApplicationOctetStream)); |
| 122 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); | 118 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); |
| 123 BrowserThread::PostBlockingPoolTaskAndReply( | 119 BrowserThread::PostBlockingPoolTaskAndReply( |
| 124 FROM_HERE, | 120 FROM_HERE, base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), |
| 125 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), | |
| 126 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, | 121 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, |
| 127 base::Passed(&sniffed_mime_type), | 122 base::Passed(&sniffed_mime_type), callback)); |
| 128 callback)); | |
| 129 } | 123 } |
| 130 | 124 |
| 131 // Fetches MIME type for a local path and returns it with a |callback|. | 125 // Fetches MIME type for a local path and returns it with a |callback|. |
| 132 void GetMimeTypeForLocalPath( | 126 void GetMimeTypeForLocalPath( |
| 133 Profile* profile, | 127 content::BrowserContext* context, |
| 134 const base::FilePath& local_path, | 128 const base::FilePath& local_path, |
| 135 const base::Callback<void(const std::string&)>& callback) { | 129 const base::Callback<void(const std::string&)>& callback) { |
| 136 #if defined(OS_CHROMEOS) | 130 #if defined(OS_CHROMEOS) |
| 137 NonNativeFileSystemDelegate* delegate = | 131 NonNativeFileSystemDelegate* delegate = |
| 138 ExtensionsAPIClient::Get()->GetNonNativeFileSystemDelegate(); | 132 ExtensionsAPIClient::Get()->GetNonNativeFileSystemDelegate(); |
| 139 if (delegate && delegate->IsUnderNonNativeLocalPath(profile, local_path)) { | 133 if (delegate && delegate->IsUnderNonNativeLocalPath(context, local_path)) { |
| 140 // For non-native files, try to get the MIME type from metadata. If not | 134 // For non-native files, try to get the MIME type from metadata. If not |
| 141 // available, then try to guess from the extension. Never sniff (because | 135 // available, then try to guess from the extension. Never sniff (because |
| 142 // it can be very slow). | 136 // it can be very slow). |
| 143 delegate->GetNonNativeLocalPathMimeType( | 137 delegate->GetNonNativeLocalPathMimeType( |
| 144 profile, | 138 context, local_path, |
| 145 local_path, | |
| 146 base::Bind(&OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted, | 139 base::Bind(&OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted, |
| 147 local_path, | 140 local_path, callback)); |
| 148 callback)); | |
| 149 return; | 141 return; |
| 150 } | 142 } |
| 151 #endif | 143 #endif |
| 152 | 144 |
| 153 // For native local files, try to guess the mime from the extension. If | 145 // For native local files, try to guess the mime from the extension. If |
| 154 // not available, then try to sniff if. | 146 // not available, then try to sniff if. |
| 155 std::unique_ptr<std::string> mime_type_from_extension(new std::string); | 147 std::unique_ptr<std::string> mime_type_from_extension(new std::string); |
| 156 std::string* const mime_type_from_extension_ptr = | 148 std::string* const mime_type_from_extension_ptr = |
| 157 mime_type_from_extension.get(); | 149 mime_type_from_extension.get(); |
| 158 BrowserThread::PostBlockingPoolTaskAndReply( | 150 BrowserThread::PostBlockingPoolTaskAndReply( |
| 159 FROM_HERE, | 151 FROM_HERE, base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), |
| 160 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), | 152 local_path, mime_type_from_extension_ptr), |
| 161 local_path, | 153 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, local_path, |
| 162 mime_type_from_extension_ptr), | 154 base::Passed(&mime_type_from_extension), callback)); |
| 163 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, | |
| 164 local_path, | |
| 165 base::Passed(&mime_type_from_extension), | |
| 166 callback)); | |
| 167 } | 155 } |
| 168 | 156 |
| 169 MimeTypeCollector::MimeTypeCollector(Profile* profile) | 157 MimeTypeCollector::MimeTypeCollector(content::BrowserContext* context) |
| 170 : profile_(profile), left_(0), weak_ptr_factory_(this) { | 158 : context_(context), left_(0), weak_ptr_factory_(this) {} |
| 171 } | |
| 172 | 159 |
| 173 MimeTypeCollector::~MimeTypeCollector() { | 160 MimeTypeCollector::~MimeTypeCollector() {} |
| 174 } | |
| 175 | 161 |
| 176 void MimeTypeCollector::CollectForURLs( | 162 void MimeTypeCollector::CollectForURLs( |
| 177 const std::vector<storage::FileSystemURL>& urls, | 163 const std::vector<storage::FileSystemURL>& urls, |
| 178 const CompletionCallback& callback) { | 164 const CompletionCallback& callback) { |
| 179 std::vector<base::FilePath> local_paths; | 165 std::vector<base::FilePath> local_paths; |
| 180 for (size_t i = 0; i < urls.size(); ++i) { | 166 for (size_t i = 0; i < urls.size(); ++i) { |
| 181 local_paths.push_back(urls[i].path()); | 167 local_paths.push_back(urls[i].path()); |
| 182 } | 168 } |
| 183 | 169 |
| 184 CollectForLocalPaths(local_paths, callback); | 170 CollectForLocalPaths(local_paths, callback); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 196 | 182 |
| 197 if (!left_) { | 183 if (!left_) { |
| 198 // Nothing to process. | 184 // Nothing to process. |
| 199 base::ThreadTaskRunnerHandle::Get()->PostTask( | 185 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 200 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); | 186 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); |
| 201 callback_ = CompletionCallback(); | 187 callback_ = CompletionCallback(); |
| 202 return; | 188 return; |
| 203 } | 189 } |
| 204 | 190 |
| 205 for (size_t i = 0; i < local_paths.size(); ++i) { | 191 for (size_t i = 0; i < local_paths.size(); ++i) { |
| 206 GetMimeTypeForLocalPath(profile_, | 192 GetMimeTypeForLocalPath(context_, local_paths[i], |
| 207 local_paths[i], | |
| 208 base::Bind(&MimeTypeCollector::OnMimeTypeCollected, | 193 base::Bind(&MimeTypeCollector::OnMimeTypeCollected, |
| 209 weak_ptr_factory_.GetWeakPtr(), | 194 weak_ptr_factory_.GetWeakPtr(), i)); |
| 210 i)); | |
| 211 } | 195 } |
| 212 } | 196 } |
| 213 | 197 |
| 214 void MimeTypeCollector::OnMimeTypeCollected(size_t index, | 198 void MimeTypeCollector::OnMimeTypeCollected(size_t index, |
| 215 const std::string& mime_type) { | 199 const std::string& mime_type) { |
| 216 (*result_)[index] = mime_type; | 200 (*result_)[index] = mime_type; |
| 217 if (!--left_) { | 201 if (!--left_) { |
| 218 base::ThreadTaskRunnerHandle::Get()->PostTask( | 202 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 219 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); | 203 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); |
| 220 // Release the callback to avoid a circullar reference in case an instance | 204 // Release the callback to avoid a circullar reference in case an instance |
| 221 // of this class is a member of a ref counted class, which instance is bound | 205 // of this class is a member of a ref counted class, which instance is bound |
| 222 // to this callback. | 206 // to this callback. |
| 223 callback_ = CompletionCallback(); | 207 callback_ = CompletionCallback(); |
| 224 } | 208 } |
| 225 } | 209 } |
| 226 | 210 |
| 227 } // namespace app_file_handler_util | 211 } // namespace app_file_handler_util |
| 228 } // namespace extensions | 212 } // namespace extensions |
| OLD | NEW |