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