[email protected] | 44013f68 | 2012-05-31 13:49:40 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6d33da17 | 2011-11-22 03:56:09 | [diff] [blame] | 5 | #include "chrome/browser/image_decoder.h" |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 6 | |
[email protected] | 3189377 | 2011-10-11 17:41:50 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 8 | #include "chrome/browser/browser_process.h" |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 9 | #include "chrome/common/chrome_utility_messages.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 10 | #include "content/public/browser/browser_thread.h" |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 11 | #include "content/public/browser/utility_process_host.h" |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 12 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 13 | using content::BrowserThread; |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 14 | using content::UtilityProcessHost; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 15 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 16 | ImageDecoder::ImageDecoder(Delegate* delegate, |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 17 | const std::string& image_data, |
| 18 | ImageCodec image_codec) |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 19 | : delegate_(delegate), |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 20 | image_data_(image_data.begin(), image_data.end()), |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 21 | image_codec_(image_codec), |
glevin | b18f4aa | 2014-09-26 15:02:33 | [diff] [blame] | 22 | task_runner_(NULL), |
| 23 | shrink_to_fit_(false) { |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 24 | } |
| 25 | |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame^] | 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), |
| 32 | task_runner_(NULL), |
| 33 | shrink_to_fit_(false) { |
| 34 | } |
| 35 | |
[email protected] | 863e6d96 | 2011-05-15 19:39:35 | [diff] [blame] | 36 | ImageDecoder::~ImageDecoder() {} |
| 37 | |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 38 | void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) { |
| 39 | task_runner_ = task_runner; |
[email protected] | cca169b5 | 2010-10-08 22:15:55 | [diff] [blame] | 40 | BrowserThread::PostTask( |
| 41 | BrowserThread::IO, FROM_HERE, |
[email protected] | 3189377 | 2011-10-11 17:41:50 | [diff] [blame] | 42 | base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_)); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 45 | bool ImageDecoder::OnMessageReceived(const IPC::Message& message) { |
| 46 | bool handled = true; |
| 47 | IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message) |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 48 | IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 49 | OnDecodeImageSucceeded) |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 50 | IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, |
| 51 | OnDecodeImageFailed) |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 52 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 1f946bc | 2011-11-17 00:52:30 | [diff] [blame] | 53 | IPC_END_MESSAGE_MAP() |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 54 | return handled; |
| 55 | } |
| 56 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 57 | void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) { |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 58 | DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 59 | if (delegate_) |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 60 | delegate_->OnImageDecoded(this, decoded_image); |
| 61 | } |
| 62 | |
| 63 | void ImageDecoder::OnDecodeImageFailed() { |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 64 | DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 65 | if (delegate_) |
| 66 | delegate_->OnDecodeImageFailed(this); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void ImageDecoder::DecodeImageInSandbox( |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 70 | const std::vector<unsigned char>& image_data) { |
[email protected] | cca169b5 | 2010-10-08 22:15:55 | [diff] [blame] | 71 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 72 | UtilityProcessHost* utility_process_host; |
| 73 | utility_process_host = UtilityProcessHost::Create(this, task_runner_.get()); |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 74 | if (image_codec_ == ROBUST_JPEG_CODEC) { |
| 75 | utility_process_host->Send( |
| 76 | new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data)); |
| 77 | } else { |
glevin | b18f4aa | 2014-09-26 15:02:33 | [diff] [blame] | 78 | utility_process_host->Send( |
| 79 | new ChromeUtilityMsg_DecodeImage(image_data, shrink_to_fit_)); |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 80 | } |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 81 | } |