Skip to content

Commit

Permalink
net: Make net::DrainableIOBuffer constructors take scoped_refptr.
Browse files Browse the repository at this point in the history
The net::DrainableIOBuffer constructors currently take in a
net::IOBuffer base as a raw pointer, then assign it to a
scoped_refptr<net::IOBuffer> member, thus taking a reference.

This CL turns the raw pointer parameter into a scoped_refptr, and uses
std::move() during the member assignment. So, the reference-increment
operation is pushed from the constructors to the construction callsites.
No new increments are introduced.

The CL also modifies the DrainableIOBuffer construction sites, using
std::move() where possible to remove reference increments.

Bug: 479898
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: I95cd0973d5e25c2565ddecfa0e06a3fc15e1f7b9
Reviewed-on: https://chromium-review.googlesource.com/1188955
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: Matt Menke <mmenke@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
Reviewed-by: Nicolas Zea <zea@chromium.org>
Reviewed-by: Cait Phillips <caitkp@chromium.org>
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Cr-Commit-Position: refs/heads/master@{#587471}
  • Loading branch information
pwnall authored and Commit Bot committed Aug 30, 2018
1 parent 9f74500 commit cd43978
Show file tree
Hide file tree
Showing 39 changed files with 145 additions and 115 deletions.
5 changes: 3 additions & 2 deletions chrome/browser/devtools/device/android_device_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ class HttpRequest {

std::string request = base::StrCat(pieces);
scoped_refptr<net::IOBuffer> base_buffer =
new net::IOBuffer(request.size());
base::MakeRefCounted<net::IOBuffer>(request.size());
memcpy(base_buffer->data(), request.data(), request.size());
request_ = new net::DrainableIOBuffer(base_buffer.get(), request.size());
request_ = base::MakeRefCounted<net::DrainableIOBuffer>(
std::move(base_buffer), request.size());

DoSendRequest(net::OK);
}
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/devtools/device/port_forwarding_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class SocketTunnel : public network::mojom::ResolveHostClient {
base::Bind(
&SocketTunnel::OnRead, base::Unretained(this), from, to, buffer));
if (result != net::ERR_IO_PENDING)
OnRead(from, to, buffer, result);
OnRead(from, to, std::move(buffer), result);
}

void OnRead(net::StreamSocket* from,
Expand All @@ -234,7 +234,7 @@ class SocketTunnel : public network::mojom::ResolveHostClient {

int total = result;
scoped_refptr<net::DrainableIOBuffer> drainable =
new net::DrainableIOBuffer(buffer.get(), total);
base::MakeRefCounted<net::DrainableIOBuffer>(std::move(buffer), total);

++pending_writes_;
result = to->Write(drainable.get(), total,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,11 @@ void NativeMessageProcessHost::DoWrite() {
!current_write_buffer_->BytesRemaining()) {
if (write_queue_.empty())
return;
current_write_buffer_ = new net::DrainableIOBuffer(
write_queue_.front().get(), write_queue_.front()->size());
scoped_refptr<net::IOBufferWithSize> buffer =
std::move(write_queue_.front());
int buffer_size = buffer->size();
current_write_buffer_ = base::MakeRefCounted<net::DrainableIOBuffer>(
std::move(buffer), buffer_size);
write_queue_.pop();
}

Expand Down
10 changes: 6 additions & 4 deletions chrome/test/chromedriver/net/websocket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ WebSocket::WebSocket(const GURL& url, WebSocketListener* listener)
: url_(url),
listener_(listener),
state_(INITIALIZED),
write_buffer_(new net::DrainableIOBuffer(new net::IOBuffer(0), 0)),
read_buffer_(new net::IOBufferWithSize(4096)) {}
write_buffer_(base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::IOBuffer>(0),
0)),
read_buffer_(base::MakeRefCounted<net::IOBufferWithSize>(4096)) {}

WebSocket::~WebSocket() {
CHECK(thread_checker_.CalledOnValidThread());
Expand Down Expand Up @@ -189,8 +191,8 @@ void WebSocket::ContinueWritingIfNecessary() {
if (!write_buffer_->BytesRemaining()) {
if (pending_write_.empty())
return;
write_buffer_ = new net::DrainableIOBuffer(
new net::StringIOBuffer(pending_write_),
write_buffer_ = base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::StringIOBuffer>(pending_write_),
pending_write_.length());
pending_write_.clear();
}
Expand Down
4 changes: 2 additions & 2 deletions components/cast_channel/cast_transport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ CastTransportImpl::WriteRequest::WriteRequest(
const net::CompletionCallback& callback)
: message_namespace(namespace_), callback(callback) {
VLOG(2) << "WriteRequest size: " << payload.size();
io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(payload),
payload.size());
io_buffer = base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::StringIOBuffer>(payload), payload.size());
}

CastTransportImpl::WriteRequest::WriteRequest(const WriteRequest& other) =
Expand Down
5 changes: 3 additions & 2 deletions components/nacl/browser/pnacl_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,9 @@ scoped_refptr<net::DrainableIOBuffer> PnaclHost::CopyFileToBuffer(
return buffer;
}

buffer = new net::DrainableIOBuffer(
new net::IOBuffer(base::checked_cast<size_t>(file_size)),
buffer = base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::IOBuffer>(
base::checked_cast<size_t>(file_size)),
base::checked_cast<size_t>(file_size));
if (file->Read(0, buffer->data(), buffer->size()) != file_size) {
PLOG(ERROR) << "CopyFileToBuffer file read failed";
Expand Down
5 changes: 3 additions & 2 deletions components/nacl/browser/pnacl_translation_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ void PnaclTranslationCacheEntry::DispatchNext(int rv) {
step_ = TRANSFER_ENTRY;
if (is_read_) {
int bytes_to_transfer = entry_->GetDataSize(1);
io_buf_ = new net::DrainableIOBuffer(
new net::IOBuffer(bytes_to_transfer), bytes_to_transfer);
io_buf_ = base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::IOBuffer>(bytes_to_transfer),
bytes_to_transfer);
ReadEntry(0, bytes_to_transfer);
} else {
WriteEntry(0, io_buf_->size());
Expand Down
13 changes: 8 additions & 5 deletions components/nacl/browser/pnacl_translation_cache_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ void PnaclTranslationCacheTest::InitBackend(bool in_mem) {
void PnaclTranslationCacheTest::StoreNexe(const std::string& key,
const std::string& nexe) {
net::TestCompletionCallback store_cb;
scoped_refptr<net::DrainableIOBuffer> nexe_buf(
new net::DrainableIOBuffer(new net::StringIOBuffer(nexe), nexe.size()));
scoped_refptr<net::DrainableIOBuffer> nexe_buf =
base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::StringIOBuffer>(nexe), nexe.size());
cache_->StoreNexe(key, nexe_buf.get(), store_cb.callback());
// Using ERR_IO_PENDING here causes the callback to wait for the result
// which should be harmless even if it returns OK immediately. This is because
Expand Down Expand Up @@ -229,9 +230,11 @@ TEST_F(PnaclTranslationCacheTest, StoreLargeOnDisk) {

TEST_F(PnaclTranslationCacheTest, InMemSizeLimit) {
InitBackend(true);
scoped_refptr<net::DrainableIOBuffer> large_buffer(new net::DrainableIOBuffer(
new net::StringIOBuffer(std::string(kMaxMemCacheSize + 1, 'a')),
kMaxMemCacheSize + 1));
scoped_refptr<net::DrainableIOBuffer> large_buffer =
base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::StringIOBuffer>(
std::string(kMaxMemCacheSize + 1, 'a')),
kMaxMemCacheSize + 1);
net::TestCompletionCallback store_cb;
cache_->StoreNexe(test_key, large_buffer.get(), store_cb.callback());
EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING));
Expand Down
2 changes: 1 addition & 1 deletion content/browser/devtools/protocol/tethering_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class SocketPump {

int total = result;
scoped_refptr<net::DrainableIOBuffer> drainable =
new net::DrainableIOBuffer(buffer.get(), total);
base::MakeRefCounted<net::DrainableIOBuffer>(std::move(buffer), total);

++pending_writes_;
result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,10 @@ int32_t PepperTCPSocketMessageFilter::OnMsgWrite(
return PP_ERROR_BADARGUMENT;
}

write_buffer_base_ = new net::IOBuffer(data_size);
write_buffer_base_ = base::MakeRefCounted<net::IOBuffer>(data_size);
memcpy(write_buffer_base_->data(), data.data(), data_size);
write_buffer_ =
new net::DrainableIOBuffer(write_buffer_base_.get(), data_size);
write_buffer_ = base::MakeRefCounted<net::DrainableIOBuffer>(
write_buffer_base_, data_size);
DoWrite(context->MakeReplyMessageContext());
return PP_OK_COMPLETIONPENDING;
}
Expand Down
5 changes: 3 additions & 2 deletions google_apis/gcm/base/socket_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ SocketInputStream::SocketInputStream(mojo::ScopedDataPipeConsumerHandle stream)
: stream_(std::move(stream)),
stream_watcher_(FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::MANUAL),
read_size_(0),
io_buffer_(new net::IOBuffer(kDefaultBufferSize)),
io_buffer_(base::MakeRefCounted<net::IOBuffer>(kDefaultBufferSize)),
read_buffer_(
new net::DrainableIOBuffer(io_buffer_.get(), kDefaultBufferSize)),
base::MakeRefCounted<net::DrainableIOBuffer>(io_buffer_,
kDefaultBufferSize)),
next_pos_(0),
last_error_(net::OK),
weak_ptr_factory_(this) {
Expand Down
3 changes: 2 additions & 1 deletion jingle/glue/fake_ssl_client_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ static const uint8_t kSslServerHello[] = {
};

net::DrainableIOBuffer* NewDrainableIOBufferWithSize(int size) {
return new net::DrainableIOBuffer(new net::IOBuffer(size), size);
return new net::DrainableIOBuffer(base::MakeRefCounted<net::IOBuffer>(size),
size);
}

} // namespace
Expand Down
34 changes: 18 additions & 16 deletions net/base/file_stream_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ constexpr char kTestData[] = "0123456789";
constexpr int kTestDataSize = base::size(kTestData) - 1;

// Creates an IOBufferWithSize that contains the kTestDataSize.
IOBufferWithSize* CreateTestDataBuffer() {
IOBufferWithSize* buf = new IOBufferWithSize(kTestDataSize);
scoped_refptr<IOBufferWithSize> CreateTestDataBuffer() {
scoped_refptr<IOBufferWithSize> buf =
base::MakeRefCounted<IOBufferWithSize>(kTestDataSize);
memcpy(buf->data(), kTestData, kTestDataSize);
return buf;
}
Expand Down Expand Up @@ -351,9 +352,10 @@ TEST_F(FileStreamTest, Write_FromOffset) {

int total_bytes_written = 0;

scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
scoped_refptr<IOBufferWithSize> buffer = CreateTestDataBuffer();
int buffer_size = buffer->size();
scoped_refptr<DrainableIOBuffer> drainable =
base::MakeRefCounted<DrainableIOBuffer>(buf.get(), buf->size());
base::MakeRefCounted<DrainableIOBuffer>(std::move(buffer), buffer_size);
while (total_bytes_written != kTestDataSize) {
rv = stream.Write(drainable.get(), drainable->BytesRemaining(),
callback.callback());
Expand Down Expand Up @@ -402,9 +404,10 @@ TEST_F(FileStreamTest, BasicReadWrite) {

int total_bytes_written = 0;

scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
scoped_refptr<IOBufferWithSize> buffer = CreateTestDataBuffer();
int buffer_size = buffer->size();
scoped_refptr<DrainableIOBuffer> drainable =
base::MakeRefCounted<DrainableIOBuffer>(buf.get(), buf->size());
base::MakeRefCounted<DrainableIOBuffer>(std::move(buffer), buffer_size);
while (total_bytes_written != kTestDataSize) {
rv = stream->Write(drainable.get(), drainable->BytesRemaining(),
callback.callback());
Expand Down Expand Up @@ -444,9 +447,10 @@ TEST_F(FileStreamTest, BasicWriteRead) {

int total_bytes_written = 0;

scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
scoped_refptr<IOBufferWithSize> buffer = CreateTestDataBuffer();
int buffer_size = buffer->size();
scoped_refptr<DrainableIOBuffer> drainable =
base::MakeRefCounted<DrainableIOBuffer>(buf.get(), buf->size());
base::MakeRefCounted<DrainableIOBuffer>(std::move(buffer), buffer_size);
while (total_bytes_written != kTestDataSize) {
rv = stream->Write(drainable.get(), drainable->BytesRemaining(),
callback.callback());
Expand Down Expand Up @@ -505,9 +509,9 @@ class TestWriteReadCompletionCallback {
total_bytes_written_(total_bytes_written),
total_bytes_read_(total_bytes_read),
data_read_(data_read),
test_data_(CreateTestDataBuffer()),
drainable_(base::MakeRefCounted<DrainableIOBuffer>(test_data_.get(),
kTestDataSize)) {}
drainable_(
base::MakeRefCounted<DrainableIOBuffer>(CreateTestDataBuffer(),
kTestDataSize)) {}

int WaitForResult() {
DCHECK(!waiting_for_result_);
Expand Down Expand Up @@ -588,7 +592,6 @@ class TestWriteReadCompletionCallback {
int* total_bytes_written_;
int* total_bytes_read_;
std::string* data_read_;
scoped_refptr<IOBufferWithSize> test_data_;
scoped_refptr<DrainableIOBuffer> drainable_;

DISALLOW_COPY_AND_ASSIGN(TestWriteReadCompletionCallback);
Expand Down Expand Up @@ -646,9 +649,9 @@ class TestWriteCloseCompletionCallback {
waiting_for_result_(false),
stream_(stream),
total_bytes_written_(total_bytes_written),
test_data_(CreateTestDataBuffer()),
drainable_(base::MakeRefCounted<DrainableIOBuffer>(test_data_.get(),
kTestDataSize)) {}
drainable_(
base::MakeRefCounted<DrainableIOBuffer>(CreateTestDataBuffer(),
kTestDataSize)) {}

int WaitForResult() {
DCHECK(!waiting_for_result_);
Expand Down Expand Up @@ -696,7 +699,6 @@ class TestWriteCloseCompletionCallback {
bool waiting_for_result_;
FileStream* stream_;
int* total_bytes_written_;
scoped_refptr<IOBufferWithSize> test_data_;
scoped_refptr<DrainableIOBuffer> drainable_;

DISALLOW_COPY_AND_ASSIGN(TestWriteCloseCompletionCallback);
Expand Down
11 changes: 4 additions & 7 deletions net/base/io_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,13 @@ StringIOBuffer::~StringIOBuffer() {
data_ = NULL;
}

DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size)
: IOBuffer(base->data()),
base_(base),
size_(size),
used_(0) {
DrainableIOBuffer::DrainableIOBuffer(scoped_refptr<IOBuffer> base, int size)
: IOBuffer(base->data()), base_(std::move(base)), size_(size), used_(0) {
AssertValidBufferSize(size);
}

DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, size_t size)
: IOBuffer(base->data()), base_(base), size_(size), used_(0) {
DrainableIOBuffer::DrainableIOBuffer(scoped_refptr<IOBuffer> base, size_t size)
: IOBuffer(base->data()), base_(std::move(base)), size_(size), used_(0) {
AssertValidBufferSize(size);
}

Expand Down
4 changes: 2 additions & 2 deletions net/base/io_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ class NET_EXPORT StringIOBuffer : public IOBuffer {
class NET_EXPORT DrainableIOBuffer : public IOBuffer {
public:
// TODO(eroman): Deprecated. Use the size_t flavor instead. crbug.com/488553
DrainableIOBuffer(IOBuffer* base, int size);
DrainableIOBuffer(IOBuffer* base, size_t size);
DrainableIOBuffer(scoped_refptr<IOBuffer> base, int size);
DrainableIOBuffer(scoped_refptr<IOBuffer> base, size_t size);

// DidConsume() changes the |data_| pointer so that |data_| always points
// to the first unconsumed byte.
Expand Down
4 changes: 2 additions & 2 deletions net/dns/dns_transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ class DnsTCPAttempt : public DnsAttempt {
if (static_cast<int>(query_size) != query_->io_buffer()->size())
return ERR_FAILED;
base::WriteBigEndian<uint16_t>(length_buffer_->data(), query_size);
buffer_ = base::MakeRefCounted<DrainableIOBuffer>(length_buffer_.get(),
buffer_ = base::MakeRefCounted<DrainableIOBuffer>(length_buffer_,
length_buffer_->size());
next_state_ = STATE_SEND_LENGTH;
return OK;
Expand Down Expand Up @@ -690,7 +690,7 @@ class DnsTCPAttempt : public DnsAttempt {
base::BindOnce(&DnsTCPAttempt::OnIOComplete, base::Unretained(this)),
kTrafficAnnotation);
}
buffer_ = base::MakeRefCounted<DrainableIOBuffer>(length_buffer_.get(),
buffer_ = base::MakeRefCounted<DrainableIOBuffer>(length_buffer_,
length_buffer_->size());
next_state_ = STATE_READ_LENGTH;
return OK;
Expand Down
2 changes: 1 addition & 1 deletion net/filter/filter_source_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int FilterSourceStream::DoReadDataComplete(int result) {

if (result >= OK) {
drainable_input_buffer_ =
base::MakeRefCounted<DrainableIOBuffer>(input_buffer_.get(), result);
base::MakeRefCounted<DrainableIOBuffer>(input_buffer_, result);
next_state_ = STATE_FILTER_DATA;
}
if (result <= OK)
Expand Down
2 changes: 1 addition & 1 deletion net/ftp/ftp_network_transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ int FtpNetworkTransaction::SendFtpCommand(const std::string& command,
write_command_buf_ =
base::MakeRefCounted<IOBufferWithSize>(command.length() + 2);
write_buf_ = base::MakeRefCounted<DrainableIOBuffer>(
write_command_buf_.get(), write_command_buf_->size());
write_command_buf_, write_command_buf_->size());
memcpy(write_command_buf_->data(), command.data(), command.length());
memcpy(write_command_buf_->data() + command.length(), kCRLF, 2);

Expand Down
4 changes: 2 additions & 2 deletions net/http/http_stream_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ int HttpStreamParser::SendRequest(
// We'll repurpose |request_headers_| to store the merged headers and
// body.
request_headers_ = base::MakeRefCounted<DrainableIOBuffer>(
merged_request_headers_and_body.get(), merged_size);
merged_request_headers_and_body, merged_size);

memcpy(request_headers_->data(), request.data(), request_headers_length_);
request_headers_->DidConsume(request_headers_length_);
Expand Down Expand Up @@ -308,7 +308,7 @@ int HttpStreamParser::SendRequest(
scoped_refptr<StringIOBuffer> headers_io_buf =
base::MakeRefCounted<StringIOBuffer>(request);
request_headers_ = base::MakeRefCounted<DrainableIOBuffer>(
headers_io_buf.get(), headers_io_buf->size());
std::move(headers_io_buf), request.size());
}

result = DoLoop(OK);
Expand Down
4 changes: 2 additions & 2 deletions net/quic/quic_http_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ int QuicHttpStream::SendRequest(const HttpRequestHeaders& request_headers,
256 * quic::kMaxPacketSize))));
// The request body buffer is empty at first.
request_body_buf_ =
base::MakeRefCounted<DrainableIOBuffer>(raw_request_body_buf_.get(), 0);
base::MakeRefCounted<DrainableIOBuffer>(raw_request_body_buf_, 0);
}

// Store the response info.
Expand Down Expand Up @@ -621,7 +621,7 @@ int QuicHttpStream::DoReadRequestBodyComplete(int rv) {
}

request_body_buf_ =
base::MakeRefCounted<DrainableIOBuffer>(raw_request_body_buf_.get(), rv);
base::MakeRefCounted<DrainableIOBuffer>(raw_request_body_buf_, rv);
if (rv == 0) { // Reached the end.
DCHECK(request_body_stream_->IsEOF());
}
Expand Down
4 changes: 2 additions & 2 deletions net/server/http_server_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class TestHttpClient {
}

void Send(const std::string& data) {
write_buffer_ =
new DrainableIOBuffer(new StringIOBuffer(data), data.length());
write_buffer_ = base::MakeRefCounted<DrainableIOBuffer>(
base::MakeRefCounted<StringIOBuffer>(data), data.length());
Write();
}

Expand Down
Loading

0 comments on commit cd43978

Please sign in to comment.