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/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
11 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.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 |
22 | 22 |
23 using content::BrowserThread; | |
24 | |
25 namespace { | 23 namespace { |
26 | 24 |
27 const char kMimeTypeApplicationOctetStream[] = "application/octet-stream"; | 25 const char kMimeTypeApplicationOctetStream[] = "application/octet-stream"; |
28 | 26 |
29 } // namespace | 27 } // namespace |
30 | 28 |
31 namespace extensions { | 29 namespace extensions { |
32 namespace app_file_handler_util { | 30 namespace app_file_handler_util { |
33 namespace { | 31 namespace { |
34 | 32 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 if (success) { | 66 if (success) { |
69 callback.Run(mime_type); | 67 callback.Run(mime_type); |
70 return; | 68 return; |
71 } | 69 } |
72 | 70 |
73 // 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 |
74 // file's extension. | 72 // file's extension. |
75 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); |
76 std::string* const mime_type_from_extension_ptr = | 74 std::string* const mime_type_from_extension_ptr = |
77 mime_type_from_extension.get(); | 75 mime_type_from_extension.get(); |
78 BrowserThread::PostBlockingPoolTaskAndReply( | 76 base::PostTaskWithTraitsAndReply( |
79 FROM_HERE, base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), | 77 FROM_HERE, base::TaskTraits().MayBlock(), |
80 local_path, mime_type_from_extension_ptr), | 78 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), local_path, |
| 79 mime_type_from_extension_ptr), |
81 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, | 80 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, |
82 base::Passed(&mime_type_from_extension), callback)); | 81 base::Passed(&mime_type_from_extension), callback)); |
83 } | 82 } |
84 #endif | 83 #endif |
85 | 84 |
86 // 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. |
87 void OnSniffMimeTypeForNativeLocalPathCompleted( | 86 void OnSniffMimeTypeForNativeLocalPathCompleted( |
88 std::unique_ptr<std::string> mime_type, | 87 std::unique_ptr<std::string> mime_type, |
89 const base::Callback<void(const std::string&)>& callback) { | 88 const base::Callback<void(const std::string&)>& callback) { |
90 // Do not return application/zip as sniffed result. If the file has .zip | 89 // Do not return application/zip as sniffed result. If the file has .zip |
(...skipping 18 matching lines...) Expand all Loading... |
109 std::unique_ptr<std::string> mime_type, | 108 std::unique_ptr<std::string> mime_type, |
110 const base::Callback<void(const std::string&)>& callback) { | 109 const base::Callback<void(const std::string&)>& callback) { |
111 if (!mime_type->empty()) { | 110 if (!mime_type->empty()) { |
112 callback.Run(*mime_type); | 111 callback.Run(*mime_type); |
113 return; | 112 return; |
114 } | 113 } |
115 | 114 |
116 std::unique_ptr<std::string> sniffed_mime_type( | 115 std::unique_ptr<std::string> sniffed_mime_type( |
117 new std::string(kMimeTypeApplicationOctetStream)); | 116 new std::string(kMimeTypeApplicationOctetStream)); |
118 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); | 117 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); |
119 BrowserThread::PostBlockingPoolTaskAndReply( | 118 base::PostTaskWithTraitsAndReply( |
120 FROM_HERE, base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), | 119 FROM_HERE, base::TaskTraits().MayBlock(), |
| 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) { |
130 #if defined(OS_CHROMEOS) | 130 #if defined(OS_CHROMEOS) |
131 NonNativeFileSystemDelegate* delegate = | 131 NonNativeFileSystemDelegate* delegate = |
132 ExtensionsAPIClient::Get()->GetNonNativeFileSystemDelegate(); | 132 ExtensionsAPIClient::Get()->GetNonNativeFileSystemDelegate(); |
133 if (delegate && delegate->IsUnderNonNativeLocalPath(context, local_path)) { | 133 if (delegate && delegate->IsUnderNonNativeLocalPath(context, local_path)) { |
134 // 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 |
135 // available, then try to guess from the extension. Never sniff (because | 135 // available, then try to guess from the extension. Never sniff (because |
136 // it can be very slow). | 136 // it can be very slow). |
137 delegate->GetNonNativeLocalPathMimeType( | 137 delegate->GetNonNativeLocalPathMimeType( |
138 context, local_path, | 138 context, local_path, |
139 base::Bind(&OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted, | 139 base::Bind(&OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted, |
140 local_path, callback)); | 140 local_path, callback)); |
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 BrowserThread::PostBlockingPoolTaskAndReply( | 150 base::PostTaskWithTraitsAndReply( |
151 FROM_HERE, base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), | 151 FROM_HERE, base::TaskTraits().MayBlock(), |
152 local_path, mime_type_from_extension_ptr), | 152 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), local_path, |
| 153 mime_type_from_extension_ptr), |
153 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, local_path, | 154 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, local_path, |
154 base::Passed(&mime_type_from_extension), callback)); | 155 base::Passed(&mime_type_from_extension), callback)); |
155 } | 156 } |
156 | 157 |
157 MimeTypeCollector::MimeTypeCollector(content::BrowserContext* context) | 158 MimeTypeCollector::MimeTypeCollector(content::BrowserContext* context) |
158 : context_(context), left_(0), weak_ptr_factory_(this) {} | 159 : context_(context), left_(0), weak_ptr_factory_(this) {} |
159 | 160 |
160 MimeTypeCollector::~MimeTypeCollector() {} | 161 MimeTypeCollector::~MimeTypeCollector() {} |
161 | 162 |
162 void MimeTypeCollector::CollectForURLs( | 163 void MimeTypeCollector::CollectForURLs( |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); | 204 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); |
204 // 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 |
205 // 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 |
206 // to this callback. | 207 // to this callback. |
207 callback_ = CompletionCallback(); | 208 callback_ = CompletionCallback(); |
208 } | 209 } |
209 } | 210 } |
210 | 211 |
211 } // namespace app_file_handler_util | 212 } // namespace app_file_handler_util |
212 } // namespace extensions | 213 } // namespace extensions |
OLD | NEW |