Commit 80da60ef authored by fdoray's avatar fdoray Committed by Commit bot

Use FileDescriptorWatcher in UsbDeviceHandleUsbfs.

This allows UsbDeviceHandleUsbfs to be used from any thread that
instantiates a FileDescriptorWatcher (not just threads that run
a MessageLoopForIO). This will facilitate the migration of
BrowserThreads to base/task_scheduler.

BUG=645114

Review-Url: https://codereview.chromium.org/2391633002
Cr-Commit-Position: refs/heads/master@{#422859}
parent 1c81ee4b
......@@ -17,9 +17,9 @@
#include "base/bind.h"
#include "base/cancelable_callback.h"
#include "base/files/file_descriptor_watcher_posix.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_pump_libevent.h"
#include "base/posix/eintr_wrapper.h"
#include "base/stl_util.h"
#include "base/threading/thread_restrictions.h"
......@@ -135,8 +135,7 @@ UsbTransferStatus ConvertTransferResult(int rc) {
} // namespace
class UsbDeviceHandleUsbfs::FileThreadHelper
: public base::MessagePumpLibevent::Watcher,
public base::MessageLoop::DestructionObserver {
: public base::MessageLoop::DestructionObserver {
public:
FileThreadHelper(int fd,
scoped_refptr<UsbDeviceHandleUsbfs> device_handle,
......@@ -145,18 +144,17 @@ class UsbDeviceHandleUsbfs::FileThreadHelper
static void Start(std::unique_ptr<FileThreadHelper> self);
// base::MessagePumpLibevent::Watcher overrides.
void OnFileCanReadWithoutBlocking(int fd) override;
void OnFileCanWriteWithoutBlocking(int fd) override;
// base::MessageLoop::DestructionObserver overrides.
void WillDestroyCurrentMessageLoop() override;
private:
// Called when |fd_| is writable without blocking.
void OnFileCanWriteWithoutBlocking();
int fd_;
scoped_refptr<UsbDeviceHandleUsbfs> device_handle_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::MessagePumpLibevent::FileDescriptorWatcher file_watcher_;
std::unique_ptr<base::FileDescriptorWatcher::Controller> watch_controller_;
base::ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(FileThreadHelper);
......@@ -181,25 +179,21 @@ void UsbDeviceHandleUsbfs::FileThreadHelper::Start(
// Linux indicates that URBs are available to reap by marking the file
// descriptor writable.
if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
self->fd_, true, base::MessageLoopForIO::WATCH_WRITE,
&self->file_watcher_, self.get())) {
USB_LOG(ERROR) << "Failed to start watching device file descriptor.";
}
self->watch_controller_ = base::FileDescriptorWatcher::WatchWritable(
self->fd_, base::Bind(&FileThreadHelper::OnFileCanWriteWithoutBlocking,
base::Unretained(self.get())));
// |self| is now owned by the current message loop.
base::MessageLoop::current()->AddDestructionObserver(self.release());
}
void UsbDeviceHandleUsbfs::FileThreadHelper::OnFileCanReadWithoutBlocking(
int fd) {
NOTREACHED(); // Only listening for writability.
void UsbDeviceHandleUsbfs::FileThreadHelper::WillDestroyCurrentMessageLoop() {
DCHECK(thread_checker_.CalledOnValidThread());
delete this;
}
void UsbDeviceHandleUsbfs::FileThreadHelper::OnFileCanWriteWithoutBlocking(
int fd) {
void UsbDeviceHandleUsbfs::FileThreadHelper::OnFileCanWriteWithoutBlocking() {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK_EQ(fd_, fd);
const size_t MAX_URBS_PER_EVENT = 10;
std::vector<usbdevfs_urb*> urbs;
......@@ -214,7 +208,7 @@ void UsbDeviceHandleUsbfs::FileThreadHelper::OnFileCanWriteWithoutBlocking(
if (errno == ENODEV) {
// Device has disconnected. Stop watching the file descriptor to avoid
// looping until |device_handle_| is closed.
file_watcher_.StopWatchingFileDescriptor();
watch_controller_.reset();
break;
}
} else {
......@@ -227,11 +221,6 @@ void UsbDeviceHandleUsbfs::FileThreadHelper::OnFileCanWriteWithoutBlocking(
base::Bind(&UsbDeviceHandleUsbfs::ReapedUrbs, device_handle_, urbs));
}
void UsbDeviceHandleUsbfs::FileThreadHelper::WillDestroyCurrentMessageLoop() {
DCHECK(thread_checker_.CalledOnValidThread());
delete this;
}
struct UsbDeviceHandleUsbfs::Transfer {
Transfer() = delete;
Transfer(scoped_refptr<net::IOBuffer> buffer,
......
......@@ -27,8 +27,8 @@ namespace device {
class UsbDeviceHandleUsbfs : public UsbDeviceHandle {
public:
// Constructs a new device handle from an existing |device| and open file
// descriptor to that device. |blocking_task_runner| must have a
// MessageLoopForIO.
// descriptor to that device. |blocking_task_runner| must run tasks on a
// thread that supports FileDescriptorWatcher.
UsbDeviceHandleUsbfs(
scoped_refptr<UsbDevice> device,
base::ScopedFD fd,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment