| 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 "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/common/chrome_utility_messages.h" | 10 #include "chrome/common/chrome_utility_messages.h" |
| 11 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/child_process_data.h" |
| 14 #include "content/public/browser/render_process_host.h" |
| 13 #include "content/public/browser/utility_process_host.h" | 15 #include "content/public/browser/utility_process_host.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 15 | 17 |
| 16 using content::BrowserThread; | 18 using content::BrowserThread; |
| 17 using content::UtilityProcessHost; | 19 using content::UtilityProcessHost; |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 // static, Leaky to allow access from any thread. | 23 // static, Leaky to allow access from any thread. |
| 22 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; | 24 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 47 : task_runner_(task_runner) { | 49 : task_runner_(task_runner) { |
| 48 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 50 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 49 } | 51 } |
| 50 | 52 |
| 51 ImageDecoder::ImageRequest::~ImageRequest() { | 53 ImageDecoder::ImageRequest::~ImageRequest() { |
| 52 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 54 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 53 ImageDecoder::Cancel(this); | 55 ImageDecoder::Cancel(this); |
| 54 } | 56 } |
| 55 | 57 |
| 56 // static | 58 // static |
| 59 void ImageDecoder::StartByFilePath( |
| 60 ImageRequest* image_request, const base::FilePath& image_file_path) { |
| 61 g_decoder.Pointer()->StartWithOptionsImpl(image_request, |
| 62 image_file_path, std::string(), |
| 63 DEFAULT_CODEC, false); |
| 64 } |
| 65 |
| 66 // static |
| 57 void ImageDecoder::Start(ImageRequest* image_request, | 67 void ImageDecoder::Start(ImageRequest* image_request, |
| 58 const std::string& image_data) { | 68 const std::string& image_data) { |
| 59 StartWithOptions(image_request, image_data, DEFAULT_CODEC, false); | 69 StartWithOptions(image_request, image_data, DEFAULT_CODEC, false); |
| 60 } | 70 } |
| 61 | 71 |
| 62 // static | 72 // static |
| 63 void ImageDecoder::StartWithOptions(ImageRequest* image_request, | 73 void ImageDecoder::StartWithOptions(ImageRequest* image_request, |
| 64 const std::string& image_data, | 74 const std::string& image_data, |
| 65 ImageCodec image_codec, | 75 ImageCodec image_codec, |
| 66 bool shrink_to_fit) { | 76 bool shrink_to_fit) { |
| 67 g_decoder.Pointer()->StartWithOptionsImpl(image_request, image_data, | 77 g_decoder.Pointer()->StartWithOptionsImpl(image_request, |
| 78 base::FilePath(), image_data, |
| 68 image_codec, shrink_to_fit); | 79 image_codec, shrink_to_fit); |
| 69 } | 80 } |
| 70 | 81 |
| 71 void ImageDecoder::StartWithOptionsImpl(ImageRequest* image_request, | 82 void ImageDecoder::StartWithOptionsImpl(ImageRequest* image_request, |
| 83 const base::FilePath& image_file_path, |
| 72 const std::string& image_data, | 84 const std::string& image_data, |
| 73 ImageCodec image_codec, | 85 ImageCodec image_codec, |
| 74 bool shrink_to_fit) { | 86 bool shrink_to_fit) { |
| 75 DCHECK(image_request); | 87 DCHECK(image_request); |
| 76 DCHECK(image_request->task_runner()); | 88 DCHECK(image_request->task_runner()); |
| 77 | 89 |
| 90 base::File image_file; |
| 91 std::vector<unsigned char> image_data_vec; |
| 92 if (!image_file_path.empty()) { |
| 93 image_file.Initialize(image_file_path, |
| 94 base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 95 } else { |
| 96 image_data_vec.assign(image_data.begin(), image_data.end()); |
| 97 } |
| 98 |
| 78 int request_id; | 99 int request_id; |
| 79 { | 100 { |
| 80 base::AutoLock lock(map_lock_); | 101 base::AutoLock lock(map_lock_); |
| 81 request_id = image_request_id_counter_++; | 102 request_id = image_request_id_counter_++; |
| 82 image_request_id_map_.insert(std::make_pair(request_id, image_request)); | 103 image_request_id_map_.insert(std::make_pair(request_id, image_request)); |
| 83 } | 104 } |
| 84 | 105 |
| 85 BrowserThread::PostTask( | 106 BrowserThread::PostTask( |
| 86 BrowserThread::IO, FROM_HERE, | 107 BrowserThread::IO, FROM_HERE, |
| 87 base::Bind( | 108 base::Bind( |
| 88 &ImageDecoder::DecodeImageInSandbox, | 109 &ImageDecoder::DecodeImageInSandbox, |
| 89 g_decoder.Pointer(), request_id, | 110 g_decoder.Pointer(), request_id, |
| 90 std::vector<unsigned char>(image_data.begin(), image_data.end()), | 111 base::Passed(image_file.Pass()), |
| 91 image_codec, shrink_to_fit)); | 112 image_data_vec, image_codec, shrink_to_fit)); |
| 92 } | 113 } |
| 93 | 114 |
| 94 // static | 115 // static |
| 95 void ImageDecoder::Cancel(ImageRequest* image_request) { | 116 void ImageDecoder::Cancel(ImageRequest* image_request) { |
| 96 DCHECK(image_request); | 117 DCHECK(image_request); |
| 97 g_decoder.Pointer()->CancelImpl(image_request); | 118 g_decoder.Pointer()->CancelImpl(image_request); |
| 98 } | 119 } |
| 99 | 120 |
| 100 void ImageDecoder::DecodeImageInSandbox( | 121 void ImageDecoder::DecodeImageInSandbox( |
| 101 int request_id, | 122 int request_id, |
| 123 base::File image_file, |
| 102 const std::vector<unsigned char>& image_data, | 124 const std::vector<unsigned char>& image_data, |
| 103 ImageCodec image_codec, | 125 ImageCodec image_codec, |
| 104 bool shrink_to_fit) { | 126 bool shrink_to_fit) { |
| 105 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 106 base::AutoLock lock(map_lock_); | 128 base::AutoLock lock(map_lock_); |
| 107 const auto it = image_request_id_map_.find(request_id); | 129 const auto it = image_request_id_map_.find(request_id); |
| 108 if (it == image_request_id_map_.end()) | 130 if (it == image_request_id_map_.end()) |
| 109 return; | 131 return; |
| 110 | 132 |
| 111 ImageRequest* image_request = it->second; | 133 ImageRequest* image_request = it->second; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 126 if (!batch_mode_timer_) { | 148 if (!batch_mode_timer_) { |
| 127 // Created here so it will call StopBatchMode() on the right thread. | 149 // Created here so it will call StopBatchMode() on the right thread. |
| 128 batch_mode_timer_.reset(new base::DelayTimer<ImageDecoder>( | 150 batch_mode_timer_.reset(new base::DelayTimer<ImageDecoder>( |
| 129 FROM_HERE, | 151 FROM_HERE, |
| 130 base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), | 152 base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), |
| 131 this, | 153 this, |
| 132 &ImageDecoder::StopBatchMode)); | 154 &ImageDecoder::StopBatchMode)); |
| 133 } | 155 } |
| 134 batch_mode_timer_->Reset(); | 156 batch_mode_timer_->Reset(); |
| 135 | 157 |
| 136 switch (image_codec) { | 158 if (!image_data.empty()) { |
| 159 switch (image_codec) { |
| 137 #if defined(OS_CHROMEOS) | 160 #if defined(OS_CHROMEOS) |
| 138 case ROBUST_JPEG_CODEC: | 161 case ROBUST_JPEG_CODEC: |
| 139 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( | 162 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( |
| 140 image_data, request_id)); | 163 image_data, request_id)); |
| 141 break; | 164 break; |
| 142 #endif // defined(OS_CHROMEOS) | 165 #endif // defined(OS_CHROMEOS) |
| 143 case DEFAULT_CODEC: | 166 case DEFAULT_CODEC: |
| 144 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | 167 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( |
| 145 image_data, shrink_to_fit, request_id)); | 168 image_data, shrink_to_fit, request_id)); |
| 146 break; | 169 break; |
| 170 } |
| 171 } else if (image_file.IsValid() && image_codec == DEFAULT_CODEC) { |
| 172 base::ProcessHandle utility_process = |
| 173 content::RenderProcessHost::run_renderer_in_process() ? |
| 174 base::GetCurrentProcessHandle() : |
| 175 utility_process_host_->GetData().handle; |
| 176 |
| 177 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImageByHandle( |
| 178 IPC::TakeFileHandleForProcess(image_file.Pass(), utility_process), |
| 179 shrink_to_fit, request_id)); |
| 180 } else { |
| 181 image_request->task_runner()->PostTask( |
| 182 FROM_HERE, |
| 183 base::Bind(&ImageDecoder::RunOnDecodeImageFailed, this, request_id)); |
| 147 } | 184 } |
| 148 } | 185 } |
| 149 | 186 |
| 150 void ImageDecoder::CancelImpl(ImageRequest* image_request) { | 187 void ImageDecoder::CancelImpl(ImageRequest* image_request) { |
| 151 base::AutoLock lock(map_lock_); | 188 base::AutoLock lock(map_lock_); |
| 152 for (auto it = image_request_id_map_.begin(); | 189 for (auto it = image_request_id_map_.begin(); |
| 153 it != image_request_id_map_.end();) { | 190 it != image_request_id_map_.end();) { |
| 154 if (it->second == image_request) { | 191 if (it->second == image_request) { |
| 155 image_request_id_map_.erase(it++); | 192 image_request_id_map_.erase(it++); |
| 156 } else { | 193 } else { |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 auto it = image_request_id_map_.find(request_id); | 320 auto it = image_request_id_map_.find(request_id); |
| 284 if (it == image_request_id_map_.end()) | 321 if (it == image_request_id_map_.end()) |
| 285 return; | 322 return; |
| 286 image_request = it->second; | 323 image_request = it->second; |
| 287 image_request_id_map_.erase(it); | 324 image_request_id_map_.erase(it); |
| 288 } | 325 } |
| 289 | 326 |
| 290 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); | 327 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); |
| 291 image_request->OnDecodeImageFailed(); | 328 image_request->OnDecodeImageFailed(); |
| 292 } | 329 } |
| OLD | NEW |