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/jingle_glue/signal_strategy.h"
17 #include "remoting/proto/internal.pb.h"
18 #include "remoting/protocol/clipboard_filter.h"
19 #include "remoting/protocol/errors.h"
20 #include "remoting/protocol/input_filter.h"
21 #include "remoting/protocol/message_reader.h"
22 #include "remoting/protocol/monitored_video_stub.h"
23 #include "remoting/protocol/session.h"
24 #include "remoting/protocol/session_manager.h"
36 class ClientControlDispatcher
;
37 class ClientEventDispatcher
;
43 class TransportFactory
;
47 class ConnectionToHost
: public SignalStrategy::Listener
,
48 public SessionManager::Listener
,
49 public Session::EventHandler
,
50 public base::NonThreadSafe
{
52 // The UI implementations maintain corresponding definitions of this
53 // enumeration in webapp/client_session.js and
54 // android/java/src/org/chromium/chromoting/jni/JniInterface.java. Be sure to
55 // update these locations if you make any changes to the ordering.
65 class HostEventCallback
{
67 virtual ~HostEventCallback() {}
69 // Called when state of the connection changes.
70 virtual void OnConnectionState(State state
, ErrorCode error
) = 0;
72 // Called when ready state of the connection changes. When |ready|
73 // is set to false some data sent by the peers may be
74 // delayed. This is used to indicate in the UI when connection is
75 // temporarily broken.
76 virtual void OnConnectionReady(bool ready
) = 0;
78 // Called when the route type (direct vs. STUN vs. proxied) changes.
79 virtual void OnRouteChanged(const std::string
& channel_name
,
80 const protocol::TransportRoute
& route
) = 0;
83 ConnectionToHost(bool allow_nat_traversal
);
84 virtual ~ConnectionToHost();
86 // Set the stubs which will handle messages from the host.
87 // The caller must ensure that stubs out-live the connection.
88 // Unless otherwise specified, all stubs must be set before Connect()
90 void set_client_stub(ClientStub
* client_stub
);
91 void set_clipboard_stub(ClipboardStub
* clipboard_stub
);
92 void set_video_stub(VideoStub
* video_stub
);
93 // If no audio stub is specified then audio will not be requested.
94 void set_audio_stub(AudioStub
* audio_stub
);
96 // Initiates a connection to the host specified by |host_jid|.
97 // |signal_strategy| is used to signal to the host, and must outlive the
98 // connection. Data channels will be negotiated over |transport_factory|.
99 // |authenticator| will be used to authenticate the session and data channels.
100 // |event_callback| will be notified of changes in the state of the connection
101 // and must outlive the ConnectionToHost.
102 // Caller must set stubs (see below) before calling Connect.
103 virtual void Connect(SignalStrategy
* signal_strategy
,
104 scoped_ptr
<TransportFactory
> transport_factory
,
105 scoped_ptr
<Authenticator
> authenticator
,
106 const std::string
& host_jid
,
107 const std::string
& host_public_key
,
108 HostEventCallback
* event_callback
);
110 // Returns the session configuration that was negotiated with the host.
111 virtual const SessionConfig
& config();
113 // Stubs for sending data to the host.
114 virtual ClipboardStub
* clipboard_forwarder();
115 virtual HostStub
* host_stub();
116 virtual InputStub
* input_stub();
118 // SignalStrategy::StatusObserver interface.
119 virtual void OnSignalStrategyStateChange(
120 SignalStrategy::State state
) OVERRIDE
;
121 virtual bool OnSignalStrategyIncomingStanza(
122 const buzz::XmlElement
* stanza
) OVERRIDE
;
124 // SessionManager::Listener interface.
125 virtual void OnSessionManagerReady() OVERRIDE
;
126 virtual void OnIncomingSession(
128 SessionManager::IncomingSessionResponse
* response
) OVERRIDE
;
130 // Session::EventHandler interface.
131 virtual void OnSessionStateChange(Session::State state
) OVERRIDE
;
132 virtual void OnSessionRouteChange(const std::string
& channel_name
,
133 const TransportRoute
& route
) OVERRIDE
;
135 // MonitoredVideoStub::EventHandler interface.
136 virtual void OnVideoChannelStatus(bool active
);
138 // Return the current state of ConnectionToHost.
142 // Callbacks for channel initialization
143 void OnChannelInitialized(bool successful
);
145 void NotifyIfChannelsReady();
147 void CloseOnError(ErrorCode error
);
149 // Stops writing in the channels.
150 void CloseChannels();
152 void SetState(State state
, ErrorCode error
);
154 bool allow_nat_traversal_
;
156 std::string host_jid_
;
157 std::string host_public_key_
;
158 scoped_ptr
<Authenticator
> authenticator_
;
160 HostEventCallback
* event_callback_
;
162 // Stub for incoming messages.
163 ClientStub
* client_stub_
;
164 ClipboardStub
* clipboard_stub_
;
165 AudioStub
* audio_stub_
;
167 SignalStrategy
* signal_strategy_
;
168 scoped_ptr
<SessionManager
> session_manager_
;
169 scoped_ptr
<Session
> session_
;
170 scoped_ptr
<MonitoredVideoStub
> monitored_video_stub_
;
172 scoped_ptr
<VideoReader
> video_reader_
;
173 scoped_ptr
<AudioReader
> audio_reader_
;
174 scoped_ptr
<ClientControlDispatcher
> control_dispatcher_
;
175 scoped_ptr
<ClientEventDispatcher
> event_dispatcher_
;
176 ClipboardFilter clipboard_forwarder_
;
177 InputFilter event_forwarder_
;
179 // Internal state of the connection.
184 DISALLOW_COPY_AND_ASSIGN(ConnectionToHost
);
187 } // namespace protocol
188 } // namespace remoting
190 #endif // REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_