summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/ice_transport.h
blob: e47fc3b546fe63af3538025e8408814a529cf91d (plain)
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_PROTOCOL_ICE_TRANSPORT_H_
#define REMOTING_PROTOCOL_ICE_TRANSPORT_H_

#include <list>
#include <map>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "remoting/protocol/datagram_channel_factory.h"
#include "remoting/protocol/ice_transport_channel.h"
#include "remoting/protocol/jingle_messages.h"
#include "remoting/protocol/transport.h"

namespace remoting {
namespace protocol {

class ChannelMultiplexer;
class PseudoTcpChannelFactory;
class SecureChannelFactory;
class MessageChannelFactory;

class IceTransport : public Transport,
                     public IceTransportChannel::Delegate,
                     public DatagramChannelFactory {
 public:
  class EventHandler {
   public:
    // Called when transport route changes.
    virtual void OnIceTransportRouteChange(const std::string& channel_name,
                                           const TransportRoute& route) = 0;

    // Called when there is an error connecting the session.
    virtual void OnIceTransportError(ErrorCode error) = 0;
  };

  // |transport_context| must outlive the session.
  IceTransport(scoped_refptr<TransportContext> transport_context,
               EventHandler* event_handler);
  ~IceTransport() override;

  MessageChannelFactory* GetChannelFactory();
  MessageChannelFactory* GetMultiplexedChannelFactory();

  // Transport interface.
  void Start(Authenticator* authenticator,
             SendTransportInfoCallback send_transport_info_callback) override;
  bool ProcessTransportInfo(buzz::XmlElement* transport_info) override;

 private:
  typedef std::map<std::string, IceTransportChannel*> ChannelsMap;

  // DatagramChannelFactory interface.
  void CreateChannel(const std::string& name,
                     const ChannelCreatedCallback& callback) override;
  void CancelChannelCreation(const std::string& name) override;

  // Passes transport info to a new |channel| in case it was received before the
  // channel was created.
  void AddPendingRemoteTransportInfo(IceTransportChannel* channel);

  // IceTransportChannel::Delegate interface.
  void OnChannelIceCredentials(IceTransportChannel* transport,
                               const std::string& ufrag,
                               const std::string& password) override;
  void OnChannelCandidate(IceTransportChannel* transport,
                          const cricket::Candidate& candidate) override;
  void OnChannelRouteChange(IceTransportChannel* transport,
                            const TransportRoute& route) override;
  void OnChannelFailed(IceTransportChannel* transport) override;
  void OnChannelDeleted(IceTransportChannel* transport) override;

  // Creates empty |pending_transport_info_message_| and schedules timer for
  // SentTransportInfo() to sent the message later.
  void EnsurePendingTransportInfoMessage();

  // Sends transport-info message with candidates from |pending_candidates_|.
  void SendTransportInfo();

  // Callback passed to StreamMessageChannelFactoryAdapter to handle read/write
  // errors on the data channels.
  void OnChannelError(int error);

  scoped_refptr<TransportContext> transport_context_;
  EventHandler* event_handler_;

  SendTransportInfoCallback send_transport_info_callback_;

  ChannelsMap channels_;
  scoped_ptr<PseudoTcpChannelFactory> pseudotcp_channel_factory_;
  scoped_ptr<SecureChannelFactory> secure_channel_factory_;
  scoped_ptr<MessageChannelFactory> message_channel_factory_;

  scoped_ptr<ChannelMultiplexer> channel_multiplexer_;
  scoped_ptr<MessageChannelFactory> mux_channel_factory_;

  // Pending remote transport info received before the local channels were
  // created.
  std::list<IceTransportInfo::IceCredentials> pending_remote_ice_credentials_;
  std::list<IceTransportInfo::NamedCandidate> pending_remote_candidates_;

  scoped_ptr<IceTransportInfo> pending_transport_info_message_;
  base::OneShotTimer transport_info_timer_;

  base::WeakPtrFactory<IceTransport> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(IceTransport);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_ICE_TRANSPORT_H_