1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/image_decoder.h" 6 7 #include "base/bind.h" 8 #include "chrome/browser/browser_process.h" 9 #include "chrome/common/chrome_utility_messages.h" 10 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/utility_process_host.h" 12 13 using content::BrowserThread; 14 using content::UtilityProcessHost; 15 16 ImageDecoder::ImageDecoder(Delegate* delegate, 17 const std::string& image_data, 18 ImageCodec image_codec) 19 : delegate_(delegate), 20 image_data_(image_data.begin(), image_data.end()), 21 image_codec_(image_codec), 22 task_runner_(NULL) { 23 } 24 25 ImageDecoder::~ImageDecoder() {} 26 27 void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) { 28 task_runner_ = task_runner; 29 BrowserThread::PostTask( 30 BrowserThread::IO, FROM_HERE, 31 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_)); 32 } 33 34 bool ImageDecoder::OnMessageReceived(const IPC::Message& message) { 35 bool handled = true; 36 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message) 37 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, 38 OnDecodeImageSucceeded) 39 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, 40 OnDecodeImageFailed) 41 IPC_MESSAGE_UNHANDLED(handled = false) 42 IPC_END_MESSAGE_MAP() 43 return handled; 44 } 45 46 void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) { 47 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 48 if (delegate_) 49 delegate_->OnImageDecoded(this, decoded_image); 50 } 51 52 void ImageDecoder::OnDecodeImageFailed() { 53 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 54 if (delegate_) 55 delegate_->OnDecodeImageFailed(this); 56 } 57 58 void ImageDecoder::DecodeImageInSandbox( 59 const std::vector<unsigned char>& image_data) { 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 61 UtilityProcessHost* utility_process_host; 62 utility_process_host = UtilityProcessHost::Create(this, task_runner_.get()); 63 if (image_codec_ == ROBUST_JPEG_CODEC) { 64 utility_process_host->Send( 65 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data)); 66 } else { 67 utility_process_host->Send(new ChromeUtilityMsg_DecodeImage(image_data)); 68 } 69 } 70