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