OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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 #ifndef CHROME_BROWSER_IMAGE_BATCH_DECODER_H_ |
| 6 #define CHROME_BROWSER_IMAGE_BATCH_DECODER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/utility_process_host.h" |
| 18 #include "content/public/browser/utility_process_host_client.h" |
| 19 |
| 20 using content::UtilityProcessHost; |
| 21 using content::BrowserThread; |
| 22 |
| 23 class SkBitmap; |
| 24 |
| 25 // Decodes images in a sandboxed process. Reuses a single UtilityProcessHost |
| 26 // in batch mode to facilitate decoding multiple images. |
| 27 class ImageBatchDecoder : public content::UtilityProcessHostClient { |
| 28 public: |
| 29 class Delegate { |
| 30 public: |
| 31 // Called when image is decoded. |
| 32 // |decoder| is used to identify the image in case of decoding several |
| 33 // images simultaneously. |
| 34 virtual void OnImageDecoded(const ImageBatchDecoder* decoder, |
| 35 const SkBitmap& decoded_image) = 0; |
| 36 |
| 37 // Called when decoding image failed. Delegate can do some cleanup in |
| 38 // this handler. |
| 39 virtual void OnDecodeImageFailed(const ImageBatchDecoder* decoder) {} |
| 40 |
| 41 protected: |
| 42 virtual ~Delegate() {} |
| 43 }; |
| 44 |
| 45 enum ImageCodec { |
| 46 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
| 47 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
| 48 }; |
| 49 |
| 50 // Constructs an ImageBatchDecoder. Callbacks for decoded images wil be |
| 51 // posted back to |task_runner|. |
| 52 explicit ImageBatchDecoder( |
| 53 scoped_refptr<base::SequencedTaskRunner> task_runner); |
| 54 |
| 55 // Starts asynchronous image decoding. Once finished, the callback will be |
| 56 // posted back to |task_runner|. |
| 57 void Start( |
| 58 Delegate* delegate, |
| 59 const std::string& image_data, |
| 60 ImageCodec image_codec); |
| 61 |
| 62 private: |
| 63 // It's a reference counted object, so destructor is private. |
| 64 ~ImageBatchDecoder() override; |
| 65 |
| 66 // Creates a UtilityProcessHost and starts it in batch mode. |
| 67 void ConstructUtilityProcessHost(); |
| 68 |
| 69 // Overidden from UtilityProcessHostClient: |
| 70 bool OnMessageReceived(const IPC::Message& message) override; |
| 71 |
| 72 // IPC message handlers. |
| 73 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int id); |
| 74 void OnDecodeImageFailed(int id); |
| 75 |
| 76 // Launches sandboxed process that will decode the image. |
| 77 void DecodeImageInSandbox(Delegate* delegate, |
| 78 const std::vector<unsigned char>& image_data, |
| 79 ImageCodec image_codec); |
| 80 |
| 81 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 82 int id_counter_ = 0; |
| 83 std::map<int, Delegate*> delegate_map_; |
| 84 content::UtilityProcessHost* utility_process_host_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(ImageBatchDecoder); |
| 87 }; |
| 88 |
| 89 #endif // CHROME_BROWSER_IMAGE_BATCH_DECODER_H_ |
OLD | NEW |