OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/connection_to_host.h" | 5 #include "remoting/protocol/connection_to_host_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "remoting/base/constants.h" | 10 #include "remoting/base/constants.h" |
11 #include "remoting/protocol/audio_reader.h" | 11 #include "remoting/protocol/audio_reader.h" |
12 #include "remoting/protocol/audio_stub.h" | 12 #include "remoting/protocol/audio_stub.h" |
13 #include "remoting/protocol/auth_util.h" | 13 #include "remoting/protocol/auth_util.h" |
14 #include "remoting/protocol/authenticator.h" | 14 #include "remoting/protocol/authenticator.h" |
15 #include "remoting/protocol/client_control_dispatcher.h" | 15 #include "remoting/protocol/client_control_dispatcher.h" |
16 #include "remoting/protocol/client_event_dispatcher.h" | 16 #include "remoting/protocol/client_event_dispatcher.h" |
17 #include "remoting/protocol/client_stub.h" | 17 #include "remoting/protocol/client_stub.h" |
18 #include "remoting/protocol/client_video_dispatcher.h" | 18 #include "remoting/protocol/client_video_dispatcher.h" |
19 #include "remoting/protocol/clipboard_stub.h" | 19 #include "remoting/protocol/clipboard_stub.h" |
20 #include "remoting/protocol/errors.h" | 20 #include "remoting/protocol/errors.h" |
21 #include "remoting/protocol/jingle_session_manager.h" | 21 #include "remoting/protocol/jingle_session_manager.h" |
22 #include "remoting/protocol/transport.h" | 22 #include "remoting/protocol/transport.h" |
23 #include "remoting/protocol/video_stub.h" | 23 #include "remoting/protocol/video_stub.h" |
24 | 24 |
25 namespace remoting { | 25 namespace remoting { |
26 namespace protocol { | 26 namespace protocol { |
27 | 27 |
28 ConnectionToHost::ConnectionToHost() | 28 ConnectionToHostImpl::ConnectionToHostImpl() |
29 : event_callback_(nullptr), | 29 : event_callback_(nullptr), |
30 client_stub_(nullptr), | 30 client_stub_(nullptr), |
31 clipboard_stub_(nullptr), | 31 clipboard_stub_(nullptr), |
32 audio_stub_(nullptr), | 32 audio_stub_(nullptr), |
33 signal_strategy_(nullptr), | 33 signal_strategy_(nullptr), |
34 state_(INITIALIZING), | 34 state_(INITIALIZING), |
35 error_(OK) { | 35 error_(OK) { |
36 } | 36 } |
37 | 37 |
38 ConnectionToHost::~ConnectionToHost() { | 38 ConnectionToHostImpl::~ConnectionToHostImpl() { |
39 CloseChannels(); | 39 CloseChannels(); |
40 | 40 |
41 if (session_.get()) | 41 if (session_.get()) |
42 session_.reset(); | 42 session_.reset(); |
43 | 43 |
44 if (session_manager_.get()) | 44 if (session_manager_.get()) |
45 session_manager_.reset(); | 45 session_manager_.reset(); |
46 | 46 |
47 if (signal_strategy_) | 47 if (signal_strategy_) |
48 signal_strategy_->RemoveListener(this); | 48 signal_strategy_->RemoveListener(this); |
49 } | 49 } |
50 | 50 |
51 void ConnectionToHost::Connect(SignalStrategy* signal_strategy, | 51 void ConnectionToHostImpl::Connect( |
52 scoped_ptr<TransportFactory> transport_factory, | 52 SignalStrategy* signal_strategy, |
53 scoped_ptr<Authenticator> authenticator, | 53 scoped_ptr<TransportFactory> transport_factory, |
54 const std::string& host_jid, | 54 scoped_ptr<Authenticator> authenticator, |
55 HostEventCallback* event_callback) { | 55 const std::string& host_jid, |
| 56 HostEventCallback* event_callback) { |
56 DCHECK(client_stub_); | 57 DCHECK(client_stub_); |
57 DCHECK(clipboard_stub_); | 58 DCHECK(clipboard_stub_); |
58 DCHECK(monitored_video_stub_); | 59 DCHECK(monitored_video_stub_); |
59 | 60 |
60 // Initialize default |candidate_config_| if set_candidate_config() wasn't | 61 // Initialize default |candidate_config_| if set_candidate_config() wasn't |
61 // called. | 62 // called. |
62 if (!candidate_config_) { | 63 if (!candidate_config_) { |
63 candidate_config_ = CandidateSessionConfig::CreateDefault(); | 64 candidate_config_ = CandidateSessionConfig::CreateDefault(); |
64 if (!audio_stub_) { | 65 if (!audio_stub_) { |
65 candidate_config_->DisableAudioChannel(); | 66 candidate_config_->DisableAudioChannel(); |
(...skipping 11 matching lines...) Expand all Loading... |
77 | 78 |
78 signal_strategy_->AddListener(this); | 79 signal_strategy_->AddListener(this); |
79 signal_strategy_->Connect(); | 80 signal_strategy_->Connect(); |
80 | 81 |
81 session_manager_.reset(new JingleSessionManager(transport_factory.Pass())); | 82 session_manager_.reset(new JingleSessionManager(transport_factory.Pass())); |
82 session_manager_->Init(signal_strategy_, this); | 83 session_manager_->Init(signal_strategy_, this); |
83 | 84 |
84 SetState(CONNECTING, OK); | 85 SetState(CONNECTING, OK); |
85 } | 86 } |
86 | 87 |
87 void ConnectionToHost::set_candidate_config( | 88 void ConnectionToHostImpl::set_candidate_config( |
88 scoped_ptr<CandidateSessionConfig> config) { | 89 scoped_ptr<CandidateSessionConfig> config) { |
89 DCHECK_EQ(state_, INITIALIZING); | 90 DCHECK_EQ(state_, INITIALIZING); |
90 | 91 |
91 candidate_config_ = config.Pass(); | 92 candidate_config_ = config.Pass(); |
92 } | 93 } |
93 | 94 |
94 | 95 const SessionConfig& ConnectionToHostImpl::config() { |
95 const SessionConfig& ConnectionToHost::config() { | |
96 return session_->config(); | 96 return session_->config(); |
97 } | 97 } |
98 | 98 |
99 ClipboardStub* ConnectionToHost::clipboard_forwarder() { | 99 ClipboardStub* ConnectionToHostImpl::clipboard_forwarder() { |
100 return &clipboard_forwarder_; | 100 return &clipboard_forwarder_; |
101 } | 101 } |
102 | 102 |
103 HostStub* ConnectionToHost::host_stub() { | 103 HostStub* ConnectionToHostImpl::host_stub() { |
104 // TODO(wez): Add a HostFilter class, equivalent to input filter. | 104 // TODO(wez): Add a HostFilter class, equivalent to input filter. |
105 return control_dispatcher_.get(); | 105 return control_dispatcher_.get(); |
106 } | 106 } |
107 | 107 |
108 InputStub* ConnectionToHost::input_stub() { | 108 InputStub* ConnectionToHostImpl::input_stub() { |
109 return &event_forwarder_; | 109 return &event_forwarder_; |
110 } | 110 } |
111 | 111 |
112 void ConnectionToHost::set_client_stub(ClientStub* client_stub) { | 112 void ConnectionToHostImpl::set_client_stub(ClientStub* client_stub) { |
113 client_stub_ = client_stub; | 113 client_stub_ = client_stub; |
114 } | 114 } |
115 | 115 |
116 void ConnectionToHost::set_clipboard_stub(ClipboardStub* clipboard_stub) { | 116 void ConnectionToHostImpl::set_clipboard_stub(ClipboardStub* clipboard_stub) { |
117 clipboard_stub_ = clipboard_stub; | 117 clipboard_stub_ = clipboard_stub; |
118 } | 118 } |
119 | 119 |
120 void ConnectionToHost::set_video_stub(VideoStub* video_stub) { | 120 void ConnectionToHostImpl::set_video_stub(VideoStub* video_stub) { |
121 DCHECK(video_stub); | 121 DCHECK(video_stub); |
122 monitored_video_stub_.reset(new MonitoredVideoStub( | 122 monitored_video_stub_.reset(new MonitoredVideoStub( |
123 video_stub, | 123 video_stub, base::TimeDelta::FromSeconds( |
124 base::TimeDelta::FromSeconds( | 124 MonitoredVideoStub::kConnectivityCheckDelaySeconds), |
125 MonitoredVideoStub::kConnectivityCheckDelaySeconds), | 125 base::Bind(&ConnectionToHostImpl::OnVideoChannelStatus, |
126 base::Bind(&ConnectionToHost::OnVideoChannelStatus, | |
127 base::Unretained(this)))); | 126 base::Unretained(this)))); |
128 } | 127 } |
129 | 128 |
130 void ConnectionToHost::set_audio_stub(AudioStub* audio_stub) { | 129 void ConnectionToHostImpl::set_audio_stub(AudioStub* audio_stub) { |
131 audio_stub_ = audio_stub; | 130 audio_stub_ = audio_stub; |
132 } | 131 } |
133 | 132 |
134 void ConnectionToHost::OnSignalStrategyStateChange( | 133 void ConnectionToHostImpl::OnSignalStrategyStateChange( |
135 SignalStrategy::State state) { | 134 SignalStrategy::State state) { |
136 DCHECK(CalledOnValidThread()); | 135 DCHECK(CalledOnValidThread()); |
137 DCHECK(event_callback_); | 136 DCHECK(event_callback_); |
138 | 137 |
139 if (state == SignalStrategy::CONNECTED) { | 138 if (state == SignalStrategy::CONNECTED) { |
140 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid(); | 139 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid(); |
141 } else if (state == SignalStrategy::DISCONNECTED) { | 140 } else if (state == SignalStrategy::DISCONNECTED) { |
142 VLOG(1) << "Connection closed."; | 141 VLOG(1) << "Connection closed."; |
143 CloseOnError(SIGNALING_ERROR); | 142 CloseOnError(SIGNALING_ERROR); |
144 } | 143 } |
145 } | 144 } |
146 | 145 |
147 bool ConnectionToHost::OnSignalStrategyIncomingStanza( | 146 bool ConnectionToHostImpl::OnSignalStrategyIncomingStanza( |
148 const buzz::XmlElement* stanza) { | 147 const buzz::XmlElement* stanza) { |
149 return false; | 148 return false; |
150 } | 149 } |
151 | 150 |
152 void ConnectionToHost::OnSessionManagerReady() { | 151 void ConnectionToHostImpl::OnSessionManagerReady() { |
153 DCHECK(CalledOnValidThread()); | 152 DCHECK(CalledOnValidThread()); |
154 | 153 |
155 // After SessionManager is initialized we can try to connect to the host. | 154 // After SessionManager is initialized we can try to connect to the host. |
156 session_ = session_manager_->Connect( | 155 session_ = session_manager_->Connect(host_jid_, authenticator_.Pass(), |
157 host_jid_, authenticator_.Pass(), candidate_config_.Pass()); | 156 candidate_config_.Pass()); |
158 session_->SetEventHandler(this); | 157 session_->SetEventHandler(this); |
159 } | 158 } |
160 | 159 |
161 void ConnectionToHost::OnIncomingSession( | 160 void ConnectionToHostImpl::OnIncomingSession( |
162 Session* session, | 161 Session* session, |
163 SessionManager::IncomingSessionResponse* response) { | 162 SessionManager::IncomingSessionResponse* response) { |
164 DCHECK(CalledOnValidThread()); | 163 DCHECK(CalledOnValidThread()); |
165 // Client always rejects incoming sessions. | 164 // Client always rejects incoming sessions. |
166 *response = SessionManager::DECLINE; | 165 *response = SessionManager::DECLINE; |
167 } | 166 } |
168 | 167 |
169 void ConnectionToHost::OnSessionStateChange( | 168 void ConnectionToHostImpl::OnSessionStateChange(Session::State state) { |
170 Session::State state) { | |
171 DCHECK(CalledOnValidThread()); | 169 DCHECK(CalledOnValidThread()); |
172 DCHECK(event_callback_); | 170 DCHECK(event_callback_); |
173 | 171 |
174 switch (state) { | 172 switch (state) { |
175 case Session::INITIALIZING: | 173 case Session::INITIALIZING: |
176 case Session::CONNECTING: | 174 case Session::CONNECTING: |
177 case Session::ACCEPTING: | 175 case Session::ACCEPTING: |
178 case Session::CONNECTED: | 176 case Session::CONNECTED: |
179 case Session::AUTHENTICATING: | 177 case Session::AUTHENTICATING: |
180 // Don't care about these events. | 178 // Don't care about these events. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 if (state_ == CONNECTED && session_->error() == SIGNALING_TIMEOUT) { | 220 if (state_ == CONNECTED && session_->error() == SIGNALING_TIMEOUT) { |
223 CloseChannels(); | 221 CloseChannels(); |
224 SetState(CLOSED, OK); | 222 SetState(CLOSED, OK); |
225 } else { | 223 } else { |
226 CloseOnError(session_->error()); | 224 CloseOnError(session_->error()); |
227 } | 225 } |
228 break; | 226 break; |
229 } | 227 } |
230 } | 228 } |
231 | 229 |
232 void ConnectionToHost::OnSessionRouteChange(const std::string& channel_name, | 230 void ConnectionToHostImpl::OnSessionRouteChange(const std::string& channel_name, |
233 const TransportRoute& route) { | 231 const TransportRoute& route) { |
234 event_callback_->OnRouteChanged(channel_name, route); | 232 event_callback_->OnRouteChanged(channel_name, route); |
235 } | 233 } |
236 | 234 |
237 void ConnectionToHost::OnChannelInitialized( | 235 void ConnectionToHostImpl::OnChannelInitialized( |
238 ChannelDispatcherBase* channel_dispatcher) { | 236 ChannelDispatcherBase* channel_dispatcher) { |
239 NotifyIfChannelsReady(); | 237 NotifyIfChannelsReady(); |
240 } | 238 } |
241 | 239 |
242 void ConnectionToHost::OnChannelError( | 240 void ConnectionToHostImpl::OnChannelError( |
243 ChannelDispatcherBase* channel_dispatcher, | 241 ChannelDispatcherBase* channel_dispatcher, |
244 ErrorCode error) { | 242 ErrorCode error) { |
245 LOG(ERROR) << "Failed to connect channel " << channel_dispatcher; | 243 LOG(ERROR) << "Failed to connect channel " << channel_dispatcher; |
246 CloseOnError(CHANNEL_CONNECTION_ERROR); | 244 CloseOnError(CHANNEL_CONNECTION_ERROR); |
247 return; | 245 return; |
248 } | 246 } |
249 | 247 |
250 void ConnectionToHost::OnVideoChannelStatus(bool active) { | 248 void ConnectionToHostImpl::OnVideoChannelStatus(bool active) { |
251 event_callback_->OnConnectionReady(active); | 249 event_callback_->OnConnectionReady(active); |
252 } | 250 } |
253 | 251 |
254 ConnectionToHost::State ConnectionToHost::state() const { | 252 ConnectionToHost::State ConnectionToHostImpl::state() const { |
255 return state_; | 253 return state_; |
256 } | 254 } |
257 | 255 |
258 void ConnectionToHost::NotifyIfChannelsReady() { | 256 void ConnectionToHostImpl::NotifyIfChannelsReady() { |
259 if (!control_dispatcher_.get() || !control_dispatcher_->is_connected()) | 257 if (!control_dispatcher_.get() || !control_dispatcher_->is_connected()) |
260 return; | 258 return; |
261 if (!event_dispatcher_.get() || !event_dispatcher_->is_connected()) | 259 if (!event_dispatcher_.get() || !event_dispatcher_->is_connected()) |
262 return; | 260 return; |
263 if (!video_dispatcher_.get() || !video_dispatcher_->is_connected()) | 261 if (!video_dispatcher_.get() || !video_dispatcher_->is_connected()) |
264 return; | 262 return; |
265 if ((!audio_reader_.get() || !audio_reader_->is_connected()) && | 263 if ((!audio_reader_.get() || !audio_reader_->is_connected()) && |
266 session_->config().is_audio_enabled()) { | 264 session_->config().is_audio_enabled()) { |
267 return; | 265 return; |
268 } | 266 } |
269 if (state_ != AUTHENTICATED) | 267 if (state_ != AUTHENTICATED) |
270 return; | 268 return; |
271 | 269 |
272 // Start forwarding clipboard and input events. | 270 // Start forwarding clipboard and input events. |
273 clipboard_forwarder_.set_clipboard_stub(control_dispatcher_.get()); | 271 clipboard_forwarder_.set_clipboard_stub(control_dispatcher_.get()); |
274 event_forwarder_.set_input_stub(event_dispatcher_.get()); | 272 event_forwarder_.set_input_stub(event_dispatcher_.get()); |
275 SetState(CONNECTED, OK); | 273 SetState(CONNECTED, OK); |
276 } | 274 } |
277 | 275 |
278 void ConnectionToHost::CloseOnError(ErrorCode error) { | 276 void ConnectionToHostImpl::CloseOnError(ErrorCode error) { |
279 CloseChannels(); | 277 CloseChannels(); |
280 SetState(FAILED, error); | 278 SetState(FAILED, error); |
281 } | 279 } |
282 | 280 |
283 void ConnectionToHost::CloseChannels() { | 281 void ConnectionToHostImpl::CloseChannels() { |
284 control_dispatcher_.reset(); | 282 control_dispatcher_.reset(); |
285 event_dispatcher_.reset(); | 283 event_dispatcher_.reset(); |
286 clipboard_forwarder_.set_clipboard_stub(nullptr); | 284 clipboard_forwarder_.set_clipboard_stub(nullptr); |
287 event_forwarder_.set_input_stub(nullptr); | 285 event_forwarder_.set_input_stub(nullptr); |
288 video_dispatcher_.reset(); | 286 video_dispatcher_.reset(); |
289 audio_reader_.reset(); | 287 audio_reader_.reset(); |
290 } | 288 } |
291 | 289 |
292 void ConnectionToHost::SetState(State state, ErrorCode error) { | 290 void ConnectionToHostImpl::SetState(State state, ErrorCode error) { |
293 DCHECK(CalledOnValidThread()); | 291 DCHECK(CalledOnValidThread()); |
294 // |error| should be specified only when |state| is set to FAILED. | 292 // |error| should be specified only when |state| is set to FAILED. |
295 DCHECK(state == FAILED || error == OK); | 293 DCHECK(state == FAILED || error == OK); |
296 | 294 |
297 if (state != state_) { | 295 if (state != state_) { |
298 state_ = state; | 296 state_ = state; |
299 error_ = error; | 297 error_ = error; |
300 event_callback_->OnConnectionState(state_, error_); | 298 event_callback_->OnConnectionState(state_, error_); |
301 } | 299 } |
302 } | 300 } |
303 | 301 |
304 } // namespace protocol | 302 } // namespace protocol |
305 } // namespace remoting | 303 } // namespace remoting |
OLD | NEW |