[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 | #include "chrome/browser/image_decoder.h" |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 6 | |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 7 | #include <utility> |
| 8 | |
[email protected] | 3189377 | 2011-10-11 17:41:50 | [diff] [blame] | 9 | #include "base/bind.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 10 | #include "base/threading/thread_task_runner_handle.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 12 | #include "chrome/browser/browser_process.h" |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 13 | #include "chrome/common/image_decoder.mojom.h" |
thestig | e3d6b75 | 2015-04-07 21:58:45 | [diff] [blame] | 14 | #include "chrome/grit/generated_resources.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 15 | #include "content/public/browser/browser_thread.h" |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 16 | #include "content/public/browser/utility_process_host.h" |
ben | d1dd50f5 | 2016-06-26 22:10:48 | [diff] [blame] | 17 | #include "services/shell/public/cpp/interface_provider.h" |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 18 | #include "third_party/skia/include/core/SkBitmap.h" |
thestig | e3d6b75 | 2015-04-07 21:58:45 | [diff] [blame] | 19 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 20 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 21 | using content::BrowserThread; |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 22 | using content::UtilityProcessHost; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 23 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | // static, Leaky to allow access from any thread. |
| 27 | base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; |
| 28 | |
| 29 | // How long to wait after the last request has been received before ending |
| 30 | // batch mode. |
| 31 | const int kBatchModeTimeoutSeconds = 5; |
| 32 | |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 33 | void OnDecodeImageDone( |
| 34 | base::Callback<void(int)> fail_callback, |
| 35 | base::Callback<void(const SkBitmap&, int)> success_callback, |
msw | b0123d1 | 2016-06-01 21:59:54 | [diff] [blame] | 36 | int request_id, |
| 37 | const SkBitmap& image) { |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 38 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
msw | b0123d1 | 2016-06-01 21:59:54 | [diff] [blame] | 39 | if (!image.isNull() && !image.empty()) |
| 40 | success_callback.Run(image, request_id); |
| 41 | else |
| 42 | fail_callback.Run(request_id); |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 43 | } |
| 44 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 45 | } // namespace |
| 46 | |
| 47 | ImageDecoder::ImageDecoder() |
thestig | d3485b69 | 2015-04-30 09:47:32 | [diff] [blame] | 48 | : image_request_id_counter_(0) { |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 49 | // A single ImageDecoder instance should live for the life of the program. |
| 50 | // Explicitly add a reference so the object isn't deleted. |
| 51 | AddRef(); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 52 | } |
| 53 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 54 | ImageDecoder::~ImageDecoder() { |
pneubeck | 9387125 | 2015-01-20 11:26:36 | [diff] [blame] | 55 | } |
| 56 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 57 | ImageDecoder::ImageRequest::ImageRequest() |
| 58 | : task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 59 | DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 60 | } |
| 61 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 62 | ImageDecoder::ImageRequest::ImageRequest( |
| 63 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 64 | : task_runner_(task_runner) { |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 65 | DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 66 | } |
[email protected] | 863e6d96 | 2011-05-15 19:39:35 | [diff] [blame] | 67 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 68 | ImageDecoder::ImageRequest::~ImageRequest() { |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 69 | DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 70 | ImageDecoder::Cancel(this); |
| 71 | } |
| 72 | |
| 73 | // static |
| 74 | void ImageDecoder::Start(ImageRequest* image_request, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 75 | std::vector<uint8_t> image_data) { |
| 76 | StartWithOptions(image_request, std::move(image_data), DEFAULT_CODEC, false); |
| 77 | } |
| 78 | |
| 79 | // static |
| 80 | void ImageDecoder::Start(ImageRequest* image_request, |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 81 | const std::string& image_data) { |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 82 | Start(image_request, |
| 83 | std::vector<uint8_t>(image_data.begin(), image_data.end())); |
| 84 | } |
| 85 | |
| 86 | // static |
| 87 | void ImageDecoder::StartWithOptions(ImageRequest* image_request, |
| 88 | std::vector<uint8_t> image_data, |
| 89 | ImageCodec image_codec, |
| 90 | bool shrink_to_fit) { |
| 91 | g_decoder.Pointer()->StartWithOptionsImpl(image_request, |
| 92 | std::move(image_data), |
| 93 | image_codec, shrink_to_fit); |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // static |
| 97 | void ImageDecoder::StartWithOptions(ImageRequest* image_request, |
| 98 | const std::string& image_data, |
| 99 | ImageCodec image_codec, |
| 100 | bool shrink_to_fit) { |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 101 | StartWithOptions(image_request, |
| 102 | std::vector<uint8_t>(image_data.begin(), image_data.end()), |
| 103 | image_codec, shrink_to_fit); |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void ImageDecoder::StartWithOptionsImpl(ImageRequest* image_request, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 107 | std::vector<uint8_t> image_data, |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 108 | ImageCodec image_codec, |
| 109 | bool shrink_to_fit) { |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 110 | DCHECK(image_request); |
| 111 | DCHECK(image_request->task_runner()); |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 112 | |
| 113 | int request_id; |
| 114 | { |
| 115 | base::AutoLock lock(map_lock_); |
| 116 | request_id = image_request_id_counter_++; |
| 117 | image_request_id_map_.insert(std::make_pair(request_id, image_request)); |
| 118 | } |
| 119 | |
[email protected] | cca169b5 | 2010-10-08 22:15:55 | [diff] [blame] | 120 | BrowserThread::PostTask( |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 121 | BrowserThread::IO, FROM_HERE, |
| 122 | base::Bind( |
| 123 | &ImageDecoder::DecodeImageInSandbox, |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 124 | g_decoder.Pointer(), request_id, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 125 | base::Passed(std::move(image_data)), |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 126 | image_codec, shrink_to_fit)); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 127 | } |
| 128 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 129 | // static |
| 130 | void ImageDecoder::Cancel(ImageRequest* image_request) { |
| 131 | DCHECK(image_request); |
| 132 | g_decoder.Pointer()->CancelImpl(image_request); |
| 133 | } |
| 134 | |
| 135 | void ImageDecoder::DecodeImageInSandbox( |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 136 | int request_id, |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 137 | std::vector<uint8_t> image_data, |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 138 | ImageCodec image_codec, |
| 139 | bool shrink_to_fit) { |
| 140 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 141 | base::AutoLock lock(map_lock_); |
| 142 | const auto it = image_request_id_map_.find(request_id); |
| 143 | if (it == image_request_id_map_.end()) |
| 144 | return; |
| 145 | |
| 146 | ImageRequest* image_request = it->second; |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 147 | if (!utility_process_host_) { |
| 148 | StartBatchMode(); |
| 149 | } |
twellington | 612b9b6 | 2015-04-01 00:33:12 | [diff] [blame] | 150 | if (!utility_process_host_) { |
| 151 | // Utility process failed to start; notify delegate and return. |
| 152 | // Without this check, we were seeing crashes on startup. Further |
| 153 | // investigation is needed to determine why the utility process |
| 154 | // is failing to start. See crbug.com/472272 |
| 155 | image_request->task_runner()->PostTask( |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 156 | FROM_HERE, |
| 157 | base::Bind(&ImageDecoder::RunOnDecodeImageFailed, this, request_id)); |
twellington | 612b9b6 | 2015-04-01 00:33:12 | [diff] [blame] | 158 | return; |
| 159 | } |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 160 | |
thestig | d3485b69 | 2015-04-30 09:47:32 | [diff] [blame] | 161 | if (!batch_mode_timer_) { |
| 162 | // Created here so it will call StopBatchMode() on the right thread. |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 163 | batch_mode_timer_.reset(new base::DelayTimer( |
| 164 | FROM_HERE, base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), this, |
thestig | d3485b69 | 2015-04-30 09:47:32 | [diff] [blame] | 165 | &ImageDecoder::StopBatchMode)); |
| 166 | } |
| 167 | batch_mode_timer_->Reset(); |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 168 | |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 169 | mojom::ImageCodec mojo_codec = mojom::ImageCodec::DEFAULT; |
edward.baker | 199f66b | 2015-05-07 19:13:30 | [diff] [blame] | 170 | #if defined(OS_CHROMEOS) |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 171 | if (image_codec == ROBUST_JPEG_CODEC) |
| 172 | mojo_codec = mojom::ImageCodec::ROBUST_JPEG; |
| 173 | if (image_codec == ROBUST_PNG_CODEC) |
| 174 | mojo_codec = mojom::ImageCodec::ROBUST_PNG; |
edward.baker | 199f66b | 2015-05-07 19:13:30 | [diff] [blame] | 175 | #endif // defined(OS_CHROMEOS) |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 176 | decoder_->DecodeImage( |
nya | 601d970 | 2016-07-26 18:39:21 | [diff] [blame^] | 177 | mojo::Array<uint8_t>(std::move(image_data)), |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 178 | mojo_codec, |
| 179 | shrink_to_fit, |
| 180 | base::Bind(&OnDecodeImageDone, |
| 181 | base::Bind(&ImageDecoder::OnDecodeImageFailed, this), |
| 182 | base::Bind(&ImageDecoder::OnDecodeImageSucceeded, this), |
| 183 | request_id)); |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void ImageDecoder::CancelImpl(ImageRequest* image_request) { |
| 187 | base::AutoLock lock(map_lock_); |
| 188 | for (auto it = image_request_id_map_.begin(); |
| 189 | it != image_request_id_map_.end();) { |
| 190 | if (it->second == image_request) { |
| 191 | image_request_id_map_.erase(it++); |
| 192 | } else { |
| 193 | ++it; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void ImageDecoder::StartBatchMode() { |
| 199 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 200 | utility_process_host_ = |
pranay.kumar | 6a17925 | 2015-05-05 11:35:15 | [diff] [blame] | 201 | UtilityProcessHost::Create( |
| 202 | this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr(); |
thestig | e3d6b75 | 2015-04-07 21:58:45 | [diff] [blame] | 203 | utility_process_host_->SetName(l10n_util::GetStringUTF16( |
| 204 | IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME)); |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 205 | if (!utility_process_host_->Start()) { |
| 206 | delete utility_process_host_.get(); |
| 207 | return; |
twellington | 612b9b6 | 2015-04-01 00:33:12 | [diff] [blame] | 208 | } |
ben | d1dd50f5 | 2016-06-26 22:10:48 | [diff] [blame] | 209 | utility_process_host_->GetRemoteInterfaces()->GetInterface(&decoder_); |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void ImageDecoder::StopBatchMode() { |
| 213 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
thestig | 3d8ebe3a | 2015-05-08 03:25:32 | [diff] [blame] | 214 | { |
| 215 | // Check for outstanding requests and wait for them to finish. |
| 216 | base::AutoLock lock(map_lock_); |
| 217 | if (!image_request_id_map_.empty()) { |
| 218 | batch_mode_timer_->Reset(); |
| 219 | return; |
| 220 | } |
| 221 | } |
| 222 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 223 | if (utility_process_host_) { |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 224 | // With Mojo, the utility process needs to be explicitly shut down by |
| 225 | // deleting the host. |
| 226 | delete utility_process_host_.get(); |
| 227 | decoder_.reset(); |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 228 | utility_process_host_.reset(); |
| 229 | } |
amistry | d59fe6b | 2015-04-30 23:50:30 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void ImageDecoder::FailAllRequests() { |
| 233 | RequestMap requests; |
| 234 | { |
| 235 | base::AutoLock lock(map_lock_); |
| 236 | requests = image_request_id_map_; |
| 237 | } |
| 238 | |
| 239 | // Since |OnProcessCrashed| and |OnProcessLaunchFailed| are run asynchronously |
| 240 | // from the actual event, it's possible for a new utility process to have been |
| 241 | // created and sent requests by the time these functions are run. This results |
| 242 | // in failing requests that are unaffected by the crash. Although not ideal, |
| 243 | // this is valid and simpler than tracking which request is sent to which |
| 244 | // utility process, and whether the request has been sent at all. |
| 245 | for (const auto& request : requests) |
| 246 | OnDecodeImageFailed(request.first); |
| 247 | } |
| 248 | |
| 249 | void ImageDecoder::OnProcessCrashed(int exit_code) { |
| 250 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 251 | FailAllRequests(); |
| 252 | } |
| 253 | |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 254 | void ImageDecoder::OnProcessLaunchFailed(int error_code) { |
amistry | d59fe6b | 2015-04-30 23:50:30 | [diff] [blame] | 255 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 256 | FailAllRequests(); |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 257 | } |
| 258 | |
amistry | 6f72f04 | 2016-04-05 20:42:07 | [diff] [blame] | 259 | bool ImageDecoder::OnMessageReceived(const IPC::Message& message) { |
| 260 | return false; |
[email protected] | 373c106 | 2011-06-09 21:11:51 | [diff] [blame] | 261 | } |
| 262 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 263 | void ImageDecoder::OnDecodeImageSucceeded( |
| 264 | const SkBitmap& decoded_image, |
| 265 | int request_id) { |
| 266 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 267 | base::AutoLock lock(map_lock_); |
| 268 | auto it = image_request_id_map_.find(request_id); |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 269 | if (it == image_request_id_map_.end()) |
| 270 | return; |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 271 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 272 | ImageRequest* image_request = it->second; |
| 273 | image_request->task_runner()->PostTask( |
| 274 | FROM_HERE, |
| 275 | base::Bind(&ImageDecoder::RunOnImageDecoded, |
| 276 | this, |
| 277 | decoded_image, |
| 278 | request_id)); |
[email protected] | 928c0e71 | 2011-03-16 15:01:57 | [diff] [blame] | 279 | } |
| 280 | |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 281 | void ImageDecoder::OnDecodeImageFailed(int request_id) { |
| 282 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 283 | base::AutoLock lock(map_lock_); |
| 284 | auto it = image_request_id_map_.find(request_id); |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 285 | if (it == image_request_id_map_.end()) |
| 286 | return; |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 287 | |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 288 | ImageRequest* image_request = it->second; |
| 289 | image_request->task_runner()->PostTask( |
| 290 | FROM_HERE, |
| 291 | base::Bind(&ImageDecoder::RunOnDecodeImageFailed, this, request_id)); |
| 292 | } |
| 293 | |
| 294 | void ImageDecoder::RunOnImageDecoded(const SkBitmap& decoded_image, |
| 295 | int request_id) { |
| 296 | ImageRequest* image_request; |
| 297 | { |
| 298 | base::AutoLock lock(map_lock_); |
| 299 | auto it = image_request_id_map_.find(request_id); |
| 300 | if (it == image_request_id_map_.end()) |
| 301 | return; |
| 302 | image_request = it->second; |
twellington | a71414d | 2015-03-26 15:09:45 | [diff] [blame] | 303 | image_request_id_map_.erase(it); |
[email protected] | 11f16d10 | 2012-08-29 23:12:15 | [diff] [blame] | 304 | } |
thestig | c5eeb9f36 | 2015-04-22 18:42:32 | [diff] [blame] | 305 | |
| 306 | DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); |
| 307 | image_request->OnImageDecoded(decoded_image); |
| 308 | } |
| 309 | |
| 310 | void ImageDecoder::RunOnDecodeImageFailed(int request_id) { |
| 311 | ImageRequest* image_request; |
| 312 | { |
| 313 | base::AutoLock lock(map_lock_); |
| 314 | auto it = image_request_id_map_.find(request_id); |
| 315 | if (it == image_request_id_map_.end()) |
| 316 | return; |
| 317 | image_request = it->second; |
| 318 | image_request_id_map_.erase(it); |
| 319 | } |
| 320 | |
| 321 | DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); |
| 322 | image_request->OnDecodeImageFailed(); |
[email protected] | 551707a | 2010-06-16 16:59:47 | [diff] [blame] | 323 | } |