blob: 2f9e83b7ab8953f41eb2d5a4bfc091e8f7e81ce7 [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),
glevinb18f4aa2014-09-26 15:02:3322 task_runner_(NULL),
23 shrink_to_fit_(false) {
[email protected]551707a2010-06-16 16:59:4724}
25
pneubeck93871252015-01-20 11:26:3626ImageDecoder::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),
32 task_runner_(NULL),
33 shrink_to_fit_(false) {
34}
35
[email protected]863e6d962011-05-15 19:39:3536ImageDecoder::~ImageDecoder() {}
37
[email protected]810dddf52013-01-25 19:37:2638void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) {
39 task_runner_ = task_runner;
[email protected]cca169b52010-10-08 22:15:5540 BrowserThread::PostTask(
41 BrowserThread::IO, FROM_HERE,
[email protected]31893772011-10-11 17:41:5042 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_));
[email protected]551707a2010-06-16 16:59:4743}
44
[email protected]373c1062011-06-09 21:11:5145bool ImageDecoder::OnMessageReceived(const IPC::Message& message) {
46 bool handled = true;
47 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message)
[email protected]2ccf45c2011-08-19 23:35:5048 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded,
[email protected]373c1062011-06-09 21:11:5149 OnDecodeImageSucceeded)
[email protected]2ccf45c2011-08-19 23:35:5050 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed,
51 OnDecodeImageFailed)
[email protected]373c1062011-06-09 21:11:5152 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]1f946bc2011-11-17 00:52:3053 IPC_END_MESSAGE_MAP()
[email protected]373c1062011-06-09 21:11:5154 return handled;
55}
56
[email protected]551707a2010-06-16 16:59:4757void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) {
[email protected]810dddf52013-01-25 19:37:2658 DCHECK(task_runner_->RunsTasksOnCurrentThread());
[email protected]551707a2010-06-16 16:59:4759 if (delegate_)
[email protected]928c0e712011-03-16 15:01:5760 delegate_->OnImageDecoded(this, decoded_image);
61}
62
63void ImageDecoder::OnDecodeImageFailed() {
[email protected]810dddf52013-01-25 19:37:2664 DCHECK(task_runner_->RunsTasksOnCurrentThread());
[email protected]928c0e712011-03-16 15:01:5765 if (delegate_)
66 delegate_->OnDecodeImageFailed(this);
[email protected]551707a2010-06-16 16:59:4767}
68
69void ImageDecoder::DecodeImageInSandbox(
[email protected]551707a2010-06-16 16:59:4770 const std::vector<unsigned char>& image_data) {
[email protected]cca169b52010-10-08 22:15:5571 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]810dddf52013-01-25 19:37:2672 UtilityProcessHost* utility_process_host;
73 utility_process_host = UtilityProcessHost::Create(this, task_runner_.get());
[email protected]11f16d102012-08-29 23:12:1574 if (image_codec_ == ROBUST_JPEG_CODEC) {
75 utility_process_host->Send(
76 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data));
77 } else {
glevinb18f4aa2014-09-26 15:02:3378 utility_process_host->Send(
79 new ChromeUtilityMsg_DecodeImage(image_data, shrink_to_fit_));
[email protected]11f16d102012-08-29 23:12:1580 }
[email protected]551707a2010-06-16 16:59:4781}