| 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 "chrome/browser/extensions/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/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 net::FilePathToFileURL(local_path), | 45 net::FilePathToFileURL(local_path), |
| 46 std::string(), // type_hint (passes no hint) | 46 std::string(), // type_hint (passes no hint) |
| 47 result); | 47 result); |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 #if defined(OS_CHROMEOS) | 51 #if defined(OS_CHROMEOS) |
| 52 // 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 |
| 53 // to |callback|. | 53 // to |callback|. |
| 54 void OnGetMimeTypeFromFileForNonNativeLocalPathCompleted( | 54 void OnGetMimeTypeFromFileForNonNativeLocalPathCompleted( |
| 55 scoped_ptr<std::string> mime_type, | 55 std::unique_ptr<std::string> mime_type, |
| 56 const base::Callback<void(const std::string&)>& callback) { | 56 const base::Callback<void(const std::string&)>& callback) { |
| 57 callback.Run(*mime_type); | 57 callback.Run(*mime_type); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Called when fetching MIME type for a non-native local path is completed. | 60 // Called when fetching MIME type for a non-native local path is completed. |
| 61 // If |success| is false, then tries to guess the MIME type by looking at the | 61 // If |success| is false, then tries to guess the MIME type by looking at the |
| 62 // file name. | 62 // file name. |
| 63 void OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted( | 63 void OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted( |
| 64 const base::FilePath& local_path, | 64 const base::FilePath& local_path, |
| 65 const base::Callback<void(const std::string&)>& callback, | 65 const base::Callback<void(const std::string&)>& callback, |
| 66 bool success, | 66 bool success, |
| 67 const std::string& mime_type) { | 67 const std::string& mime_type) { |
| 68 if (success) { | 68 if (success) { |
| 69 callback.Run(mime_type); | 69 callback.Run(mime_type); |
| 70 return; | 70 return; |
| 71 } | 71 } |
| 72 | 72 |
| 73 // 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 |
| 74 // file's extension. | 74 // file's extension. |
| 75 scoped_ptr<std::string> mime_type_from_extension(new std::string); | 75 std::unique_ptr<std::string> mime_type_from_extension(new std::string); |
| 76 std::string* const mime_type_from_extension_ptr = | 76 std::string* const mime_type_from_extension_ptr = |
| 77 mime_type_from_extension.get(); | 77 mime_type_from_extension.get(); |
| 78 BrowserThread::PostBlockingPoolTaskAndReply( | 78 BrowserThread::PostBlockingPoolTaskAndReply( |
| 79 FROM_HERE, | 79 FROM_HERE, |
| 80 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), | 80 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), |
| 81 local_path, | 81 local_path, |
| 82 mime_type_from_extension_ptr), | 82 mime_type_from_extension_ptr), |
| 83 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, | 83 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, |
| 84 base::Passed(&mime_type_from_extension), | 84 base::Passed(&mime_type_from_extension), |
| 85 callback)); | 85 callback)); |
| 86 } | 86 } |
| 87 #endif | 87 #endif |
| 88 | 88 |
| 89 // Called when sniffing for MIME type in the native local file is completed. | 89 // Called when sniffing for MIME type in the native local file is completed. |
| 90 void OnSniffMimeTypeForNativeLocalPathCompleted( | 90 void OnSniffMimeTypeForNativeLocalPathCompleted( |
| 91 scoped_ptr<std::string> mime_type, | 91 std::unique_ptr<std::string> mime_type, |
| 92 const base::Callback<void(const std::string&)>& callback) { | 92 const base::Callback<void(const std::string&)>& callback) { |
| 93 // Do not return application/zip as sniffed result. If the file has .zip | 93 // Do not return application/zip as sniffed result. If the file has .zip |
| 94 // extension, it should be already returned as application/zip. If the file | 94 // extension, it should be already returned as application/zip. If the file |
| 95 // does not have .zip extension and couldn't find mime type from the | 95 // does not have .zip extension and couldn't find mime type from the |
| 96 // extension, it might be unknown internally zipped file. | 96 // extension, it might be unknown internally zipped file. |
| 97 if (*mime_type == "application/zip") { | 97 if (*mime_type == "application/zip") { |
| 98 callback.Run(kMimeTypeApplicationOctetStream); | 98 callback.Run(kMimeTypeApplicationOctetStream); |
| 99 return; | 99 return; |
| 100 } | 100 } |
| 101 | 101 |
| 102 callback.Run(*mime_type); | 102 callback.Run(*mime_type); |
| 103 } | 103 } |
| 104 | 104 |
| 105 } // namespace | 105 } // namespace |
| 106 | 106 |
| 107 // Handles response of net::GetMimeTypeFromFile for native file systems. If | 107 // Handles response of net::GetMimeTypeFromFile for native file systems. If |
| 108 // MIME type is available, then forwards it to |callback|. Otherwise, fallbacks | 108 // MIME type is available, then forwards it to |callback|. Otherwise, fallbacks |
| 109 // to sniffing. | 109 // to sniffing. |
| 110 void OnGetMimeTypeFromFileForNativeLocalPathCompleted( | 110 void OnGetMimeTypeFromFileForNativeLocalPathCompleted( |
| 111 const base::FilePath& local_path, | 111 const base::FilePath& local_path, |
| 112 scoped_ptr<std::string> mime_type, | 112 std::unique_ptr<std::string> mime_type, |
| 113 const base::Callback<void(const std::string&)>& callback) { | 113 const base::Callback<void(const std::string&)>& callback) { |
| 114 if (!mime_type->empty()) { | 114 if (!mime_type->empty()) { |
| 115 callback.Run(*mime_type); | 115 callback.Run(*mime_type); |
| 116 return; | 116 return; |
| 117 } | 117 } |
| 118 | 118 |
| 119 scoped_ptr<std::string> sniffed_mime_type( | 119 std::unique_ptr<std::string> sniffed_mime_type( |
| 120 new std::string(kMimeTypeApplicationOctetStream)); | 120 new std::string(kMimeTypeApplicationOctetStream)); |
| 121 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); | 121 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); |
| 122 BrowserThread::PostBlockingPoolTaskAndReply( | 122 BrowserThread::PostBlockingPoolTaskAndReply( |
| 123 FROM_HERE, | 123 FROM_HERE, |
| 124 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), | 124 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), |
| 125 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, | 125 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, |
| 126 base::Passed(&sniffed_mime_type), | 126 base::Passed(&sniffed_mime_type), |
| 127 callback)); | 127 callback)); |
| 128 } | 128 } |
| 129 | 129 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 142 local_path, | 142 local_path, |
| 143 base::Bind(&OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted, | 143 base::Bind(&OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted, |
| 144 local_path, | 144 local_path, |
| 145 callback)); | 145 callback)); |
| 146 return; | 146 return; |
| 147 } | 147 } |
| 148 #endif | 148 #endif |
| 149 | 149 |
| 150 // For native local files, try to guess the mime from the extension. If | 150 // For native local files, try to guess the mime from the extension. If |
| 151 // not available, then try to sniff if. | 151 // not available, then try to sniff if. |
| 152 scoped_ptr<std::string> mime_type_from_extension(new std::string); | 152 std::unique_ptr<std::string> mime_type_from_extension(new std::string); |
| 153 std::string* const mime_type_from_extension_ptr = | 153 std::string* const mime_type_from_extension_ptr = |
| 154 mime_type_from_extension.get(); | 154 mime_type_from_extension.get(); |
| 155 BrowserThread::PostBlockingPoolTaskAndReply( | 155 BrowserThread::PostBlockingPoolTaskAndReply( |
| 156 FROM_HERE, | 156 FROM_HERE, |
| 157 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), | 157 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), |
| 158 local_path, | 158 local_path, |
| 159 mime_type_from_extension_ptr), | 159 mime_type_from_extension_ptr), |
| 160 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, | 160 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, |
| 161 local_path, | 161 local_path, |
| 162 base::Passed(&mime_type_from_extension), | 162 base::Passed(&mime_type_from_extension), |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); | 216 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); |
| 217 // Release the callback to avoid a circullar reference in case an instance | 217 // Release the callback to avoid a circullar reference in case an instance |
| 218 // of this class is a member of a ref counted class, which instance is bound | 218 // of this class is a member of a ref counted class, which instance is bound |
| 219 // to this callback. | 219 // to this callback. |
| 220 callback_ = CompletionCallback(); | 220 callback_ = CompletionCallback(); |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 | 223 |
| 224 } // namespace app_file_handler_util | 224 } // namespace app_file_handler_util |
| 225 } // namespace extensions | 225 } // namespace extensions |
| OLD | NEW |