blob: 3fb7aa8d1f41a07d41c5995860794d9f5b558294 [file] [log] [blame]
[email protected]4d9dcff2011-04-06 22:49:081// Copyright (c) 2011 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]551707a2010-06-16 16:59:4711
[email protected]631bb742011-11-02 11:29:3912using content::BrowserThread;
13
[email protected]551707a2010-06-16 16:59:4714ImageDecoder::ImageDecoder(Delegate* delegate,
[email protected]928c0e712011-03-16 15:01:5715 const std::string& image_data)
[email protected]551707a2010-06-16 16:59:4716 : delegate_(delegate),
[email protected]928c0e712011-03-16 15:01:5717 image_data_(image_data.begin(), image_data.end()),
18 target_thread_id_(BrowserThread::UI) {
[email protected]551707a2010-06-16 16:59:4719}
20
[email protected]863e6d962011-05-15 19:39:3521ImageDecoder::~ImageDecoder() {}
22
[email protected]551707a2010-06-16 16:59:4723void ImageDecoder::Start() {
[email protected]928c0e712011-03-16 15:01:5724 if (!BrowserThread::GetCurrentThreadIdentifier(&target_thread_id_)) {
25 NOTREACHED();
26 return;
27 }
[email protected]cca169b52010-10-08 22:15:5528 BrowserThread::PostTask(
29 BrowserThread::IO, FROM_HERE,
[email protected]31893772011-10-11 17:41:5030 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_));
[email protected]551707a2010-06-16 16:59:4731}
32
[email protected]373c1062011-06-09 21:11:5133bool ImageDecoder::OnMessageReceived(const IPC::Message& message) {
34 bool handled = true;
35 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message)
[email protected]2ccf45c2011-08-19 23:35:5036 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded,
[email protected]373c1062011-06-09 21:11:5137 OnDecodeImageSucceeded)
[email protected]2ccf45c2011-08-19 23:35:5038 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed,
39 OnDecodeImageFailed)
[email protected]373c1062011-06-09 21:11:5140 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]1f946bc2011-11-17 00:52:3041 IPC_END_MESSAGE_MAP()
[email protected]373c1062011-06-09 21:11:5142 return handled;
43}
44
[email protected]551707a2010-06-16 16:59:4745void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) {
[email protected]928c0e712011-03-16 15:01:5746 DCHECK(BrowserThread::CurrentlyOn(target_thread_id_));
[email protected]551707a2010-06-16 16:59:4747 if (delegate_)
[email protected]928c0e712011-03-16 15:01:5748 delegate_->OnImageDecoded(this, decoded_image);
49}
50
51void ImageDecoder::OnDecodeImageFailed() {
52 DCHECK(BrowserThread::CurrentlyOn(target_thread_id_));
53 if (delegate_)
54 delegate_->OnDecodeImageFailed(this);
[email protected]551707a2010-06-16 16:59:4755}
56
57void ImageDecoder::DecodeImageInSandbox(
[email protected]551707a2010-06-16 16:59:4758 const std::vector<unsigned char>& image_data) {
[email protected]cca169b52010-10-08 22:15:5559 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]551707a2010-06-16 16:59:4760 UtilityProcessHost* utility_process_host =
[email protected]4d9dcff2011-04-06 22:49:0861 new UtilityProcessHost(this,
[email protected]928c0e712011-03-16 15:01:5762 target_thread_id_);
[email protected]2ccf45c2011-08-19 23:35:5063 utility_process_host->Send(new ChromeUtilityMsg_DecodeImage(image_data));
[email protected]551707a2010-06-16 16:59:4764}