| 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 "extensions/browser/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/task_scheduler/post_task.h" | 9 #include "base/task_scheduler/post_task.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 callback.Run(mime_type); | 67 callback.Run(mime_type); |
| 68 return; | 68 return; |
| 69 } | 69 } |
| 70 | 70 |
| 71 // MIME type not available with metadata, hence try to guess it from the | 71 // MIME type not available with metadata, hence try to guess it from the |
| 72 // file's extension. | 72 // file's extension. |
| 73 std::unique_ptr<std::string> mime_type_from_extension(new std::string); | 73 std::unique_ptr<std::string> mime_type_from_extension(new std::string); |
| 74 std::string* const mime_type_from_extension_ptr = | 74 std::string* const mime_type_from_extension_ptr = |
| 75 mime_type_from_extension.get(); | 75 mime_type_from_extension.get(); |
| 76 base::PostTaskWithTraitsAndReply( | 76 base::PostTaskWithTraitsAndReply( |
| 77 FROM_HERE, base::TaskTraits().MayBlock(), | 77 FROM_HERE, {base::MayBlock()}, |
| 78 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), local_path, | 78 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), local_path, |
| 79 mime_type_from_extension_ptr), | 79 mime_type_from_extension_ptr), |
| 80 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, | 80 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, |
| 81 base::Passed(&mime_type_from_extension), callback)); | 81 base::Passed(&mime_type_from_extension), callback)); |
| 82 } | 82 } |
| 83 #endif | 83 #endif |
| 84 | 84 |
| 85 // Called when sniffing for MIME type in the native local file is completed. | 85 // Called when sniffing for MIME type in the native local file is completed. |
| 86 void OnSniffMimeTypeForNativeLocalPathCompleted( | 86 void OnSniffMimeTypeForNativeLocalPathCompleted( |
| 87 std::unique_ptr<std::string> mime_type, | 87 std::unique_ptr<std::string> mime_type, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 109 const base::Callback<void(const std::string&)>& callback) { | 109 const base::Callback<void(const std::string&)>& callback) { |
| 110 if (!mime_type->empty()) { | 110 if (!mime_type->empty()) { |
| 111 callback.Run(*mime_type); | 111 callback.Run(*mime_type); |
| 112 return; | 112 return; |
| 113 } | 113 } |
| 114 | 114 |
| 115 std::unique_ptr<std::string> sniffed_mime_type( | 115 std::unique_ptr<std::string> sniffed_mime_type( |
| 116 new std::string(kMimeTypeApplicationOctetStream)); | 116 new std::string(kMimeTypeApplicationOctetStream)); |
| 117 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); | 117 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); |
| 118 base::PostTaskWithTraitsAndReply( | 118 base::PostTaskWithTraitsAndReply( |
| 119 FROM_HERE, base::TaskTraits().MayBlock(), | 119 FROM_HERE, {base::MayBlock()}, |
| 120 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), | 120 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), |
| 121 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, | 121 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, |
| 122 base::Passed(&sniffed_mime_type), callback)); | 122 base::Passed(&sniffed_mime_type), callback)); |
| 123 } | 123 } |
| 124 | 124 |
| 125 // 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|. |
| 126 void GetMimeTypeForLocalPath( | 126 void GetMimeTypeForLocalPath( |
| 127 content::BrowserContext* context, | 127 content::BrowserContext* context, |
| 128 const base::FilePath& local_path, | 128 const base::FilePath& local_path, |
| 129 const base::Callback<void(const std::string&)>& callback) { | 129 const base::Callback<void(const std::string&)>& callback) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 141 return; | 141 return; |
| 142 } | 142 } |
| 143 #endif | 143 #endif |
| 144 | 144 |
| 145 // 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 |
| 146 // not available, then try to sniff if. | 146 // not available, then try to sniff if. |
| 147 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); |
| 148 std::string* const mime_type_from_extension_ptr = | 148 std::string* const mime_type_from_extension_ptr = |
| 149 mime_type_from_extension.get(); | 149 mime_type_from_extension.get(); |
| 150 base::PostTaskWithTraitsAndReply( | 150 base::PostTaskWithTraitsAndReply( |
| 151 FROM_HERE, base::TaskTraits().MayBlock(), | 151 FROM_HERE, {base::MayBlock()}, |
| 152 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), local_path, | 152 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), local_path, |
| 153 mime_type_from_extension_ptr), | 153 mime_type_from_extension_ptr), |
| 154 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, local_path, | 154 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, local_path, |
| 155 base::Passed(&mime_type_from_extension), callback)); | 155 base::Passed(&mime_type_from_extension), callback)); |
| 156 } | 156 } |
| 157 | 157 |
| 158 MimeTypeCollector::MimeTypeCollector(content::BrowserContext* context) | 158 MimeTypeCollector::MimeTypeCollector(content::BrowserContext* context) |
| 159 : context_(context), left_(0), weak_ptr_factory_(this) {} | 159 : context_(context), left_(0), weak_ptr_factory_(this) {} |
| 160 | 160 |
| 161 MimeTypeCollector::~MimeTypeCollector() {} | 161 MimeTypeCollector::~MimeTypeCollector() {} |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); | 204 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); |
| 205 // Release the callback to avoid a circullar reference in case an instance | 205 // Release the callback to avoid a circullar reference in case an instance |
| 206 // of this class is a member of a ref counted class, which instance is bound | 206 // of this class is a member of a ref counted class, which instance is bound |
| 207 // to this callback. | 207 // to this callback. |
| 208 callback_ = CompletionCallback(); | 208 callback_ = CompletionCallback(); |
| 209 } | 209 } |
| 210 } | 210 } |
| 211 | 211 |
| 212 } // namespace app_file_handler_util | 212 } // namespace app_file_handler_util |
| 213 } // namespace extensions | 213 } // namespace extensions |
| OLD | NEW |