blob: b5b71668a3bc7bc1ca11c765f38b857a6726ad6e [file] [log] [blame]
[email protected]44013f682012-05-31 13:49:401// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]551707a2010-06-16 16:59:472// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6d33da172011-11-22 03:56:095#include "chrome/browser/image_decoder.h"
[email protected]551707a2010-06-16 16:59:476
[email protected]31893772011-10-11 17:41:507#include "base/bind.h"
[email protected]551707a2010-06-16 16:59:478#include "chrome/browser/browser_process.h"
[email protected]373c1062011-06-09 21:11:519#include "chrome/common/chrome_utility_messages.h"
[email protected]c38831a12011-10-28 12:44:4910#include "content/public/browser/browser_thread.h"
[email protected]c4f883a2012-02-03 17:02:0711#include "content/public/browser/utility_process_host.h"
[email protected]551707a2010-06-16 16:59:4712
[email protected]631bb742011-11-02 11:29:3913using content::BrowserThread;
[email protected]c4f883a2012-02-03 17:02:0714using content::UtilityProcessHost;
[email protected]631bb742011-11-02 11:29:3915
[email protected]551707a2010-06-16 16:59:4716ImageDecoder::ImageDecoder(Delegate* delegate,
[email protected]11f16d102012-08-29 23:12:1517 const std::string& image_data,
18 ImageCodec image_codec)
[email protected]551707a2010-06-16 16:59:4719 : delegate_(delegate),
[email protected]928c0e712011-03-16 15:01:5720 image_data_(image_data.begin(), image_data.end()),
[email protected]11f16d102012-08-29 23:12:1521 image_codec_(image_codec),
[email protected]810dddf52013-01-25 19:37:2622 task_runner_(NULL) {
[email protected]551707a2010-06-16 16:59:4723}
24
[email protected]863e6d962011-05-15 19:39:3525ImageDecoder::~ImageDecoder() {}
26
[email protected]810dddf52013-01-25 19:37:2627void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) {
28 task_runner_ = task_runner;
[email protected]cca169b52010-10-08 22:15:5529 BrowserThread::PostTask(
30 BrowserThread::IO, FROM_HERE,
[email protected]31893772011-10-11 17:41:5031 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_));
[email protected]551707a2010-06-16 16:59:4732}
33
[email protected]373c1062011-06-09 21:11:5134bool ImageDecoder::OnMessageReceived(const IPC::Message& message) {
35 bool handled = true;
36 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message)
[email protected]2ccf45c2011-08-19 23:35:5037 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded,
[email protected]373c1062011-06-09 21:11:5138 OnDecodeImageSucceeded)
[email protected]2ccf45c2011-08-19 23:35:5039 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed,
40 OnDecodeImageFailed)
[email protected]373c1062011-06-09 21:11:5141 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]1f946bc2011-11-17 00:52:3042 IPC_END_MESSAGE_MAP()
[email protected]373c1062011-06-09 21:11:5143 return handled;
44}
45
[email protected]551707a2010-06-16 16:59:4746void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) {
[email protected]810dddf52013-01-25 19:37:2647 DCHECK(task_runner_->RunsTasksOnCurrentThread());
[email protected]551707a2010-06-16 16:59:4748 if (delegate_)
[email protected]928c0e712011-03-16 15:01:5749 delegate_->OnImageDecoded(this, decoded_image);
50}
51
52void ImageDecoder::OnDecodeImageFailed() {
[email protected]810dddf52013-01-25 19:37:2653 DCHECK(task_runner_->RunsTasksOnCurrentThread());
[email protected]928c0e712011-03-16 15:01:5754 if (delegate_)
55 delegate_->OnDecodeImageFailed(this);
[email protected]551707a2010-06-16 16:59:4756}
57
58void ImageDecoder::DecodeImageInSandbox(
[email protected]551707a2010-06-16 16:59:4759 const std::vector<unsigned char>& image_data) {
[email protected]cca169b52010-10-08 22:15:5560 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]810dddf52013-01-25 19:37:2661 UtilityProcessHost* utility_process_host;
62 utility_process_host = UtilityProcessHost::Create(this, task_runner_.get());
[email protected]c4f883a2012-02-03 17:02:0763 utility_process_host->EnableZygote();
[email protected]11f16d102012-08-29 23:12:1564 if (image_codec_ == ROBUST_JPEG_CODEC) {
65 utility_process_host->Send(
66 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data));
67 } else {
68 utility_process_host->Send(new ChromeUtilityMsg_DecodeImage(image_data));
69 }
[email protected]551707a2010-06-16 16:59:4770}