Chromium Code Reviews| 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 #include "chrome/browser/image_decoder.h" | 5 #include "chrome/browser/image_decoder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/common/chrome_utility_messages.h" | 9 #include "chrome/common/chrome_utility_messages.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/utility_process_host.h" | 11 #include "content/public/browser/utility_process_host.h" |
| 12 | 12 |
| 13 using content::BrowserThread; | 13 using content::BrowserThread; |
| 14 using content::UtilityProcessHost; | 14 using content::UtilityProcessHost; |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // static, Leaky to allow access from any thread. | 18 // static, Leaky to allow access from any thread. |
| 19 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; | 19 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; |
| 20 | 20 |
| 21 // How long to wait after the last request has been received before ending | 21 // How long to wait after the last request has been received before ending |
| 22 // batch mode. | 22 // batch mode. |
| 23 const int kBatchModeTimeoutSeconds = 5; | 23 const int kBatchModeTimeoutSeconds = 5; |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 class ImageDecoder::Job : public base::RefCountedThreadSafe<ImageDecoder::Job> { | |
| 28 public: | |
| 29 Job(ImageDecoder* decoder, ImageDecoder::ImageRequest* request); | |
| 30 | |
| 31 void Cancel(); | |
| 32 void NotifyDecodeSucceeded(const SkBitmap& decoded_image); | |
| 33 void NotifyDecodeFailed(); | |
| 34 | |
| 35 ImageDecoder::ImageRequest* image_request() const { return request_; } | |
| 36 | |
| 37 private: | |
| 38 friend class base::RefCountedThreadSafe<ImageDecoder::Job>; | |
| 39 ~Job() = default; | |
| 40 | |
| 41 // Runs on the requests's task runner. | |
| 42 void OnSuccess(const SkBitmap& decoded_image); | |
| 43 void OnFailed(); | |
| 44 | |
| 45 void RemoveTask(); | |
| 46 | |
| 47 // Not owned, and must remain valid for the life of this object. | |
| 48 ImageDecoder* decoder_; | |
|
Lei Zhang
2015/04/08 01:30:10
Isn't there just a single ImageDecoder that will o
Anand Mistry (off Chromium)
2015/04/08 03:13:46
Good point. Removed and use lazy instance.
| |
| 49 | |
| 50 base::Lock lock_; | |
| 51 bool is_cancelled_; | |
|
Lei Zhang
2015/04/08 01:30:10
Just null out |request_| instead?
Anand Mistry (off Chromium)
2015/04/08 03:13:46
Can't. Doing so would require always accessing |re
| |
| 52 // Only valid while |is_cancelled_| == false. | |
| 53 ImageDecoder::ImageRequest* request_; | |
| 54 }; | |
| 55 | |
| 56 ImageDecoder::Job::Job(ImageDecoder* decoder, | |
| 57 ImageDecoder::ImageRequest* request) | |
| 58 : decoder_(decoder), is_cancelled_(false), request_(request) { | |
| 59 } | |
| 60 | |
| 61 void ImageDecoder::Job::Cancel() { | |
| 62 base::AutoLock lock(lock_); | |
| 63 is_cancelled_ = true; | |
| 64 } | |
| 65 | |
| 66 void ImageDecoder::Job::NotifyDecodeSucceeded(const SkBitmap& decoded_image) { | |
| 67 base::AutoLock lock(lock_); | |
| 68 if (is_cancelled_) | |
| 69 return; | |
| 70 | |
| 71 request_->task_runner()->PostTask( | |
| 72 FROM_HERE, | |
| 73 base::Bind(&ImageDecoder::Job::OnSuccess, this, decoded_image)); | |
| 74 } | |
| 75 | |
| 76 void ImageDecoder::Job::NotifyDecodeFailed() { | |
| 77 base::AutoLock lock(lock_); | |
| 78 if (is_cancelled_) | |
| 79 return; | |
| 80 | |
| 81 request_->task_runner()->PostTask( | |
| 82 FROM_HERE, base::Bind(&ImageDecoder::Job::OnFailed, this)); | |
| 83 } | |
| 84 | |
| 85 void ImageDecoder::Job::OnSuccess(const SkBitmap& decoded_image) { | |
| 86 base::AutoLock lock(lock_); | |
| 87 if (is_cancelled_) | |
| 88 return; | |
| 89 | |
| 90 DCHECK(request_->task_runner()->RunsTasksOnCurrentThread()); | |
| 91 decoder_->RemoveJob(this); | |
|
Lei Zhang
2015/04/08 01:30:10
This and the RemoveJob() call in ImageDecoder::Job
Anand Mistry (off Chromium)
2015/04/08 03:13:45
ImageDecoder::CancelImpl also does. However, I can
| |
| 92 request_->OnImageDecoded(decoded_image); | |
| 93 } | |
| 94 | |
| 95 void ImageDecoder::Job::OnFailed() { | |
| 96 base::AutoLock lock(lock_); | |
| 97 if (is_cancelled_) | |
| 98 return; | |
| 99 | |
| 100 DCHECK(request_->task_runner()->RunsTasksOnCurrentThread()); | |
| 101 decoder_->RemoveJob(this); | |
| 102 request_->OnDecodeImageFailed(); | |
| 103 } | |
| 104 | |
| 27 ImageDecoder::ImageDecoder() | 105 ImageDecoder::ImageDecoder() |
| 28 : image_request_id_counter_(0), last_request_(base::TimeTicks::Now()) { | 106 : image_request_id_counter_(0), last_request_(base::TimeTicks::Now()) { |
| 29 // A single ImageDecoder instance should live for the life of the program. | 107 // A single ImageDecoder instance should live for the life of the program. |
| 30 // Explicitly add a reference so the object isn't deleted. | 108 // Explicitly add a reference so the object isn't deleted. |
| 31 AddRef(); | 109 AddRef(); |
| 32 } | 110 } |
| 33 | 111 |
| 34 ImageDecoder::~ImageDecoder() { | 112 ImageDecoder::~ImageDecoder() { |
| 35 } | 113 } |
| 36 | 114 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 47 void ImageDecoder::Start(ImageRequest* image_request, | 125 void ImageDecoder::Start(ImageRequest* image_request, |
| 48 const std::string& image_data) { | 126 const std::string& image_data) { |
| 49 StartWithOptions(image_request, image_data, DEFAULT_CODEC, false); | 127 StartWithOptions(image_request, image_data, DEFAULT_CODEC, false); |
| 50 } | 128 } |
| 51 | 129 |
| 52 // static | 130 // static |
| 53 void ImageDecoder::StartWithOptions(ImageRequest* image_request, | 131 void ImageDecoder::StartWithOptions(ImageRequest* image_request, |
| 54 const std::string& image_data, | 132 const std::string& image_data, |
| 55 ImageCodec image_codec, | 133 ImageCodec image_codec, |
| 56 bool shrink_to_fit) { | 134 bool shrink_to_fit) { |
| 135 g_decoder.Pointer()->StartWithOptionsImpl(image_request, image_data, | |
| 136 image_codec, shrink_to_fit); | |
| 137 } | |
| 138 | |
| 139 void ImageDecoder::StartWithOptionsImpl(ImageRequest* image_request, | |
| 140 const std::string& image_data, | |
| 141 ImageCodec image_codec, | |
| 142 bool shrink_to_fit) { | |
| 57 DCHECK(image_request); | 143 DCHECK(image_request); |
| 58 DCHECK(image_request->task_runner()); | 144 DCHECK(image_request->task_runner()); |
| 145 | |
| 146 scoped_refptr<Job> job(new Job(this, image_request)); | |
| 147 int request_id; | |
| 148 { | |
| 149 base::AutoLock lock(map_lock_); | |
| 150 request_id = image_request_id_counter_++; | |
| 151 image_request_id_map_.insert(std::make_pair(request_id, job)); | |
| 152 } | |
| 59 BrowserThread::PostTask( | 153 BrowserThread::PostTask( |
| 60 BrowserThread::IO, FROM_HERE, | 154 BrowserThread::IO, FROM_HERE, |
| 61 base::Bind( | 155 base::Bind( |
| 62 &ImageDecoder::DecodeImageInSandbox, | 156 &ImageDecoder::DecodeImageInSandbox, this, request_id, |
| 63 g_decoder.Pointer(), image_request, | |
| 64 std::vector<unsigned char>(image_data.begin(), image_data.end()), | 157 std::vector<unsigned char>(image_data.begin(), image_data.end()), |
| 65 image_codec, shrink_to_fit)); | 158 image_codec, shrink_to_fit)); |
| 66 } | 159 } |
| 67 | 160 |
| 68 // static | 161 // static |
| 69 void ImageDecoder::Cancel(ImageRequest* image_request) { | 162 void ImageDecoder::Cancel(ImageRequest* image_request) { |
| 70 DCHECK(image_request); | 163 DCHECK(image_request); |
| 71 g_decoder.Pointer()->CancelImpl(image_request); | 164 g_decoder.Pointer()->CancelImpl(image_request); |
| 72 } | 165 } |
| 73 | 166 |
| 74 void ImageDecoder::DecodeImageInSandbox( | 167 void ImageDecoder::DecodeImageInSandbox( |
| 75 ImageRequest* image_request, | 168 int request_id, |
| 76 const std::vector<unsigned char>& image_data, | 169 const std::vector<unsigned char>& image_data, |
| 77 ImageCodec image_codec, | 170 ImageCodec image_codec, |
| 78 bool shrink_to_fit) { | 171 bool shrink_to_fit) { |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 172 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 80 if (!utility_process_host_) { | 173 if (!utility_process_host_) { |
| 81 StartBatchMode(); | 174 StartBatchMode(); |
| 82 } | 175 } |
| 83 if (!utility_process_host_) { | 176 if (!utility_process_host_) { |
| 84 // Utility process failed to start; notify delegate and return. | 177 // Utility process failed to start; notify delegate and return. |
| 85 // Without this check, we were seeing crashes on startup. Further | 178 // Without this check, we were seeing crashes on startup. Further |
| 86 // investigation is needed to determine why the utility process | 179 // investigation is needed to determine why the utility process |
| 87 // is failing to start. See crbug.com/472272 | 180 // is failing to start. See crbug.com/472272 |
| 88 image_request->task_runner()->PostTask( | 181 OnDecodeImageFailed(request_id); |
| 89 FROM_HERE, base::Bind(&ImageRequest::OnDecodeImageFailed, | |
| 90 base::Unretained(image_request))); | |
| 91 return; | 182 return; |
| 92 } | 183 } |
| 93 | 184 |
| 94 last_request_ = base::TimeTicks::Now(); | 185 last_request_ = base::TimeTicks::Now(); |
| 95 base::AutoLock lock(map_lock_); | |
| 96 image_request_id_map_.insert( | |
| 97 std::make_pair(image_request_id_counter_, image_request)); | |
| 98 | 186 |
| 99 switch (image_codec) { | 187 switch (image_codec) { |
| 100 case ROBUST_JPEG_CODEC: | 188 case ROBUST_JPEG_CODEC: |
| 101 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( | 189 utility_process_host_->Send( |
| 102 image_data, image_request_id_counter_)); | 190 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data, request_id)); |
| 103 break; | 191 break; |
| 104 case DEFAULT_CODEC: | 192 case DEFAULT_CODEC: |
| 105 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | 193 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( |
| 106 image_data, shrink_to_fit, image_request_id_counter_)); | 194 image_data, shrink_to_fit, request_id)); |
| 107 break; | 195 break; |
| 108 } | 196 } |
| 109 | |
| 110 ++image_request_id_counter_; | |
| 111 } | 197 } |
| 112 | 198 |
| 113 void ImageDecoder::CancelImpl(ImageRequest* image_request) { | 199 void ImageDecoder::CancelImpl(ImageRequest* image_request) { |
| 114 base::AutoLock lock(map_lock_); | 200 scoped_refptr<Job> job; |
| 115 for (auto it = image_request_id_map_.begin(); | 201 |
| 116 it != image_request_id_map_.end();) { | 202 { |
| 117 if (it->second == image_request) { | 203 base::AutoLock lock(map_lock_); |
| 118 image_request_id_map_.erase(it++); | 204 for (auto it = image_request_id_map_.begin(); |
| 119 } else { | 205 it != image_request_id_map_.end();) { |
| 120 ++it; | 206 if (it->second->image_request() == image_request) { |
| 207 // There should only be one. | |
|
Lei Zhang
2015/04/08 01:30:10
If a caller does Start(), Start(), Cancel(), would
Anand Mistry (off Chromium)
2015/04/08 03:13:45
I wrote that off since it seems like a "Bad Idea",
| |
| 208 DCHECK(!job); | |
| 209 job = it->second; | |
| 210 image_request_id_map_.erase(it++); | |
| 211 } else { | |
| 212 ++it; | |
| 213 } | |
| 121 } | 214 } |
| 122 } | 215 } |
| 216 | |
| 217 // Will block if |OnImageDecoded| or |OnDecodeImageFailed| is running. | |
| 218 if (job) | |
| 219 job->Cancel(); | |
| 123 } | 220 } |
| 124 | 221 |
| 125 void ImageDecoder::StartBatchMode() { | 222 void ImageDecoder::StartBatchMode() { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 223 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 127 utility_process_host_ = | 224 utility_process_host_ = |
| 128 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get()) | 225 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get()) |
| 129 ->AsWeakPtr(); | 226 ->AsWeakPtr(); |
| 130 if (!utility_process_host_->StartBatchMode()) { | 227 if (!utility_process_host_->StartBatchMode()) { |
| 131 utility_process_host_.reset(); | 228 utility_process_host_.reset(); |
| 132 return; | 229 return; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 160 OnDecodeImageFailed) | 257 OnDecodeImageFailed) |
| 161 IPC_MESSAGE_UNHANDLED(handled = false) | 258 IPC_MESSAGE_UNHANDLED(handled = false) |
| 162 IPC_END_MESSAGE_MAP() | 259 IPC_END_MESSAGE_MAP() |
| 163 return handled; | 260 return handled; |
| 164 } | 261 } |
| 165 | 262 |
| 166 void ImageDecoder::OnDecodeImageSucceeded( | 263 void ImageDecoder::OnDecodeImageSucceeded( |
| 167 const SkBitmap& decoded_image, | 264 const SkBitmap& decoded_image, |
| 168 int request_id) { | 265 int request_id) { |
| 169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 266 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 170 base::AutoLock lock(map_lock_); | |
| 171 auto it = image_request_id_map_.find(request_id); | |
| 172 if (it != image_request_id_map_.end()) { | |
| 173 ImageRequest* image_request = it->second; | |
| 174 image_request->task_runner()->PostTask( | |
| 175 FROM_HERE, base::Bind(&ImageRequest::OnImageDecoded, | |
| 176 base::Unretained(image_request), decoded_image)); | |
| 177 | 267 |
| 178 image_request_id_map_.erase(it); | 268 scoped_refptr<Job> job; |
| 269 { | |
| 270 base::AutoLock lock(map_lock_); | |
| 271 auto it = image_request_id_map_.find(request_id); | |
| 272 if (it != image_request_id_map_.end()) { | |
| 273 job = it->second; | |
| 274 // NOTE: The job isn't removed from the map because it can still be | |
| 275 // cancelled. | |
| 276 } | |
| 179 } | 277 } |
| 278 // Call Job's functions outside |map_lock_| to avoid a potential deadlock with | |
| 279 // |Job.lock_|. | |
| 280 if (job) | |
| 281 job->NotifyDecodeSucceeded(decoded_image); | |
| 180 } | 282 } |
| 181 | 283 |
| 182 void ImageDecoder::OnDecodeImageFailed(int request_id) { | 284 void ImageDecoder::OnDecodeImageFailed(int request_id) { |
| 183 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 285 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 286 | |
| 287 scoped_refptr<Job> job; | |
| 288 { | |
| 289 base::AutoLock lock(map_lock_); | |
| 290 auto it = image_request_id_map_.find(request_id); | |
| 291 if (it != image_request_id_map_.end()) { | |
| 292 job = it->second; | |
| 293 } | |
| 294 } | |
| 295 if (job) | |
| 296 job->NotifyDecodeFailed(); | |
| 297 } | |
| 298 | |
| 299 void ImageDecoder::RemoveJob(const scoped_refptr<Job>& job) { | |
| 184 base::AutoLock lock(map_lock_); | 300 base::AutoLock lock(map_lock_); |
| 185 auto it = image_request_id_map_.find(request_id); | 301 for (auto it = image_request_id_map_.begin(); |
| 186 if (it != image_request_id_map_.end()) { | 302 it != image_request_id_map_.end();) { |
| 187 ImageRequest* image_request = it->second; | 303 if (it->second == job) { |
| 188 image_request->task_runner()->PostTask( | 304 image_request_id_map_.erase(it++); |
|
Lei Zhang
2015/04/08 01:30:10
Why keep going? Isn't there only a single instance
Anand Mistry (off Chromium)
2015/04/08 03:13:45
Done.
| |
| 189 FROM_HERE, base::Bind(&ImageRequest::OnDecodeImageFailed, | 305 } else { |
| 190 base::Unretained(image_request))); | 306 ++it; |
| 191 | 307 } |
| 192 image_request_id_map_.erase(it); | |
| 193 } | 308 } |
| 194 } | 309 } |
| OLD | NEW |