Index: chrome/browser/image_batch_decoder.cc |
diff --git a/chrome/browser/image_batch_decoder.cc b/chrome/browser/image_batch_decoder.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c9d515bf00ebdf8d531548f87f81412d05f26a59 |
--- /dev/null |
+++ b/chrome/browser/image_batch_decoder.cc |
@@ -0,0 +1,90 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/image_batch_decoder.h" |
+ |
+#include "base/bind.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/common/chrome_utility_messages.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+using content::BrowserThread; |
+ |
+ImageBatchDecoder::ImageBatchDecoder( |
+ scoped_refptr<base::SequencedTaskRunner> task_runner) |
+ : task_runner_(task_runner) { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind( |
+ &ImageBatchDecoder::ConstructUtilityProcessHost, |
+ this)); |
+} |
+ |
+ImageBatchDecoder::~ImageBatchDecoder() { |
+ utility_process_host_->EndBatchMode(); |
+} |
+ |
+void ImageBatchDecoder::ConstructUtilityProcessHost() { |
+ utility_process_host_ = |
+ UtilityProcessHost::Create(this, task_runner_.get()); |
+ utility_process_host_->StartBatchMode(); |
+} |
+ |
+void ImageBatchDecoder::Start(Delegate* delegate, |
+ const std::string& image_data, |
+ ImageCodec image_codec) { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind( |
+ &ImageBatchDecoder::DecodeImageInSandbox, |
+ this, |
+ delegate, |
+ std::vector<unsigned char>(image_data.begin(), image_data.end()), |
+ image_codec)); |
+} |
+ |
+bool ImageBatchDecoder::OnMessageReceived(const IPC::Message& message) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP(ImageBatchDecoder, message) |
+ IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, |
+ OnDecodeImageSucceeded) |
+ IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, |
+ OnDecodeImageFailed) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ return handled; |
+} |
+ |
+void ImageBatchDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image, |
+ int id) { |
+ DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
+ if (delegate_map_.find(id) != delegate_map_.end()) { |
+ delegate_map_[id]->OnImageDecoded(this, decoded_image); |
+ delegate_map_.erase(id); |
+ } |
+} |
+ |
+void ImageBatchDecoder::OnDecodeImageFailed(int id) { |
+ DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
+ if (delegate_map_.find(id) != delegate_map_.end()) { |
+ delegate_map_[id]->OnDecodeImageFailed(this); |
+ delegate_map_.erase(id); |
+ } |
+} |
+ |
+void ImageBatchDecoder::DecodeImageInSandbox( |
+ Delegate* delegate, |
+ const std::vector<unsigned char>& image_data, |
+ ImageCodec image_codec) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ delegate_map_[id_counter_] = delegate; |
+ if (image_codec == ROBUST_JPEG_CODEC) { |
+ utility_process_host_->Send( |
+ new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data, id_counter_)); |
+ } else { |
+ utility_process_host_->Send( |
+ new ChromeUtilityMsg_DecodeImage(image_data, false, id_counter_)); |
+ } |
+ ++id_counter_; |
+} |