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