OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "u2f_hid_device.h" | 5 #include "u2f_hid_device.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "crypto/random.h" | 10 #include "crypto/random.h" |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 | 190 |
191 connection_->Read(base::Bind(&U2fHidDevice::OnRead, | 191 connection_->Read(base::Bind(&U2fHidDevice::OnRead, |
192 weak_factory_.GetWeakPtr(), | 192 weak_factory_.GetWeakPtr(), |
193 base::Passed(&callback))); | 193 base::Passed(&callback))); |
194 } | 194 } |
195 | 195 |
196 void U2fHidDevice::OnRead(U2fHidMessageCallback callback, | 196 void U2fHidDevice::OnRead(U2fHidMessageCallback callback, |
197 bool success, | 197 bool success, |
198 scoped_refptr<net::IOBuffer> buf, | 198 scoped_refptr<net::IOBuffer> buf, |
199 size_t size) { | 199 size_t size) { |
200 if (!success) { | 200 if (!success || !buf) { |
201 std::move(callback).Run(success, nullptr); | 201 std::move(callback).Run(success, nullptr); |
202 return; | 202 return; |
203 } | 203 } |
204 | 204 |
205 scoped_refptr<net::IOBufferWithSize> buffer(new net::IOBufferWithSize(size)); | 205 std::vector<uint8_t> read_buffer(buf->data(), buf->data() + size); |
206 memcpy(buffer->data(), buf->data(), size); | |
207 std::unique_ptr<U2fMessage> read_message = | 206 std::unique_ptr<U2fMessage> read_message = |
208 U2fMessage::CreateFromSerializedData(buffer); | 207 U2fMessage::CreateFromSerializedData(read_buffer); |
209 | 208 |
210 if (!read_message) { | 209 if (!read_message) { |
211 std::move(callback).Run(false, nullptr); | 210 std::move(callback).Run(false, nullptr); |
212 return; | 211 return; |
213 } | 212 } |
214 | 213 |
215 // Received a message from a different channel, so try again | 214 // Received a message from a different channel, so try again |
216 if (channel_id_ != read_message->channel_id()) { | 215 if (channel_id_ != read_message->channel_id()) { |
217 connection_->Read(base::Bind(&U2fHidDevice::OnRead, | 216 connection_->Read(base::Bind(&U2fHidDevice::OnRead, |
218 weak_factory_.GetWeakPtr(), | 217 weak_factory_.GetWeakPtr(), |
(...skipping 10 matching lines...) Expand all Loading... |
229 connection_->Read( | 228 connection_->Read( |
230 base::Bind(&U2fHidDevice::OnReadContinuation, weak_factory_.GetWeakPtr(), | 229 base::Bind(&U2fHidDevice::OnReadContinuation, weak_factory_.GetWeakPtr(), |
231 base::Passed(&read_message), base::Passed(&callback))); | 230 base::Passed(&read_message), base::Passed(&callback))); |
232 } | 231 } |
233 | 232 |
234 void U2fHidDevice::OnReadContinuation(std::unique_ptr<U2fMessage> message, | 233 void U2fHidDevice::OnReadContinuation(std::unique_ptr<U2fMessage> message, |
235 U2fHidMessageCallback callback, | 234 U2fHidMessageCallback callback, |
236 bool success, | 235 bool success, |
237 scoped_refptr<net::IOBuffer> buf, | 236 scoped_refptr<net::IOBuffer> buf, |
238 size_t size) { | 237 size_t size) { |
239 if (!success) { | 238 if (!success || !buf) { |
240 std::move(callback).Run(success, nullptr); | 239 std::move(callback).Run(success, nullptr); |
241 return; | 240 return; |
242 } | 241 } |
243 | 242 |
244 scoped_refptr<net::IOBufferWithSize> buffer(new net::IOBufferWithSize(size)); | 243 std::vector<uint8_t> read_buffer(buf->data(), buf->data() + size); |
245 memcpy(buffer->data(), buf->data(), size); | 244 message->AddContinuationPacket(read_buffer); |
246 message->AddContinuationPacket(buffer); | |
247 if (message->MessageComplete()) { | 245 if (message->MessageComplete()) { |
248 std::move(callback).Run(success, std::move(message)); | 246 std::move(callback).Run(success, std::move(message)); |
249 return; | 247 return; |
250 } | 248 } |
251 connection_->Read( | 249 connection_->Read( |
252 base::Bind(&U2fHidDevice::OnReadContinuation, weak_factory_.GetWeakPtr(), | 250 base::Bind(&U2fHidDevice::OnReadContinuation, weak_factory_.GetWeakPtr(), |
253 base::Passed(&message), base::Passed(&callback))); | 251 base::Passed(&message), base::Passed(&callback))); |
254 } | 252 } |
255 | 253 |
256 void U2fHidDevice::MessageReceived(const DeviceCallback& callback, | 254 void U2fHidDevice::MessageReceived(const DeviceCallback& callback, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 return id.str(); | 303 return id.str(); |
306 } | 304 } |
307 | 305 |
308 // static | 306 // static |
309 bool U2fHidDevice::IsTestEnabled() { | 307 bool U2fHidDevice::IsTestEnabled() { |
310 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 308 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
311 return command_line->HasSwitch(switches::kEnableU2fHidTest); | 309 return command_line->HasSwitch(switches::kEnableU2fHidTest); |
312 } | 310 } |
313 | 311 |
314 } // namespace device | 312 } // namespace device |
OLD | NEW |