[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> |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 10 | #include <string> |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 16 | #include "base/sequence_checker.h" |
| 17 | #include "base/sequenced_task_runner.h" |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 18 | #include "base/synchronization/lock.h" |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 19 | #include "base/timer/timer.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 20 | #include "build/build_config.h" |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 21 | #include "chrome/common/image_decoder.mojom.h" |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 22 | #include "content/public/browser/utility_process_host.h" |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 23 | #include "content/public/browser/utility_process_host_client.h" |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 24 | |
| 25 | class SkBitmap; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 26 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 27 | // This is a helper class for decoding images safely in a utility process. To |
| 28 | // use this, call ImageDecoder::Start(...) or |
| 29 | // ImageDecoder::StartWithOptions(...) on any thread. |
| 30 | // |
| 31 | // Internally, most of the work happens on the IO thread, and then |
| 32 | // the callback (ImageRequest::OnImageDecoded or |
| 33 | // ImageRequest::OnDecodeImageFailed) is posted back to the |task_runner_| |
| 34 | // associated with the ImageRequest. |
| 35 | // The Cancel() method runs on whichever thread called it. |map_lock_| is used |
| 36 | // to protect the data that is accessed from multiple threads. |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 37 | class ImageDecoder : public content::UtilityProcessHostClient { |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 38 | public: |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 39 | // ImageRequest objects needs to be created and destroyed on the same |
| 40 | // SequencedTaskRunner. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 41 | class ImageRequest { |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 42 | public: |
| 43 | // Called when image is decoded. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 44 | virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0; |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 45 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 46 | // Called when decoding image failed. ImageRequest can do some cleanup in |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 47 | // this handler. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 48 | virtual void OnDecodeImageFailed() {} |
| 49 | |
| 50 | base::SequencedTaskRunner* task_runner() const { |
| 51 | return task_runner_.get(); |
| 52 | } |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 53 | |
| 54 | protected: |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 55 | // Creates an ImageRequest that runs on the thread creating it. |
| 56 | ImageRequest(); |
| 57 | // Explicitly pass in |task_runner| if the current thread is part of a |
| 58 | // thread pool. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 59 | explicit ImageRequest( |
| 60 | const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 61 | virtual ~ImageRequest(); |
| 62 | |
| 63 | private: |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 64 | // The thread to post OnImageDecoded() or OnDecodeImageFailed() once the |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 65 | // the image has been decoded. |
| 66 | const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 67 | |
| 68 | base::SequenceChecker sequence_checker_; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 69 | }; |
| 70 | |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 71 | enum ImageCodec { |
| 72 | DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
edward.baker | 199f66b | 2015-05-07 19:13:30 | [diff] [blame] | 73 | #if defined(OS_CHROMEOS) |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 74 | ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
satorux | f7e76f0 | 2016-03-09 06:30:45 | [diff] [blame] | 75 | ROBUST_PNG_CODEC, // Restrict decoding to robust PNG codec. |
edward.baker | 199f66b | 2015-05-07 19:13:30 | [diff] [blame] | 76 | #endif // defined(OS_CHROMEOS) |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 77 | }; |
| 78 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 79 | // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 80 | // shrink_to_fit = false. |
| 81 | static void Start(ImageRequest* image_request, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 82 | std::vector<uint8_t> image_data); |
| 83 | // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. |
| 84 | static void Start(ImageRequest* image_request, |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 85 | const std::string& image_data); |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 86 | |
[email protected] | cdc8a3e | 2013-06-12 04:18:17 | [diff] [blame] | 87 | // Starts asynchronous image decoding. Once finished, the callback will be |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 88 | // posted back to image_request's |task_runner_|. |
| 89 | static void StartWithOptions(ImageRequest* image_request, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 90 | std::vector<uint8_t> image_data, |
| 91 | ImageCodec image_codec, |
| 92 | bool shrink_to_fit); |
| 93 | // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. |
| 94 | static void StartWithOptions(ImageRequest* image_request, |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 95 | const std::string& image_data, |
| 96 | ImageCodec image_codec, |
| 97 | bool shrink_to_fit); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 98 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 99 | // Removes all instances of |image_request| from |image_request_id_map_|, |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 100 | // ensuring callbacks are not made to the image_request after it is destroyed. |
| 101 | static void Cancel(ImageRequest* image_request); |
[email protected] | 11b0856d | 2012-06-13 19:21:56 | [diff] [blame] | 102 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 103 | private: |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 104 | friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; |
| 105 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 106 | using RequestMap = std::map<int, ImageRequest*>; |
| 107 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 108 | ImageDecoder(); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 109 | // It's a reference counted object, so destructor is private. |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 110 | ~ImageDecoder() override; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 111 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 112 | // Sends a request to the sandboxed process to decode the image. Starts |
twellington | 612b9b6 | 2015-04-01 00:33:12 | [diff] [blame] | 113 | // batch mode if necessary. If the utility process fails to start, |
| 114 | // an OnDecodeImageFailed task is posted to image_request's |task_runner_|. |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 115 | void DecodeImageInSandbox(int request_id, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 116 | std::vector<uint8_t> image_data, |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 117 | ImageCodec image_codec, |
| 118 | bool shrink_to_fit); |
| 119 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 120 | void StartWithOptionsImpl(ImageRequest* image_request, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 121 | std::vector<uint8_t> image_data, |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 122 | ImageCodec image_codec, |
| 123 | bool shrink_to_fit); |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 124 | void CancelImpl(ImageRequest* image_request); |
| 125 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 126 | // Starts UtilityProcessHost in batch mode and starts |batch_mode_timer_|. |
twellington | 612b9b6 | 2015-04-01 00:33:12 | [diff] [blame] | 127 | // If the utility process fails to start, the method resets |
| 128 | // |utility_process_host_| and returns. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 129 | void StartBatchMode(); |
| 130 | |
| 131 | // Stops batch mode if no requests have come in since |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 132 | // |kBatchModeTimeoutSeconds|. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 133 | void StopBatchMode(); |
| 134 | |
amistry | d59fe6b | 2015-04-30 23:50:30 | [diff] [blame] | 135 | // Fails all outstanding requests. |
| 136 | void FailAllRequests(); |
| 137 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 138 | // Overidden from UtilityProcessHostClient. |
amistry | d59fe6b | 2015-04-30 23:50:30 | [diff] [blame] | 139 | void OnProcessCrashed(int exit_code) override; |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 140 | void OnProcessLaunchFailed(int error_code) override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 141 | bool OnMessageReceived(const IPC::Message& message) override; |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 142 | |
| 143 | // IPC message handlers. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 144 | void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); |
| 145 | void OnDecodeImageFailed(int request_id); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 146 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 147 | // For the ImageRequest identified by |request_id|, call its OnImageDecoded() |
| 148 | // or OnDecodeImageFailed() method on its task runner thread. |
| 149 | void RunOnImageDecoded(const SkBitmap& decoded_image, int request_id); |
| 150 | void RunOnDecodeImageFailed(int request_id); |
| 151 | |
| 152 | // id to use for the next Start() request that comes in. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 153 | int image_request_id_counter_; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 154 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 155 | // Map of request id's to ImageRequests. |
| 156 | RequestMap image_request_id_map_; |
| 157 | |
| 158 | // Protects |image_request_id_map_| and |image_request_id_counter_|. |
| 159 | base::Lock map_lock_; |
| 160 | |
| 161 | // The UtilityProcessHost requests are sent to. |
| 162 | base::WeakPtr<content::UtilityProcessHost> utility_process_host_; |
| 163 | |
thestig | d3485b69 | 2015-04-30 09:47:32 | [diff] [blame] | 164 | // Calls StopBatchMode() after |kBatchModeTimeoutSeconds| have elapsed, |
| 165 | // unless a new decoding request resets the timer. |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 166 | std::unique_ptr<base::DelayTimer> batch_mode_timer_; |
| 167 | |
| 168 | // Mojo service connection. Must always be bound/reset and used on the IO |
| 169 | // thread. |
| 170 | mojom::ImageDecoderPtr decoder_; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 171 | |
| 172 | DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
| 173 | }; |
| 174 | |
[email protected] | 6d33da17 | 2011-11-22 03:56:09 | [diff] [blame] | 175 | #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |