| 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 "base/callback.h" |
| 8 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 9 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/common/chrome_switches.h" |
| 10 #include "chrome/common/chrome_utility_messages.h" | 12 #include "chrome/common/chrome_utility_messages.h" |
| 11 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "components/image_decoder/public/interfaces/image_decoder.mojom.h" |
| 12 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/utility_process_host.h" | 16 #include "content/public/browser/utility_process_host.h" |
| 17 #include "content/public/common/service_registry.h" |
| 18 #include "skia/public/type_converters.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 15 | 20 |
| 16 using content::BrowserThread; | 21 using content::BrowserThread; |
| 17 using content::UtilityProcessHost; | 22 using content::UtilityProcessHost; |
| 18 | 23 |
| 19 namespace { | 24 namespace { |
| 20 | 25 |
| 21 // static, Leaky to allow access from any thread. | 26 // static, Leaky to allow access from any thread. |
| 22 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; | 27 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; |
| 23 | 28 |
| 24 // How long to wait after the last request has been received before ending | 29 // How long to wait after the last request has been received before ending |
| 25 // batch mode. | 30 // batch mode. |
| 26 const int kBatchModeTimeoutSeconds = 5; | 31 const int kBatchModeTimeoutSeconds = 5; |
| 27 | 32 |
| 33 void OnDecodeImageDone( |
| 34 base::Callback<void(int)> fail_callback, |
| 35 base::Callback<void(const SkBitmap&, int)> success_callback, |
| 36 int request_id, skia::BitmapPtr image) { |
| 37 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 38 if (image) { |
| 39 SkBitmap bitmap = image.To<SkBitmap>(); |
| 40 if (!bitmap.empty()) { |
| 41 success_callback.Run(bitmap, request_id); |
| 42 return; |
| 43 } |
| 44 } |
| 45 fail_callback.Run(request_id); |
| 46 } |
| 47 |
| 28 } // namespace | 48 } // namespace |
| 29 | 49 |
| 30 ImageDecoder::ImageDecoder() | 50 ImageDecoder::ImageDecoder() |
| 31 : image_request_id_counter_(0) { | 51 : image_request_id_counter_(0) { |
| 32 // A single ImageDecoder instance should live for the life of the program. | 52 // A single ImageDecoder instance should live for the life of the program. |
| 33 // Explicitly add a reference so the object isn't deleted. | 53 // Explicitly add a reference so the object isn't deleted. |
| 34 AddRef(); | 54 AddRef(); |
| 35 } | 55 } |
| 36 | 56 |
| 37 ImageDecoder::~ImageDecoder() { | 57 ImageDecoder::~ImageDecoder() { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 if (!batch_mode_timer_) { | 146 if (!batch_mode_timer_) { |
| 127 // Created here so it will call StopBatchMode() on the right thread. | 147 // Created here so it will call StopBatchMode() on the right thread. |
| 128 batch_mode_timer_.reset(new base::DelayTimer<ImageDecoder>( | 148 batch_mode_timer_.reset(new base::DelayTimer<ImageDecoder>( |
| 129 FROM_HERE, | 149 FROM_HERE, |
| 130 base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), | 150 base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), |
| 131 this, | 151 this, |
| 132 &ImageDecoder::StopBatchMode)); | 152 &ImageDecoder::StopBatchMode)); |
| 133 } | 153 } |
| 134 batch_mode_timer_->Reset(); | 154 batch_mode_timer_->Reset(); |
| 135 | 155 |
| 136 switch (image_codec) { | 156 if (switches::MojoUtilityServicesEnabled()) { |
| 157 image_decoder::ImageCodec mojo_codec = image_decoder::IMAGE_CODEC_DEFAULT; |
| 137 #if defined(OS_CHROMEOS) | 158 #if defined(OS_CHROMEOS) |
| 138 case ROBUST_JPEG_CODEC: | 159 if (image_codec == ROBUST_JPEG_CODEC) |
| 139 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( | 160 mojo_codec = image_decoder::IMAGE_CODEC_ROBUST_JPEG; |
| 140 image_data, request_id)); | |
| 141 break; | |
| 142 #endif // defined(OS_CHROMEOS) | 161 #endif // defined(OS_CHROMEOS) |
| 143 case DEFAULT_CODEC: | 162 (*decoder_)->DecodeImage( |
| 144 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | 163 mojo::Array<uint8_t>::From(image_data), |
| 145 image_data, shrink_to_fit, request_id)); | 164 mojo_codec, |
| 146 break; | 165 shrink_to_fit, |
| 166 base::Bind(&OnDecodeImageDone, |
| 167 base::Bind(&ImageDecoder::OnDecodeImageFailed, this), |
| 168 base::Bind(&ImageDecoder::OnDecodeImageSucceeded, this), |
| 169 request_id)); |
| 170 } else { |
| 171 switch (image_codec) { |
| 172 #if defined(OS_CHROMEOS) |
| 173 case ROBUST_JPEG_CODEC: |
| 174 utility_process_host_->Send( |
| 175 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data, request_id)); |
| 176 break; |
| 177 #endif // defined(OS_CHROMEOS) |
| 178 case DEFAULT_CODEC: |
| 179 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( |
| 180 image_data, shrink_to_fit, request_id)); |
| 181 break; |
| 182 } |
| 147 } | 183 } |
| 148 } | 184 } |
| 149 | 185 |
| 150 void ImageDecoder::CancelImpl(ImageRequest* image_request) { | 186 void ImageDecoder::CancelImpl(ImageRequest* image_request) { |
| 151 base::AutoLock lock(map_lock_); | 187 base::AutoLock lock(map_lock_); |
| 152 for (auto it = image_request_id_map_.begin(); | 188 for (auto it = image_request_id_map_.begin(); |
| 153 it != image_request_id_map_.end();) { | 189 it != image_request_id_map_.end();) { |
| 154 if (it->second == image_request) { | 190 if (it->second == image_request) { |
| 155 image_request_id_map_.erase(it++); | 191 image_request_id_map_.erase(it++); |
| 156 } else { | 192 } else { |
| 157 ++it; | 193 ++it; |
| 158 } | 194 } |
| 159 } | 195 } |
| 160 } | 196 } |
| 161 | 197 |
| 162 void ImageDecoder::StartBatchMode() { | 198 void ImageDecoder::StartBatchMode() { |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 199 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 164 utility_process_host_ = | 200 utility_process_host_ = |
| 165 UtilityProcessHost::Create( | 201 UtilityProcessHost::Create( |
| 166 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr(); | 202 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr(); |
| 167 utility_process_host_->SetName(l10n_util::GetStringUTF16( | 203 utility_process_host_->SetName(l10n_util::GetStringUTF16( |
| 168 IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME)); | 204 IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME)); |
| 169 if (!utility_process_host_->StartBatchMode()) { | 205 if (switches::MojoUtilityServicesEnabled()) { |
| 170 utility_process_host_.reset(); | 206 decoder_.reset(new image_decoder::ImageDecoderPtr); |
| 171 return; | 207 if (!utility_process_host_->StartMojoMode()) { |
| 208 utility_process_host_.reset(); |
| 209 return; |
| 210 } |
| 211 content::ServiceRegistry* service_registry = |
| 212 utility_process_host_->GetServiceRegistry(); |
| 213 service_registry->ConnectToRemoteService(mojo::GetProxy(decoder_.get())); |
| 214 } else { |
| 215 if (!utility_process_host_->StartBatchMode()) { |
| 216 utility_process_host_.reset(); |
| 217 return; |
| 218 } |
| 172 } | 219 } |
| 173 } | 220 } |
| 174 | 221 |
| 175 void ImageDecoder::StopBatchMode() { | 222 void ImageDecoder::StopBatchMode() { |
| 176 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 223 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 177 { | 224 { |
| 178 // Check for outstanding requests and wait for them to finish. | 225 // Check for outstanding requests and wait for them to finish. |
| 179 base::AutoLock lock(map_lock_); | 226 base::AutoLock lock(map_lock_); |
| 180 if (!image_request_id_map_.empty()) { | 227 if (!image_request_id_map_.empty()) { |
| 181 batch_mode_timer_->Reset(); | 228 batch_mode_timer_->Reset(); |
| 182 return; | 229 return; |
| 183 } | 230 } |
| 184 } | 231 } |
| 185 | 232 |
| 186 if (utility_process_host_) { | 233 if (utility_process_host_) { |
| 187 utility_process_host_->EndBatchMode(); | 234 if (switches::MojoUtilityServicesEnabled()) { |
| 235 // In Mojo, the utility process needs to be explicitly shut down by |
| 236 // deleting the host. |
| 237 delete utility_process_host_.get(); |
| 238 decoder_.reset(); |
| 239 } else { |
| 240 utility_process_host_->EndBatchMode(); |
| 241 } |
| 188 utility_process_host_.reset(); | 242 utility_process_host_.reset(); |
| 189 } | 243 } |
| 190 } | 244 } |
| 191 | 245 |
| 192 void ImageDecoder::FailAllRequests() { | 246 void ImageDecoder::FailAllRequests() { |
| 193 RequestMap requests; | 247 RequestMap requests; |
| 194 { | 248 { |
| 195 base::AutoLock lock(map_lock_); | 249 base::AutoLock lock(map_lock_); |
| 196 requests = image_request_id_map_; | 250 requests = image_request_id_map_; |
| 197 } | 251 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 auto it = image_request_id_map_.find(request_id); | 337 auto it = image_request_id_map_.find(request_id); |
| 284 if (it == image_request_id_map_.end()) | 338 if (it == image_request_id_map_.end()) |
| 285 return; | 339 return; |
| 286 image_request = it->second; | 340 image_request = it->second; |
| 287 image_request_id_map_.erase(it); | 341 image_request_id_map_.erase(it); |
| 288 } | 342 } |
| 289 | 343 |
| 290 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); | 344 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); |
| 291 image_request->OnDecodeImageFailed(); | 345 image_request->OnDecodeImageFailed(); |
| 292 } | 346 } |
| OLD | NEW |