Chromium Code Reviews| Index: device/u2f/u2f_message.cc |
| diff --git a/device/u2f/u2f_message.cc b/device/u2f/u2f_message.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4880d1cc3fc94df3469a453bc616df8cb499c286 |
| --- /dev/null |
| +++ b/device/u2f/u2f_message.cc |
| @@ -0,0 +1,69 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/u2f/u2f_message.h" |
| +#include "device/u2f/u2f_packet.h" |
| + |
| +namespace device { |
| + |
| +U2fMessage::U2fMessage(const char channel_id[U2fPacket::kChannelIdSize], |
| + const u2f_message_type type, |
| + const std::vector<char> data) { |
| + size_t size = data.size(); |
|
Reilly Grant (use Gerrit)
2016/11/23 22:21:50
Name this remaining_size. I was confused why we we
Casey Piper
2016/11/29 22:11:15
Done.
|
| + char sequence = 0; |
| + |
| + if (size > kMaxMessageSize) |
| + return; |
|
Reilly Grant (use Gerrit)
2016/11/23 22:21:50
If you're doing validation in the constructor I wo
|
| + |
| + std::vector<char> payload; |
| + std::vector<char>::const_iterator first = data.begin(); |
| + std::vector<char>::const_iterator last; |
| + |
| + if (size > kInitPacketDataSize) { |
| + last = data.begin() + kInitPacketDataSize; |
| + size -= kInitPacketDataSize; |
| + } else { |
| + last = data.begin() + size; |
| + size = 0; |
| + } |
| + |
| + payload.insert(std::end(payload), first, last); |
| + |
| + packets_.push_back(scoped_refptr<U2fPacket>( |
| + static_cast<U2fPacket*>(new U2fInitPacket(channel_id, type, payload)))); |
|
Reilly Grant (use Gerrit)
2016/11/23 22:21:50
A static cast is unnecessary here.
Casey Piper
2016/11/29 22:11:15
Acknowledged.
|
| + |
| + while (size > 0) { |
| + first = last; |
| + if (size > kContPacketDataSize) { |
| + last = first + kContPacketDataSize; |
| + size -= kContPacketDataSize; |
| + } else { |
| + last = first + size; |
| + size = 0; |
| + } |
| + payload.clear(); |
| + payload.insert(std::end(payload), first, last); |
|
Reilly Grant (use Gerrit)
2016/11/23 22:21:50
I found this reuse of payload confusing. You can j
Casey Piper
2016/11/29 22:11:15
Done.
|
| + |
| + packets_.push_back(scoped_refptr<U2fPacket>(static_cast<U2fPacket*>( |
| + new U2fContPacket(channel_id, sequence, payload)))); |
| + sequence++; |
| + } |
| +} |
| + |
| +U2fMessage::~U2fMessage() {} |
| + |
| +scoped_refptr<net::IOBufferWithSize> U2fMessage::GetNextPacket() { |
| + if (NumPackets() > 0) { |
| + scoped_refptr<net::IOBufferWithSize> buf = packets_.front()->GetBuffer(); |
| + packets_.pop_front(); |
| + return buf; |
| + } |
| + return 0; |
|
Reilly Grant (use Gerrit)
2016/11/23 22:21:50
s/0/nullptr/
Casey Piper
2016/11/29 22:11:15
Done.
|
| +} |
| + |
| +size_t U2fMessage::NumPackets() { |
| + return packets_.size(); |
| +} |
| + |
| +} // namespace device |