OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 base::MessageLoopProxy* message_loop, | 28 base::MessageLoopProxy* message_loop, |
29 pp::Instance* pp_instance, | 29 pp::Instance* pp_instance, |
30 bool allow_nat_traversal) | 30 bool allow_nat_traversal) |
31 : message_loop_(message_loop), | 31 : message_loop_(message_loop), |
32 pp_instance_(pp_instance), | 32 pp_instance_(pp_instance), |
33 allow_nat_traversal_(allow_nat_traversal), | 33 allow_nat_traversal_(allow_nat_traversal), |
34 event_callback_(NULL), | 34 event_callback_(NULL), |
35 client_stub_(NULL), | 35 client_stub_(NULL), |
36 video_stub_(NULL), | 36 video_stub_(NULL), |
37 state_(CONNECTING), | 37 state_(CONNECTING), |
38 error_(OK), | 38 error_(OK) { |
39 control_connected_(false), | |
40 input_connected_(false), | |
41 video_connected_(false) { | |
42 } | 39 } |
43 | 40 |
44 ConnectionToHost::~ConnectionToHost() { | 41 ConnectionToHost::~ConnectionToHost() { |
45 } | 42 } |
46 | 43 |
47 InputStub* ConnectionToHost::input_stub() { | 44 InputStub* ConnectionToHost::input_stub() { |
48 return input_dispatcher_.get(); | 45 return event_dispatcher_.get(); |
49 } | 46 } |
50 | 47 |
51 HostStub* ConnectionToHost::host_stub() { | 48 HostStub* ConnectionToHost::host_stub() { |
52 return control_dispatcher_.get(); | 49 return control_dispatcher_.get(); |
53 } | 50 } |
54 | 51 |
55 void ConnectionToHost::Connect(scoped_refptr<XmppProxy> xmpp_proxy, | 52 void ConnectionToHost::Connect(scoped_refptr<XmppProxy> xmpp_proxy, |
56 const std::string& your_jid, | 53 const std::string& your_jid, |
57 const std::string& host_jid, | 54 const std::string& host_jid, |
58 const std::string& host_public_key, | 55 const std::string& host_public_key, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 CloseOnError(NETWORK_FAILURE); | 180 CloseOnError(NETWORK_FAILURE); |
184 } | 181 } |
185 break; | 182 break; |
186 | 183 |
187 case Session::CLOSED: | 184 case Session::CLOSED: |
188 CloseChannels(); | 185 CloseChannels(); |
189 SetState(CLOSED, OK); | 186 SetState(CLOSED, OK); |
190 break; | 187 break; |
191 | 188 |
192 case Session::CONNECTED: | 189 case Session::CONNECTED: |
193 video_reader_.reset( | 190 video_reader_.reset(VideoReader::Create( |
194 VideoReader::Create(message_loop_, session_->config())); | 191 message_loop_, session_->config())); |
195 video_reader_->Init( | 192 video_reader_->Init(session_.get(), video_stub_, base::Bind( |
196 session_.get(), video_stub_, | 193 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); |
197 base::Bind(&ConnectionToHost::OnVideoChannelInitialized, | |
198 base::Unretained(this))); | |
199 break; | |
200 | 194 |
201 case Session::CONNECTED_CHANNELS: | |
202 control_dispatcher_.reset(new ClientControlDispatcher()); | 195 control_dispatcher_.reset(new ClientControlDispatcher()); |
203 control_dispatcher_->Init(session_.get()); | 196 control_dispatcher_->Init(session_.get(), base::Bind( |
| 197 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); |
204 control_dispatcher_->set_client_stub(client_stub_); | 198 control_dispatcher_->set_client_stub(client_stub_); |
205 input_dispatcher_.reset(new ClientEventDispatcher()); | |
206 input_dispatcher_->Init(session_.get()); | |
207 | 199 |
208 control_connected_ = true; | 200 event_dispatcher_.reset(new ClientEventDispatcher()); |
209 input_connected_ = true; | 201 event_dispatcher_->Init(session_.get(), base::Bind( |
210 NotifyIfChannelsReady(); | 202 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); |
211 break; | 203 break; |
212 | 204 |
213 default: | 205 default: |
214 // Ignore the other states by default. | 206 // Ignore the other states by default. |
215 break; | 207 break; |
216 } | 208 } |
217 } | 209 } |
218 | 210 |
219 void ConnectionToHost::OnVideoChannelInitialized(bool successful) { | 211 void ConnectionToHost::OnChannelInitialized(bool successful) { |
220 if (!successful) { | 212 if (!successful) { |
221 LOG(ERROR) << "Failed to connect video channel"; | 213 LOG(ERROR) << "Failed to connect video channel"; |
222 CloseOnError(NETWORK_FAILURE); | 214 CloseOnError(NETWORK_FAILURE); |
223 return; | 215 return; |
224 } | 216 } |
225 | 217 |
226 video_connected_ = true; | |
227 NotifyIfChannelsReady(); | 218 NotifyIfChannelsReady(); |
228 } | 219 } |
229 | 220 |
230 void ConnectionToHost::NotifyIfChannelsReady() { | 221 void ConnectionToHost::NotifyIfChannelsReady() { |
231 if (control_connected_ && input_connected_ && video_connected_ && | 222 if (control_dispatcher_.get() && control_dispatcher_->is_connected() && |
| 223 event_dispatcher_.get() && event_dispatcher_->is_connected() && |
| 224 video_reader_.get() && video_reader_->is_connected() && |
232 state_ == CONNECTING) { | 225 state_ == CONNECTING) { |
233 SetState(CONNECTED, OK); | 226 SetState(CONNECTED, OK); |
234 SetState(AUTHENTICATED, OK); | 227 SetState(AUTHENTICATED, OK); |
235 } | 228 } |
236 } | 229 } |
237 | 230 |
238 void ConnectionToHost::CloseOnError(Error error) { | 231 void ConnectionToHost::CloseOnError(Error error) { |
239 CloseChannels(); | 232 CloseChannels(); |
240 SetState(FAILED, error); | 233 SetState(FAILED, error); |
241 } | 234 } |
242 | 235 |
243 void ConnectionToHost::CloseChannels() { | 236 void ConnectionToHost::CloseChannels() { |
244 control_dispatcher_.reset(); | 237 control_dispatcher_.reset(); |
245 input_dispatcher_.reset(); | 238 event_dispatcher_.reset(); |
246 video_reader_.reset(); | 239 video_reader_.reset(); |
247 } | 240 } |
248 | 241 |
249 void ConnectionToHost::SetState(State state, Error error) { | 242 void ConnectionToHost::SetState(State state, Error error) { |
250 DCHECK(message_loop_->BelongsToCurrentThread()); | 243 DCHECK(message_loop_->BelongsToCurrentThread()); |
251 // |error| should be specified only when |state| is set to FAILED. | 244 // |error| should be specified only when |state| is set to FAILED. |
252 DCHECK(state == FAILED || error == OK); | 245 DCHECK(state == FAILED || error == OK); |
253 | 246 |
254 if (state != state_) { | 247 if (state != state_) { |
255 state_ = state; | 248 state_ = state; |
256 error_ = error; | 249 error_ = error; |
257 event_callback_->OnConnectionState(state_, error_); | 250 event_callback_->OnConnectionState(state_, error_); |
258 } | 251 } |
259 } | 252 } |
260 | 253 |
261 } // namespace protocol | 254 } // namespace protocol |
262 } // namespace remoting | 255 } // namespace remoting |
OLD | NEW |