Index: chrome/browser/image_batch_decoder.h |
diff --git a/chrome/browser/image_batch_decoder.h b/chrome/browser/image_batch_decoder.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a467ea1fdb212fef91cab2df567ac82ce0e31216 |
--- /dev/null |
+++ b/chrome/browser/image_batch_decoder.h |
@@ -0,0 +1,89 @@ |
+// 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. |
+ |
+#ifndef CHROME_BROWSER_IMAGE_BATCH_DECODER_H_ |
+#define CHROME_BROWSER_IMAGE_BATCH_DECODER_H_ |
+ |
+#include <map> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/bind.h" |
+#include "base/compiler_specific.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/threading/sequenced_worker_pool.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/utility_process_host.h" |
+#include "content/public/browser/utility_process_host_client.h" |
+ |
+using content::UtilityProcessHost; |
+using content::BrowserThread; |
+ |
+class SkBitmap; |
+ |
+// Decodes images in a sandboxed process. Reuses a single UtilityProcessHost |
+// in batch mode to facilitate decoding multiple images. |
+class ImageBatchDecoder : public content::UtilityProcessHostClient { |
+ public: |
+ class Delegate { |
+ public: |
+ // Called when image is decoded. |
+ // |decoder| is used to identify the image in case of decoding several |
+ // images simultaneously. |
+ virtual void OnImageDecoded(const ImageBatchDecoder* decoder, |
+ const SkBitmap& decoded_image) = 0; |
+ |
+ // Called when decoding image failed. Delegate can do some cleanup in |
+ // this handler. |
+ virtual void OnDecodeImageFailed(const ImageBatchDecoder* decoder) {} |
+ |
+ protected: |
+ virtual ~Delegate() {} |
+ }; |
+ |
+ enum ImageCodec { |
+ DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
+ ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
+ }; |
+ |
+ // Constructs an ImageBatchDecoder. Callbacks for decoded images wil be |
+ // posted back to |task_runner|. |
+ explicit ImageBatchDecoder( |
+ scoped_refptr<base::SequencedTaskRunner> task_runner); |
+ |
+ // Starts asynchronous image decoding. Once finished, the callback will be |
+ // posted back to |task_runner|. |
+ void Start( |
+ Delegate* delegate, |
+ const std::string& image_data, |
+ ImageCodec image_codec); |
+ |
+ private: |
+ // It's a reference counted object, so destructor is private. |
+ ~ImageBatchDecoder() override; |
+ |
+ // Creates a UtilityProcessHost and starts it in batch mode. |
+ void ConstructUtilityProcessHost(); |
+ |
+ // Overidden from UtilityProcessHostClient: |
+ bool OnMessageReceived(const IPC::Message& message) override; |
+ |
+ // IPC message handlers. |
+ void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int id); |
+ void OnDecodeImageFailed(int id); |
+ |
+ // Launches sandboxed process that will decode the image. |
+ void DecodeImageInSandbox(Delegate* delegate, |
+ const std::vector<unsigned char>& image_data, |
+ ImageCodec image_codec); |
+ |
+ scoped_refptr<base::SequencedTaskRunner> task_runner_; |
+ int id_counter_ = 0; |
+ std::map<int, Delegate*> delegate_map_; |
+ content::UtilityProcessHost* utility_process_host_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ImageBatchDecoder); |
+}; |
+ |
+#endif // CHROME_BROWSER_IMAGE_BATCH_DECODER_H_ |