Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: chrome/browser/image_decoder.cc

Issue 1844103004: Convert the utility process image decoder into a Mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/image_decoder.h ('k') | chrome/chrome_common.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/thread_task_runner_handle.h" 8 #include "base/thread_task_runner_handle.h"
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
11 #include "chrome/common/chrome_utility_messages.h" 11 #include "chrome/common/image_decoder.mojom.h"
12 #include "chrome/grit/generated_resources.h" 12 #include "chrome/grit/generated_resources.h"
13 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/utility_process_host.h" 14 #include "content/public/browser/utility_process_host.h"
15 #include "content/public/common/service_registry.h"
16 #include "skia/public/type_converters.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/base/l10n/l10n_util.h" 18 #include "ui/base/l10n/l10n_util.h"
16 19
17 using content::BrowserThread; 20 using content::BrowserThread;
18 using content::UtilityProcessHost; 21 using content::UtilityProcessHost;
19 22
20 namespace { 23 namespace {
21 24
22 // static, Leaky to allow access from any thread. 25 // static, Leaky to allow access from any thread.
23 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; 26 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER;
24 27
25 // How long to wait after the last request has been received before ending 28 // How long to wait after the last request has been received before ending
26 // batch mode. 29 // batch mode.
27 const int kBatchModeTimeoutSeconds = 5; 30 const int kBatchModeTimeoutSeconds = 5;
28 31
32 void OnDecodeImageDone(
33 base::Callback<void(int)> fail_callback,
34 base::Callback<void(const SkBitmap&, int)> success_callback,
35 int request_id, skia::mojom::BitmapPtr image) {
36 DCHECK_CURRENTLY_ON(BrowserThread::IO);
37 if (image) {
38 SkBitmap bitmap = image.To<SkBitmap>();
39 if (!bitmap.empty()) {
40 success_callback.Run(bitmap, request_id);
41 return;
42 }
43 }
44 fail_callback.Run(request_id);
45 }
46
29 } // namespace 47 } // namespace
30 48
31 ImageDecoder::ImageDecoder() 49 ImageDecoder::ImageDecoder()
32 : image_request_id_counter_(0) { 50 : image_request_id_counter_(0) {
33 // A single ImageDecoder instance should live for the life of the program. 51 // A single ImageDecoder instance should live for the life of the program.
34 // Explicitly add a reference so the object isn't deleted. 52 // Explicitly add a reference so the object isn't deleted.
35 AddRef(); 53 AddRef();
36 } 54 }
37 55
38 ImageDecoder::~ImageDecoder() { 56 ImageDecoder::~ImageDecoder() {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 } 143 }
126 144
127 if (!batch_mode_timer_) { 145 if (!batch_mode_timer_) {
128 // Created here so it will call StopBatchMode() on the right thread. 146 // Created here so it will call StopBatchMode() on the right thread.
129 batch_mode_timer_.reset(new base::DelayTimer( 147 batch_mode_timer_.reset(new base::DelayTimer(
130 FROM_HERE, base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), this, 148 FROM_HERE, base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), this,
131 &ImageDecoder::StopBatchMode)); 149 &ImageDecoder::StopBatchMode));
132 } 150 }
133 batch_mode_timer_->Reset(); 151 batch_mode_timer_->Reset();
134 152
135 switch (image_codec) { 153 mojom::ImageCodec mojo_codec = mojom::ImageCodec::DEFAULT;
136 #if defined(OS_CHROMEOS) 154 #if defined(OS_CHROMEOS)
137 case ROBUST_JPEG_CODEC: 155 if (image_codec == ROBUST_JPEG_CODEC)
138 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( 156 mojo_codec = mojom::ImageCodec::ROBUST_JPEG;
139 image_data, request_id)); 157 if (image_codec == ROBUST_PNG_CODEC)
140 break; 158 mojo_codec = mojom::ImageCodec::ROBUST_PNG;
141 case ROBUST_PNG_CODEC:
142 utility_process_host_->Send(new ChromeUtilityMsg_RobustPNGDecodeImage(
143 image_data, request_id));
144 break;
145 #endif // defined(OS_CHROMEOS) 159 #endif // defined(OS_CHROMEOS)
146 case DEFAULT_CODEC: 160 decoder_->DecodeImage(
147 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( 161 mojo::Array<uint8_t>::From(image_data),
148 image_data, shrink_to_fit, request_id)); 162 mojo_codec,
149 break; 163 shrink_to_fit,
150 } 164 base::Bind(&OnDecodeImageDone,
165 base::Bind(&ImageDecoder::OnDecodeImageFailed, this),
166 base::Bind(&ImageDecoder::OnDecodeImageSucceeded, this),
167 request_id));
151 } 168 }
152 169
153 void ImageDecoder::CancelImpl(ImageRequest* image_request) { 170 void ImageDecoder::CancelImpl(ImageRequest* image_request) {
154 base::AutoLock lock(map_lock_); 171 base::AutoLock lock(map_lock_);
155 for (auto it = image_request_id_map_.begin(); 172 for (auto it = image_request_id_map_.begin();
156 it != image_request_id_map_.end();) { 173 it != image_request_id_map_.end();) {
157 if (it->second == image_request) { 174 if (it->second == image_request) {
158 image_request_id_map_.erase(it++); 175 image_request_id_map_.erase(it++);
159 } else { 176 } else {
160 ++it; 177 ++it;
161 } 178 }
162 } 179 }
163 } 180 }
164 181
165 void ImageDecoder::StartBatchMode() { 182 void ImageDecoder::StartBatchMode() {
166 DCHECK_CURRENTLY_ON(BrowserThread::IO); 183 DCHECK_CURRENTLY_ON(BrowserThread::IO);
167 utility_process_host_ = 184 utility_process_host_ =
168 UtilityProcessHost::Create( 185 UtilityProcessHost::Create(
169 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr(); 186 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr();
170 utility_process_host_->SetName(l10n_util::GetStringUTF16( 187 utility_process_host_->SetName(l10n_util::GetStringUTF16(
171 IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME)); 188 IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME));
172 if (!utility_process_host_->StartBatchMode()) { 189 if (!utility_process_host_->Start()) {
173 utility_process_host_.reset(); 190 delete utility_process_host_.get();
174 return; 191 return;
175 } 192 }
193 content::ServiceRegistry* service_registry =
194 utility_process_host_->GetServiceRegistry();
195 service_registry->ConnectToRemoteService(mojo::GetProxy(&decoder_));
176 } 196 }
177 197
178 void ImageDecoder::StopBatchMode() { 198 void ImageDecoder::StopBatchMode() {
179 DCHECK_CURRENTLY_ON(BrowserThread::IO); 199 DCHECK_CURRENTLY_ON(BrowserThread::IO);
180 { 200 {
181 // Check for outstanding requests and wait for them to finish. 201 // Check for outstanding requests and wait for them to finish.
182 base::AutoLock lock(map_lock_); 202 base::AutoLock lock(map_lock_);
183 if (!image_request_id_map_.empty()) { 203 if (!image_request_id_map_.empty()) {
184 batch_mode_timer_->Reset(); 204 batch_mode_timer_->Reset();
185 return; 205 return;
186 } 206 }
187 } 207 }
188 208
189 if (utility_process_host_) { 209 if (utility_process_host_) {
190 utility_process_host_->EndBatchMode(); 210 // With Mojo, the utility process needs to be explicitly shut down by
211 // deleting the host.
212 delete utility_process_host_.get();
213 decoder_.reset();
191 utility_process_host_.reset(); 214 utility_process_host_.reset();
192 } 215 }
193 } 216 }
194 217
195 void ImageDecoder::FailAllRequests() { 218 void ImageDecoder::FailAllRequests() {
196 RequestMap requests; 219 RequestMap requests;
197 { 220 {
198 base::AutoLock lock(map_lock_); 221 base::AutoLock lock(map_lock_);
199 requests = image_request_id_map_; 222 requests = image_request_id_map_;
200 } 223 }
(...skipping 11 matching lines...) Expand all
212 void ImageDecoder::OnProcessCrashed(int exit_code) { 235 void ImageDecoder::OnProcessCrashed(int exit_code) {
213 DCHECK_CURRENTLY_ON(BrowserThread::IO); 236 DCHECK_CURRENTLY_ON(BrowserThread::IO);
214 FailAllRequests(); 237 FailAllRequests();
215 } 238 }
216 239
217 void ImageDecoder::OnProcessLaunchFailed() { 240 void ImageDecoder::OnProcessLaunchFailed() {
218 DCHECK_CURRENTLY_ON(BrowserThread::IO); 241 DCHECK_CURRENTLY_ON(BrowserThread::IO);
219 FailAllRequests(); 242 FailAllRequests();
220 } 243 }
221 244
222 bool ImageDecoder::OnMessageReceived( 245 bool ImageDecoder::OnMessageReceived(const IPC::Message& message) {
223 const IPC::Message& message) { 246 return false;
224 bool handled = true;
225 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message)
226 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded,
227 OnDecodeImageSucceeded)
228 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed,
229 OnDecodeImageFailed)
230 IPC_MESSAGE_UNHANDLED(handled = false)
231 IPC_END_MESSAGE_MAP()
232 return handled;
233 } 247 }
234 248
235 void ImageDecoder::OnDecodeImageSucceeded( 249 void ImageDecoder::OnDecodeImageSucceeded(
236 const SkBitmap& decoded_image, 250 const SkBitmap& decoded_image,
237 int request_id) { 251 int request_id) {
238 DCHECK_CURRENTLY_ON(BrowserThread::IO); 252 DCHECK_CURRENTLY_ON(BrowserThread::IO);
239 base::AutoLock lock(map_lock_); 253 base::AutoLock lock(map_lock_);
240 auto it = image_request_id_map_.find(request_id); 254 auto it = image_request_id_map_.find(request_id);
241 if (it == image_request_id_map_.end()) 255 if (it == image_request_id_map_.end())
242 return; 256 return;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 auto it = image_request_id_map_.find(request_id); 300 auto it = image_request_id_map_.find(request_id);
287 if (it == image_request_id_map_.end()) 301 if (it == image_request_id_map_.end())
288 return; 302 return;
289 image_request = it->second; 303 image_request = it->second;
290 image_request_id_map_.erase(it); 304 image_request_id_map_.erase(it);
291 } 305 }
292 306
293 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); 307 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread());
294 image_request->OnDecodeImageFailed(); 308 image_request->OnDecodeImageFailed();
295 } 309 }
OLDNEW
« no previous file with comments | « chrome/browser/image_decoder.h ('k') | chrome/chrome_common.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698