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 #ifndef REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_
6 #define REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_
11 #include "base/callback_forward.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "remoting/proto/internal.pb.h"
16 #include "remoting/protocol/channel_dispatcher_base.h"
17 #include "remoting/protocol/clipboard_filter.h"
18 #include "remoting/protocol/errors.h"
19 #include "remoting/protocol/input_filter.h"
20 #include "remoting/protocol/message_reader.h"
21 #include "remoting/protocol/monitored_video_stub.h"
22 #include "remoting/protocol/session.h"
23 #include "remoting/protocol/session_config.h"
24 #include "remoting/protocol/session_manager.h"
25 #include "remoting/signaling/signal_strategy.h"
37 class ClientControlDispatcher
;
38 class ClientEventDispatcher
;
44 class TransportFactory
;
45 class ClientVideoDispatcher
;
48 class ConnectionToHost
: public SignalStrategy::Listener
,
49 public SessionManager::Listener
,
50 public Session::EventHandler
,
51 public ChannelDispatcherBase::EventHandler
,
52 public base::NonThreadSafe
{
54 // The UI implementations maintain corresponding definitions of this
55 // enumeration in webapp/client_session.js and
56 // android/java/src/org/chromium/chromoting/jni/JniInterface.java. Be sure to
57 // update these locations if you make any changes to the ordering.
67 class HostEventCallback
{
69 virtual ~HostEventCallback() {}
71 // Called when state of the connection changes.
72 virtual void OnConnectionState(State state
, ErrorCode error
) = 0;
74 // Called when ready state of the connection changes. When |ready|
75 // is set to false some data sent by the peers may be
76 // delayed. This is used to indicate in the UI when connection is
77 // temporarily broken.
78 virtual void OnConnectionReady(bool ready
) = 0;
80 // Called when the route type (direct vs. STUN vs. proxied) changes.
81 virtual void OnRouteChanged(const std::string
& channel_name
,
82 const protocol::TransportRoute
& route
) = 0;
86 ~ConnectionToHost() override
;
88 // Allows to set a custom protocol configuration (e.g. for tests). Cannot be
89 // called after Connect().
90 void set_candidate_config(scoped_ptr
<CandidateSessionConfig
> config
);
92 // Set the stubs which will handle messages from the host.
93 // The caller must ensure that stubs out-live the connection.
94 // Unless otherwise specified, all stubs must be set before Connect()
96 void set_client_stub(ClientStub
* client_stub
);
97 void set_clipboard_stub(ClipboardStub
* clipboard_stub
);
98 void set_video_stub(VideoStub
* video_stub
);
99 // If no audio stub is specified then audio will not be requested.
100 void set_audio_stub(AudioStub
* audio_stub
);
102 // Initiates a connection to the host specified by |host_jid|.
103 // |signal_strategy| is used to signal to the host, and must outlive the
104 // connection. Data channels will be negotiated over |transport_factory|.
105 // |authenticator| will be used to authenticate the session and data channels.
106 // |event_callback| will be notified of changes in the state of the connection
107 // and must outlive the ConnectionToHost.
108 // Caller must set stubs (see below) before calling Connect.
109 virtual void Connect(SignalStrategy
* signal_strategy
,
110 scoped_ptr
<TransportFactory
> transport_factory
,
111 scoped_ptr
<Authenticator
> authenticator
,
112 const std::string
& host_jid
,
113 HostEventCallback
* event_callback
);
115 // Returns the session configuration that was negotiated with the host.
116 virtual const SessionConfig
& config();
118 // Stubs for sending data to the host.
119 virtual ClipboardStub
* clipboard_forwarder();
120 virtual HostStub
* host_stub();
121 virtual InputStub
* input_stub();
123 // SignalStrategy::StatusObserver interface.
124 void OnSignalStrategyStateChange(SignalStrategy::State state
) override
;
125 bool OnSignalStrategyIncomingStanza(const buzz::XmlElement
* stanza
) override
;
127 // SessionManager::Listener interface.
128 void OnSessionManagerReady() override
;
129 void OnIncomingSession(
131 SessionManager::IncomingSessionResponse
* response
) override
;
133 // Session::EventHandler interface.
134 void OnSessionStateChange(Session::State state
) override
;
135 void OnSessionRouteChange(const std::string
& channel_name
,
136 const TransportRoute
& route
) override
;
138 // ChannelDispatcherBase::EventHandler interface.
139 void OnChannelInitialized(ChannelDispatcherBase
* channel_dispatcher
) override
;
140 void OnChannelError(ChannelDispatcherBase
* channel_dispatcher
,
141 ErrorCode error
) override
;
143 // MonitoredVideoStub::EventHandler interface.
144 virtual void OnVideoChannelStatus(bool active
);
146 // Return the current state of ConnectionToHost.
150 void NotifyIfChannelsReady();
152 void CloseOnError(ErrorCode error
);
154 // Stops writing in the channels.
155 void CloseChannels();
157 void SetState(State state
, ErrorCode error
);
159 std::string host_jid_
;
160 std::string host_public_key_
;
161 scoped_ptr
<Authenticator
> authenticator_
;
163 HostEventCallback
* event_callback_
;
165 scoped_ptr
<CandidateSessionConfig
> candidate_config_
;
167 // Stub for incoming messages.
168 ClientStub
* client_stub_
;
169 ClipboardStub
* clipboard_stub_
;
170 AudioStub
* audio_stub_
;
172 SignalStrategy
* signal_strategy_
;
173 scoped_ptr
<SessionManager
> session_manager_
;
174 scoped_ptr
<Session
> session_
;
175 scoped_ptr
<MonitoredVideoStub
> monitored_video_stub_
;
177 scoped_ptr
<ClientVideoDispatcher
> video_dispatcher_
;
178 scoped_ptr
<AudioReader
> audio_reader_
;
179 scoped_ptr
<ClientControlDispatcher
> control_dispatcher_
;
180 scoped_ptr
<ClientEventDispatcher
> event_dispatcher_
;
181 ClipboardFilter clipboard_forwarder_
;
182 InputFilter event_forwarder_
;
184 // Internal state of the connection.
189 DISALLOW_COPY_AND_ASSIGN(ConnectionToHost
);
192 } // namespace protocol
193 } // namespace remoting
195 #endif // REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_