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 "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 25 matching lines...) Expand all Loading... | |
| 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 |
| 57 void ImageDecoder::Start(ImageRequest* image_request, | 59 void ImageDecoder::Start(ImageRequest* image_request, |
| 60 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 | |
| 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 if (!image_file_path.empty()) { | |
| 92 image_file.Initialize(image_file_path, | |
| 93 base::File::FLAG_OPEN | base::File::FLAG_READ); | |
|
asanka
2015/06/11 03:30:51
Indentation is off.
yoshiki
2015/06/11 08:22:09
Done.
| |
| 94 } | |
| 95 | |
| 78 int request_id; | 96 int request_id; |
| 79 { | 97 { |
| 80 base::AutoLock lock(map_lock_); | 98 base::AutoLock lock(map_lock_); |
| 81 request_id = image_request_id_counter_++; | 99 request_id = image_request_id_counter_++; |
| 82 image_request_id_map_.insert(std::make_pair(request_id, image_request)); | 100 image_request_id_map_.insert(std::make_pair(request_id, image_request)); |
| 83 } | 101 } |
| 84 | 102 |
| 85 BrowserThread::PostTask( | 103 BrowserThread::PostTask( |
| 86 BrowserThread::IO, FROM_HERE, | 104 BrowserThread::IO, FROM_HERE, |
| 87 base::Bind( | 105 base::Bind( |
| 88 &ImageDecoder::DecodeImageInSandbox, | 106 &ImageDecoder::DecodeImageInSandbox, |
| 89 g_decoder.Pointer(), request_id, | 107 g_decoder.Pointer(), request_id, |
| 108 base::Passed(image_file.Pass()), | |
| 90 std::vector<unsigned char>(image_data.begin(), image_data.end()), | 109 std::vector<unsigned char>(image_data.begin(), image_data.end()), |
| 91 image_codec, shrink_to_fit)); | 110 image_codec, shrink_to_fit)); |
| 92 } | 111 } |
| 93 | 112 |
| 94 // static | 113 // static |
| 95 void ImageDecoder::Cancel(ImageRequest* image_request) { | 114 void ImageDecoder::Cancel(ImageRequest* image_request) { |
| 96 DCHECK(image_request); | 115 DCHECK(image_request); |
| 97 g_decoder.Pointer()->CancelImpl(image_request); | 116 g_decoder.Pointer()->CancelImpl(image_request); |
| 98 } | 117 } |
| 99 | 118 |
| 100 void ImageDecoder::DecodeImageInSandbox( | 119 void ImageDecoder::DecodeImageInSandbox( |
| 101 int request_id, | 120 int request_id, |
| 121 base::File image_file, | |
| 102 const std::vector<unsigned char>& image_data, | 122 const std::vector<unsigned char>& image_data, |
| 103 ImageCodec image_codec, | 123 ImageCodec image_codec, |
| 104 bool shrink_to_fit) { | 124 bool shrink_to_fit) { |
| 105 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 106 base::AutoLock lock(map_lock_); | 126 base::AutoLock lock(map_lock_); |
| 107 const auto it = image_request_id_map_.find(request_id); | 127 const auto it = image_request_id_map_.find(request_id); |
| 108 if (it == image_request_id_map_.end()) | 128 if (it == image_request_id_map_.end()) |
| 109 return; | 129 return; |
| 110 | 130 |
| 111 ImageRequest* image_request = it->second; | 131 ImageRequest* image_request = it->second; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 126 if (!batch_mode_timer_) { | 146 if (!batch_mode_timer_) { |
| 127 // Created here so it will call StopBatchMode() on the right thread. | 147 // Created here so it will call StopBatchMode() on the right thread. |
| 128 batch_mode_timer_.reset(new base::DelayTimer<ImageDecoder>( | 148 batch_mode_timer_.reset(new base::DelayTimer<ImageDecoder>( |
| 129 FROM_HERE, | 149 FROM_HERE, |
| 130 base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), | 150 base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), |
| 131 this, | 151 this, |
| 132 &ImageDecoder::StopBatchMode)); | 152 &ImageDecoder::StopBatchMode)); |
| 133 } | 153 } |
| 134 batch_mode_timer_->Reset(); | 154 batch_mode_timer_->Reset(); |
| 135 | 155 |
| 136 switch (image_codec) { | 156 if (!image_data.empty()) { |
| 157 switch (image_codec) { | |
| 137 #if defined(OS_CHROMEOS) | 158 #if defined(OS_CHROMEOS) |
| 138 case ROBUST_JPEG_CODEC: | 159 case ROBUST_JPEG_CODEC: |
| 139 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( | 160 utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage( |
| 140 image_data, request_id)); | 161 image_data, request_id)); |
| 141 break; | 162 break; |
| 142 #endif // defined(OS_CHROMEOS) | 163 #endif // defined(OS_CHROMEOS) |
| 143 case DEFAULT_CODEC: | 164 case DEFAULT_CODEC: |
| 144 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | 165 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( |
| 145 image_data, shrink_to_fit, request_id)); | 166 image_data, shrink_to_fit, request_id)); |
| 146 break; | 167 break; |
| 168 } | |
| 169 } else if (image_file.IsValid() && image_codec == DEFAULT_CODEC) { | |
| 170 base::ProcessHandle utility_process = | |
| 171 content::RenderProcessHost::run_renderer_in_process() ? | |
| 172 base::GetCurrentProcessHandle() : | |
| 173 utility_process_host_->GetData().handle; | |
| 174 | |
| 175 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImageByHandle( | |
| 176 IPC::TakeFileHandleForProcess(image_file.Pass(), utility_process), | |
| 177 shrink_to_fit, request_id)); | |
| 178 } else { | |
| 179 image_request->task_runner()->PostTask( | |
| 180 FROM_HERE, | |
| 181 base::Bind(&ImageDecoder::RunOnDecodeImageFailed, this, request_id)); | |
| 147 } | 182 } |
| 148 } | 183 } |
| 149 | 184 |
| 150 void ImageDecoder::CancelImpl(ImageRequest* image_request) { | 185 void ImageDecoder::CancelImpl(ImageRequest* image_request) { |
| 151 base::AutoLock lock(map_lock_); | 186 base::AutoLock lock(map_lock_); |
| 152 for (auto it = image_request_id_map_.begin(); | 187 for (auto it = image_request_id_map_.begin(); |
| 153 it != image_request_id_map_.end();) { | 188 it != image_request_id_map_.end();) { |
| 154 if (it->second == image_request) { | 189 if (it->second == image_request) { |
| 155 image_request_id_map_.erase(it++); | 190 image_request_id_map_.erase(it++); |
| 156 } else { | 191 } else { |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 auto it = image_request_id_map_.find(request_id); | 318 auto it = image_request_id_map_.find(request_id); |
| 284 if (it == image_request_id_map_.end()) | 319 if (it == image_request_id_map_.end()) |
| 285 return; | 320 return; |
| 286 image_request = it->second; | 321 image_request = it->second; |
| 287 image_request_id_map_.erase(it); | 322 image_request_id_map_.erase(it); |
| 288 } | 323 } |
| 289 | 324 |
| 290 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); | 325 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); |
| 291 image_request->OnDecodeImageFailed(); | 326 image_request->OnDecodeImageFailed(); |
| 292 } | 327 } |
| OLD | NEW |