[email protected] | 4d9dcff | 2011-04-06 22:49:08 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 11 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 12 | using content::BrowserThread; |
| 13 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 14 | ImageDecoder::ImageDecoder(Delegate* delegate, |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 15 | const std::string& image_data) |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 16 | : delegate_(delegate), |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 17 | image_data_(image_data.begin(), image_data.end()), |
| 18 | target_thread_id_(BrowserThread::UI) { |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 19 | } |
| 20 | |
[email protected] | 863e6d96 | 2011-05-15 19:39:35 | [diff] [blame] | 21 | ImageDecoder::~ImageDecoder() {} |
| 22 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 23 | void ImageDecoder::Start() { |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 24 | if (!BrowserThread::GetCurrentThreadIdentifier(&target_thread_id_)) { |
| 25 | NOTREACHED(); |
| 26 | return; |
| 27 | } |
[email protected] | cca169b5 | 2010-10-08 22:15:55 | [diff] [blame] | 28 | BrowserThread::PostTask( |
| 29 | BrowserThread::IO, FROM_HERE, |
[email protected] | 3189377 | 2011-10-11 17:41:50 | [diff] [blame] | 30 | base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_)); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 33 | bool ImageDecoder::OnMessageReceived(const IPC::Message& message) { |
| 34 | bool handled = true; |
| 35 | IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message) |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 36 | IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 37 | OnDecodeImageSucceeded) |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 38 | IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, |
| 39 | OnDecodeImageFailed) |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 40 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 1f946bc | 2011-11-17 00:52:30 | [diff] [blame] | 41 | IPC_END_MESSAGE_MAP() |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 42 | return handled; |
| 43 | } |
| 44 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 45 | void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) { |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 46 | DCHECK(BrowserThread::CurrentlyOn(target_thread_id_)); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 47 | if (delegate_) |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 48 | delegate_->OnImageDecoded(this, decoded_image); |
| 49 | } |
| 50 | |
| 51 | void ImageDecoder::OnDecodeImageFailed() { |
| 52 | DCHECK(BrowserThread::CurrentlyOn(target_thread_id_)); |
| 53 | if (delegate_) |
| 54 | delegate_->OnDecodeImageFailed(this); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void ImageDecoder::DecodeImageInSandbox( |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 58 | const std::vector<unsigned char>& image_data) { |
[email protected] | cca169b5 | 2010-10-08 22:15:55 | [diff] [blame] | 59 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 60 | UtilityProcessHost* utility_process_host = |
[email protected] | 4d9dcff | 2011-04-06 22:49:08 | [diff] [blame] | 61 | new UtilityProcessHost(this, |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 62 | target_thread_id_); |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 63 | utility_process_host->Send(new ChromeUtilityMsg_DecodeImage(image_data)); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 64 | } |