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