[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 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 8 | #include <map> |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 9 | #include <string> |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 14 | #include "base/sequence_checker.h" |
| 15 | #include "base/sequenced_task_runner.h" |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 17 | |
| 18 | class SkBitmap; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 19 | |
rockot | 2a0f4fb3 | 2016-11-11 18:44:19 | [diff] [blame^] | 20 | // This is a helper class for decoding images safely in a sandboxed service. To |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 21 | // use this, call ImageDecoder::Start(...) or |
| 22 | // ImageDecoder::StartWithOptions(...) on any thread. |
| 23 | // |
rockot | 2a0f4fb3 | 2016-11-11 18:44:19 | [diff] [blame^] | 24 | // ImageRequest::OnImageDecoded or ImageRequest::OnDecodeImageFailed is posted |
| 25 | // back to the |task_runner_| associated with the ImageRequest. |
| 26 | // |
| 27 | // The Cancel() method runs on whichever thread called it. |
| 28 | // |
| 29 | // TODO(rockot): Use of this class should be replaced with direct image_decoder |
| 30 | // client library usage. |
| 31 | class ImageDecoder { |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 32 | public: |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 33 | // ImageRequest objects needs to be created and destroyed on the same |
| 34 | // SequencedTaskRunner. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 35 | class ImageRequest { |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 36 | public: |
| 37 | // Called when image is decoded. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 38 | virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0; |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 39 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 40 | // Called when decoding image failed. ImageRequest can do some cleanup in |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 41 | // this handler. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 42 | virtual void OnDecodeImageFailed() {} |
| 43 | |
| 44 | base::SequencedTaskRunner* task_runner() const { |
| 45 | return task_runner_.get(); |
| 46 | } |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 47 | |
| 48 | protected: |
rockot | 2a0f4fb3 | 2016-11-11 18:44:19 | [diff] [blame^] | 49 | // Creates an ImageRequest that runs on the thread which created it. |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 50 | ImageRequest(); |
rockot | 2a0f4fb3 | 2016-11-11 18:44:19 | [diff] [blame^] | 51 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 52 | // Explicitly pass in |task_runner| if the current thread is part of a |
| 53 | // thread pool. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 54 | explicit ImageRequest( |
| 55 | const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 56 | virtual ~ImageRequest(); |
| 57 | |
| 58 | private: |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 59 | // The thread to post OnImageDecoded() or OnDecodeImageFailed() once the |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 60 | // the image has been decoded. |
| 61 | const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 62 | |
| 63 | base::SequenceChecker sequence_checker_; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 64 | }; |
| 65 | |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 66 | enum ImageCodec { |
| 67 | DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
edward.baker | 199f66b | 2015-05-07 19:13:30 | [diff] [blame] | 68 | #if defined(OS_CHROMEOS) |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 69 | ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
satorux | f7e76f0 | 2016-03-09 06:30:45 | [diff] [blame] | 70 | ROBUST_PNG_CODEC, // Restrict decoding to robust PNG codec. |
edward.baker | 199f66b | 2015-05-07 19:13:30 | [diff] [blame] | 71 | #endif // defined(OS_CHROMEOS) |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 72 | }; |
| 73 | |
rockot | 2a0f4fb3 | 2016-11-11 18:44:19 | [diff] [blame^] | 74 | ImageDecoder(); |
| 75 | ~ImageDecoder(); |
| 76 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 77 | // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 78 | // shrink_to_fit = false. |
| 79 | static void Start(ImageRequest* image_request, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame] | 80 | std::vector<uint8_t> image_data); |
| 81 | // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. |
| 82 | static void Start(ImageRequest* image_request, |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 83 | const std::string& image_data); |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 84 | |
[email protected] | cdc8a3e | 2013-06-12 04:18:17 | [diff] [blame] | 85 | // Starts asynchronous image decoding. Once finished, the callback will be |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 86 | // posted back to image_request's |task_runner_|. |
| 87 | static void StartWithOptions(ImageRequest* image_request, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame] | 88 | std::vector<uint8_t> image_data, |
| 89 | ImageCodec image_codec, |
| 90 | bool shrink_to_fit); |
| 91 | // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. |
| 92 | static void StartWithOptions(ImageRequest* image_request, |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 93 | const std::string& image_data, |
| 94 | ImageCodec image_codec, |
| 95 | bool shrink_to_fit); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 96 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 97 | // Removes all instances of |image_request| from |image_request_id_map_|, |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 98 | // ensuring callbacks are not made to the image_request after it is destroyed. |
| 99 | static void Cancel(ImageRequest* image_request); |
[email protected] | 11b0856d | 2012-06-13 19:21:56 | [diff] [blame] | 100 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 101 | private: |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 102 | using RequestMap = std::map<int, ImageRequest*>; |
| 103 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 104 | void StartWithOptionsImpl(ImageRequest* image_request, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame] | 105 | std::vector<uint8_t> image_data, |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 106 | ImageCodec image_codec, |
| 107 | bool shrink_to_fit); |
rockot | 2a0f4fb3 | 2016-11-11 18:44:19 | [diff] [blame^] | 108 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 109 | void CancelImpl(ImageRequest* image_request); |
| 110 | |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 111 | // IPC message handlers. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 112 | void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); |
| 113 | void OnDecodeImageFailed(int request_id); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 114 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 115 | // id to use for the next Start() request that comes in. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 116 | int image_request_id_counter_; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 117 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 118 | // Map of request id's to ImageRequests. |
| 119 | RequestMap image_request_id_map_; |
| 120 | |
| 121 | // Protects |image_request_id_map_| and |image_request_id_counter_|. |
| 122 | base::Lock map_lock_; |
| 123 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 124 | DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
| 125 | }; |
| 126 | |
[email protected] | 6d33da17 | 2011-11-22 03:56:09 | [diff] [blame] | 127 | #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |