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 ImageDecoder::ImageDecoder(Delegate* delegate, | 16 // static, Leaky to allow access from any thread. |
| 17 const std::string& image_data, | 17 base::LazyInstance<ImageDecoder>::Leaky image_decoder_instance_ = |
|
dcheng
2015/03/23 11:52:09
You /could/ make this a base::LazyInstance<scoped_
Theresa
2015/03/23 17:33:10
I'm not sure that a reference needs to be leaked.
dcheng
2015/03/24 05:18:18
To clarify, I'm proposing that we eliminate the Im
Theresa
2015/03/24 23:51:42
Done. Thank you for the clarification :) I should
dcheng
2015/03/25 14:10:09
Yep, that's the best way.
| |
| 18 ImageCodec image_codec) | 18 LAZY_INSTANCE_INITIALIZER; |
| 19 : delegate_(delegate), | 19 |
| 20 image_data_(image_data.begin(), image_data.end()), | 20 // static |
| 21 image_codec_(image_codec), | 21 ImageDecoder* ImageDecoder::GetInstance() { |
| 22 task_runner_(NULL), | 22 return image_decoder_instance_.Pointer(); |
| 23 shrink_to_fit_(false) { | |
| 24 } | 23 } |
| 25 | 24 |
| 26 ImageDecoder::ImageDecoder(Delegate* delegate, | 25 ImageDecoder::ImageDecoder() : image_decoder_impl_(new ImageDecoderImpl()) { |
| 27 const std::vector<char>& image_data, | |
| 28 ImageCodec image_codec) | |
| 29 : delegate_(delegate), | |
| 30 image_data_(image_data.begin(), image_data.end()), | |
| 31 image_codec_(image_codec), | |
| 32 task_runner_(NULL), | |
| 33 shrink_to_fit_(false) { | |
| 34 } | 26 } |
| 35 | 27 |
| 36 ImageDecoder::~ImageDecoder() {} | 28 ImageDecoder::~ImageDecoder() { |
| 37 | |
| 38 void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) { | |
| 39 task_runner_ = task_runner; | |
| 40 BrowserThread::PostTask( | |
| 41 BrowserThread::IO, FROM_HERE, | |
| 42 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_)); | |
| 43 } | 29 } |
| 44 | 30 |
| 45 bool ImageDecoder::OnMessageReceived(const IPC::Message& message) { | 31 ImageDecoder::ImageRequest::ImageRequest( |
| 32 const scoped_refptr<base::SequencedTaskRunner>& t) | |
| 33 : t(t) { | |
| 34 } | |
| 35 | |
| 36 ImageDecoder::ImageRequest::~ImageRequest() { | |
| 37 ImageDecoder::Cancel(this); | |
| 38 } | |
| 39 | |
| 40 void ImageDecoder::Start(ImageRequest* image_request, | |
| 41 const std::string& image_data, | |
| 42 ImageCodec image_codec, | |
| 43 bool shrink_to_fit) { | |
| 44 DCHECK(image_request); | |
| 45 DCHECK(image_request->task_runner()); | |
| 46 BrowserThread::PostTask( | |
| 47 BrowserThread::IO, FROM_HERE, | |
| 48 base::Bind( | |
| 49 &ImageDecoder::ImageDecoderImpl::DecodeImageInSandbox, | |
| 50 GetInstance()->image_decoder_impl_, image_request, | |
| 51 std::vector<unsigned char>(image_data.begin(), image_data.end()), | |
| 52 image_codec, shrink_to_fit)); | |
| 53 } | |
| 54 | |
| 55 void ImageDecoder::Cancel(ImageRequest* image_request) { | |
| 56 DCHECK(image_request); | |
| 57 GetInstance()->image_decoder_impl_->Cancel(image_request); | |
| 58 } | |
| 59 | |
| 60 // ImageDecoder::ImageDecoderImpl Methods | |
| 61 | |
| 62 ImageDecoder::ImageDecoderImpl::ImageDecoderImpl() | |
| 63 : image_request_id_counter_(0), | |
| 64 last_request_(base::TimeTicks::Now()), | |
| 65 batch_mode_started_(false) { | |
| 66 } | |
| 67 | |
| 68 ImageDecoder::ImageDecoderImpl::~ImageDecoderImpl() { | |
| 69 } | |
| 70 | |
| 71 void ImageDecoder::ImageDecoderImpl::DecodeImageInSandbox( | |
| 72 ImageRequest* image_request, | |
| 73 const std::vector<unsigned char>& image_data, | |
| 74 ImageCodec image_codec, | |
| 75 bool shrink_to_fit) { | |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 77 if (!batch_mode_started_) { | |
|
dcheng
2015/03/23 11:52:09
Can we just check that utility_process_host_ is no
Theresa
2015/03/23 17:33:10
Done.
| |
| 78 StartBatchMode(); | |
| 79 } | |
| 80 | |
| 81 last_request_ = base::TimeTicks::Now(); | |
| 82 base::AutoLock lock(map_lock_); | |
| 83 image_request_id_map_.insert( | |
| 84 std::pair<int, ImageRequest*>(image_request_id_counter_, image_request)); | |
|
dcheng
2015/03/23 11:52:09
Use std::make_pair so you don't explicitly need to
Theresa
2015/03/23 17:33:10
Done.
| |
| 85 | |
| 86 switch (image_codec) { | |
| 87 case ROBUST_JPEG_CODEC: | |
| 88 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( | |
| 89 image_data, image_request_id_counter_)); | |
| 90 break; | |
| 91 case DEFAULT_CODEC: | |
| 92 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | |
| 93 image_data, shrink_to_fit, image_request_id_counter_)); | |
| 94 break; | |
| 95 default: | |
|
dcheng
2015/03/23 11:52:09
You should be OK omitting this; adding a new enum
Theresa
2015/03/23 17:33:10
Done.
| |
| 96 NOTREACHED(); | |
| 97 } | |
| 98 | |
| 99 ++image_request_id_counter_; | |
| 100 } | |
| 101 | |
| 102 void ImageDecoder::ImageDecoderImpl::Cancel(ImageRequest* image_request) { | |
| 103 base::AutoLock lock(map_lock_); | |
| 104 std::vector<int> canceled_requests; | |
| 105 | |
| 106 for (RequestMap::iterator it = image_request_id_map_.begin(); | |
|
dcheng
2015/03/23 11:52:09
Use a foreach here to save some typing as well.
A
Theresa
2015/03/23 17:33:10
Done.
| |
| 107 it != image_request_id_map_.end(); it++) { | |
| 108 if (it->second == image_request) { | |
| 109 canceled_requests.push_back(it->first); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 for (auto id : canceled_requests) { | |
| 114 image_request_id_map_.erase(id); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 void ImageDecoder::ImageDecoderImpl::StartBatchMode() { | |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 120 utility_process_host_ = | |
| 121 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get()) | |
| 122 ->AsWeakPtr(); | |
| 123 utility_process_host_->StartBatchMode(); | |
| 124 batch_mode_started_ = true; | |
| 125 batch_mode_timer_.Start(FROM_HERE, kBatchModeTimeout, this, | |
| 126 &ImageDecoder::ImageDecoderImpl::StopBatchMode); | |
| 127 } | |
| 128 | |
| 129 void ImageDecoder::ImageDecoderImpl::StopBatchMode() { | |
| 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 131 if ((base::TimeTicks::Now() - last_request_) < kBatchModeTimeout) { | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 if (utility_process_host_) { | |
| 136 utility_process_host_->EndBatchMode(); | |
| 137 utility_process_host_.reset(); | |
| 138 } | |
| 139 batch_mode_started_ = false; | |
| 140 batch_mode_timer_.Stop(); | |
| 141 } | |
| 142 | |
| 143 bool ImageDecoder::ImageDecoderImpl::OnMessageReceived( | |
| 144 const IPC::Message& message) { | |
| 46 bool handled = true; | 145 bool handled = true; |
| 47 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message) | 146 IPC_BEGIN_MESSAGE_MAP(ImageDecoder::ImageDecoderImpl, message) |
| 48 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, | 147 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, |
| 49 OnDecodeImageSucceeded) | 148 OnDecodeImageSucceeded) |
| 50 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, | 149 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, |
| 51 OnDecodeImageFailed) | 150 OnDecodeImageFailed) |
| 52 IPC_MESSAGE_UNHANDLED(handled = false) | 151 IPC_MESSAGE_UNHANDLED(handled = false) |
| 53 IPC_END_MESSAGE_MAP() | 152 IPC_END_MESSAGE_MAP() |
| 54 return handled; | 153 return handled; |
| 55 } | 154 } |
| 56 | 155 |
| 57 void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) { | 156 void ImageDecoder::ImageDecoderImpl::OnDecodeImageSucceeded( |
| 58 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 157 const SkBitmap& decoded_image, |
| 59 if (delegate_) | 158 int id) { |
| 60 delegate_->OnImageDecoded(this, decoded_image); | 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 160 base::AutoLock lock(map_lock_); | |
| 161 if (image_request_id_map_.find(id) != image_request_id_map_.end()) { | |
|
dcheng
2015/03/23 11:52:09
It's a pretty common idiom to do this:
auto it = i
Theresa
2015/03/23 17:33:10
Done.
| |
| 162 ImageRequest* image_request = image_request_id_map_.find(id)->second; | |
| 163 image_request->task_runner()->PostTask( | |
| 164 FROM_HERE, base::Bind(&ImageRequest::OnImageDecoded, | |
| 165 base::Unretained(image_request), decoded_image)); | |
| 166 | |
| 167 image_request_id_map_.erase(id); | |
| 168 } | |
| 61 } | 169 } |
| 62 | 170 |
| 63 void ImageDecoder::OnDecodeImageFailed() { | 171 void ImageDecoder::ImageDecoderImpl::OnDecodeImageFailed(int id) { |
| 64 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 65 if (delegate_) | 173 base::AutoLock lock(map_lock_); |
| 66 delegate_->OnDecodeImageFailed(this); | 174 if (image_request_id_map_.find(id) != image_request_id_map_.end()) { |
| 67 } | 175 ImageRequest* image_request = image_request_id_map_.find(id)->second; |
| 176 image_request->task_runner()->PostTask( | |
| 177 FROM_HERE, base::Bind(&ImageRequest::OnDecodeImageFailed, | |
| 178 base::Unretained(image_request))); | |
| 68 | 179 |
| 69 void ImageDecoder::DecodeImageInSandbox( | 180 image_request_id_map_.erase(id); |
| 70 const std::vector<unsigned char>& image_data) { | |
| 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 72 UtilityProcessHost* utility_process_host; | |
| 73 utility_process_host = UtilityProcessHost::Create(this, task_runner_.get()); | |
| 74 if (image_codec_ == ROBUST_JPEG_CODEC) { | |
| 75 utility_process_host->Send( | |
| 76 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data)); | |
| 77 } else { | |
| 78 utility_process_host->Send( | |
| 79 new ChromeUtilityMsg_DecodeImage(image_data, shrink_to_fit_)); | |
| 80 } | 181 } |
| 81 } | 182 } |
| OLD | NEW |