Chromium Code Reviews| 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/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.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" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 if (bytes_read >= 0) { | 34 if (bytes_read >= 0) { |
| 35 net::SniffMimeType(&content[0], | 35 net::SniffMimeType(&content[0], |
| 36 bytes_read, | 36 bytes_read, |
| 37 net::FilePathToFileURL(local_path), | 37 net::FilePathToFileURL(local_path), |
| 38 std::string(), // type_hint (passes no hint) | 38 std::string(), // type_hint (passes no hint) |
| 39 result); | 39 result); |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 | 42 |
| 43 #if defined(OS_CHROMEOS) | 43 #if defined(OS_CHROMEOS) |
| 44 // Converts a result passed as a scoped pointer to a dereferenced value passed | |
| 45 // to |callback|. | |
| 46 void OnGetMimeTypeFromFileForNonNativeLocalPathCompleted( | |
| 47 scoped_ptr<std::string> mime_type, | |
| 48 const base::Callback<void(const std::string&)>& callback) { | |
| 49 callback.Run(*mime_type); | |
| 50 } | |
| 51 | |
| 44 // Called when fetching MIME type for a non-native local path is completed. | 52 // Called when fetching MIME type for a non-native local path is completed. |
| 45 // If |success| is false, then tries to guess the MIME type by looking at the | 53 // If |success| is false, then tries to guess the MIME type by looking at the |
| 46 // file name. | 54 // file name. |
| 47 void OnGetMimeTypeForNonNativeLocalPathCompleted( | 55 void OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted( |
| 48 const base::FilePath& local_path, | 56 const base::FilePath& local_path, |
| 49 const base::Callback<void(const std::string&)>& callback, | 57 const base::Callback<void(const std::string&)>& callback, |
| 50 bool success, | 58 bool success, |
| 51 const std::string& mime_type) { | 59 const std::string& mime_type) { |
| 52 if (success) { | 60 if (success) { |
| 53 callback.Run(mime_type); | 61 callback.Run(mime_type); |
| 54 return; | 62 return; |
| 55 } | 63 } |
| 56 | 64 |
| 57 std::string mime_type_from_extension; | 65 // MIME type not available with metadata, hence try to guess is from the |
|
not at google - send to devlin
2014/07/08 16:02:28
s/is/it
mtomasz
2014/07/09 02:21:02
Done.
| |
| 58 net::GetMimeTypeFromFile(local_path, &mime_type_from_extension); | 66 // file's extension. |
| 59 callback.Run(mime_type_from_extension); | 67 scoped_ptr<std::string> mime_type_from_extension(new std::string); |
| 68 std::string* const mime_type_from_extension_ptr = | |
| 69 mime_type_from_extension.get(); | |
|
not at google - send to devlin
2014/07/08 16:02:28
this pattern is such a pain :( it seems like it's
mtomasz
2014/07/09 02:21:02
I'm not a huge fan of it either, but this pattern
not at google - send to devlin
2014/07/09 02:38:20
alright.
but let it be on the record that I think
| |
| 70 BrowserThread::PostBlockingPoolTaskAndReply( | |
| 71 FROM_HERE, | |
| 72 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), | |
| 73 local_path, | |
| 74 mime_type_from_extension_ptr), | |
| 75 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, | |
| 76 base::Passed(&mime_type_from_extension), | |
| 77 callback)); | |
| 60 } | 78 } |
| 61 #endif | 79 #endif |
| 62 | 80 |
| 63 // Called when sniffing for MIME type in the native local file is completed. | 81 // Called when sniffing for MIME type in the native local file is completed. |
| 64 void OnSniffMimeTypeForNativeLocalPathCompleted( | 82 void OnSniffMimeTypeForNativeLocalPathCompleted( |
| 65 scoped_ptr<std::string> mime_type, | 83 scoped_ptr<std::string> mime_type, |
| 66 const base::Callback<void(const std::string&)>& callback) { | 84 const base::Callback<void(const std::string&)>& callback) { |
| 67 callback.Run(*mime_type); | 85 callback.Run(*mime_type); |
| 68 } | 86 } |
| 69 | 87 |
| 70 } // namespace | 88 } // namespace |
| 71 | 89 |
| 90 // Handles response of net::GetMimeTypeFromFile for native file systems. If | |
| 91 // MIME type is available, then forwards it to |callback|. Otherwise, fallbacks | |
| 92 // to sniffing. | |
| 93 void OnGetMimeTypeFromFileForNativeLocalPathCompleted( | |
| 94 const base::FilePath& local_path, | |
| 95 scoped_ptr<std::string> mime_type, | |
| 96 const base::Callback<void(const std::string&)>& callback) { | |
| 97 if (!mime_type->empty()) { | |
| 98 callback.Run(*mime_type); | |
| 99 return; | |
| 100 } | |
| 101 | |
| 102 scoped_ptr<std::string> sniffed_mime_type(new std::string); | |
| 103 std::string* const sniffed_mime_type_ptr = sniffed_mime_type.get(); | |
| 104 BrowserThread::PostBlockingPoolTaskAndReply( | |
| 105 FROM_HERE, | |
| 106 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), | |
| 107 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, | |
| 108 base::Passed(&sniffed_mime_type), | |
| 109 callback)); | |
| 110 } | |
| 111 | |
| 112 // Fetches MIME type for a local path and returns it with a |callback|. | |
| 72 void GetMimeTypeForLocalPath( | 113 void GetMimeTypeForLocalPath( |
| 73 Profile* profile, | 114 Profile* profile, |
| 74 const base::FilePath& local_path, | 115 const base::FilePath& local_path, |
| 75 const base::Callback<void(const std::string&)>& callback) { | 116 const base::Callback<void(const std::string&)>& callback) { |
| 76 #if defined(OS_CHROMEOS) | 117 #if defined(OS_CHROMEOS) |
| 77 if (file_manager::util::IsUnderNonNativeLocalPath(profile, local_path)) { | 118 if (file_manager::util::IsUnderNonNativeLocalPath(profile, local_path)) { |
| 78 // For non-native files, try to get the MIME type from metadata. If not | 119 // For non-native files, try to get the MIME type from metadata. If not |
| 79 // available, then try to guess from the extension. Never sniff (because | 120 // available, then try to guess from the extension. Never sniff (because |
| 80 // it can be very slow). | 121 // it can be very slow). |
| 81 file_manager::util::GetNonNativeLocalPathMimeType( | 122 file_manager::util::GetNonNativeLocalPathMimeType( |
| 82 profile, | 123 profile, |
| 83 local_path, | 124 local_path, |
| 84 base::Bind(&OnGetMimeTypeForNonNativeLocalPathCompleted, | 125 base::Bind(&OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted, |
| 85 local_path, | 126 local_path, |
| 86 callback)); | 127 callback)); |
| 87 return; | 128 return; |
| 88 } | 129 } |
| 89 #endif | 130 #endif |
| 90 | 131 |
| 91 // For native local files, try to guess the mime from the extension. If | 132 // For native local files, try to guess the mime from the extension. If |
| 92 // not availble, then try to sniff if. | 133 // not available, then try to sniff if. |
| 93 std::string mime_type_from_extension; | 134 scoped_ptr<std::string> mime_type_from_extension(new std::string); |
| 94 if (net::GetMimeTypeFromFile(local_path, &mime_type_from_extension)) { | 135 std::string* const mime_type_from_extension_ptr = |
| 95 callback.Run(mime_type_from_extension); | 136 mime_type_from_extension.get(); |
| 96 } else { | 137 BrowserThread::PostBlockingPoolTaskAndReply( |
| 97 scoped_ptr<std::string> sniffed_mime_type(new std::string); | 138 FROM_HERE, |
| 98 std::string* sniffed_mime_type_ptr = sniffed_mime_type.get(); | 139 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), |
| 99 BrowserThread::PostBlockingPoolTaskAndReply( | 140 local_path, |
| 100 FROM_HERE, | 141 mime_type_from_extension_ptr), |
| 101 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), | 142 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, |
| 102 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, | 143 local_path, |
| 103 base::Passed(&sniffed_mime_type), | 144 base::Passed(&mime_type_from_extension), |
| 104 callback)); | 145 callback)); |
| 105 } | |
| 106 } | 146 } |
| 107 | 147 |
| 108 MimeTypeCollector::MimeTypeCollector(Profile* profile) | 148 MimeTypeCollector::MimeTypeCollector(Profile* profile) |
| 109 : profile_(profile), left_(0), weak_ptr_factory_(this) { | 149 : profile_(profile), left_(0), weak_ptr_factory_(this) { |
| 110 } | 150 } |
| 111 | 151 |
| 112 MimeTypeCollector::~MimeTypeCollector() { | 152 MimeTypeCollector::~MimeTypeCollector() { |
| 113 } | 153 } |
| 114 | 154 |
| 115 void MimeTypeCollector::CollectForURLs( | 155 void MimeTypeCollector::CollectForURLs( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); | 198 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); |
| 159 // Release the callback to avoid a circullar reference in case an instance | 199 // Release the callback to avoid a circullar reference in case an instance |
| 160 // of this class is a member of a ref counted class, which instance is bound | 200 // of this class is a member of a ref counted class, which instance is bound |
| 161 // to this callback. | 201 // to this callback. |
| 162 callback_ = CompletionCallback(); | 202 callback_ = CompletionCallback(); |
| 163 } | 203 } |
| 164 } | 204 } |
| 165 | 205 |
| 166 } // namespace app_file_handler_util | 206 } // namespace app_file_handler_util |
| 167 } // namespace extensions | 207 } // namespace extensions |
| OLD | NEW |