[email protected] | 44013f68 | 2012-05-31 13:49:40 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 6d33da17 | 2011-11-22 03:56:09 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_IMAGE_DECODER_H_ |
6 | #define CHROME_BROWSER_IMAGE_DECODER_H_ | ||||
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 7 | |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 8 | #include <string> |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 9 | #include <vector> |
10 | |||||
[email protected] | dfd5064 | 2011-11-28 20:00:44 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
13 | #include "base/threading/sequenced_worker_pool.h" | ||||
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 14 | #include "content/public/browser/utility_process_host_client.h" |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 15 | |
16 | class SkBitmap; | ||||
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 17 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 18 | // Decodes an image in a sandboxed process. |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 19 | class ImageDecoder : public content::UtilityProcessHostClient { |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 20 | public: |
21 | class Delegate { | ||||
22 | public: | ||||
23 | // Called when image is decoded. | ||||
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 24 | // |decoder| is used to identify the image in case of decoding several |
25 | // images simultaneously. | ||||
26 | virtual void OnImageDecoded(const ImageDecoder* decoder, | ||||
27 | const SkBitmap& decoded_image) = 0; | ||||
28 | |||||
29 | // Called when decoding image failed. Delegate can do some cleanup in | ||||
30 | // this handler. | ||||
31 | virtual void OnDecodeImageFailed(const ImageDecoder* decoder) {} | ||||
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 32 | |
33 | protected: | ||||
34 | virtual ~Delegate() {} | ||||
35 | }; | ||||
36 | |||||
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 37 | enum ImageCodec { |
38 | DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). | ||||
39 | ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. | ||||
40 | }; | ||||
41 | |||||
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 42 | ImageDecoder(Delegate* delegate, |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 43 | const std::string& image_data, |
44 | ImageCodec image_codec); | ||||
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 45 | |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame^] | 46 | ImageDecoder(Delegate* delegate, |
47 | const std::vector<char>& image_data, | ||||
48 | ImageCodec image_codec); | ||||
49 | |||||
[email protected] | cdc8a3e | 2013-06-12 04:18:17 | [diff] [blame] | 50 | // Starts asynchronous image decoding. Once finished, the callback will be |
51 | // posted back to |task_runner|. | ||||
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 52 | void Start(scoped_refptr<base::SequencedTaskRunner> task_runner); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 53 | |
[email protected] | bebc8e12 | 2012-06-12 19:52:34 | [diff] [blame] | 54 | const std::vector<unsigned char>& get_image_data() const { |
55 | return image_data_; | ||||
56 | } | ||||
57 | |||||
[email protected] | 11b0856d | 2012-06-13 19:21:56 | [diff] [blame] | 58 | void set_delegate(Delegate* delegate) { delegate_ = delegate; } |
glevin | b18f4aa | 2014-09-26 15:02:33 | [diff] [blame] | 59 | void set_shrink_to_fit(bool shrink_to_fit) { shrink_to_fit_ = shrink_to_fit; } |
[email protected] | 11b0856d | 2012-06-13 19:21:56 | [diff] [blame] | 60 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 61 | private: |
62 | // It's a reference counted object, so destructor is private. | ||||
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 63 | ~ImageDecoder() override; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 64 | |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 65 | // Overidden from UtilityProcessHostClient: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 66 | bool OnMessageReceived(const IPC::Message& message) override; |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 67 | |
68 | // IPC message handlers. | ||||
69 | void OnDecodeImageSucceeded(const SkBitmap& decoded_image); | ||||
70 | void OnDecodeImageFailed(); | ||||
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 71 | |
72 | // Launches sandboxed process that will decode the image. | ||||
[email protected] | 4d9dcff | 2011-04-06 22:49:08 | [diff] [blame] | 73 | void DecodeImageInSandbox(const std::vector<unsigned char>& image_data); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 74 | |
75 | Delegate* delegate_; | ||||
76 | std::vector<unsigned char> image_data_; | ||||
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 77 | const ImageCodec image_codec_; |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 78 | scoped_refptr<base::SequencedTaskRunner> task_runner_; |
glevin | b18f4aa | 2014-09-26 15:02:33 | [diff] [blame] | 79 | bool shrink_to_fit_; // if needed for IPC msg size limit |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 80 | |
81 | DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | ||||
82 | }; | ||||
83 | |||||
[email protected] | 6d33da17 | 2011-11-22 03:56:09 | [diff] [blame] | 84 | #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |