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

Unified Diff: device/hid/hid_connection_linux.cc

Issue 161823002: Clean up HID backend and API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Many cleanup, such device ID, woww. Created 6 years, 10 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
Index: device/hid/hid_connection_linux.cc
diff --git a/device/hid/hid_connection_linux.cc b/device/hid/hid_connection_linux.cc
index 0722fabc0ea6b71496dd7470ee40e441a1d27c82..5d81252451609d30da9583fadcd25885d441bf97 100644
--- a/device/hid/hid_connection_linux.cc
+++ b/device/hid/hid_connection_linux.cc
@@ -8,6 +8,7 @@
#include <fcntl.h>
#include <libudev.h>
#include <linux/hidraw.h>
+
#include <string>
#include "base/threading/thread_restrictions.h"
@@ -15,7 +16,6 @@
#include "device/hid/hid_service.h"
#include "device/hid/hid_service_linux.h"
-
namespace device {
namespace {
@@ -94,9 +94,11 @@ void HidConnectionLinux::OnFileCanReadWithoutBlocking(int fd) {
Disconnect();
return;
}
- scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(bytes));
- memcpy(io_buffer->data(), buffer, bytes);
- input_reports_.push(std::make_pair(io_buffer, bytes));
+
+ PendingHidReport report;
+ report.buffer = new net::IOBufferWithSize(bytes);
+ memcpy(report.buffer->data(), buffer, bytes);
+ pending_reports_.push(report);
ProcessReadQueue();
}
@@ -111,41 +113,42 @@ void HidConnectionLinux::Disconnect() {
initialized_ = false;
device_file_watcher_.StopWatchingFileDescriptor();
close(device_file_);
- while (!read_queue_.empty()) {
- PendingRequest callback = read_queue_.front();
- read_queue_.pop();
- callback.c.Run(false, 0);
+ while (!pending_reads_.empty()) {
+ PendingHidRead pending_read = pending_reads_.front();
+ pending_reads_.pop();
+ pending_read.callback.Run(false, 0);
}
}
-void HidConnectionLinux::Read(scoped_refptr<net::IOBuffer> buffer,
- size_t size,
+void HidConnectionLinux::Read(scoped_refptr<net::IOBufferWithSize> buffer,
const IOCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
+ PendingHidRead pending_read;
+ pending_read.buffer = buffer;
+ pending_read.callback = callback;
if (!initialized_) {
- DCHECK(read_queue_.empty());
+ DCHECK(pending_reads_.empty());
// There might be unread reports.
- if (!input_reports_.empty()){
- read_queue_.push(MakeTuple(buffer, size, callback));
+ if (!pending_reports_.empty()) {
+ pending_reads_.push(pending_read);
ProcessReadQueue();
}
callback.Run(false, 0);
return;
} else {
- read_queue_.push(MakeTuple(buffer, size, callback));
+ pending_reads_.push(pending_read);
ProcessReadQueue();
}
}
-void HidConnectionLinux::Write(scoped_refptr<net::IOBuffer> buffer,
- size_t size,
+void HidConnectionLinux::Write(scoped_refptr<net::IOBufferWithSize> buffer,
const IOCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!initialized_) {
callback.Run(false, 0);
return;
} else {
- int bytes = write(device_file_, buffer->data(), size);
+ int bytes = write(device_file_, buffer->data(), buffer->size());
if (bytes < 0) {
Disconnect();
callback.Run(false, 0);
@@ -155,9 +158,9 @@ void HidConnectionLinux::Write(scoped_refptr<net::IOBuffer> buffer,
}
}
-void HidConnectionLinux::GetFeatureReport(scoped_refptr<net::IOBuffer> buffer,
- size_t size,
- const IOCallback& callback) {
+void HidConnectionLinux::GetFeatureReport(
+ scoped_refptr<net::IOBufferWithSize> buffer,
+ const IOCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!initialized_) {
callback.Run(false, 0);
@@ -166,9 +169,9 @@ void HidConnectionLinux::GetFeatureReport(scoped_refptr<net::IOBuffer> buffer,
NOTIMPLEMENTED();
}
-void HidConnectionLinux::SendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
- size_t size,
- const IOCallback& callback) {
+void HidConnectionLinux::SendFeatureReport(
+ scoped_refptr<net::IOBufferWithSize> buffer,
+ const IOCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!initialized_) {
callback.Run(false, 0);
@@ -178,16 +181,16 @@ void HidConnectionLinux::SendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
}
void HidConnectionLinux::ProcessReadQueue() {
- while(read_queue_.size() && input_reports_.size()) {
- PendingRequest request = read_queue_.front();
- read_queue_.pop();
- PendingReport report = input_reports_.front();
- if (report.second > request.b) {
- request.c.Run(false, report.second);
+ while (pending_reads_.size() && pending_reports_.size()) {
+ PendingHidRead read = pending_reads_.front();
+ pending_reads_.pop();
+ PendingHidReport report = pending_reports_.front();
+ if (report.buffer->size() > read.buffer->size()) {
+ read.callback.Run(false, report.buffer->size());
} else {
- memcpy(request.a->data(), report.first->data(), report.second);
- input_reports_.pop();
- request.c.Run(true, report.second);
+ memcpy(read.buffer->data(), report.buffer->data(), report.buffer->size());
+ pending_reports_.pop();
+ read.callback.Run(true, report.buffer->size());
}
}
}

Powered by Google App Engine
This is Rietveld 408576698