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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "google_apis/gcm/base/socket_stream.h"
#include <algorithm>
#include <cstddef>
#include <cstring>
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/notimplemented.h"
#include "base/numerics/safe_conversions.h"
#include "net/base/io_buffer.h"
#include "net/socket/stream_socket.h"
namespace gcm {
namespace {
// TODO(zea): consider having dynamically-sized buffers if this becomes too
// expensive.
const size_t kDefaultBufferSize = 8*1024;
} // namespace
SocketInputStream::SocketInputStream(mojo::ScopedDataPipeConsumerHandle stream)
: stream_(std::move(stream)),
stream_watcher_(FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::MANUAL),
read_size_(0),
io_buffer_(
base::MakeRefCounted<net::IOBufferWithSize>(kDefaultBufferSize)),
read_buffer_(
base::MakeRefCounted<net::DrainableIOBuffer>(io_buffer_,
kDefaultBufferSize)),
next_pos_(0),
last_error_(net::OK) {
stream_watcher_.Watch(
stream_.get(),
MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
base::BindRepeating(&SocketInputStream::ReadMore,
base::Unretained(this)));
}
SocketInputStream::~SocketInputStream() {
}
bool SocketInputStream::Next(const void** data, int* size) {
if (GetState() != EMPTY && GetState() != READY) {
NOTREACHED() << "Invalid input stream read attempt.";
}
if (GetState() == EMPTY) {
DVLOG(1) << "No unread data remaining, ending read.";
return false;
}
DCHECK_EQ(GetState(), READY)
<< " Input stream must have pending data before reading.";
DCHECK_LT(next_pos_, read_buffer_->BytesConsumed());
*data = io_buffer_->data() + next_pos_;
*size = UnreadByteCount();
next_pos_ = read_buffer_->BytesConsumed();
DVLOG(1) << "Consuming " << *size << " bytes in input buffer.";
return true;
}
void SocketInputStream::BackUp(int count) {
DCHECK(GetState() == READY || GetState() == EMPTY);
// TODO(zea): investigating crbug.com/409985
CHECK_GT(count, 0);
CHECK_LE(count, next_pos_);
next_pos_ -= count;
DVLOG(1) << "Backing up " << count << " bytes in input buffer. "
<< "Current position now at " << next_pos_
<< " of " << read_buffer_->BytesConsumed();
}
bool SocketInputStream::Skip(int count) {
NOTIMPLEMENTED();
return false;
}
int64_t SocketInputStream::ByteCount() const {
DCHECK_NE(GetState(), CLOSED);
DCHECK_NE(GetState(), READING);
return next_pos_;
}
int SocketInputStream::UnreadByteCount() const {
DCHECK_NE(GetState(), CLOSED);
DCHECK_NE(GetState(), READING);
return read_buffer_->BytesConsumed() - next_pos_;
}
net::Error SocketInputStream::Refresh(base::OnceClosure callback,
int byte_limit) {
DCHECK(!read_callback_);
DCHECK_NE(GetState(), CLOSED);
DCHECK_NE(GetState(), READING);
DCHECK_GT(byte_limit, 0);
if (byte_limit > read_buffer_->BytesRemaining()) {
LOG(ERROR) << "Out of buffer space, closing input stream.";
CloseStream(net::ERR_FILE_TOO_BIG);
return net::OK;
}
read_size_ = base::checked_cast<size_t>(byte_limit);
read_callback_ = std::move(callback);
stream_watcher_.ArmOrNotify();
last_error_ = net::ERR_IO_PENDING;
return net::ERR_IO_PENDING;
}
void SocketInputStream::ReadMore(
MojoResult result,
const mojo::HandleSignalsState& /* ignored */) {
DCHECK(read_callback_);
DCHECK_NE(0u, read_size_);
size_t num_bytes = read_size_;
if (result == MOJO_RESULT_OK) {
DVLOG(1) << "Refreshing input stream, limit of " << num_bytes << " bytes.";
result = stream_->ReadData(MOJO_READ_DATA_FLAG_NONE,
read_buffer_->first(num_bytes), num_bytes);
DVLOG(1) << "Read returned mojo result" << result;
}
if (result == MOJO_RESULT_SHOULD_WAIT) {
stream_watcher_.ArmOrNotify();
return;
}
read_size_ = 0;
if (result != MOJO_RESULT_OK) {
CloseStream(net::ERR_FAILED);
std::move(read_callback_).Run();
return;
}
// If an EOF has been received, close the stream.
if (result == MOJO_RESULT_OK && num_bytes == 0) {
CloseStream(net::ERR_CONNECTION_CLOSED);
std::move(read_callback_).Run();
return;
}
// If an error occurred before the completion callback could complete, ignore
// the result.
if (GetState() == CLOSED)
return;
last_error_ = net::OK;
read_buffer_->DidConsume(base::checked_cast<uint32_t>(num_bytes));
// TODO(zea): investigating crbug.com/409985
CHECK_GT(UnreadByteCount(), 0);
DVLOG(1) << "Refresh complete with " << num_bytes << " new bytes. "
<< "Current position " << next_pos_ << " of "
<< read_buffer_->BytesConsumed() << ".";
std::move(read_callback_).Run();
}
void SocketInputStream::RebuildBuffer() {
DVLOG(1) << "Rebuilding input stream, consumed "
<< next_pos_ << " bytes.";
DCHECK_NE(GetState(), READING);
DCHECK_NE(GetState(), CLOSED);
int unread_data_size = 0;
const void* unread_data_ptr = nullptr;
Next(&unread_data_ptr, &unread_data_size);
ResetInternal();
if (unread_data_ptr != io_buffer_->data()) {
DVLOG(1) << "Have " << unread_data_size
<< " unread bytes remaining, shifting.";
// Move any remaining unread data to the start of the buffer;
std::copy(static_cast<const char*>(unread_data_ptr),
static_cast<const char*>(unread_data_ptr) + unread_data_size,
io_buffer_->data());
} else {
DVLOG(1) << "Have " << unread_data_size << " unread bytes remaining.";
}
read_buffer_->DidConsume(unread_data_size);
// TODO(zea): investigating crbug.com/409985
CHECK_GE(UnreadByteCount(), 0);
}
net::Error SocketInputStream::last_error() const {
return last_error_;
}
SocketInputStream::State SocketInputStream::GetState() const {
if (last_error_ < net::ERR_IO_PENDING)
return CLOSED;
if (last_error_ == net::ERR_IO_PENDING)
return READING;
DCHECK_EQ(last_error_, net::OK);
if (read_buffer_->BytesConsumed() == next_pos_)
return EMPTY;
return READY;
}
void SocketInputStream::ResetInternal() {
read_buffer_->SetOffset(0);
next_pos_ = 0;
last_error_ = net::OK;
weak_ptr_factory_.InvalidateWeakPtrs(); // Invalidate any callbacks.
}
void SocketInputStream::CloseStream(net::Error error) {
DCHECK_LT(error, net::ERR_IO_PENDING);
ResetInternal();
last_error_ = error;
}
SocketOutputStream::SocketOutputStream(
mojo::ScopedDataPipeProducerHandle stream)
: stream_(std::move(stream)),
stream_watcher_(FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::MANUAL),
io_buffer_(
base::MakeRefCounted<net::IOBufferWithSize>(kDefaultBufferSize)),
next_pos_(0),
last_error_(net::OK) {
stream_watcher_.Watch(
stream_.get(),
MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
base::BindRepeating(&SocketOutputStream::WriteMore,
base::Unretained(this)));
}
SocketOutputStream::~SocketOutputStream() {
}
bool SocketOutputStream::Next(void** data, int* size) {
DCHECK_NE(GetState(), CLOSED);
DCHECK_NE(GetState(), FLUSHING);
if (next_pos_ == io_buffer_->size())
return false;
*data = io_buffer_->data() + next_pos_;
*size = io_buffer_->size() - next_pos_;
next_pos_ = io_buffer_->size();
return true;
}
void SocketOutputStream::BackUp(int count) {
DCHECK_GE(count, 0);
if (count > next_pos_)
next_pos_ = 0;
next_pos_ -= count;
DVLOG(1) << "Backing up " << count << " bytes in output buffer. "
<< next_pos_ << " bytes used.";
}
int64_t SocketOutputStream::ByteCount() const {
DCHECK_NE(GetState(), CLOSED);
DCHECK_NE(GetState(), FLUSHING);
return next_pos_;
}
net::Error SocketOutputStream::Flush(base::OnceClosure callback) {
DCHECK(!write_callback_);
DCHECK_EQ(GetState(), READY);
if (!write_buffer_) {
write_buffer_ = base::MakeRefCounted<net::DrainableIOBuffer>(
io_buffer_.get(), next_pos_);
}
last_error_ = net::ERR_IO_PENDING;
stream_watcher_.ArmOrNotify();
write_callback_ = std::move(callback);
return net::ERR_IO_PENDING;
}
void SocketOutputStream::WriteMore(MojoResult result,
const mojo::HandleSignalsState& state) {
DCHECK(write_callback_);
DCHECK(write_buffer_);
const base::span<const uint8_t> bytes = write_buffer_->first(
base::checked_cast<size_t>(write_buffer_->BytesRemaining()));
DVLOG(1) << "Flushing " << bytes.size() << " bytes into socket.";
size_t bytes_written = 0;
if (result == MOJO_RESULT_OK) {
result =
stream_->WriteData(bytes, MOJO_WRITE_DATA_FLAG_NONE, bytes_written);
}
if (result == MOJO_RESULT_SHOULD_WAIT) {
stream_watcher_.ArmOrNotify();
return;
}
if (result != MOJO_RESULT_OK) {
LOG(ERROR) << "Failed to flush socket.";
last_error_ = net::ERR_FAILED;
std::move(write_callback_).Run();
return;
}
DVLOG(1) << "Wrote " << bytes_written;
// If an error occurred before the completion callback could complete, ignore
// the result.
if (GetState() == CLOSED)
return;
DCHECK_GE(bytes_written, 0u);
last_error_ = net::OK;
write_buffer_->DidConsume(base::checked_cast<uint32_t>(bytes_written));
if (write_buffer_->BytesRemaining() > 0) {
DVLOG(1) << "Partial flush complete. Retrying.";
// Only a partial write was completed. Flush again to finish the write.
Flush(std::move(write_callback_));
return;
}
DVLOG(1) << "Socket flush complete.";
write_buffer_ = nullptr;
next_pos_ = 0;
std::move(write_callback_).Run();
}
SocketOutputStream::State SocketOutputStream::GetState() const{
if (last_error_ < net::ERR_IO_PENDING)
return CLOSED;
if (last_error_ == net::ERR_IO_PENDING)
return FLUSHING;
DCHECK_EQ(last_error_, net::OK);
if (next_pos_ == 0)
return EMPTY;
return READY;
}
net::Error SocketOutputStream::last_error() const {
return last_error_;
}
} // namespace gcm
|