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
3 // found in the LICENSE file.
5 #include "chrome/browser/image_decoder.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/common/chrome_utility_messages.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/utility_process_host.h"
13 using content::BrowserThread
;
14 using content::UtilityProcessHost
;
16 ImageDecoder::ImageDecoder(Delegate
* delegate
,
17 const std::string
& image_data
,
18 ImageCodec image_codec
)
19 : delegate_(delegate
),
20 image_data_(image_data
.begin(), image_data
.end()),
21 image_codec_(image_codec
),
25 ImageDecoder::~ImageDecoder() {}
27 void ImageDecoder::Start(scoped_refptr
<base::SequencedTaskRunner
> task_runner
) {
28 task_runner_
= task_runner
;
29 BrowserThread::PostTask(
30 BrowserThread::IO
, FROM_HERE
,
31 base::Bind(&ImageDecoder::DecodeImageInSandbox
, this, image_data_
));
34 bool ImageDecoder::OnMessageReceived(const IPC::Message
& message
) {
36 IPC_BEGIN_MESSAGE_MAP(ImageDecoder
, message
)
37 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded
,
38 OnDecodeImageSucceeded
)
39 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed
,
41 IPC_MESSAGE_UNHANDLED(handled
= false)
46 void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap
& decoded_image
) {
47 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
49 delegate_
->OnImageDecoded(this, decoded_image
);
52 void ImageDecoder::OnDecodeImageFailed() {
53 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
55 delegate_
->OnDecodeImageFailed(this);
58 void ImageDecoder::DecodeImageInSandbox(
59 const std::vector
<unsigned char>& image_data
) {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
61 UtilityProcessHost
* utility_process_host
;
62 utility_process_host
= UtilityProcessHost::Create(this, task_runner_
.get());
63 utility_process_host
->EnableZygote();
64 if (image_codec_
== ROBUST_JPEG_CODEC
) {
65 utility_process_host
->Send(
66 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data
));
68 utility_process_host
->Send(new ChromeUtilityMsg_DecodeImage(image_data
));