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
),
23 shrink_to_fit_(false) {
26 ImageDecoder::ImageDecoder(Delegate
* delegate
,
27 const std::vector
<char>& image_data
,
28 ImageCodec image_codec
)
29 : delegate_(delegate
),
30 image_data_(image_data
.begin(), image_data
.end()),
31 image_codec_(image_codec
),
33 shrink_to_fit_(false) {
36 ImageDecoder::~ImageDecoder() {}
38 void ImageDecoder::Start(scoped_refptr
<base::SequencedTaskRunner
> task_runner
) {
39 task_runner_
= task_runner
;
40 BrowserThread::PostTask(
41 BrowserThread::IO
, FROM_HERE
,
42 base::Bind(&ImageDecoder::DecodeImageInSandbox
, this, image_data_
));
45 bool ImageDecoder::OnMessageReceived(const IPC::Message
& message
) {
47 IPC_BEGIN_MESSAGE_MAP(ImageDecoder
, message
)
48 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded
,
49 OnDecodeImageSucceeded
)
50 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed
,
52 IPC_MESSAGE_UNHANDLED(handled
= false)
57 void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap
& decoded_image
) {
58 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
60 delegate_
->OnImageDecoded(this, decoded_image
);
63 void ImageDecoder::OnDecodeImageFailed() {
64 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
66 delegate_
->OnDecodeImageFailed(this);
69 void ImageDecoder::DecodeImageInSandbox(
70 const std::vector
<unsigned char>& image_data
) {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
72 UtilityProcessHost
* utility_process_host
;
73 utility_process_host
= UtilityProcessHost::Create(this, task_runner_
.get());
74 if (image_codec_
== ROBUST_JPEG_CODEC
) {
75 utility_process_host
->Send(
76 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data
));
78 utility_process_host
->Send(
79 new ChromeUtilityMsg_DecodeImage(image_data
, shrink_to_fit_
));