OLD | NEW |
1 // Copyright 2015 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_impl.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" | |
15 #include "remoting/protocol/client_control_dispatcher.h" | 14 #include "remoting/protocol/client_control_dispatcher.h" |
16 #include "remoting/protocol/client_event_dispatcher.h" | 15 #include "remoting/protocol/client_event_dispatcher.h" |
17 #include "remoting/protocol/client_stub.h" | 16 #include "remoting/protocol/client_stub.h" |
18 #include "remoting/protocol/client_video_dispatcher.h" | 17 #include "remoting/protocol/client_video_dispatcher.h" |
19 #include "remoting/protocol/clipboard_stub.h" | 18 #include "remoting/protocol/clipboard_stub.h" |
20 #include "remoting/protocol/errors.h" | 19 #include "remoting/protocol/errors.h" |
21 #include "remoting/protocol/ice_transport.h" | 20 #include "remoting/protocol/ice_transport.h" |
22 #include "remoting/protocol/jingle_session_manager.h" | |
23 #include "remoting/protocol/transport.h" | 21 #include "remoting/protocol/transport.h" |
24 #include "remoting/protocol/transport_context.h" | |
25 #include "remoting/protocol/video_stub.h" | 22 #include "remoting/protocol/video_stub.h" |
26 | 23 |
27 namespace remoting { | 24 namespace remoting { |
28 namespace protocol { | 25 namespace protocol { |
29 | 26 |
30 ConnectionToHostImpl::ConnectionToHostImpl() | 27 ConnectionToHostImpl::ConnectionToHostImpl() {} |
31 : event_callback_(nullptr), | 28 ConnectionToHostImpl::~ConnectionToHostImpl() {} |
32 client_stub_(nullptr), | |
33 clipboard_stub_(nullptr), | |
34 audio_stub_(nullptr), | |
35 signal_strategy_(nullptr), | |
36 state_(INITIALIZING), | |
37 error_(OK) {} | |
38 | |
39 ConnectionToHostImpl::~ConnectionToHostImpl() { | |
40 CloseChannels(); | |
41 | |
42 if (session_.get()) | |
43 session_.reset(); | |
44 | |
45 if (session_manager_.get()) | |
46 session_manager_.reset(); | |
47 | |
48 if (signal_strategy_) | |
49 signal_strategy_->RemoveListener(this); | |
50 } | |
51 | 29 |
52 #define RETURN_STRING_LITERAL(x) \ | 30 #define RETURN_STRING_LITERAL(x) \ |
53 case x: \ | 31 case x: \ |
54 return #x; | 32 return #x; |
55 | 33 |
56 const char* ConnectionToHost::StateToString(State state) { | 34 const char* ConnectionToHost::StateToString(State state) { |
57 switch (state) { | 35 switch (state) { |
58 RETURN_STRING_LITERAL(INITIALIZING); | 36 RETURN_STRING_LITERAL(INITIALIZING); |
59 RETURN_STRING_LITERAL(CONNECTING); | 37 RETURN_STRING_LITERAL(CONNECTING); |
60 RETURN_STRING_LITERAL(AUTHENTICATED); | 38 RETURN_STRING_LITERAL(AUTHENTICATED); |
61 RETURN_STRING_LITERAL(CONNECTED); | 39 RETURN_STRING_LITERAL(CONNECTED); |
62 RETURN_STRING_LITERAL(CLOSED); | 40 RETURN_STRING_LITERAL(CLOSED); |
63 RETURN_STRING_LITERAL(FAILED); | 41 RETURN_STRING_LITERAL(FAILED); |
64 } | 42 } |
65 NOTREACHED(); | 43 NOTREACHED(); |
66 return nullptr; | 44 return nullptr; |
67 } | 45 } |
68 | 46 |
69 void ConnectionToHostImpl::Connect( | 47 void ConnectionToHostImpl::Connect(scoped_ptr<Session> session, |
70 SignalStrategy* signal_strategy, | 48 HostEventCallback* event_callback) { |
71 scoped_refptr<TransportContext> transport_context, | |
72 scoped_ptr<Authenticator> authenticator, | |
73 const std::string& host_jid, | |
74 HostEventCallback* event_callback) { | |
75 DCHECK(client_stub_); | 49 DCHECK(client_stub_); |
76 DCHECK(clipboard_stub_); | 50 DCHECK(clipboard_stub_); |
77 DCHECK(monitored_video_stub_); | 51 DCHECK(monitored_video_stub_); |
78 | 52 |
79 // Initialize default |candidate_config_| if set_candidate_config() wasn't | 53 session_ = session.Pass(); |
80 // called. | 54 session_->SetEventHandler(this); |
81 if (!candidate_config_) | |
82 candidate_config_ = CandidateSessionConfig::CreateDefault(); | |
83 if (!audio_stub_) | |
84 candidate_config_->DisableAudioChannel(); | |
85 | 55 |
86 signal_strategy_ = signal_strategy; | |
87 event_callback_ = event_callback; | 56 event_callback_ = event_callback; |
88 authenticator_ = authenticator.Pass(); | |
89 | |
90 // Save jid of the host. The actual connection is created later after | |
91 // |signal_strategy_| is connected. | |
92 host_jid_ = host_jid; | |
93 | |
94 signal_strategy_->AddListener(this); | |
95 | |
96 session_manager_.reset(new JingleSessionManager( | |
97 make_scoped_ptr(new IceTransportFactory(transport_context)), | |
98 signal_strategy)); | |
99 session_manager_->set_protocol_config(candidate_config_->Clone()); | |
100 | 57 |
101 SetState(CONNECTING, OK); | 58 SetState(CONNECTING, OK); |
102 | |
103 switch (signal_strategy_->GetState()) { | |
104 case SignalStrategy::CONNECTING: | |
105 // Nothing to do here. Just need to wait until |signal_strategy_| becomes | |
106 // connected. | |
107 break; | |
108 case SignalStrategy::CONNECTED: | |
109 StartSession(); | |
110 break; | |
111 case SignalStrategy::DISCONNECTED: | |
112 signal_strategy_->Connect(); | |
113 break; | |
114 } | |
115 } | |
116 | |
117 void ConnectionToHostImpl::set_candidate_config( | |
118 scoped_ptr<CandidateSessionConfig> config) { | |
119 DCHECK_EQ(state_, INITIALIZING); | |
120 | |
121 candidate_config_ = config.Pass(); | |
122 } | 59 } |
123 | 60 |
124 const SessionConfig& ConnectionToHostImpl::config() { | 61 const SessionConfig& ConnectionToHostImpl::config() { |
125 return session_->config(); | 62 return session_->config(); |
126 } | 63 } |
127 | 64 |
128 ClipboardStub* ConnectionToHostImpl::clipboard_forwarder() { | 65 ClipboardStub* ConnectionToHostImpl::clipboard_forwarder() { |
129 return &clipboard_forwarder_; | 66 return &clipboard_forwarder_; |
130 } | 67 } |
131 | 68 |
(...skipping 20 matching lines...) Expand all Loading... |
152 video_stub, base::TimeDelta::FromSeconds( | 89 video_stub, base::TimeDelta::FromSeconds( |
153 MonitoredVideoStub::kConnectivityCheckDelaySeconds), | 90 MonitoredVideoStub::kConnectivityCheckDelaySeconds), |
154 base::Bind(&ConnectionToHostImpl::OnVideoChannelStatus, | 91 base::Bind(&ConnectionToHostImpl::OnVideoChannelStatus, |
155 base::Unretained(this)))); | 92 base::Unretained(this)))); |
156 } | 93 } |
157 | 94 |
158 void ConnectionToHostImpl::set_audio_stub(AudioStub* audio_stub) { | 95 void ConnectionToHostImpl::set_audio_stub(AudioStub* audio_stub) { |
159 audio_stub_ = audio_stub; | 96 audio_stub_ = audio_stub; |
160 } | 97 } |
161 | 98 |
162 void ConnectionToHostImpl::StartSession() { | |
163 DCHECK(CalledOnValidThread()); | |
164 DCHECK_EQ(state_, CONNECTING); | |
165 | |
166 session_ = session_manager_->Connect(host_jid_, authenticator_.Pass()); | |
167 session_->SetEventHandler(this); | |
168 } | |
169 | |
170 void ConnectionToHostImpl::OnSignalStrategyStateChange( | |
171 SignalStrategy::State state) { | |
172 DCHECK(CalledOnValidThread()); | |
173 DCHECK(event_callback_); | |
174 | |
175 if (state == SignalStrategy::CONNECTED) { | |
176 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid(); | |
177 // After signaling has been connected we can try connecting to the host. | |
178 if (state_ == CONNECTING) | |
179 StartSession(); | |
180 } else if (state == SignalStrategy::DISCONNECTED) { | |
181 VLOG(1) << "Connection closed."; | |
182 CloseOnError(SIGNALING_ERROR); | |
183 } | |
184 } | |
185 | |
186 bool ConnectionToHostImpl::OnSignalStrategyIncomingStanza( | |
187 const buzz::XmlElement* stanza) { | |
188 return false; | |
189 } | |
190 | |
191 void ConnectionToHostImpl::OnSessionStateChange(Session::State state) { | 99 void ConnectionToHostImpl::OnSessionStateChange(Session::State state) { |
192 DCHECK(CalledOnValidThread()); | 100 DCHECK(CalledOnValidThread()); |
193 DCHECK(event_callback_); | 101 DCHECK(event_callback_); |
194 | 102 |
195 switch (state) { | 103 switch (state) { |
196 case Session::INITIALIZING: | 104 case Session::INITIALIZING: |
197 case Session::CONNECTING: | 105 case Session::CONNECTING: |
198 case Session::ACCEPTING: | 106 case Session::ACCEPTING: |
199 case Session::ACCEPTED: | 107 case Session::ACCEPTED: |
200 case Session::AUTHENTICATING: | 108 case Session::AUTHENTICATING: |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 226 |
319 if (state != state_) { | 227 if (state != state_) { |
320 state_ = state; | 228 state_ = state; |
321 error_ = error; | 229 error_ = error; |
322 event_callback_->OnConnectionState(state_, error_); | 230 event_callback_->OnConnectionState(state_, error_); |
323 } | 231 } |
324 } | 232 } |
325 | 233 |
326 } // namespace protocol | 234 } // namespace protocol |
327 } // namespace remoting | 235 } // namespace remoting |
OLD | NEW |