[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), |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 22 | task_runner_(NULL) { |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 23 | } |
| 24 | |
[email protected] | 863e6d96 | 2011-05-15 19:39:35 | [diff] [blame] | 25 | ImageDecoder::~ImageDecoder() {} |
| 26 | |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 27 | void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) { |
| 28 | task_runner_ = task_runner; |
[email protected] | cca169b5 | 2010-10-08 22:15:55 | [diff] [blame] | 29 | BrowserThread::PostTask( |
| 30 | BrowserThread::IO, FROM_HERE, |
[email protected] | 3189377 | 2011-10-11 17:41:50 | [diff] [blame] | 31 | base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_)); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 34 | bool ImageDecoder::OnMessageReceived(const IPC::Message& message) { |
| 35 | bool handled = true; |
| 36 | IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message) |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 37 | IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 38 | OnDecodeImageSucceeded) |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 39 | IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, |
| 40 | OnDecodeImageFailed) |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 41 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 1f946bc | 2011-11-17 00:52:30 | [diff] [blame] | 42 | IPC_END_MESSAGE_MAP() |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 43 | return handled; |
| 44 | } |
| 45 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 46 | void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) { |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 47 | DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 48 | if (delegate_) |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 49 | delegate_->OnImageDecoded(this, decoded_image); |
| 50 | } |
| 51 | |
| 52 | void ImageDecoder::OnDecodeImageFailed() { |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 53 | DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 54 | if (delegate_) |
| 55 | delegate_->OnDecodeImageFailed(this); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void ImageDecoder::DecodeImageInSandbox( |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 59 | const std::vector<unsigned char>& image_data) { |
[email protected] | cca169b5 | 2010-10-08 22:15:55 | [diff] [blame] | 60 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 61 | UtilityProcessHost* utility_process_host; |
| 62 | utility_process_host = UtilityProcessHost::Create(this, task_runner_.get()); |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 63 | utility_process_host->EnableZygote(); |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 64 | 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] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 70 | } |