| 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()) { |
| 137 case ROBUST_JPEG_CODEC: | 157 (*decoder_)->DecodeImage( |
| 138 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( | 158 mojo::Array<uint8_t>::From(image_data), |
| 139 image_data, request_id)); | 159 image_codec == ROBUST_JPEG_CODEC |
| 140 break; | 160 ? image_decoder::IMAGE_CODEC_ROBUST_JPEG |
| 141 case DEFAULT_CODEC: | 161 : image_decoder::IMAGE_CODEC_DEFAULT, |
| 142 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | 162 shrink_to_fit, |
| 143 image_data, shrink_to_fit, request_id)); | 163 base::Bind(&OnDecodeImageDone, |
| 144 break; | 164 base::Bind(&ImageDecoder::OnDecodeImageFailed, this), |
| 165 base::Bind(&ImageDecoder::OnDecodeImageSucceeded, this), |
| 166 request_id)); |
| 167 } else { |
| 168 switch (image_codec) { |
| 169 case ROBUST_JPEG_CODEC: |
| 170 utility_process_host_->Send( |
| 171 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data, request_id)); |
| 172 break; |
| 173 case DEFAULT_CODEC: |
| 174 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( |
| 175 image_data, shrink_to_fit, request_id)); |
| 176 break; |
| 177 } |
| 145 } | 178 } |
| 146 } | 179 } |
| 147 | 180 |
| 148 void ImageDecoder::CancelImpl(ImageRequest* image_request) { | 181 void ImageDecoder::CancelImpl(ImageRequest* image_request) { |
| 149 base::AutoLock lock(map_lock_); | 182 base::AutoLock lock(map_lock_); |
| 150 for (auto it = image_request_id_map_.begin(); | 183 for (auto it = image_request_id_map_.begin(); |
| 151 it != image_request_id_map_.end();) { | 184 it != image_request_id_map_.end();) { |
| 152 if (it->second == image_request) { | 185 if (it->second == image_request) { |
| 153 image_request_id_map_.erase(it++); | 186 image_request_id_map_.erase(it++); |
| 154 } else { | 187 } else { |
| 155 ++it; | 188 ++it; |
| 156 } | 189 } |
| 157 } | 190 } |
| 158 } | 191 } |
| 159 | 192 |
| 160 void ImageDecoder::StartBatchMode() { | 193 void ImageDecoder::StartBatchMode() { |
| 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 194 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 162 utility_process_host_ = | 195 utility_process_host_ = |
| 163 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get()) | 196 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get()) |
| 164 ->AsWeakPtr(); | 197 ->AsWeakPtr(); |
| 165 utility_process_host_->SetName(l10n_util::GetStringUTF16( | 198 utility_process_host_->SetName(l10n_util::GetStringUTF16( |
| 166 IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME)); | 199 IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME)); |
| 167 if (!utility_process_host_->StartBatchMode()) { | 200 if (switches::MojoUtilityServicesEnabled()) { |
| 168 utility_process_host_.reset(); | 201 decoder_.reset(new image_decoder::ImageDecoderPtr); |
| 169 return; | 202 if (!utility_process_host_->StartMojoMode()) { |
| 203 utility_process_host_.reset(); |
| 204 return; |
| 205 } |
| 206 content::ServiceRegistry* service_registry = |
| 207 utility_process_host_->GetServiceRegistry(); |
| 208 service_registry->ConnectToRemoteService(decoder_.get()); |
| 209 } else { |
| 210 if (!utility_process_host_->StartBatchMode()) { |
| 211 utility_process_host_.reset(); |
| 212 return; |
| 213 } |
| 170 } | 214 } |
| 171 } | 215 } |
| 172 | 216 |
| 173 void ImageDecoder::StopBatchMode() { | 217 void ImageDecoder::StopBatchMode() { |
| 174 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 218 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 175 if (utility_process_host_) { | 219 if (utility_process_host_) { |
| 176 utility_process_host_->EndBatchMode(); | 220 if (switches::MojoUtilityServicesEnabled()) { |
| 221 // In Mojo, the utility process needs to be explicitly shut down by |
| 222 // deleting the host. |
| 223 delete utility_process_host_.get(); |
| 224 decoder_.reset(); |
| 225 } else { |
| 226 utility_process_host_->EndBatchMode(); |
| 227 } |
| 177 utility_process_host_.reset(); | 228 utility_process_host_.reset(); |
| 178 } | 229 } |
| 179 | 230 |
| 180 // There could be outstanding request that are taking too long. Fail these so | 231 // There could be outstanding request that are taking too long. Fail these so |
| 181 // that there aren't any dangling requests. | 232 // that there aren't any dangling requests. |
| 182 FailAllRequests(); | 233 FailAllRequests(); |
| 183 } | 234 } |
| 184 | 235 |
| 185 void ImageDecoder::FailAllRequests() { | 236 void ImageDecoder::FailAllRequests() { |
| 186 RequestMap requests; | 237 RequestMap requests; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 auto it = image_request_id_map_.find(request_id); | 327 auto it = image_request_id_map_.find(request_id); |
| 277 if (it == image_request_id_map_.end()) | 328 if (it == image_request_id_map_.end()) |
| 278 return; | 329 return; |
| 279 image_request = it->second; | 330 image_request = it->second; |
| 280 image_request_id_map_.erase(it); | 331 image_request_id_map_.erase(it); |
| 281 } | 332 } |
| 282 | 333 |
| 283 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); | 334 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); |
| 284 image_request->OnDecodeImageFailed(); | 335 image_request->OnDecodeImageFailed(); |
| 285 } | 336 } |
| OLD | NEW |