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

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

Issue 1028543002: Turn the utility process image decoder into a Mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up and add test. Created 5 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
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 "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process.h"
9 #include "chrome/common/chrome_switches.h"
9 #include "chrome/common/chrome_utility_messages.h" 10 #include "chrome/common/chrome_utility_messages.h"
10 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/utility_process_host.h" 12 #include "content/public/browser/utility_process_host.h"
13 #include "content/public/common/service_registry.h"
14 #include "services/image_decoder/public/interfaces/image_decoder.mojom.h"
15 #include "skia/public/type_converters.h"
12 16
13 using content::BrowserThread; 17 using content::BrowserThread;
14 using content::UtilityProcessHost; 18 using content::UtilityProcessHost;
15 19
16 ImageDecoder::ImageDecoder(Delegate* delegate, 20 ImageDecoder::ImageDecoder(Delegate* delegate,
17 const std::string& image_data, 21 const std::string& image_data,
18 ImageCodec image_codec) 22 ImageCodec image_codec)
19 : delegate_(delegate), 23 : delegate_(delegate),
20 image_data_(image_data.begin(), image_data.end()), 24 image_data_(image_data.begin(), image_data.end()),
21 image_codec_(image_codec), 25 image_codec_(image_codec),
22 task_runner_(NULL), 26 task_runner_(NULL),
23 shrink_to_fit_(false) { 27 shrink_to_fit_(false) {
24 } 28 }
25 29
26 ImageDecoder::ImageDecoder(Delegate* delegate, 30 ImageDecoder::ImageDecoder(Delegate* delegate,
27 const std::vector<char>& image_data, 31 const std::vector<char>& image_data,
28 ImageCodec image_codec) 32 ImageCodec image_codec)
29 : delegate_(delegate), 33 : delegate_(delegate),
30 image_data_(image_data.begin(), image_data.end()), 34 image_data_(image_data.begin(), image_data.end()),
31 image_codec_(image_codec), 35 image_codec_(image_codec),
32 task_runner_(NULL), 36 task_runner_(NULL),
33 shrink_to_fit_(false) { 37 shrink_to_fit_(false) {
34 } 38 }
35 39
36 ImageDecoder::~ImageDecoder() {} 40 ImageDecoder::~ImageDecoder() {}
sky 2015/04/01 15:00:45 I'm not familiar with this class. What thread is t
Anand Mistry (off Chromium) 2015/04/02 04:29:45 Most likely the task_runner_ thread (usually UI) s
sky 2015/04/02 17:20:10 I'm ok with that, as long as you add DCHECKs and b
Theresa 2015/04/13 18:52:27 I'm pretty sure this method actually never gets ru
37 41
38 void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) { 42 void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) {
39 task_runner_ = task_runner; 43 task_runner_ = task_runner;
40 BrowserThread::PostTask( 44 BrowserThread::PostTask(
41 BrowserThread::IO, FROM_HERE, 45 BrowserThread::IO, FROM_HERE,
42 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_)); 46 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_));
43 } 47 }
44 48
45 bool ImageDecoder::OnMessageReceived(const IPC::Message& message) { 49 bool ImageDecoder::OnMessageReceived(const IPC::Message& message) {
46 bool handled = true; 50 bool handled = true;
(...skipping 12 matching lines...) Expand all
59 if (delegate_) 63 if (delegate_)
60 delegate_->OnImageDecoded(this, decoded_image); 64 delegate_->OnImageDecoded(this, decoded_image);
61 } 65 }
62 66
63 void ImageDecoder::OnDecodeImageFailed() { 67 void ImageDecoder::OnDecodeImageFailed() {
64 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 68 DCHECK(task_runner_->RunsTasksOnCurrentThread());
65 if (delegate_) 69 if (delegate_)
66 delegate_->OnDecodeImageFailed(this); 70 delegate_->OnDecodeImageFailed(this);
67 } 71 }
68 72
73 void ImageDecoder::OnDecodeImageDone(bool success, skia::BitmapPtr image) {
sky 2015/04/01 15:00:45 Order doesn't match header.
Anand Mistry (off Chromium) 2015/04/02 04:29:45 You mean ordering of functions? Fixed that.
74 DCHECK_CURRENTLY_ON(BrowserThread::IO);
75 decoder_.reset();
76 if (delegate_ && success && image) {
77 SkBitmap bitmap = image.To<SkBitmap>();
78 if (!bitmap.empty()) {
79 task_runner_->PostTask(
80 FROM_HERE,
81 base::Bind(&ImageDecoder::OnDecodeImageSucceeded, this, bitmap));
82 return;
83 }
84 }
85 task_runner_->PostTask(FROM_HERE,
86 base::Bind(&ImageDecoder::OnDecodeImageFailed, this));
87 }
88
89 void ImageDecoder::OnConnectionError() {
90 DCHECK_CURRENTLY_ON(BrowserThread::IO);
91 // Destroys the local Mojo endpoint, which releases Mojo's references to this
92 // object from the DecodeImage callback. Also, we need to make sure this is
93 // destroyed on the IO thread since Mojo is thread-hostile.
94 decoder_.reset();
95 }
96
69 void ImageDecoder::DecodeImageInSandbox( 97 void ImageDecoder::DecodeImageInSandbox(
70 const std::vector<unsigned char>& image_data) { 98 const std::vector<unsigned char>& image_data) {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
72 UtilityProcessHost* utility_process_host; 100 UtilityProcessHost* utility_process_host;
73 utility_process_host = UtilityProcessHost::Create(this, task_runner_.get()); 101 utility_process_host = UtilityProcessHost::Create(this, task_runner_.get());
74 if (image_codec_ == ROBUST_JPEG_CODEC) { 102
75 utility_process_host->Send( 103 if (switches::MojoUtilityServicesEnabled()) {
76 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data)); 104 if (!utility_process_host->StartMojoMode()) {
105 OnDecodeImageDone(false, nullptr);
106 return;
107 }
108 content::ServiceRegistry* service_registry =
109 utility_process_host->GetServiceRegistry();
110 service_registry->ConnectToRemoteService(&decoder_);
111 decoder_.set_error_handler(this);
112
113 decoder_->DecodeImage(
114 mojo::Array<uint8_t>::From(image_data),
115 image_codec_ == ROBUST_JPEG_CODEC ? services::IMAGE_CODEC_ROBUST_JPEG
116 : services::IMAGE_CODEC_DEFAULT,
117 shrink_to_fit_, base::Bind(&ImageDecoder::OnDecodeImageDone, this));
77 } else { 118 } else {
78 utility_process_host->Send( 119 if (image_codec_ == ROBUST_JPEG_CODEC) {
79 new ChromeUtilityMsg_DecodeImage(image_data, shrink_to_fit_)); 120 utility_process_host->Send(
121 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data));
122 } else {
123 utility_process_host->Send(
124 new ChromeUtilityMsg_DecodeImage(image_data, shrink_to_fit_));
125 }
80 } 126 }
81 } 127 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698