Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / net / socket / buffered_write_stream_socket.cc
blob503df8659451c9334bbec29d1fee5f9512485bd2
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/socket/buffered_write_stream_socket.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/message_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
13 namespace net {
15 namespace {
17 void AppendBuffer(GrowableIOBuffer* dst, IOBuffer* src, int src_len) {
18 int old_capacity = dst->capacity();
19 dst->SetCapacity(old_capacity + src_len);
20 memcpy(dst->StartOfBuffer() + old_capacity, src->data(), src_len);
23 } // anonymous namespace
25 BufferedWriteStreamSocket::BufferedWriteStreamSocket(
26 StreamSocket* socket_to_wrap)
27 : wrapped_socket_(socket_to_wrap),
28 io_buffer_(new GrowableIOBuffer()),
29 backup_buffer_(new GrowableIOBuffer()),
30 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
31 callback_pending_(false),
32 wrapped_write_in_progress_(false),
33 error_(0) {
36 BufferedWriteStreamSocket::~BufferedWriteStreamSocket() {
39 int BufferedWriteStreamSocket::Read(IOBuffer* buf, int buf_len,
40 const CompletionCallback& callback) {
41 return wrapped_socket_->Read(buf, buf_len, callback);
44 int BufferedWriteStreamSocket::Write(IOBuffer* buf, int buf_len,
45 const CompletionCallback& callback) {
46 if (error_) {
47 return error_;
49 GrowableIOBuffer* idle_buffer =
50 wrapped_write_in_progress_ ? backup_buffer_.get() : io_buffer_.get();
51 AppendBuffer(idle_buffer, buf, buf_len);
52 if (!callback_pending_) {
53 MessageLoop::current()->PostTask(
54 FROM_HERE,
55 base::Bind(&BufferedWriteStreamSocket::DoDelayedWrite,
56 weak_factory_.GetWeakPtr()));
57 callback_pending_ = true;
59 return buf_len;
62 bool BufferedWriteStreamSocket::SetReceiveBufferSize(int32 size) {
63 return wrapped_socket_->SetReceiveBufferSize(size);
66 bool BufferedWriteStreamSocket::SetSendBufferSize(int32 size) {
67 return wrapped_socket_->SetSendBufferSize(size);
70 int BufferedWriteStreamSocket::Connect(const CompletionCallback& callback) {
71 return wrapped_socket_->Connect(callback);
74 void BufferedWriteStreamSocket::Disconnect() {
75 wrapped_socket_->Disconnect();
78 bool BufferedWriteStreamSocket::IsConnected() const {
79 return wrapped_socket_->IsConnected();
82 bool BufferedWriteStreamSocket::IsConnectedAndIdle() const {
83 return wrapped_socket_->IsConnectedAndIdle();
86 int BufferedWriteStreamSocket::GetPeerAddress(IPEndPoint* address) const {
87 return wrapped_socket_->GetPeerAddress(address);
90 int BufferedWriteStreamSocket::GetLocalAddress(IPEndPoint* address) const {
91 return wrapped_socket_->GetLocalAddress(address);
94 const BoundNetLog& BufferedWriteStreamSocket::NetLog() const {
95 return wrapped_socket_->NetLog();
98 void BufferedWriteStreamSocket::SetSubresourceSpeculation() {
99 wrapped_socket_->SetSubresourceSpeculation();
102 void BufferedWriteStreamSocket::SetOmniboxSpeculation() {
103 wrapped_socket_->SetOmniboxSpeculation();
106 bool BufferedWriteStreamSocket::WasEverUsed() const {
107 return wrapped_socket_->WasEverUsed();
110 bool BufferedWriteStreamSocket::UsingTCPFastOpen() const {
111 return wrapped_socket_->UsingTCPFastOpen();
114 bool BufferedWriteStreamSocket::WasNpnNegotiated() const {
115 return wrapped_socket_->WasNpnNegotiated();
118 NextProto BufferedWriteStreamSocket::GetNegotiatedProtocol() const {
119 return wrapped_socket_->GetNegotiatedProtocol();
122 bool BufferedWriteStreamSocket::GetSSLInfo(SSLInfo* ssl_info) {
123 return wrapped_socket_->GetSSLInfo(ssl_info);
126 void BufferedWriteStreamSocket::DoDelayedWrite() {
127 int result = wrapped_socket_->Write(
128 io_buffer_, io_buffer_->RemainingCapacity(),
129 base::Bind(&BufferedWriteStreamSocket::OnIOComplete,
130 base::Unretained(this)));
131 if (result == ERR_IO_PENDING) {
132 callback_pending_ = true;
133 wrapped_write_in_progress_ = true;
134 } else {
135 OnIOComplete(result);
139 void BufferedWriteStreamSocket::OnIOComplete(int result) {
140 callback_pending_ = false;
141 wrapped_write_in_progress_ = false;
142 if (backup_buffer_->RemainingCapacity()) {
143 AppendBuffer(io_buffer_.get(), backup_buffer_.get(),
144 backup_buffer_->RemainingCapacity());
145 backup_buffer_->SetCapacity(0);
147 if (result < 0) {
148 error_ = result;
149 io_buffer_->SetCapacity(0);
150 } else {
151 io_buffer_->set_offset(io_buffer_->offset() + result);
152 if (io_buffer_->RemainingCapacity()) {
153 DoDelayedWrite();
154 } else {
155 io_buffer_->SetCapacity(0);
160 } // namespace net