1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/chromedriver/net/pipe_reader_posix.h"
#include <errno.h>
#include <memory>
#include <utility>
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/task/current_thread.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#if BUILDFLAG(IS_FUCHSIA)
#include <poll.h>
#include <sys/ioctl.h>
#endif // BUILDFLAG(IS_FUCHSIA)
PipeReaderPosix::PipeReaderPosix()
: fd_(base::kInvalidPlatformFile), read_fd_watcher_(FROM_HERE) {}
PipeReaderPosix::~PipeReaderPosix() {
Close();
}
int PipeReaderPosix::Bind(base::ScopedFD fd) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK(!fd_.is_valid());
fd_ = std::move(fd);
if (!base::SetNonBlocking(fd_.get())) {
int rv = net::MapSystemError(errno);
Close();
return rv;
}
is_connected_ = true;
return net::OK;
}
bool PipeReaderPosix::IsConnected() const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return is_connected_;
}
int PipeReaderPosix::Read(net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
// Use base::Unretained() is safe here because OnFileCanReadWithoutBlocking()
// won't be called if |this| is gone.
int rv = ReadIfReady(
buf, buf_len,
base::BindOnce(&PipeReaderPosix::RetryRead, base::Unretained(this)));
if (rv == net::ERR_IO_PENDING) {
read_buf_ = buf;
read_buf_len_ = buf_len;
read_callback_ = std::move(callback);
}
is_connected_ = (rv == net::ERR_IO_PENDING) || (rv > 0);
return rv;
}
int PipeReaderPosix::ReadIfReady(net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK(fd_.is_valid());
CHECK(read_if_ready_callback_.is_null());
DCHECK(!callback.is_null());
DCHECK_LT(0, buf_len);
int rv = DoRead(buf, buf_len);
if (rv != net::ERR_IO_PENDING) {
return rv;
}
if (!base::CurrentIOThread::Get()->WatchFileDescriptor(
fd_.get(), true, base::MessagePumpForIO::WATCH_READ,
&read_fd_watcher_, this)) {
PLOG(ERROR) << "WatchFileDescriptor failed on read";
return net::MapSystemError(errno);
}
read_if_ready_callback_ = std::move(callback);
return net::ERR_IO_PENDING;
}
int PipeReaderPosix::CancelReadIfReady() {
DCHECK(read_if_ready_callback_);
bool ok = read_fd_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
read_if_ready_callback_.Reset();
return net::OK;
}
void PipeReaderPosix::Close() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
is_connected_ = false;
bool ok = read_fd_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
// These needs to be done after the StopWatchingFileDescriptor() calls, but
// before deleting the write buffer.
fd_.reset();
if (!read_callback_.is_null()) {
read_buf_.reset();
read_buf_len_ = 0;
read_callback_.Reset();
}
read_if_ready_callback_.Reset();
}
void PipeReaderPosix::DetachFromThread() {
DETACH_FROM_THREAD(thread_checker_);
}
void PipeReaderPosix::OnFileCanReadWithoutBlocking(int fd) {
DCHECK(read_if_ready_callback_);
bool ok = read_fd_watcher_.StopWatchingFileDescriptor();
DCHECK(ok);
std::move(read_if_ready_callback_).Run(net::OK);
}
void PipeReaderPosix::OnFileCanWriteWithoutBlocking(int fd) {}
int PipeReaderPosix::DoRead(net::IOBuffer* buf, int buf_len) {
int rv = HANDLE_EINTR(read(fd_.get(), buf->data(), buf_len));
if (rv == 0) {
return net::ERR_CONNECTION_CLOSED;
}
return rv > 0 ? rv : net::MapSystemError(errno);
}
void PipeReaderPosix::RetryRead(int rv) {
DCHECK(read_callback_);
DCHECK(read_buf_);
DCHECK_LT(0, read_buf_len_);
if (rv == net::OK) {
rv = ReadIfReady(
read_buf_.get(), read_buf_len_,
base::BindOnce(&PipeReaderPosix::RetryRead, base::Unretained(this)));
if (rv == net::ERR_IO_PENDING) {
return;
}
}
is_connected_ = (rv == net::ERR_IO_PENDING) || (rv > 0);
read_buf_ = nullptr;
read_buf_len_ = 0;
std::move(read_callback_).Run(rv);
}
|