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