Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Unified Diff: device/u2f/u2f_hid_device.cc

Issue 2824803003: Add timeout task to U2F HID state machine (Closed)
Patch Set: Create ArmTimeout function Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « device/u2f/u2f_hid_device.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/u2f/u2f_hid_device.cc
diff --git a/device/u2f/u2f_hid_device.cc b/device/u2f/u2f_hid_device.cc
index c313b96e4c165aab270e68e616c552fa4401d62c..fa96d388675313799db04e70c36c2e75bfef758e 100644
--- a/device/u2f/u2f_hid_device.cc
+++ b/device/u2f/u2f_hid_device.cc
@@ -7,6 +7,7 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
+#include "base/threading/thread_task_runner_handle.h"
#include "crypto/random.h"
#include "device/base/device_client.h"
#include "device/hid/hid_connection.h"
@@ -29,7 +30,7 @@ U2fHidDevice::U2fHidDevice(scoped_refptr<HidDeviceInfo> device_info)
U2fHidDevice::~U2fHidDevice() {
// Cleanup connection
- if (connection_)
+ if (connection_ && !connection_->closed())
connection_->Close();
}
@@ -41,19 +42,26 @@ void U2fHidDevice::DeviceTransact(std::unique_ptr<U2fApduCommand> command,
void U2fHidDevice::Transition(std::unique_ptr<U2fApduCommand> command,
const DeviceCallback& callback) {
switch (state_) {
- case State::INIT:
+ case State::INIT: {
state_ = State::BUSY;
+ ArmTimeout(callback);
Connect(base::Bind(&U2fHidDevice::OnConnect, weak_factory_.GetWeakPtr(),
base::Passed(&command), callback));
break;
- case State::CONNECTED:
+ }
Reilly Grant (use Gerrit) 2017/04/17 23:31:05 nit: braces unnecessary
piperc 2017/04/17 23:43:59 Acknowledged.
+ case State::CONNECTED: {
state_ = State::BUSY;
+ ArmTimeout(callback);
AllocateChannel(std::move(command), callback);
break;
+ }
Reilly Grant (use Gerrit) 2017/04/17 23:31:05 nit: braces unnecessary
piperc 2017/04/17 23:43:59 Acknowledged.
case State::IDLE: {
state_ = State::BUSY;
std::unique_ptr<U2fMessage> msg = U2fMessage::Create(
channel_id_, U2fMessage::Type::CMD_MSG, command->GetEncodedCommand());
+
+ ArmTimeout(callback);
+ // Write message to the device
WriteMessage(std::move(msg), true,
base::Bind(&U2fHidDevice::MessageReceived,
weak_factory_.GetWeakPtr(), callback));
@@ -86,6 +94,10 @@ void U2fHidDevice::Connect(const HidService::ConnectCallback& callback) {
void U2fHidDevice::OnConnect(std::unique_ptr<U2fApduCommand> command,
const DeviceCallback& callback,
scoped_refptr<HidConnection> connection) {
+ if (state_ == State::DEVICE_ERROR)
+ return;
+ timeout_callback_.Cancel();
+
if (connection) {
connection_ = connection;
state_ = State::CONNECTED;
@@ -114,6 +126,10 @@ void U2fHidDevice::OnAllocateChannel(std::vector<uint8_t> nonce,
const DeviceCallback& callback,
bool success,
std::unique_ptr<U2fMessage> message) {
+ if (state_ == State::DEVICE_ERROR)
+ return;
+ timeout_callback_.Cancel();
+
if (!success || !message) {
state_ = State::DEVICE_ERROR;
Transition(nullptr, callback);
@@ -213,9 +229,10 @@ void U2fHidDevice::OnRead(U2fHidMessageCallback callback,
// Received a message from a different channel, so try again
if (channel_id_ != read_message->channel_id()) {
- connection_->Read(base::Bind(&U2fHidDevice::OnRead,
- weak_factory_.GetWeakPtr(),
- base::Passed(&callback)));
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
Reilly Grant (use Gerrit) 2017/04/17 23:31:05 I think my recent changes to HidConnection make it
piperc 2017/04/17 23:43:59 Acknowledged.
+ FROM_HERE,
+ base::Bind(&U2fHidDevice::ReadMessage, weak_factory_.GetWeakPtr(),
+ base::Passed(&callback)));
return;
}
@@ -254,11 +271,16 @@ void U2fHidDevice::OnReadContinuation(std::unique_ptr<U2fMessage> message,
void U2fHidDevice::MessageReceived(const DeviceCallback& callback,
bool success,
std::unique_ptr<U2fMessage> message) {
+ if (state_ == State::DEVICE_ERROR)
+ return;
+ timeout_callback_.Cancel();
+
if (!success) {
state_ = State::DEVICE_ERROR;
Transition(nullptr, callback);
return;
}
+
std::unique_ptr<U2fApduResponse> response = nullptr;
if (message)
response = U2fApduResponse::CreateFromMessage(message->GetMessagePayload());
@@ -297,8 +319,23 @@ void U2fHidDevice::OnWink(const WinkCallback& callback,
callback.Run();
}
+void U2fHidDevice::ArmTimeout(const DeviceCallback& callback) {
+ DCHECK(timeout_callback_.IsCancelled());
+ timeout_callback_.Reset(base::Bind(&U2fHidDevice::OnTimeout,
+ weak_factory_.GetWeakPtr(), callback));
+ // Setup timeout task for 3 seconds
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
+ FROM_HERE, timeout_callback_.callback(),
+ base::TimeDelta::FromMilliseconds(3000));
+}
+
+void U2fHidDevice::OnTimeout(const DeviceCallback& callback) {
+ state_ = State::DEVICE_ERROR;
+ Transition(nullptr, callback);
+}
+
std::string U2fHidDevice::GetId() {
- std::ostringstream id("hid:");
+ std::ostringstream id("hid:", std::ios::ate);
id << device_info_->device_id();
return id.str();
}
« no previous file with comments | « device/u2f/u2f_hid_device.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698