Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / protocol / channel_socket_adapter.h
blob28e2493755d27013c67f7e8738f4670b6b334f37
1 // Copyright 2015 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 REMOTING_PROTOCOL_CHANNEL_SOCKET_ADAPTER_H_
6 #define REMOTING_PROTOCOL_CHANNEL_SOCKET_ADAPTER_H_
8 #include "base/callback_forward.h"
9 #include "base/compiler_specific.h"
10 #include "base/threading/thread_checker.h"
11 #include "remoting/protocol/p2p_datagram_socket.h"
12 #include "third_party/webrtc/base/asyncpacketsocket.h"
13 #include "third_party/webrtc/base/sigslot.h"
14 #include "third_party/webrtc/base/socketaddress.h"
16 namespace cricket {
17 class TransportChannel;
18 } // namespace cricket
20 namespace remoting {
21 namespace protocol {
23 // TransportChannelSocketAdapter implements P2PDatagramSocket interface on
24 // top of libjingle's TransportChannel. It is used by LibjingleTransportFactory
25 // to provide P2PDatagramSocket interface for channels.
26 class TransportChannelSocketAdapter : public P2PDatagramSocket,
27 public sigslot::has_slots<> {
28 public:
29 // Doesn't take ownership of the |channel|. The |channel| must outlive
30 // this adapter.
31 explicit TransportChannelSocketAdapter(cricket::TransportChannel* channel);
32 ~TransportChannelSocketAdapter() override;
34 // Sets callback that should be called when the adapter is being
35 // destroyed. The callback is not allowed to touch the adapter, but
36 // can do anything else, e.g. destroy the TransportChannel.
37 void SetOnDestroyedCallback(const base::Closure& callback);
39 // Closes the stream. |error_code| specifies error code that will
40 // be returned by Recv() and Send() after the stream is closed.
41 // Must be called before the session and the channel are destroyed.
42 void Close(int error_code);
44 // P2PDatagramSocket interface.
45 int Recv(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
46 const net::CompletionCallback& callback) override;
47 int Send(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
48 const net::CompletionCallback& callback) override;
50 private:
51 void OnNewPacket(cricket::TransportChannel* channel,
52 const char* data,
53 size_t data_size,
54 const rtc::PacketTime& packet_time,
55 int flags);
56 void OnWritableState(cricket::TransportChannel* channel);
57 void OnChannelDestroyed(cricket::TransportChannel* channel);
59 base::ThreadChecker thread_checker_;
61 cricket::TransportChannel* channel_;
63 base::Closure destruction_callback_;
65 net::CompletionCallback read_callback_;
66 scoped_refptr<net::IOBuffer> read_buffer_;
67 int read_buffer_size_;
69 net::CompletionCallback write_callback_;
70 scoped_refptr<net::IOBuffer> write_buffer_;
71 int write_buffer_size_;
73 int closed_error_code_;
75 DISALLOW_COPY_AND_ASSIGN(TransportChannelSocketAdapter);
78 } // namespace protocol
79 } // namespace remoting
81 #endif // REMOTING_PROTOCOL_CHANNEL_SOCKET_ADAPTER_H_