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 "base/timer/timer.h"
16 #include "remoting/proto/internal.pb.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
;
48 class ConnectionToHost
: public SignalStrategy::Listener
,
49 public SessionManager::Listener
,
50 public Session::EventHandler
,
51 public base::NonThreadSafe
{
53 // The UI implementations maintain corresponding definitions of this
54 // enumeration in webapp/client_session.js and
55 // android/java/src/org/chromium/chromoting/jni/JniInterface.java. Be sure to
56 // update these locations if you make any changes to the ordering.
66 class HostEventCallback
{
68 virtual ~HostEventCallback() {}
70 // Called when state of the connection changes.
71 virtual void OnConnectionState(State state
, ErrorCode error
) = 0;
73 // Called when ready state of the connection changes. When |ready|
74 // is set to false some data sent by the peers may be
75 // delayed. This is used to indicate in the UI when connection is
76 // temporarily broken.
77 virtual void OnConnectionReady(bool ready
) = 0;
79 // Called when the route type (direct vs. STUN vs. proxied) changes.
80 virtual void OnRouteChanged(const std::string
& channel_name
,
81 const protocol::TransportRoute
& route
) = 0;
85 virtual ~ConnectionToHost();
87 // Allows to set a custom protocol configuration (e.g. for tests). Cannot be
88 // called after Connect().
89 void set_candidate_config(scoped_ptr
<CandidateSessionConfig
> config
);
91 // Set the stubs which will handle messages from the host.
92 // The caller must ensure that stubs out-live the connection.
93 // Unless otherwise specified, all stubs must be set before Connect()
95 void set_client_stub(ClientStub
* client_stub
);
96 void set_clipboard_stub(ClipboardStub
* clipboard_stub
);
97 void set_video_stub(VideoStub
* video_stub
);
98 // If no audio stub is specified then audio will not be requested.
99 void set_audio_stub(AudioStub
* audio_stub
);
101 // Initiates a connection to the host specified by |host_jid|.
102 // |signal_strategy| is used to signal to the host, and must outlive the
103 // connection. Data channels will be negotiated over |transport_factory|.
104 // |authenticator| will be used to authenticate the session and data channels.
105 // |event_callback| will be notified of changes in the state of the connection
106 // and must outlive the ConnectionToHost.
107 // Caller must set stubs (see below) before calling Connect.
108 virtual void Connect(SignalStrategy
* signal_strategy
,
109 scoped_ptr
<TransportFactory
> transport_factory
,
110 scoped_ptr
<Authenticator
> authenticator
,
111 const std::string
& host_jid
,
112 HostEventCallback
* event_callback
);
114 // Returns the session configuration that was negotiated with the host.
115 virtual const SessionConfig
& config();
117 // Stubs for sending data to the host.
118 virtual ClipboardStub
* clipboard_forwarder();
119 virtual HostStub
* host_stub();
120 virtual InputStub
* input_stub();
122 // SignalStrategy::StatusObserver interface.
123 virtual void OnSignalStrategyStateChange(
124 SignalStrategy::State state
) OVERRIDE
;
125 virtual bool OnSignalStrategyIncomingStanza(
126 const buzz::XmlElement
* stanza
) OVERRIDE
;
128 // SessionManager::Listener interface.
129 virtual void OnSessionManagerReady() OVERRIDE
;
130 virtual void OnIncomingSession(
132 SessionManager::IncomingSessionResponse
* response
) OVERRIDE
;
134 // Session::EventHandler interface.
135 virtual void OnSessionStateChange(Session::State state
) OVERRIDE
;
136 virtual void OnSessionRouteChange(const std::string
& channel_name
,
137 const TransportRoute
& route
) OVERRIDE
;
139 // MonitoredVideoStub::EventHandler interface.
140 virtual void OnVideoChannelStatus(bool active
);
142 // Return the current state of ConnectionToHost.
146 // Callbacks for channel initialization
147 void OnChannelInitialized(bool successful
);
149 void NotifyIfChannelsReady();
151 void CloseOnError(ErrorCode error
);
153 // Stops writing in the channels.
154 void CloseChannels();
156 void SetState(State state
, ErrorCode error
);
158 std::string host_jid_
;
159 std::string host_public_key_
;
160 scoped_ptr
<Authenticator
> authenticator_
;
162 HostEventCallback
* event_callback_
;
164 scoped_ptr
<CandidateSessionConfig
> candidate_config_
;
166 // Stub for incoming messages.
167 ClientStub
* client_stub_
;
168 ClipboardStub
* clipboard_stub_
;
169 AudioStub
* audio_stub_
;
171 SignalStrategy
* signal_strategy_
;
172 scoped_ptr
<SessionManager
> session_manager_
;
173 scoped_ptr
<Session
> session_
;
174 scoped_ptr
<MonitoredVideoStub
> monitored_video_stub_
;
176 scoped_ptr
<VideoReader
> video_reader_
;
177 scoped_ptr
<AudioReader
> audio_reader_
;
178 scoped_ptr
<ClientControlDispatcher
> control_dispatcher_
;
179 scoped_ptr
<ClientEventDispatcher
> event_dispatcher_
;
180 ClipboardFilter clipboard_forwarder_
;
181 InputFilter event_forwarder_
;
183 // Internal state of the connection.
188 DISALLOW_COPY_AND_ASSIGN(ConnectionToHost
);
191 } // namespace protocol
192 } // namespace remoting
194 #endif // REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_