OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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.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 "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
11 #include "remoting/base/constants.h" | 11 #include "remoting/base/constants.h" |
12 #include "remoting/jingle_glue/javascript_signal_strategy.h" | 12 #include "remoting/jingle_glue/javascript_signal_strategy.h" |
13 #include "remoting/jingle_glue/xmpp_signal_strategy.h" | 13 #include "remoting/jingle_glue/xmpp_signal_strategy.h" |
14 #include "remoting/protocol/auth_util.h" | 14 #include "remoting/protocol/auth_util.h" |
15 #include "remoting/protocol/authenticator.h" | 15 #include "remoting/protocol/authenticator.h" |
16 #include "remoting/protocol/client_control_dispatcher.h" | 16 #include "remoting/protocol/client_control_dispatcher.h" |
17 #include "remoting/protocol/client_event_dispatcher.h" | 17 #include "remoting/protocol/client_event_dispatcher.h" |
18 #include "remoting/protocol/client_stub.h" | 18 #include "remoting/protocol/client_stub.h" |
| 19 #include "remoting/protocol/errors.h" |
19 #include "remoting/protocol/jingle_session_manager.h" | 20 #include "remoting/protocol/jingle_session_manager.h" |
20 #include "remoting/protocol/pepper_transport_factory.h" | 21 #include "remoting/protocol/pepper_transport_factory.h" |
21 #include "remoting/protocol/video_reader.h" | 22 #include "remoting/protocol/video_reader.h" |
22 #include "remoting/protocol/video_stub.h" | 23 #include "remoting/protocol/video_stub.h" |
23 #include "remoting/protocol/util.h" | 24 #include "remoting/protocol/util.h" |
24 | 25 |
25 namespace remoting { | 26 namespace remoting { |
26 namespace protocol { | 27 namespace protocol { |
27 | 28 |
28 ConnectionToHost::ConnectionToHost( | 29 ConnectionToHost::ConnectionToHost( |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 108 |
108 void ConnectionToHost::OnSignalStrategyStateChange( | 109 void ConnectionToHost::OnSignalStrategyStateChange( |
109 SignalStrategy::State state) { | 110 SignalStrategy::State state) { |
110 DCHECK(message_loop_->BelongsToCurrentThread()); | 111 DCHECK(message_loop_->BelongsToCurrentThread()); |
111 DCHECK(event_callback_); | 112 DCHECK(event_callback_); |
112 | 113 |
113 if (state == SignalStrategy::CONNECTED) { | 114 if (state == SignalStrategy::CONNECTED) { |
114 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid(); | 115 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid(); |
115 } else if (state == SignalStrategy::DISCONNECTED) { | 116 } else if (state == SignalStrategy::DISCONNECTED) { |
116 VLOG(1) << "Connection closed."; | 117 VLOG(1) << "Connection closed."; |
117 CloseOnError(NETWORK_FAILURE); | 118 CloseOnError(SIGNALING_ERROR); |
118 } | 119 } |
119 } | 120 } |
120 | 121 |
121 void ConnectionToHost::OnSessionManagerReady() { | 122 void ConnectionToHost::OnSessionManagerReady() { |
122 DCHECK(message_loop_->BelongsToCurrentThread()); | 123 DCHECK(message_loop_->BelongsToCurrentThread()); |
123 | 124 |
124 // After SessionManager is initialized we can try to connect to the host. | 125 // After SessionManager is initialized we can try to connect to the host. |
125 scoped_ptr<CandidateSessionConfig> candidate_config = | 126 scoped_ptr<CandidateSessionConfig> candidate_config = |
126 CandidateSessionConfig::CreateDefault(); | 127 CandidateSessionConfig::CreateDefault(); |
127 session_ = session_manager_->Connect( | 128 session_ = session_manager_->Connect( |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 event_dispatcher_->Init(session_.get(), base::Bind( | 170 event_dispatcher_->Init(session_.get(), base::Bind( |
170 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); | 171 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); |
171 break; | 172 break; |
172 | 173 |
173 case Session::CLOSED: | 174 case Session::CLOSED: |
174 CloseChannels(); | 175 CloseChannels(); |
175 SetState(CLOSED, OK); | 176 SetState(CLOSED, OK); |
176 break; | 177 break; |
177 | 178 |
178 case Session::FAILED: | 179 case Session::FAILED: |
179 switch (session_->error()) { | 180 // If we were connected then treat signaling timeout error as if |
180 case Session::PEER_IS_OFFLINE: | 181 // the connection was closed by the peer. |
181 CloseOnError(HOST_IS_OFFLINE); | 182 // |
182 break; | 183 // TODO(sergeyu): This logic belongs to the webapp, but we |
183 case Session::SESSION_REJECTED: | 184 // currently don't expose this error code to the webapp, and it |
184 case Session::AUTHENTICATION_FAILED: | 185 // would be hard to add it because client plugin and webapp |
185 CloseOnError(SESSION_REJECTED); | 186 // versions may not be in sync. It should be easy to do after we |
186 break; | 187 // are finished moving the client plugin to NaCl. |
187 case Session::INCOMPATIBLE_PROTOCOL: | 188 if (state_ == CONNECTED && session_->error() == SIGNALING_TIMEOUT) { |
188 CloseOnError(INCOMPATIBLE_PROTOCOL); | 189 CloseChannels(); |
189 break; | 190 SetState(CLOSED, OK); |
190 case Session::CHANNEL_CONNECTION_ERROR: | 191 } else { |
191 case Session::UNKNOWN_ERROR: | 192 CloseOnError(session_->error()); |
192 CloseOnError(NETWORK_FAILURE); | |
193 break; | |
194 case Session::OK: | |
195 DLOG(FATAL) << "Error code isn't set"; | |
196 CloseOnError(NETWORK_FAILURE); | |
197 } | 193 } |
198 break; | 194 break; |
199 } | 195 } |
200 } | 196 } |
201 | 197 |
202 void ConnectionToHost::OnChannelInitialized(bool successful) { | 198 void ConnectionToHost::OnChannelInitialized(bool successful) { |
203 if (!successful) { | 199 if (!successful) { |
204 LOG(ERROR) << "Failed to connect video channel"; | 200 LOG(ERROR) << "Failed to connect video channel"; |
205 CloseOnError(NETWORK_FAILURE); | 201 CloseOnError(CHANNEL_CONNECTION_ERROR); |
206 return; | 202 return; |
207 } | 203 } |
208 | 204 |
209 NotifyIfChannelsReady(); | 205 NotifyIfChannelsReady(); |
210 } | 206 } |
211 | 207 |
212 void ConnectionToHost::NotifyIfChannelsReady() { | 208 void ConnectionToHost::NotifyIfChannelsReady() { |
213 if (control_dispatcher_.get() && control_dispatcher_->is_connected() && | 209 if (control_dispatcher_.get() && control_dispatcher_->is_connected() && |
214 event_dispatcher_.get() && event_dispatcher_->is_connected() && | 210 event_dispatcher_.get() && event_dispatcher_->is_connected() && |
215 video_reader_.get() && video_reader_->is_connected() && | 211 video_reader_.get() && video_reader_->is_connected() && |
216 state_ == CONNECTING) { | 212 state_ == CONNECTING) { |
217 // Start forwarding input events to |event_dispatcher_|. | 213 // Start forwarding input events to |event_dispatcher_|. |
218 event_forwarder_.set_input_stub(event_dispatcher_.get()); | 214 event_forwarder_.set_input_stub(event_dispatcher_.get()); |
219 SetState(CONNECTED, OK); | 215 SetState(CONNECTED, OK); |
220 } | 216 } |
221 } | 217 } |
222 | 218 |
223 void ConnectionToHost::CloseOnError(Error error) { | 219 void ConnectionToHost::CloseOnError(ErrorCode error) { |
224 CloseChannels(); | 220 CloseChannels(); |
225 SetState(FAILED, error); | 221 SetState(FAILED, error); |
226 } | 222 } |
227 | 223 |
228 void ConnectionToHost::CloseChannels() { | 224 void ConnectionToHost::CloseChannels() { |
229 control_dispatcher_.reset(); | 225 control_dispatcher_.reset(); |
230 event_dispatcher_.reset(); | 226 event_dispatcher_.reset(); |
231 event_forwarder_.set_input_stub(NULL); | 227 event_forwarder_.set_input_stub(NULL); |
232 video_reader_.reset(); | 228 video_reader_.reset(); |
233 } | 229 } |
234 | 230 |
235 void ConnectionToHost::SetState(State state, Error error) { | 231 void ConnectionToHost::SetState(State state, ErrorCode error) { |
236 DCHECK(message_loop_->BelongsToCurrentThread()); | 232 DCHECK(message_loop_->BelongsToCurrentThread()); |
237 // |error| should be specified only when |state| is set to FAILED. | 233 // |error| should be specified only when |state| is set to FAILED. |
238 DCHECK(state == FAILED || error == OK); | 234 DCHECK(state == FAILED || error == OK); |
239 | 235 |
240 if (state != state_) { | 236 if (state != state_) { |
241 state_ = state; | 237 state_ = state; |
242 error_ = error; | 238 error_ = error; |
243 event_callback_->OnConnectionState(state_, error_); | 239 event_callback_->OnConnectionState(state_, error_); |
244 } | 240 } |
245 } | 241 } |
246 | 242 |
247 } // namespace protocol | 243 } // namespace protocol |
248 } // namespace remoting | 244 } // namespace remoting |
OLD | NEW |