OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_IMAGE_DECODER_H_ | 5 #ifndef CHROME_BROWSER_IMAGE_DECODER_H_ |
6 #define CHROME_BROWSER_IMAGE_DECODER_H_ | 6 #define CHROME_BROWSER_IMAGE_DECODER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | |
10 #include <string> | 9 #include <string> |
11 #include <vector> | 10 #include <vector> |
12 | 11 |
13 #include "base/lazy_instance.h" | |
14 #include "base/macros.h" | 12 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
16 #include "base/sequence_checker.h" | 14 #include "base/sequence_checker.h" |
17 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
18 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
19 #include "base/timer/timer.h" | |
20 #include "build/build_config.h" | |
21 #include "chrome/common/image_decoder.mojom.h" | |
22 #include "content/public/browser/utility_process_host.h" | |
23 #include "content/public/browser/utility_process_host_client.h" | |
24 | 17 |
25 class SkBitmap; | 18 class SkBitmap; |
26 | 19 |
27 // This is a helper class for decoding images safely in a utility process. To | 20 // This is a helper class for decoding images safely in a sandboxed service. To |
28 // use this, call ImageDecoder::Start(...) or | 21 // use this, call ImageDecoder::Start(...) or |
29 // ImageDecoder::StartWithOptions(...) on any thread. | 22 // ImageDecoder::StartWithOptions(...) on any thread. |
30 // | 23 // |
31 // Internally, most of the work happens on the IO thread, and then | 24 // ImageRequest::OnImageDecoded or ImageRequest::OnDecodeImageFailed is posted |
32 // the callback (ImageRequest::OnImageDecoded or | 25 // back to the |task_runner_| associated with the ImageRequest. |
33 // ImageRequest::OnDecodeImageFailed) is posted back to the |task_runner_| | 26 // |
34 // associated with the ImageRequest. | 27 // The Cancel() method runs on whichever thread called it. |
35 // The Cancel() method runs on whichever thread called it. |map_lock_| is used | 28 // |
36 // to protect the data that is accessed from multiple threads. | 29 // TODO(rockot): Use of this class should be replaced with direct image_decoder |
37 class ImageDecoder : public content::UtilityProcessHostClient { | 30 // client library usage. |
| 31 class ImageDecoder { |
38 public: | 32 public: |
39 // ImageRequest objects needs to be created and destroyed on the same | 33 // ImageRequest objects needs to be created and destroyed on the same |
40 // SequencedTaskRunner. | 34 // SequencedTaskRunner. |
41 class ImageRequest { | 35 class ImageRequest { |
42 public: | 36 public: |
43 // Called when image is decoded. | 37 // Called when image is decoded. |
44 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0; | 38 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0; |
45 | 39 |
46 // Called when decoding image failed. ImageRequest can do some cleanup in | 40 // Called when decoding image failed. ImageRequest can do some cleanup in |
47 // this handler. | 41 // this handler. |
48 virtual void OnDecodeImageFailed() {} | 42 virtual void OnDecodeImageFailed() {} |
49 | 43 |
50 base::SequencedTaskRunner* task_runner() const { | 44 base::SequencedTaskRunner* task_runner() const { |
51 return task_runner_.get(); | 45 return task_runner_.get(); |
52 } | 46 } |
53 | 47 |
54 protected: | 48 protected: |
55 // Creates an ImageRequest that runs on the thread creating it. | 49 // Creates an ImageRequest that runs on the thread which created it. |
56 ImageRequest(); | 50 ImageRequest(); |
| 51 |
57 // Explicitly pass in |task_runner| if the current thread is part of a | 52 // Explicitly pass in |task_runner| if the current thread is part of a |
58 // thread pool. | 53 // thread pool. |
59 explicit ImageRequest( | 54 explicit ImageRequest( |
60 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 55 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
61 virtual ~ImageRequest(); | 56 virtual ~ImageRequest(); |
62 | 57 |
63 private: | 58 private: |
64 // The thread to post OnImageDecoded() or OnDecodeImageFailed() once the | 59 // The thread to post OnImageDecoded() or OnDecodeImageFailed() once the |
65 // the image has been decoded. | 60 // the image has been decoded. |
66 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 61 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
67 | 62 |
68 base::SequenceChecker sequence_checker_; | 63 base::SequenceChecker sequence_checker_; |
69 }; | 64 }; |
70 | 65 |
71 enum ImageCodec { | 66 enum ImageCodec { |
72 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). | 67 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
73 #if defined(OS_CHROMEOS) | 68 #if defined(OS_CHROMEOS) |
74 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. | 69 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
75 ROBUST_PNG_CODEC, // Restrict decoding to robust PNG codec. | 70 ROBUST_PNG_CODEC, // Restrict decoding to robust PNG codec. |
76 #endif // defined(OS_CHROMEOS) | 71 #endif // defined(OS_CHROMEOS) |
77 }; | 72 }; |
78 | 73 |
| 74 ImageDecoder(); |
| 75 ~ImageDecoder(); |
| 76 |
79 // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and | 77 // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and |
80 // shrink_to_fit = false. | 78 // shrink_to_fit = false. |
81 static void Start(ImageRequest* image_request, | 79 static void Start(ImageRequest* image_request, |
82 std::vector<uint8_t> image_data); | 80 std::vector<uint8_t> image_data); |
83 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. | 81 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. |
84 static void Start(ImageRequest* image_request, | 82 static void Start(ImageRequest* image_request, |
85 const std::string& image_data); | 83 const std::string& image_data); |
86 | 84 |
87 // Starts asynchronous image decoding. Once finished, the callback will be | 85 // Starts asynchronous image decoding. Once finished, the callback will be |
88 // posted back to image_request's |task_runner_|. | 86 // posted back to image_request's |task_runner_|. |
89 static void StartWithOptions(ImageRequest* image_request, | 87 static void StartWithOptions(ImageRequest* image_request, |
90 std::vector<uint8_t> image_data, | 88 std::vector<uint8_t> image_data, |
91 ImageCodec image_codec, | 89 ImageCodec image_codec, |
92 bool shrink_to_fit); | 90 bool shrink_to_fit); |
93 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. | 91 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. |
94 static void StartWithOptions(ImageRequest* image_request, | 92 static void StartWithOptions(ImageRequest* image_request, |
95 const std::string& image_data, | 93 const std::string& image_data, |
96 ImageCodec image_codec, | 94 ImageCodec image_codec, |
97 bool shrink_to_fit); | 95 bool shrink_to_fit); |
98 | 96 |
99 // Removes all instances of |image_request| from |image_request_id_map_|, | 97 // Removes all instances of |image_request| from |image_request_id_map_|, |
100 // ensuring callbacks are not made to the image_request after it is destroyed. | 98 // ensuring callbacks are not made to the image_request after it is destroyed. |
101 static void Cancel(ImageRequest* image_request); | 99 static void Cancel(ImageRequest* image_request); |
102 | 100 |
103 private: | 101 private: |
104 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; | |
105 | |
106 using RequestMap = std::map<int, ImageRequest*>; | 102 using RequestMap = std::map<int, ImageRequest*>; |
107 | 103 |
108 ImageDecoder(); | |
109 // It's a reference counted object, so destructor is private. | |
110 ~ImageDecoder() override; | |
111 | |
112 // Sends a request to the sandboxed process to decode the image. Starts | |
113 // batch mode if necessary. If the utility process fails to start, | |
114 // an OnDecodeImageFailed task is posted to image_request's |task_runner_|. | |
115 void DecodeImageInSandbox(int request_id, | |
116 std::vector<uint8_t> image_data, | |
117 ImageCodec image_codec, | |
118 bool shrink_to_fit); | |
119 | |
120 void StartWithOptionsImpl(ImageRequest* image_request, | 104 void StartWithOptionsImpl(ImageRequest* image_request, |
121 std::vector<uint8_t> image_data, | 105 std::vector<uint8_t> image_data, |
122 ImageCodec image_codec, | 106 ImageCodec image_codec, |
123 bool shrink_to_fit); | 107 bool shrink_to_fit); |
| 108 |
124 void CancelImpl(ImageRequest* image_request); | 109 void CancelImpl(ImageRequest* image_request); |
125 | 110 |
126 // Starts UtilityProcessHost in batch mode and starts |batch_mode_timer_|. | |
127 // If the utility process fails to start, the method resets | |
128 // |utility_process_host_| and returns. | |
129 void StartBatchMode(); | |
130 | |
131 // Stops batch mode if no requests have come in since | |
132 // |kBatchModeTimeoutSeconds|. | |
133 void StopBatchMode(); | |
134 | |
135 // Fails all outstanding requests. | |
136 void FailAllRequests(); | |
137 | |
138 // Overidden from UtilityProcessHostClient. | |
139 void OnProcessCrashed(int exit_code) override; | |
140 void OnProcessLaunchFailed(int error_code) override; | |
141 bool OnMessageReceived(const IPC::Message& message) override; | |
142 | |
143 // IPC message handlers. | 111 // IPC message handlers. |
144 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); | 112 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); |
145 void OnDecodeImageFailed(int request_id); | 113 void OnDecodeImageFailed(int request_id); |
146 | 114 |
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. | 115 // id to use for the next Start() request that comes in. |
153 int image_request_id_counter_; | 116 int image_request_id_counter_; |
154 | 117 |
155 // Map of request id's to ImageRequests. | 118 // Map of request id's to ImageRequests. |
156 RequestMap image_request_id_map_; | 119 RequestMap image_request_id_map_; |
157 | 120 |
158 // Protects |image_request_id_map_| and |image_request_id_counter_|. | 121 // Protects |image_request_id_map_| and |image_request_id_counter_|. |
159 base::Lock map_lock_; | 122 base::Lock map_lock_; |
160 | 123 |
161 // The UtilityProcessHost requests are sent to. | |
162 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | |
163 | |
164 // Calls StopBatchMode() after |kBatchModeTimeoutSeconds| have elapsed, | |
165 // unless a new decoding request resets the timer. | |
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_; | |
171 | |
172 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | 124 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
173 }; | 125 }; |
174 | 126 |
175 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | 127 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |
OLD | NEW |