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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 ConnectionToHost::State ConnectionToHost::state() const { | 152 ConnectionToHost::State ConnectionToHost::state() const { |
153 return state_; | 153 return state_; |
154 } | 154 } |
155 | 155 |
156 void ConnectionToHost::OnSessionStateChange( | 156 void ConnectionToHost::OnSessionStateChange( |
157 Session::State state) { | 157 Session::State state) { |
158 DCHECK(message_loop_->BelongsToCurrentThread()); | 158 DCHECK(message_loop_->BelongsToCurrentThread()); |
159 DCHECK(event_callback_); | 159 DCHECK(event_callback_); |
160 | 160 |
161 switch (state) { | 161 switch (state) { |
| 162 case Session::INITIALIZING: |
| 163 case Session::CONNECTING: |
| 164 case Session::CONNECTED: |
| 165 // Don't care about these events. |
| 166 break; |
| 167 |
| 168 case Session::AUTHENTICATED: |
| 169 video_reader_.reset(VideoReader::Create( |
| 170 message_loop_, session_->config())); |
| 171 video_reader_->Init(session_.get(), video_stub_, base::Bind( |
| 172 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); |
| 173 |
| 174 control_dispatcher_.reset(new ClientControlDispatcher()); |
| 175 control_dispatcher_->Init(session_.get(), base::Bind( |
| 176 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); |
| 177 control_dispatcher_->set_client_stub(client_stub_); |
| 178 |
| 179 event_dispatcher_.reset(new ClientEventDispatcher()); |
| 180 event_dispatcher_->Init(session_.get(), base::Bind( |
| 181 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); |
| 182 break; |
| 183 |
| 184 case Session::CLOSED: |
| 185 CloseChannels(); |
| 186 SetState(CLOSED, OK); |
| 187 break; |
| 188 |
162 case Session::FAILED: | 189 case Session::FAILED: |
163 switch (session_->error()) { | 190 switch (session_->error()) { |
164 case Session::PEER_IS_OFFLINE: | 191 case Session::PEER_IS_OFFLINE: |
165 CloseOnError(HOST_IS_OFFLINE); | 192 CloseOnError(HOST_IS_OFFLINE); |
166 break; | 193 break; |
167 case Session::SESSION_REJECTED: | 194 case Session::SESSION_REJECTED: |
168 case Session::AUTHENTICATION_FAILED: | 195 case Session::AUTHENTICATION_FAILED: |
169 CloseOnError(SESSION_REJECTED); | 196 CloseOnError(SESSION_REJECTED); |
170 break; | 197 break; |
171 case Session::INCOMPATIBLE_PROTOCOL: | 198 case Session::INCOMPATIBLE_PROTOCOL: |
172 CloseOnError(INCOMPATIBLE_PROTOCOL); | 199 CloseOnError(INCOMPATIBLE_PROTOCOL); |
173 break; | 200 break; |
174 case Session::CHANNEL_CONNECTION_ERROR: | 201 case Session::CHANNEL_CONNECTION_ERROR: |
175 CloseOnError(NETWORK_FAILURE); | 202 CloseOnError(NETWORK_FAILURE); |
176 break; | 203 break; |
177 case Session::OK: | 204 case Session::OK: |
178 DLOG(FATAL) << "Error code isn't set"; | 205 DLOG(FATAL) << "Error code isn't set"; |
179 CloseOnError(NETWORK_FAILURE); | 206 CloseOnError(NETWORK_FAILURE); |
180 } | 207 } |
181 break; | 208 break; |
182 | |
183 case Session::CLOSED: | |
184 CloseChannels(); | |
185 SetState(CLOSED, OK); | |
186 break; | |
187 | |
188 case Session::CONNECTED: | |
189 video_reader_.reset(VideoReader::Create( | |
190 message_loop_, session_->config())); | |
191 video_reader_->Init(session_.get(), video_stub_, base::Bind( | |
192 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); | |
193 | |
194 control_dispatcher_.reset(new ClientControlDispatcher()); | |
195 control_dispatcher_->Init(session_.get(), base::Bind( | |
196 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); | |
197 control_dispatcher_->set_client_stub(client_stub_); | |
198 | |
199 event_dispatcher_.reset(new ClientEventDispatcher()); | |
200 event_dispatcher_->Init(session_.get(), base::Bind( | |
201 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); | |
202 break; | |
203 | |
204 default: | |
205 // Ignore the other states by default. | |
206 break; | |
207 } | 209 } |
208 } | 210 } |
209 | 211 |
210 void ConnectionToHost::OnChannelInitialized(bool successful) { | 212 void ConnectionToHost::OnChannelInitialized(bool successful) { |
211 if (!successful) { | 213 if (!successful) { |
212 LOG(ERROR) << "Failed to connect video channel"; | 214 LOG(ERROR) << "Failed to connect video channel"; |
213 CloseOnError(NETWORK_FAILURE); | 215 CloseOnError(NETWORK_FAILURE); |
214 return; | 216 return; |
215 } | 217 } |
216 | 218 |
217 NotifyIfChannelsReady(); | 219 NotifyIfChannelsReady(); |
218 } | 220 } |
219 | 221 |
220 void ConnectionToHost::NotifyIfChannelsReady() { | 222 void ConnectionToHost::NotifyIfChannelsReady() { |
221 if (control_dispatcher_.get() && control_dispatcher_->is_connected() && | 223 if (control_dispatcher_.get() && control_dispatcher_->is_connected() && |
222 event_dispatcher_.get() && event_dispatcher_->is_connected() && | 224 event_dispatcher_.get() && event_dispatcher_->is_connected() && |
223 video_reader_.get() && video_reader_->is_connected() && | 225 video_reader_.get() && video_reader_->is_connected() && |
224 state_ == CONNECTING) { | 226 state_ == CONNECTING) { |
225 SetState(CONNECTED, OK); | 227 SetState(CONNECTED, OK); |
226 SetState(AUTHENTICATED, OK); | |
227 } | 228 } |
228 } | 229 } |
229 | 230 |
230 void ConnectionToHost::CloseOnError(Error error) { | 231 void ConnectionToHost::CloseOnError(Error error) { |
231 CloseChannels(); | 232 CloseChannels(); |
232 SetState(FAILED, error); | 233 SetState(FAILED, error); |
233 } | 234 } |
234 | 235 |
235 void ConnectionToHost::CloseChannels() { | 236 void ConnectionToHost::CloseChannels() { |
236 control_dispatcher_.reset(); | 237 control_dispatcher_.reset(); |
237 event_dispatcher_.reset(); | 238 event_dispatcher_.reset(); |
238 video_reader_.reset(); | 239 video_reader_.reset(); |
239 } | 240 } |
240 | 241 |
241 void ConnectionToHost::SetState(State state, Error error) { | 242 void ConnectionToHost::SetState(State state, Error error) { |
242 DCHECK(message_loop_->BelongsToCurrentThread()); | 243 DCHECK(message_loop_->BelongsToCurrentThread()); |
243 // |error| should be specified only when |state| is set to FAILED. | 244 // |error| should be specified only when |state| is set to FAILED. |
244 DCHECK(state == FAILED || error == OK); | 245 DCHECK(state == FAILED || error == OK); |
245 | 246 |
246 if (state != state_) { | 247 if (state != state_) { |
247 state_ = state; | 248 state_ = state; |
248 error_ = error; | 249 error_ = error; |
249 event_callback_->OnConnectionState(state_, error_); | 250 event_callback_->OnConnectionState(state_, error_); |
250 } | 251 } |
251 } | 252 } |
252 | 253 |
253 } // namespace protocol | 254 } // namespace protocol |
254 } // namespace remoting | 255 } // namespace remoting |
OLD | NEW |