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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/lazy_instance.h" | |
12 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
13 #include "build/build_config.h" | 12 #include "build/build_config.h" |
14 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/common/service_manager_connection.h" | 14 #include "content/public/common/service_manager_connection.h" |
16 #include "ipc/ipc_channel.h" | 15 #include "ipc/ipc_channel.h" |
17 #include "services/image_decoder/public/cpp/decode.h" | 16 #include "services/image_decoder/public/cpp/decode.h" |
18 #include "services/service_manager/public/cpp/connector.h" | 17 #include "services/service_manager/public/cpp/connector.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
20 | 19 |
21 namespace { | 20 namespace { |
22 | 21 |
23 // static, Leaky to allow access from any thread. | |
24 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; | |
25 | |
26 const int64_t kMaxImageSizeInBytes = | 22 const int64_t kMaxImageSizeInBytes = |
27 static_cast<int64_t>(IPC::Channel::kMaximumMessageSize); | 23 static_cast<int64_t>(IPC::Channel::kMaximumMessageSize); |
28 | 24 |
29 // Note that this is always called on the thread which initiated the | 25 // Note that this is always called on the thread which initiated the |
30 // corresponding image_decoder::Decode request. | 26 // corresponding image_decoder::Decode request. |
31 void OnDecodeImageDone( | 27 void OnDecodeImageDone( |
32 base::Callback<void(int)> fail_callback, | 28 base::Callback<void(int)> fail_callback, |
33 base::Callback<void(const SkBitmap&, int)> success_callback, | 29 base::Callback<void(const SkBitmap&, int)> success_callback, |
34 int request_id, | 30 int request_id, |
35 const SkBitmap& image) { | 31 const SkBitmap& image) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 84 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
89 : task_runner_(task_runner) { | 85 : task_runner_(task_runner) { |
90 DCHECK(sequence_checker_.CalledOnValidSequence()); | 86 DCHECK(sequence_checker_.CalledOnValidSequence()); |
91 } | 87 } |
92 | 88 |
93 ImageDecoder::ImageRequest::~ImageRequest() { | 89 ImageDecoder::ImageRequest::~ImageRequest() { |
94 DCHECK(sequence_checker_.CalledOnValidSequence()); | 90 DCHECK(sequence_checker_.CalledOnValidSequence()); |
95 ImageDecoder::Cancel(this); | 91 ImageDecoder::Cancel(this); |
96 } | 92 } |
97 | 93 |
98 ImageDecoder::ImageDecoder() : image_request_id_counter_(0) {} | 94 // static |
99 | 95 ImageDecoder* ImageDecoder::GetInstance() { |
100 ImageDecoder::~ImageDecoder() {} | 96 static auto image_decoder = new ImageDecoder(); |
Nico
2017/02/02 22:13:19
style guide says "auto*" for pointers
scottmg
2017/02/04 01:44:32
Huh, I didn't know that. I have some places to fix
scottmg
2017/02/06 20:46:29
[I was going to make a CL with a link to the style
| |
97 return image_decoder; | |
98 } | |
101 | 99 |
102 // static | 100 // static |
103 void ImageDecoder::Start(ImageRequest* image_request, | 101 void ImageDecoder::Start(ImageRequest* image_request, |
104 std::vector<uint8_t> image_data) { | 102 std::vector<uint8_t> image_data) { |
105 StartWithOptions(image_request, std::move(image_data), DEFAULT_CODEC, false); | 103 StartWithOptions(image_request, std::move(image_data), DEFAULT_CODEC, false); |
106 } | 104 } |
107 | 105 |
108 // static | 106 // static |
109 void ImageDecoder::Start(ImageRequest* image_request, | 107 void ImageDecoder::Start(ImageRequest* image_request, |
110 const std::string& image_data) { | 108 const std::string& image_data) { |
111 Start(image_request, | 109 Start(image_request, |
112 std::vector<uint8_t>(image_data.begin(), image_data.end())); | 110 std::vector<uint8_t>(image_data.begin(), image_data.end())); |
113 } | 111 } |
114 | 112 |
115 // static | 113 // static |
116 void ImageDecoder::StartWithOptions(ImageRequest* image_request, | 114 void ImageDecoder::StartWithOptions(ImageRequest* image_request, |
117 std::vector<uint8_t> image_data, | 115 std::vector<uint8_t> image_data, |
118 ImageCodec image_codec, | 116 ImageCodec image_codec, |
119 bool shrink_to_fit) { | 117 bool shrink_to_fit) { |
120 g_decoder.Get().StartWithOptionsImpl(image_request, std::move(image_data), | 118 ImageDecoder::GetInstance()->StartWithOptionsImpl( |
121 image_codec, shrink_to_fit); | 119 image_request, std::move(image_data), image_codec, shrink_to_fit); |
122 } | 120 } |
123 | 121 |
124 // static | 122 // static |
125 void ImageDecoder::StartWithOptions(ImageRequest* image_request, | 123 void ImageDecoder::StartWithOptions(ImageRequest* image_request, |
126 const std::string& image_data, | 124 const std::string& image_data, |
127 ImageCodec image_codec, | 125 ImageCodec image_codec, |
128 bool shrink_to_fit) { | 126 bool shrink_to_fit) { |
129 StartWithOptions(image_request, | 127 StartWithOptions(image_request, |
130 std::vector<uint8_t>(image_data.begin(), image_data.end()), | 128 std::vector<uint8_t>(image_data.begin(), image_data.end()), |
131 image_codec, shrink_to_fit); | 129 image_codec, shrink_to_fit); |
132 } | 130 } |
133 | 131 |
132 ImageDecoder::ImageDecoder() : image_request_id_counter_(0) {} | |
133 | |
134 void ImageDecoder::StartWithOptionsImpl(ImageRequest* image_request, | 134 void ImageDecoder::StartWithOptionsImpl(ImageRequest* image_request, |
135 std::vector<uint8_t> image_data, | 135 std::vector<uint8_t> image_data, |
136 ImageCodec image_codec, | 136 ImageCodec image_codec, |
137 bool shrink_to_fit) { | 137 bool shrink_to_fit) { |
138 DCHECK(image_request); | 138 DCHECK(image_request); |
139 DCHECK(image_request->task_runner()); | 139 DCHECK(image_request->task_runner()); |
140 | 140 |
141 int request_id; | 141 int request_id; |
142 { | 142 { |
143 base::AutoLock lock(map_lock_); | 143 base::AutoLock lock(map_lock_); |
(...skipping 22 matching lines...) Expand all Loading... | |
166 // implementation. | 166 // implementation. |
167 content::BrowserThread::PostTask( | 167 content::BrowserThread::PostTask( |
168 content::BrowserThread::IO, FROM_HERE, | 168 content::BrowserThread::IO, FROM_HERE, |
169 base::Bind(&DecodeImage, base::Passed(&image_data), codec, shrink_to_fit, | 169 base::Bind(&DecodeImage, base::Passed(&image_data), codec, shrink_to_fit, |
170 callback, make_scoped_refptr(image_request->task_runner()))); | 170 callback, make_scoped_refptr(image_request->task_runner()))); |
171 } | 171 } |
172 | 172 |
173 // static | 173 // static |
174 void ImageDecoder::Cancel(ImageRequest* image_request) { | 174 void ImageDecoder::Cancel(ImageRequest* image_request) { |
175 DCHECK(image_request); | 175 DCHECK(image_request); |
176 g_decoder.Get().CancelImpl(image_request); | 176 ImageDecoder::GetInstance()->CancelImpl(image_request); |
177 } | 177 } |
178 | 178 |
179 void ImageDecoder::CancelImpl(ImageRequest* image_request) { | 179 void ImageDecoder::CancelImpl(ImageRequest* image_request) { |
180 base::AutoLock lock(map_lock_); | 180 base::AutoLock lock(map_lock_); |
181 for (auto it = image_request_id_map_.begin(); | 181 for (auto it = image_request_id_map_.begin(); |
182 it != image_request_id_map_.end();) { | 182 it != image_request_id_map_.end();) { |
183 if (it->second == image_request) { | 183 if (it->second == image_request) { |
184 image_request_id_map_.erase(it++); | 184 image_request_id_map_.erase(it++); |
185 } else { | 185 } else { |
186 ++it; | 186 ++it; |
(...skipping 25 matching lines...) Expand all Loading... | |
212 auto it = image_request_id_map_.find(request_id); | 212 auto it = image_request_id_map_.find(request_id); |
213 if (it == image_request_id_map_.end()) | 213 if (it == image_request_id_map_.end()) |
214 return; | 214 return; |
215 image_request = it->second; | 215 image_request = it->second; |
216 image_request_id_map_.erase(it); | 216 image_request_id_map_.erase(it); |
217 } | 217 } |
218 | 218 |
219 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); | 219 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); |
220 image_request->OnDecodeImageFailed(); | 220 image_request->OnDecodeImageFailed(); |
221 } | 221 } |
OLD | NEW |