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 "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 #include "net/base/filename_util.h" | 11 #include "net/base/filename_util.h" |
12 #include "net/base/mime_sniffer.h" | 12 #include "net/base/mime_sniffer.h" |
13 #include "net/base/mime_util.h" | 13 #include "net/base/mime_util.h" |
14 #include "storage/browser/fileapi/file_system_url.h" | 14 #include "storage/browser/fileapi/file_system_url.h" |
15 | 15 |
16 #if defined(OS_CHROMEOS) | 16 #if defined(OS_CHROMEOS) |
17 #include "chrome/browser/chromeos/file_manager/filesystem_api_util.h" | 17 #include "chrome/browser/chromeos/file_manager/filesystem_api_util.h" |
18 #endif | 18 #endif |
19 | 19 |
20 using content::BrowserThread; | 20 using content::BrowserThread; |
21 | 21 |
| 22 namespace { |
| 23 |
| 24 const char kMimeTypeApplicationOctetStream[] = "application/octet-stream"; |
| 25 |
| 26 } // namespace |
| 27 |
22 namespace extensions { | 28 namespace extensions { |
23 namespace app_file_handler_util { | 29 namespace app_file_handler_util { |
24 namespace { | 30 namespace { |
25 | 31 |
26 // Detects MIME type by reading initial bytes from the file. If found, then | 32 // Detects MIME type by reading initial bytes from the file. If found, then |
27 // writes the MIME type to |result|. | 33 // writes the MIME type to |result|. |
28 void SniffMimeType(const base::FilePath& local_path, std::string* result) { | 34 void SniffMimeType(const base::FilePath& local_path, std::string* result) { |
29 std::vector<char> content(net::kMaxBytesToSniff); | 35 std::vector<char> content(net::kMaxBytesToSniff); |
30 | 36 |
31 const int bytes_read = | 37 const int bytes_read = |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, | 81 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, |
76 base::Passed(&mime_type_from_extension), | 82 base::Passed(&mime_type_from_extension), |
77 callback)); | 83 callback)); |
78 } | 84 } |
79 #endif | 85 #endif |
80 | 86 |
81 // Called when sniffing for MIME type in the native local file is completed. | 87 // Called when sniffing for MIME type in the native local file is completed. |
82 void OnSniffMimeTypeForNativeLocalPathCompleted( | 88 void OnSniffMimeTypeForNativeLocalPathCompleted( |
83 scoped_ptr<std::string> mime_type, | 89 scoped_ptr<std::string> mime_type, |
84 const base::Callback<void(const std::string&)>& callback) { | 90 const base::Callback<void(const std::string&)>& callback) { |
| 91 // Do not return application/zip as sniffed result. If the file has .zip |
| 92 // extension, it should be already returned as application/zip. If the file |
| 93 // does not have .zip extension and couldn't find mime type from the |
| 94 // extension, it might be unknown internally zipped file. |
| 95 if (*mime_type == "application/zip") { |
| 96 callback.Run(kMimeTypeApplicationOctetStream); |
| 97 return; |
| 98 } |
| 99 |
85 callback.Run(*mime_type); | 100 callback.Run(*mime_type); |
86 } | 101 } |
87 | 102 |
88 } // namespace | 103 } // namespace |
89 | 104 |
90 // Handles response of net::GetMimeTypeFromFile for native file systems. If | 105 // Handles response of net::GetMimeTypeFromFile for native file systems. If |
91 // MIME type is available, then forwards it to |callback|. Otherwise, fallbacks | 106 // MIME type is available, then forwards it to |callback|. Otherwise, fallbacks |
92 // to sniffing. | 107 // to sniffing. |
93 void OnGetMimeTypeFromFileForNativeLocalPathCompleted( | 108 void OnGetMimeTypeFromFileForNativeLocalPathCompleted( |
94 const base::FilePath& local_path, | 109 const base::FilePath& local_path, |
95 scoped_ptr<std::string> mime_type, | 110 scoped_ptr<std::string> mime_type, |
96 const base::Callback<void(const std::string&)>& callback) { | 111 const base::Callback<void(const std::string&)>& callback) { |
97 if (!mime_type->empty()) { | 112 if (!mime_type->empty()) { |
98 callback.Run(*mime_type); | 113 callback.Run(*mime_type); |
99 return; | 114 return; |
100 } | 115 } |
101 | 116 |
102 scoped_ptr<std::string> sniffed_mime_type(new std::string); | 117 scoped_ptr<std::string> sniffed_mime_type( |
| 118 new std::string(kMimeTypeApplicationOctetStream)); |
103 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); | 119 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); |
104 BrowserThread::PostBlockingPoolTaskAndReply( | 120 BrowserThread::PostBlockingPoolTaskAndReply( |
105 FROM_HERE, | 121 FROM_HERE, |
106 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), | 122 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), |
107 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, | 123 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, |
108 base::Passed(&sniffed_mime_type), | 124 base::Passed(&sniffed_mime_type), |
109 callback)); | 125 callback)); |
110 } | 126 } |
111 | 127 |
112 // Fetches MIME type for a local path and returns it with a |callback|. | 128 // Fetches MIME type for a local path and returns it with a |callback|. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); | 214 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); |
199 // Release the callback to avoid a circullar reference in case an instance | 215 // Release the callback to avoid a circullar reference in case an instance |
200 // of this class is a member of a ref counted class, which instance is bound | 216 // of this class is a member of a ref counted class, which instance is bound |
201 // to this callback. | 217 // to this callback. |
202 callback_ = CompletionCallback(); | 218 callback_ = CompletionCallback(); |
203 } | 219 } |
204 } | 220 } |
205 | 221 |
206 } // namespace app_file_handler_util | 222 } // namespace app_file_handler_util |
207 } // namespace extensions | 223 } // namespace extensions |
OLD | NEW |