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_ = |
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 void ImageDecoder::Start(Delegate* delegate, |
32 const std::string& image_data, | |
33 ImageCodec image_codec, | |
34 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
35 bool shrink_to_fit) { | |
36 DCHECK(task_runner); | |
37 DCHECK(delegate); | |
38 BrowserThread::PostTask( | |
39 BrowserThread::IO, FROM_HERE, | |
40 base::Bind( | |
41 &ImageDecoder::ImageDecoderImpl::DecodeImageInSandbox, | |
42 image_decoder_impl_, delegate, | |
43 std::vector<unsigned char>(image_data.begin(), image_data.end()), | |
44 image_codec, task_runner, shrink_to_fit)); | |
45 } | |
46 | |
47 void ImageDecoder::RemoveDelegate(Delegate* delegate) { | |
48 BrowserThread::PostTask( | |
dcheng
2015/03/19 14:25:46
So this is tricky.
Due to IDMap's non-thread safe
Theresa
2015/03/19 21:54:56
I could go back to using a std::map and use base::
dcheng
2015/03/20 00:14:45
Yeah, I think that's the best idea.
It might also
Theresa
2015/03/20 19:09:01
Changed to a lock protected std::map.
atm, I thin
| |
49 BrowserThread::IO, FROM_HERE, | |
50 base::Bind( | |
51 &ImageDecoder::ImageDecoderImpl::RemoveDelegate, | |
52 image_decoder_impl_, delegate)); | |
53 } | |
54 | |
55 // ImageDecoder::ImageDecoderImpl Methods | |
56 | |
57 ImageDecoder::ImageDecoderImpl::ImageDecoderImpl() | |
58 : delegate_id_counter_(0), | |
59 last_request_(base::TimeTicks::Now()), | |
60 batch_mode_started_(false) { | |
61 } | |
62 | |
63 ImageDecoder::ImageDecoderImpl::~ImageDecoderImpl() { | |
64 } | |
65 | |
66 ImageDecoder::ImageDecoderImpl::RequestInfo::RequestInfo( | |
67 Delegate* delegate, | |
68 scoped_refptr<base::SequencedTaskRunner> task_runner) | |
69 : delegate(delegate), task_runner(task_runner) { | |
70 } | |
71 | |
72 ImageDecoder::ImageDecoderImpl::RequestInfo::~RequestInfo() { | |
73 } | |
74 | |
75 void ImageDecoder::ImageDecoderImpl::DecodeImageInSandbox( | |
76 Delegate* delegate, | |
77 const std::vector<unsigned char>& image_data, | |
78 ImageCodec image_codec, | |
79 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
80 bool shrink_to_fit) { | |
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
82 if (!batch_mode_started_) { | |
83 StartBatchMode(); | |
84 } | |
85 | |
86 last_request_ = base::TimeTicks::Now(); | |
87 RequestInfo* request_info = new RequestInfo(delegate, task_runner); | |
88 request_info_id_map_.AddWithID(request_info, delegate_id_counter_); | |
dcheng
2015/03/19 14:25:46
For future reference, I think you can just use pla
Theresa
2015/03/19 21:54:56
Acknowledged. I didn't read the API closely enough
| |
89 | |
90 if (image_codec == ROBUST_JPEG_CODEC) { | |
dcheng
2015/03/19 14:25:46
Consider writing this as a switch, so if someone e
Theresa
2015/03/19 21:54:56
Done.
| |
91 utility_process_host_->Send( | |
92 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data, | |
93 delegate_id_counter_)); | |
94 } else { | |
95 utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage( | |
96 image_data, shrink_to_fit, delegate_id_counter_)); | |
97 } | |
98 | |
99 ++delegate_id_counter_; | |
100 } | |
101 | |
102 void ImageDecoder::ImageDecoderImpl::RemoveDelegate(Delegate* delegate) { | |
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
104 std::vector<int> canceled_requests; | |
105 | |
106 for (IDMap<RequestInfo>::iterator iter(&request_info_id_map_); | |
107 !iter.IsAtEnd(); iter.Advance()) { | |
108 if (iter.GetCurrentValue()->delegate == delegate) { | |
109 canceled_requests.push_back(iter.GetCurrentKey()); | |
110 } | |
111 } | |
112 | |
113 for (auto id : canceled_requests) { | |
114 RequestInfo* request_info = request_info_id_map_.Lookup(id); | |
115 request_info_id_map_.Remove(id); | |
116 delete request_info; | |
dcheng
2015/03/19 14:25:46
For future reference, IDMap has a setting that ind
Theresa
2015/03/19 21:54:56
Acknowledged.
| |
117 request_info = nullptr; | |
118 } | |
119 } | |
120 | |
121 void ImageDecoder::ImageDecoderImpl::StartBatchMode() { | |
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
123 utility_process_host_ = | |
124 UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get()) | |
125 ->AsWeakPtr(); | |
126 utility_process_host_->StartBatchMode(); | |
127 batch_mode_started_ = true; | |
128 batch_mode_timer_.Start(FROM_HERE, | |
129 kBatchModeTimeout, | |
130 this, | |
131 &ImageDecoder::ImageDecoderImpl::StopBatchMode); | |
132 } | |
133 | |
134 void ImageDecoder::ImageDecoderImpl::StopBatchMode() { | |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
136 if ((base::TimeTicks::Now() - last_request_) < kBatchModeTimeout) { | |
137 return; | |
138 } | |
139 | |
140 if (utility_process_host_) { | |
141 utility_process_host_->EndBatchMode(); | |
142 utility_process_host_.reset(); | |
143 } | |
144 batch_mode_started_ = false; | |
145 batch_mode_timer_.Stop(); | |
146 } | |
147 | |
148 bool ImageDecoder::ImageDecoderImpl::OnMessageReceived( | |
149 const IPC::Message& message) { | |
46 bool handled = true; | 150 bool handled = true; |
47 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message) | 151 IPC_BEGIN_MESSAGE_MAP(ImageDecoder::ImageDecoderImpl, message) |
48 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, | 152 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, |
49 OnDecodeImageSucceeded) | 153 OnDecodeImageSucceeded) |
50 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, | 154 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, |
51 OnDecodeImageFailed) | 155 OnDecodeImageFailed) |
52 IPC_MESSAGE_UNHANDLED(handled = false) | 156 IPC_MESSAGE_UNHANDLED(handled = false) |
53 IPC_END_MESSAGE_MAP() | 157 IPC_END_MESSAGE_MAP() |
54 return handled; | 158 return handled; |
55 } | 159 } |
56 | 160 |
57 void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) { | 161 void ImageDecoder::ImageDecoderImpl::OnDecodeImageSucceeded( |
58 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 162 const SkBitmap& decoded_image, |
59 if (delegate_) | 163 int id) { |
60 delegate_->OnImageDecoded(this, decoded_image); | 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
165 if (request_info_id_map_.Lookup(id)) { | |
166 RequestInfo* request_info = request_info_id_map_.Lookup(id); | |
167 request_info->task_runner->PostTask( | |
168 FROM_HERE, | |
169 base::Bind(&Delegate::OnImageDecoded, | |
170 base::Unretained(request_info->delegate), | |
171 decoded_image)); | |
172 | |
173 request_info_id_map_.Remove(id); | |
174 delete request_info; | |
175 request_info = nullptr; | |
176 } | |
61 } | 177 } |
62 | 178 |
63 void ImageDecoder::OnDecodeImageFailed() { | 179 void ImageDecoder::ImageDecoderImpl::OnDecodeImageFailed(int id) { |
64 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
65 if (delegate_) | 181 if (request_info_id_map_.Lookup(id)) { |
66 delegate_->OnDecodeImageFailed(this); | 182 RequestInfo* request_info = request_info_id_map_.Lookup(id); |
67 } | 183 request_info->task_runner->PostTask( |
184 FROM_HERE, | |
185 base::Bind(&Delegate::OnDecodeImageFailed, | |
186 base::Unretained(request_info->delegate))); | |
68 | 187 |
69 void ImageDecoder::DecodeImageInSandbox( | 188 request_info_id_map_.Remove(id); |
70 const std::vector<unsigned char>& image_data) { | 189 delete request_info; |
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 190 request_info = nullptr; |
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 } | 191 } |
81 } | 192 } |
OLD | NEW |