Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: chrome/browser/extensions/api/file_handlers/mime_util.cc

Issue 2675203004: Use TaskScheduler instead of blocking pool in mime_util.cc. (Closed)
Patch Set: upload Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/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 "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
12 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
13 #include "net/base/filename_util.h" 14 #include "net/base/filename_util.h"
14 #include "net/base/mime_sniffer.h" 15 #include "net/base/mime_sniffer.h"
15 #include "net/base/mime_util.h" 16 #include "net/base/mime_util.h"
16 #include "storage/browser/fileapi/file_system_url.h" 17 #include "storage/browser/fileapi/file_system_url.h"
17 18
18 #if defined(OS_CHROMEOS) 19 #if defined(OS_CHROMEOS)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 if (success) { 69 if (success) {
69 callback.Run(mime_type); 70 callback.Run(mime_type);
70 return; 71 return;
71 } 72 }
72 73
73 // MIME type not available with metadata, hence try to guess it from the 74 // MIME type not available with metadata, hence try to guess it from the
74 // file's extension. 75 // file's extension.
75 std::unique_ptr<std::string> mime_type_from_extension(new std::string); 76 std::unique_ptr<std::string> mime_type_from_extension(new std::string);
76 std::string* const mime_type_from_extension_ptr = 77 std::string* const mime_type_from_extension_ptr =
77 mime_type_from_extension.get(); 78 mime_type_from_extension.get();
78 BrowserThread::PostBlockingPoolTaskAndReply( 79 base::PostTaskWithTraitsAndReply(
79 FROM_HERE, 80 FROM_HERE, base::TaskTraits().MayBlock().WithPriority(
80 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), 81 base::TaskPriority::BACKGROUND),
benwells 2017/02/14 06:03:21 I think the priority for all of these should be US
81 local_path, 82 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), local_path,
82 mime_type_from_extension_ptr), 83 mime_type_from_extension_ptr),
83 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted, 84 base::Bind(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted,
84 base::Passed(&mime_type_from_extension), 85 base::Passed(&mime_type_from_extension), 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 std::unique_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
(...skipping 16 matching lines...) Expand all
112 std::unique_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 std::unique_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 base::PostTaskWithTraitsAndReply(
123 FROM_HERE, 123 FROM_HERE, base::TaskTraits().MayBlock().WithPriority(
124 base::TaskPriority::BACKGROUND),
124 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr), 125 base::Bind(&SniffMimeType, local_path, sniffed_mime_type_ptr),
125 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted, 126 base::Bind(&OnSniffMimeTypeForNativeLocalPathCompleted,
126 base::Passed(&sniffed_mime_type), 127 base::Passed(&sniffed_mime_type), callback));
127 callback));
128 } 128 }
129 129
130 // Fetches MIME type for a local path and returns it with a |callback|. 130 // Fetches MIME type for a local path and returns it with a |callback|.
131 void GetMimeTypeForLocalPath( 131 void GetMimeTypeForLocalPath(
132 Profile* profile, 132 Profile* profile,
133 const base::FilePath& local_path, 133 const base::FilePath& local_path,
134 const base::Callback<void(const std::string&)>& callback) { 134 const base::Callback<void(const std::string&)>& callback) {
135 #if defined(OS_CHROMEOS) 135 #if defined(OS_CHROMEOS)
136 if (file_manager::util::IsUnderNonNativeLocalPath(profile, local_path)) { 136 if (file_manager::util::IsUnderNonNativeLocalPath(profile, local_path)) {
137 // For non-native files, try to get the MIME type from metadata. If not 137 // For non-native files, try to get the MIME type from metadata. If not
138 // available, then try to guess from the extension. Never sniff (because 138 // available, then try to guess from the extension. Never sniff (because
139 // it can be very slow). 139 // it can be very slow).
140 file_manager::util::GetNonNativeLocalPathMimeType( 140 file_manager::util::GetNonNativeLocalPathMimeType(
141 profile, 141 profile,
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 std::unique_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 base::PostTaskWithTraitsAndReply(
156 FROM_HERE, 156 FROM_HERE, base::TaskTraits().MayBlock().WithPriority(
157 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), 157 base::TaskPriority::BACKGROUND),
158 local_path, 158 base::Bind(base::IgnoreResult(&net::GetMimeTypeFromFile), local_path,
159 mime_type_from_extension_ptr), 159 mime_type_from_extension_ptr),
160 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, 160 base::Bind(&OnGetMimeTypeFromFileForNativeLocalPathCompleted, local_path,
161 local_path, 161 base::Passed(&mime_type_from_extension), callback));
162 base::Passed(&mime_type_from_extension),
163 callback));
164 } 162 }
165 163
166 MimeTypeCollector::MimeTypeCollector(Profile* profile) 164 MimeTypeCollector::MimeTypeCollector(Profile* profile)
167 : profile_(profile), left_(0), weak_ptr_factory_(this) { 165 : profile_(profile), left_(0), weak_ptr_factory_(this) {
168 } 166 }
169 167
170 MimeTypeCollector::~MimeTypeCollector() { 168 MimeTypeCollector::~MimeTypeCollector() {
171 } 169 }
172 170
173 void MimeTypeCollector::CollectForURLs( 171 void MimeTypeCollector::CollectForURLs(
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 FROM_HERE, base::Bind(callback_, base::Passed(&result_))); 214 FROM_HERE, base::Bind(callback_, base::Passed(&result_)));
217 // 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
218 // 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
219 // to this callback. 217 // to this callback.
220 callback_ = CompletionCallback(); 218 callback_ = CompletionCallback();
221 } 219 }
222 } 220 }
223 221
224 } // namespace app_file_handler_util 222 } // namespace app_file_handler_util
225 } // namespace extensions 223 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698