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 namespace { |
|
Lei Zhang
2015/03/25 21:44:46
nit: add a blank line after this and before the en
Theresa
2015/03/25 23:13:15
Done.
| |
| 17 const std::string& image_data, | 17 // static, Leaky to allow access from any thread. |
| 18 ImageCodec image_codec) | 18 base::LazyInstance<ImageDecoder>::Leaky g_decoder_ = LAZY_INSTANCE_INITIALIZER; |
|
Lei Zhang
2015/03/25 21:44:46
nit: just |g_decoder| since it's not a member vari
Theresa
2015/03/25 23:13:15
Done.
| |
| 19 : delegate_(delegate), | 19 } // namespace |
| 20 image_data_(image_data.begin(), image_data.end()), | 20 |
| 21 image_codec_(image_codec), | 21 ImageDecoder::ImageDecoder() |
| 22 task_runner_(NULL), | 22 : image_request_id_counter_(0), last_request_(base::TimeTicks::Now()) { |
| 23 shrink_to_fit_(false) { | 23 // A single ImageDecoder instance should live for the life of the program. |
| 24 // Explicitly add a reference so the object isn't deleted. | |
| 25 AddRef(); | |
| 24 } | 26 } |
| 25 | 27 |
| 26 ImageDecoder::ImageDecoder(Delegate* delegate, | 28 ImageDecoder::~ImageDecoder() { |
| 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 } | 29 } |
| 35 | 30 |
| 36 ImageDecoder::~ImageDecoder() {} | 31 ImageDecoder::ImageRequest::ImageRequest( |
| 37 | 32 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 38 void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) { | 33 : task_runner_(task_runner) { |
| 39 task_runner_ = task_runner; | |
| 40 BrowserThread::PostTask( | |
| 41 BrowserThread::IO, FROM_HERE, | |
| 42 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_)); | |
| 43 } | 34 } |
| 44 | 35 |
| 45 bool ImageDecoder::OnMessageReceived(const IPC::Message& message) { | 36 ImageDecoder::ImageRequest::~ImageRequest() { |
| 37 ImageDecoder::Cancel(this); | |
| 38 } | |
| 39 | |
| 40 // static | |
| 41 void ImageDecoder::Start(ImageRequest* image_request, | |
| 42 const std::string& image_data, | |
| 43 ImageCodec image_codec, | |
| 44 bool shrink_to_fit) { | |
| 45 DCHECK(image_request); | |
| 46 DCHECK(image_request->task_runner()); | |
| 47 BrowserThread::PostTask( | |
| 48 BrowserThread::IO, FROM_HERE, | |
| 49 base::Bind( | |
| 50 &ImageDecoder::DecodeImageInSandbox, | |
| 51 g_decoder_.Pointer(), image_request, | |
| 52 std::vector<unsigned char>(image_data.begin(), image_data.end()), | |
| 53 image_codec, shrink_to_fit)); | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 void ImageDecoder::Cancel(ImageRequest* image_request) { | |
| 58 DCHECK(image_request); | |
| 59 g_decoder_.Pointer()->CancelImpl(image_request); | |
| 60 } | |
| 61 | |
| 62 void ImageDecoder::DecodeImageInSandbox( | |
| 63 ImageRequest* image_request, | |
| 64 const std::vector<unsigned char>& image_data, | |
| 65 ImageCodec image_codec, | |
| 66 bool shrink_to_fit) { | |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
|
Lei Zhang
2015/03/25 21:44:46
DCHECK_CURRENTLY_ON()
Theresa
2015/03/25 23:13:15
Done.
| |
| 68 if (!utility_process_host_) { | |
| 69 StartBatchMode(); | |
| 70 } | |
| 71 | |
| 72 last_request_ = base::TimeTicks::Now(); | |
|
Lei Zhang
2015/03/25 21:44:46
If |batch_mode_timer_| is running, can't you use b
Theresa
2015/03/25 23:13:15
Yes, Reset() would effectively do the same thing.
| |
| 73 base::AutoLock lock(map_lock_); | |
| 74 image_request_id_map_.insert( | |
| 75 std::make_pair(image_request_id_counter_, image_request)); | |
| 76 | |
| 77 switch (image_codec) { | |
| 78 case ROBUST_JPEG_CODEC: | |
| 79 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( | |
| 80 image_data, image_request_id_counter_)); | |
| 81 break; | |
| 82 case DEFAULT_CODEC: | |
| 83 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | |
| 84 image_data, shrink_to_fit, image_request_id_counter_)); | |
| 85 break; | |
| 86 } | |
| 87 | |
| 88 ++image_request_id_counter_; | |
| 89 } | |
| 90 | |
| 91 void ImageDecoder::CancelImpl(ImageRequest* image_request) { | |
| 92 base::AutoLock lock(map_lock_); | |
| 93 for (auto it = image_request_id_map_.begin(); | |
| 94 it != image_request_id_map_.end();) { | |
| 95 if (it->second == image_request) { | |
| 96 image_request_id_map_.erase(it++); | |
| 97 } else { | |
| 98 ++it; | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void ImageDecoder::StartBatchMode() { | |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 105 utility_process_host_ = | |
| 106 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get()) | |
| 107 ->AsWeakPtr(); | |
| 108 utility_process_host_->StartBatchMode(); | |
| 109 batch_mode_timer_.Start( | |
| 110 FROM_HERE, base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), | |
| 111 this, &ImageDecoder::StopBatchMode); | |
| 112 } | |
| 113 | |
| 114 void ImageDecoder::StopBatchMode() { | |
| 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 116 if ((base::TimeTicks::Now() - last_request_) | |
| 117 < base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds)) { | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 if (utility_process_host_) { | |
| 122 utility_process_host_->EndBatchMode(); | |
| 123 utility_process_host_.reset(); | |
| 124 } | |
| 125 batch_mode_timer_.Stop(); | |
| 126 } | |
| 127 | |
| 128 bool ImageDecoder::OnMessageReceived( | |
| 129 const IPC::Message& message) { | |
| 46 bool handled = true; | 130 bool handled = true; |
| 47 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message) | 131 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message) |
| 48 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, | 132 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, |
| 49 OnDecodeImageSucceeded) | 133 OnDecodeImageSucceeded) |
| 50 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, | 134 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, |
| 51 OnDecodeImageFailed) | 135 OnDecodeImageFailed) |
| 52 IPC_MESSAGE_UNHANDLED(handled = false) | 136 IPC_MESSAGE_UNHANDLED(handled = false) |
| 53 IPC_END_MESSAGE_MAP() | 137 IPC_END_MESSAGE_MAP() |
| 54 return handled; | 138 return handled; |
| 55 } | 139 } |
| 56 | 140 |
| 57 void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) { | 141 void ImageDecoder::OnDecodeImageSucceeded( |
| 58 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 142 const SkBitmap& decoded_image, |
| 59 if (delegate_) | 143 int id) { |
| 60 delegate_->OnImageDecoded(this, decoded_image); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 145 base::AutoLock lock(map_lock_); | |
| 146 auto it = image_request_id_map_.find(id); | |
| 147 if (it != image_request_id_map_.end()) { | |
| 148 ImageRequest* image_request = it->second; | |
| 149 image_request->task_runner()->PostTask( | |
| 150 FROM_HERE, base::Bind(&ImageRequest::OnImageDecoded, | |
| 151 base::Unretained(image_request), decoded_image)); | |
| 152 | |
| 153 image_request_id_map_.erase(it); | |
| 154 } | |
| 61 } | 155 } |
| 62 | 156 |
| 63 void ImageDecoder::OnDecodeImageFailed() { | 157 void ImageDecoder::OnDecodeImageFailed(int id) { |
| 64 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 65 if (delegate_) | 159 base::AutoLock lock(map_lock_); |
| 66 delegate_->OnDecodeImageFailed(this); | 160 auto it = image_request_id_map_.find(id); |
| 67 } | 161 if (it != image_request_id_map_.end()) { |
| 162 ImageRequest* image_request = it->second; | |
| 163 image_request->task_runner()->PostTask( | |
| 164 FROM_HERE, base::Bind(&ImageRequest::OnDecodeImageFailed, | |
| 165 base::Unretained(image_request))); | |
| 68 | 166 |
| 69 void ImageDecoder::DecodeImageInSandbox( | 167 image_request_id_map_.erase(it); |
| 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 } | 168 } |
| 81 } | 169 } |
| OLD | NEW |