1 // Copyright 2014 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 #ifndef MOJO_SERVICES_NETWORK_TCP_CONNECTED_SOCKET_H_
6 #define MOJO_SERVICES_NETWORK_TCP_CONNECTED_SOCKET_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "mojo/application/public/cpp/app_lifetime_helper.h"
11 #include "mojo/common/handle_watcher.h"
12 #include "mojo/services/network/public/interfaces/tcp_connected_socket.mojom.h"
13 #include "net/socket/tcp_socket.h"
14 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
18 class MojoToNetPendingBuffer
;
19 class NetToMojoPendingBuffer
;
21 class TCPConnectedSocketImpl
: public TCPConnectedSocket
, public ErrorHandler
{
23 TCPConnectedSocketImpl(scoped_ptr
<net::TCPSocket
> socket
,
24 ScopedDataPipeConsumerHandle send_stream
,
25 ScopedDataPipeProducerHandle receive_stream
,
26 InterfaceRequest
<TCPConnectedSocket
> request
,
27 scoped_ptr
<mojo::AppRefCount
> app_refcount
);
28 ~TCPConnectedSocketImpl() override
;
31 // ErrorHandler methods:
32 void OnConnectionError() override
;
34 // "Receiving" in this context means reading from TCPSocket and writing to
35 // the Mojo receive_stream.
37 void OnReceiveStreamReady(MojoResult result
);
38 void DidReceive(bool completed_synchronously
, int result
);
39 void ShutdownReceive();
40 void ListenForReceivePeerClosed();
41 void OnReceiveDataPipeClosed(MojoResult result
);
43 // "Writing" is reading from the Mojo send_stream and writing to the
46 void OnSendStreamReady(MojoResult result
);
47 void DidSend(bool completed_asynchronously
, int result
);
49 void ListenForSendPeerClosed();
50 void OnSendDataPipeClosed(MojoResult result
);
52 void DeleteIfNeeded();
54 scoped_ptr
<net::TCPSocket
> socket_
;
56 // The *stream handles will be null while there is an in-progress transation
57 // between net and mojo. During this time, the handle will be owned by the
60 // For reading from the network and writing to Mojo pipe.
61 ScopedDataPipeConsumerHandle send_stream_
;
62 scoped_refptr
<NetToMojoPendingBuffer
> pending_receive_
;
63 common::HandleWatcher receive_handle_watcher_
;
65 // For reading from the Mojo pipe and writing to the network.
66 ScopedDataPipeProducerHandle receive_stream_
;
67 scoped_refptr
<MojoToNetPendingBuffer
> pending_send_
;
68 common::HandleWatcher send_handle_watcher_
;
70 // To bind to the message pipe.
71 Binding
<TCPConnectedSocket
> binding_
;
73 scoped_ptr
<mojo::AppRefCount
> app_refcount_
;
75 base::WeakPtrFactory
<TCPConnectedSocketImpl
> weak_ptr_factory_
;
80 #endif // MOJO_SERVICES_NETWORK_TCP_CONNECTED_SOCKET_H_