[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 | |
[email protected] | dfd5064 | 2011-11-28 20:00:44 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
[email protected] | 810dddf5 | 2013-01-25 19:37:26 | [diff] [blame] | 16 | #include "base/threading/sequenced_worker_pool.h" |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 17 | #include "base/timer/timer.h" |
| 18 | #include "content/public/browser/utility_process_host.h" |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 19 | #include "content/public/browser/utility_process_host_client.h" |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 20 | |
| 21 | class SkBitmap; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 22 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 23 | // This is a helper class for decoding images safely in a utility process. To |
| 24 | // use this, call ImageDecoder::Start(...) or |
| 25 | // ImageDecoder::StartWithOptions(...) on any thread. |
| 26 | // |
| 27 | // Internally, most of the work happens on the IO thread, and then |
| 28 | // the callback (ImageRequest::OnImageDecoded or |
| 29 | // ImageRequest::OnDecodeImageFailed) is posted back to the |task_runner_| |
| 30 | // associated with the ImageRequest. |
| 31 | // The Cancel() method runs on whichever thread called it. |map_lock_| is used |
| 32 | // to protect the data that is accessed from multiple threads. |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 33 | class ImageDecoder : public content::UtilityProcessHostClient { |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 34 | public: |
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: |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 49 | explicit ImageRequest( |
| 50 | const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 51 | virtual ~ImageRequest(); |
| 52 | |
| 53 | private: |
| 54 | // The thread to post OnImageDecoded or OnDecodeImageFailed once the |
| 55 | // the image has been decoded. |
| 56 | const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 57 | }; |
| 58 | |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 59 | enum ImageCodec { |
| 60 | DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
| 61 | ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
| 62 | }; |
| 63 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 64 | // Calls StartWithOptions with ImageCodec::DEFAULT_CODEC and |
| 65 | // shrink_to_fit = false. |
| 66 | static void Start(ImageRequest* image_request, |
| 67 | const std::string& image_data); |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 68 | |
[email protected] | cdc8a3e | 2013-06-12 04:18:17 | [diff] [blame] | 69 | // Starts asynchronous image decoding. Once finished, the callback will be |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 70 | // posted back to image_request's |task_runner_|. |
| 71 | static void StartWithOptions(ImageRequest* image_request, |
| 72 | const std::string& image_data, |
| 73 | ImageCodec image_codec, |
| 74 | bool shrink_to_fit); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 75 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 76 | // Removes all instances of image_request from |image_request_id_map_|, |
| 77 | // ensuring callbacks are not made to the image_request after it is destroyed. |
| 78 | static void Cancel(ImageRequest* image_request); |
[email protected] | 11b0856d | 2012-06-13 19:21:56 | [diff] [blame] | 79 | |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 80 | private: |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 81 | friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; |
| 82 | |
| 83 | ImageDecoder(); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 84 | // It's a reference counted object, so destructor is private. |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 85 | ~ImageDecoder() override; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 86 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 87 | // Sends a request to the sandboxed process to decode the image. Starts |
twellington | 612b9b6 | 2015-04-01 00:33:12 | [diff] [blame^] | 88 | // batch mode if necessary. If the utility process fails to start, |
| 89 | // an OnDecodeImageFailed task is posted to image_request's |task_runner_|. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 90 | void DecodeImageInSandbox(ImageRequest* image_request, |
| 91 | const std::vector<unsigned char>& image_data, |
| 92 | ImageCodec image_codec, |
| 93 | bool shrink_to_fit); |
| 94 | |
| 95 | void CancelImpl(ImageRequest* image_request); |
| 96 | |
| 97 | using RequestMap = std::map<int, ImageRequest*>; |
| 98 | |
| 99 | // Starts UtilityProcessHost in batch mode and starts |batch_mode_timer_|. |
twellington | 612b9b6 | 2015-04-01 00:33:12 | [diff] [blame^] | 100 | // If the utility process fails to start, the method resets |
| 101 | // |utility_process_host_| and returns. |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 102 | void StartBatchMode(); |
| 103 | |
| 104 | // Stops batch mode if no requests have come in since |
| 105 | // kBatchModeTimeoutSeconds. |
| 106 | void StopBatchMode(); |
| 107 | |
| 108 | // Overidden from UtilityProcessHostClient. |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 109 | bool OnMessageReceived(const IPC::Message& message) override; |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 110 | |
| 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 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 115 | // id to use for the next Start request that comes in. |
| 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 | |
| 124 | // The UtilityProcessHost requests are sent to. |
| 125 | base::WeakPtr<content::UtilityProcessHost> utility_process_host_; |
| 126 | |
| 127 | // Calls StopBatchMode after kBatchModeTimeoutSeconds have elapsed. |
| 128 | base::RepeatingTimer<ImageDecoder> batch_mode_timer_; |
| 129 | |
| 130 | // The time Start was last called. |
| 131 | base::TimeTicks last_request_; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 132 | |
| 133 | DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
| 134 | }; |
| 135 | |
[email protected] | 6d33da17 | 2011-11-22 03:56:09 | [diff] [blame] | 136 | #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |