OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/image_decoder.h" | 5 #include "chrome/browser/image_decoder.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/common/chrome_switches.h" | |
11 #include "chrome/common/chrome_utility_messages.h" | 12 #include "chrome/common/chrome_utility_messages.h" |
12 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/utility_process_host.h" | 15 #include "content/public/browser/utility_process_host.h" |
16 #include "content/public/common/service_registry.h" | |
17 #include "services/image_decoder/public/interfaces/image_decoder.mojom.h" | |
18 #include "skia/public/type_converters.h" | |
15 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
16 | 20 |
17 using content::BrowserThread; | 21 using content::BrowserThread; |
18 using content::UtilityProcessHost; | 22 using content::UtilityProcessHost; |
19 | 23 |
20 namespace { | 24 namespace { |
21 | 25 |
22 // static, Leaky to allow access from any thread. | 26 // static, Leaky to allow access from any thread. |
23 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; | 27 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; |
24 | 28 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 } | 214 } |
211 | 215 |
212 last_request_ = base::TimeTicks::Now(); | 216 last_request_ = base::TimeTicks::Now(); |
213 { | 217 { |
214 base::AutoLock lock(map_lock_); | 218 base::AutoLock lock(map_lock_); |
215 auto it = image_request_id_map_.find(request_id); | 219 auto it = image_request_id_map_.find(request_id); |
216 if (it != image_request_id_map_.end()) | 220 if (it != image_request_id_map_.end()) |
217 it->second->SetStarted(); | 221 it->second->SetStarted(); |
218 } | 222 } |
219 | 223 |
220 switch (image_codec) { | 224 if (switches::MojoUtilityServicesEnabled()) { |
221 case ROBUST_JPEG_CODEC: | 225 decoder_->DecodeImage( |
222 utility_process_host_->Send( | 226 mojo::Array<uint8_t>::From(image_data), |
223 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data, request_id)); | 227 image_codec == ROBUST_JPEG_CODEC ? services::IMAGE_CODEC_ROBUST_JPEG |
224 break; | 228 : services::IMAGE_CODEC_DEFAULT, |
225 case DEFAULT_CODEC: | 229 shrink_to_fit, |
226 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | 230 base::Bind(&ImageDecoder::OnDecodeImageDone, this, request_id)); |
227 image_data, shrink_to_fit, request_id)); | 231 } else { |
228 break; | 232 switch (image_codec) { |
233 case ROBUST_JPEG_CODEC: | |
234 utility_process_host_->Send( | |
235 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data, request_id)); | |
236 break; | |
237 case DEFAULT_CODEC: | |
238 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | |
239 image_data, shrink_to_fit, request_id)); | |
240 break; | |
241 } | |
229 } | 242 } |
230 } | 243 } |
231 | 244 |
232 void ImageDecoder::CancelImpl(ImageRequest* image_request) { | 245 void ImageDecoder::CancelImpl(ImageRequest* image_request) { |
233 std::set<scoped_refptr<Job>> jobs; | 246 std::set<scoped_refptr<Job>> jobs; |
234 | 247 |
235 { | 248 { |
236 base::AutoLock lock(map_lock_); | 249 base::AutoLock lock(map_lock_); |
237 for (const auto& request : image_request_id_map_) { | 250 for (const auto& request : image_request_id_map_) { |
238 if (request.second->image_request() == image_request) { | 251 if (request.second->image_request() == image_request) { |
(...skipping 10 matching lines...) Expand all Loading... | |
249 job->Cancel(); | 262 job->Cancel(); |
250 } | 263 } |
251 | 264 |
252 void ImageDecoder::StartBatchMode() { | 265 void ImageDecoder::StartBatchMode() { |
253 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 266 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
254 utility_process_host_ = | 267 utility_process_host_ = |
255 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get()) | 268 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get()) |
256 ->AsWeakPtr(); | 269 ->AsWeakPtr(); |
257 utility_process_host_->SetName(l10n_util::GetStringUTF16( | 270 utility_process_host_->SetName(l10n_util::GetStringUTF16( |
258 IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME)); | 271 IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME)); |
259 if (!utility_process_host_->StartBatchMode()) { | 272 if (switches::MojoUtilityServicesEnabled()) { |
260 utility_process_host_.reset(); | 273 decoder_.reset(); |
261 return; | 274 if (!utility_process_host_->StartMojoMode()) { |
275 utility_process_host_.reset(); | |
276 return; | |
277 } | |
278 content::ServiceRegistry* service_registry = | |
279 utility_process_host_->GetServiceRegistry(); | |
280 service_registry->ConnectToRemoteService(&decoder_); | |
281 } else { | |
282 if (!utility_process_host_->StartBatchMode()) { | |
283 utility_process_host_.reset(); | |
284 return; | |
285 } | |
262 } | 286 } |
263 batch_mode_timer_.Start( | 287 batch_mode_timer_.Start( |
264 FROM_HERE, base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), | 288 FROM_HERE, base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), |
265 this, &ImageDecoder::StopBatchMode); | 289 this, &ImageDecoder::StopBatchMode); |
266 } | 290 } |
267 | 291 |
268 void ImageDecoder::StopBatchMode() { | 292 void ImageDecoder::StopBatchMode() { |
269 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 293 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
270 if ((base::TimeTicks::Now() - last_request_) | 294 if ((base::TimeTicks::Now() - last_request_) |
271 < base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds)) { | 295 < base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds)) { |
272 return; | 296 return; |
273 } | 297 } |
274 | 298 |
275 if (utility_process_host_) { | 299 if (utility_process_host_) { |
276 utility_process_host_->EndBatchMode(); | 300 if (switches::MojoUtilityServicesEnabled()) { |
301 // In Mojo, the utility process needs to be explicitly shut down by | |
302 // deleting the host. | |
303 delete utility_process_host_.get(); | |
304 } else { | |
305 utility_process_host_->EndBatchMode(); | |
306 } | |
277 utility_process_host_.reset(); | 307 utility_process_host_.reset(); |
308 decoder_.reset(); | |
Theresa
2015/04/13 18:52:27
nit: decoder_ only needs to be reset if mojo is en
Anand Mistry (off Chromium)
2015/05/01 06:49:03
Done.
| |
278 } | 309 } |
279 batch_mode_timer_.Stop(); | 310 batch_mode_timer_.Stop(); |
280 | 311 |
281 // There could be outstanding request that are taking too long. Fail these so | 312 // There could be outstanding request that are taking too long. Fail these so |
282 // that there aren't any dangling requests. | 313 // that there aren't any dangling requests. |
283 FailAllSentRequests(); | 314 FailAllSentRequests(); |
284 } | 315 } |
285 | 316 |
286 void ImageDecoder::FailAllSentRequests() { | 317 void ImageDecoder::FailAllSentRequests() { |
287 RequestMap requests; | 318 RequestMap requests; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 base::AutoLock lock(map_lock_); | 382 base::AutoLock lock(map_lock_); |
352 auto it = image_request_id_map_.find(request_id); | 383 auto it = image_request_id_map_.find(request_id); |
353 if (it != image_request_id_map_.end()) { | 384 if (it != image_request_id_map_.end()) { |
354 job = it->second; | 385 job = it->second; |
355 } | 386 } |
356 } | 387 } |
357 if (job) | 388 if (job) |
358 job->NotifyDecodeFailed(); | 389 job->NotifyDecodeFailed(); |
359 } | 390 } |
360 | 391 |
392 void ImageDecoder::OnDecodeImageDone(int request_id, skia::BitmapPtr image) { | |
393 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
394 if (image) { | |
395 SkBitmap bitmap = image->To<SkBitmap>(); | |
396 if (!bitmap.empty()) { | |
397 OnDecodeImageSucceeded(bitmap, request_id); | |
398 return; | |
399 } | |
400 } | |
401 OnDecodeImageFailed(request_id); | |
402 } | |
403 | |
361 void ImageDecoder::RemoveJob(const scoped_refptr<Job>& job) { | 404 void ImageDecoder::RemoveJob(const scoped_refptr<Job>& job) { |
362 base::AutoLock lock(map_lock_); | 405 base::AutoLock lock(map_lock_); |
363 for (auto it = image_request_id_map_.begin(); | 406 for (auto it = image_request_id_map_.begin(); |
364 it != image_request_id_map_.end();) { | 407 it != image_request_id_map_.end();) { |
365 if (it->second == job) { | 408 if (it->second == job) { |
366 image_request_id_map_.erase(it++); | 409 image_request_id_map_.erase(it++); |
367 break; | 410 break; |
368 } else { | 411 } else { |
369 ++it; | 412 ++it; |
370 } | 413 } |
371 } | 414 } |
372 } | 415 } |
OLD | NEW |