| 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/pepper_session.h" | 5 #include "remoting/protocol/pepper_session.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 // message. This is neccessary to be able to pack multiple candidates | 27 // message. This is neccessary to be able to pack multiple candidates |
| 28 // into one transport-info messages. The value needs to be greater | 28 // into one transport-info messages. The value needs to be greater |
| 29 // than zero because ports are opened asynchronously in the browser | 29 // than zero because ports are opened asynchronously in the browser |
| 30 // process. | 30 // process. |
| 31 const int kTransportInfoSendDelayMs = 2; | 31 const int kTransportInfoSendDelayMs = 2; |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 PepperSession::PepperSession(PepperSessionManager* session_manager) | 34 PepperSession::PepperSession(PepperSessionManager* session_manager) |
| 35 : session_manager_(session_manager), | 35 : session_manager_(session_manager), |
| 36 state_(INITIALIZING), | 36 state_(INITIALIZING), |
| 37 error_(ERROR_NO_ERROR) { | 37 error_(OK) { |
| 38 } | 38 } |
| 39 | 39 |
| 40 PepperSession::~PepperSession() { | 40 PepperSession::~PepperSession() { |
| 41 control_channel_socket_.reset(); | 41 control_channel_socket_.reset(); |
| 42 event_channel_socket_.reset(); | 42 event_channel_socket_.reset(); |
| 43 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); | 43 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); |
| 44 session_manager_->SessionDestroyed(this); | 44 session_manager_->SessionDestroyed(this); |
| 45 } | 45 } |
| 46 | 46 |
| 47 PepperSession::Error PepperSession::error() { | |
| 48 DCHECK(CalledOnValidThread()); | |
| 49 return error_; | |
| 50 } | |
| 51 | |
| 52 void PepperSession::SetStateChangeCallback(StateChangeCallback* callback) { | 47 void PepperSession::SetStateChangeCallback(StateChangeCallback* callback) { |
| 53 DCHECK(CalledOnValidThread()); | 48 DCHECK(CalledOnValidThread()); |
| 54 state_change_callback_.reset(callback); | 49 state_change_callback_.reset(callback); |
| 55 } | 50 } |
| 56 | 51 |
| 52 Session::Error PepperSession::error() { |
| 53 DCHECK(CalledOnValidThread()); |
| 54 return error_; |
| 55 } |
| 56 |
| 57 void PepperSession::StartConnection( | 57 void PepperSession::StartConnection( |
| 58 const std::string& peer_jid, | 58 const std::string& peer_jid, |
| 59 const std::string& peer_public_key, | 59 const std::string& peer_public_key, |
| 60 const std::string& client_token, | 60 const std::string& client_token, |
| 61 CandidateSessionConfig* config, | 61 CandidateSessionConfig* config, |
| 62 Session::StateChangeCallback* state_change_callback) { | 62 Session::StateChangeCallback* state_change_callback) { |
| 63 DCHECK(CalledOnValidThread()); | 63 DCHECK(CalledOnValidThread()); |
| 64 | 64 |
| 65 peer_jid_ = peer_jid; | 65 peer_jid_ = peer_jid; |
| 66 peer_public_key_ = peer_public_key; | 66 peer_public_key_ = peer_public_key; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 91 void PepperSession::OnSessionInitiateResponse( | 91 void PepperSession::OnSessionInitiateResponse( |
| 92 const buzz::XmlElement* response) { | 92 const buzz::XmlElement* response) { |
| 93 const std::string& type = response->Attr(buzz::QName("", "type")); | 93 const std::string& type = response->Attr(buzz::QName("", "type")); |
| 94 if (type != "result") { | 94 if (type != "result") { |
| 95 LOG(ERROR) << "Received error in response to session-initiate message: \"" | 95 LOG(ERROR) << "Received error in response to session-initiate message: \"" |
| 96 << response->Str() | 96 << response->Str() |
| 97 << "\" Terminating the session."; | 97 << "\" Terminating the session."; |
| 98 | 98 |
| 99 // TODO(sergeyu): There may be different reasons for error | 99 // TODO(sergeyu): There may be different reasons for error |
| 100 // here. Parse the response stanza to find failure reason. | 100 // here. Parse the response stanza to find failure reason. |
| 101 OnError(ERROR_PEER_IS_OFFLINE); | 101 OnError(PEER_IS_OFFLINE); |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 void PepperSession::OnError(Error error) { | 105 void PepperSession::OnError(Error error) { |
| 106 error_ = error; | 106 error_ = error; |
| 107 CloseInternal(true); | 107 CloseInternal(true); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void PepperSession::CreateStreamChannel( | 110 void PepperSession::CreateStreamChannel( |
| 111 const std::string& name, | 111 const std::string& name, |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 | 215 |
| 216 switch (message.action) { | 216 switch (message.action) { |
| 217 case JingleMessage::SESSION_ACCEPT: | 217 case JingleMessage::SESSION_ACCEPT: |
| 218 OnAccept(message, reply); | 218 OnAccept(message, reply); |
| 219 break; | 219 break; |
| 220 | 220 |
| 221 case JingleMessage::TRANSPORT_INFO: | 221 case JingleMessage::TRANSPORT_INFO: |
| 222 ProcessTransportInfo(message); | 222 ProcessTransportInfo(message); |
| 223 break; | 223 break; |
| 224 | 224 |
| 225 case JingleMessage::SESSION_REJECT: | |
| 226 OnReject(message, reply); | |
| 227 break; | |
| 228 | |
| 229 case JingleMessage::SESSION_TERMINATE: | 225 case JingleMessage::SESSION_TERMINATE: |
| 230 OnTerminate(message, reply); | 226 OnTerminate(message, reply); |
| 231 break; | 227 break; |
| 232 | 228 |
| 233 default: | 229 default: |
| 234 *reply = JingleMessageReply(JingleMessageReply::UNEXPECTED_REQUEST); | 230 *reply = JingleMessageReply(JingleMessageReply::UNEXPECTED_REQUEST); |
| 235 } | 231 } |
| 236 } | 232 } |
| 237 | 233 |
| 238 void PepperSession::OnAccept(const JingleMessage& message, | 234 void PepperSession::OnAccept(const JingleMessage& message, |
| 239 JingleMessageReply* reply) { | 235 JingleMessageReply* reply) { |
| 240 if (state_ != CONNECTING) { | 236 if (state_ != CONNECTING) { |
| 241 *reply = JingleMessageReply(JingleMessageReply::UNEXPECTED_REQUEST); | 237 *reply = JingleMessageReply(JingleMessageReply::UNEXPECTED_REQUEST); |
| 242 return; | 238 return; |
| 243 } | 239 } |
| 244 | 240 |
| 245 if (!InitializeConfigFromDescription(message.description.get())) { | 241 if (!InitializeConfigFromDescription(message.description.get())) { |
| 246 OnError(ERROR_INCOMPATIBLE_PROTOCOL); | 242 OnError(INCOMPATIBLE_PROTOCOL); |
| 247 return; | 243 return; |
| 248 } | 244 } |
| 249 | 245 |
| 250 CreateChannels(); | 246 CreateChannels(); |
| 251 SetState(CONNECTED); | 247 SetState(CONNECTED); |
| 252 | 248 |
| 253 // In case there is transport information in the accept message. | 249 // In case there is transport information in the accept message. |
| 254 ProcessTransportInfo(message); | 250 ProcessTransportInfo(message); |
| 255 } | 251 } |
| 256 | 252 |
| 257 void PepperSession::ProcessTransportInfo(const JingleMessage& message) { | 253 void PepperSession::ProcessTransportInfo(const JingleMessage& message) { |
| 258 for (std::list<cricket::Candidate>::const_iterator it = | 254 for (std::list<cricket::Candidate>::const_iterator it = |
| 259 message.candidates.begin(); | 255 message.candidates.begin(); |
| 260 it != message.candidates.end(); ++it) { | 256 it != message.candidates.end(); ++it) { |
| 261 ChannelsMap::iterator channel = channels_.find(it->name()); | 257 ChannelsMap::iterator channel = channels_.find(it->name()); |
| 262 if (channel == channels_.end()) { | 258 if (channel == channels_.end()) { |
| 263 LOG(WARNING) << "Received candidate for unknown channel " << it->name(); | 259 LOG(WARNING) << "Received candidate for unknown channel " << it->name(); |
| 264 continue; | 260 continue; |
| 265 } | 261 } |
| 266 channel->second->AddRemoveCandidate(*it); | 262 channel->second->AddRemoveCandidate(*it); |
| 267 } | 263 } |
| 268 } | 264 } |
| 269 | 265 |
| 270 void PepperSession::OnReject(const JingleMessage& message, | |
| 271 JingleMessageReply* reply) { | |
| 272 if (state_ != CONNECTING) { | |
| 273 *reply = JingleMessageReply(JingleMessageReply::UNEXPECTED_REQUEST); | |
| 274 return; | |
| 275 } | |
| 276 | |
| 277 // TODO(sergeyu): Parse exact rejection reason from reply and pass it | |
| 278 // to OnError(). | |
| 279 OnError(ERROR_SESSION_REJECTED); | |
| 280 } | |
| 281 | |
| 282 void PepperSession::OnTerminate(const JingleMessage& message, | 266 void PepperSession::OnTerminate(const JingleMessage& message, |
| 283 JingleMessageReply* reply) { | 267 JingleMessageReply* reply) { |
| 284 if (state_ == CONNECTING) { | 268 if (state_ == CONNECTING) { |
| 285 // If we are not connected yet, then interpret terminate message | 269 switch (message.reason) { |
| 286 // as rejection. | 270 case JingleMessage::DECLINED: |
| 287 OnError(ERROR_SESSION_REJECTED); | 271 OnError(SESSION_REJECTED); |
| 272 break; |
| 273 |
| 274 case JingleMessage::INCOMPATIBLE_PARAMETERS: |
| 275 OnError(INCOMPATIBLE_PROTOCOL); |
| 276 break; |
| 277 |
| 278 default: |
| 279 LOG(WARNING) << "Received session-terminate message " |
| 280 "with an unexpected reason."; |
| 281 OnError(SESSION_REJECTED); |
| 282 } |
| 288 return; | 283 return; |
| 289 } | 284 } |
| 290 | 285 |
| 291 CloseInternal(false); | 286 CloseInternal(false); |
| 292 } | 287 } |
| 293 | 288 |
| 294 bool PepperSession::InitializeConfigFromDescription( | 289 bool PepperSession::InitializeConfigFromDescription( |
| 295 const ContentDescription* description) { | 290 const ContentDescription* description) { |
| 296 DCHECK(description); | 291 DCHECK(description); |
| 297 | 292 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 base::Bind(&PepperSession::OnChannelConnected, | 343 base::Bind(&PepperSession::OnChannelConnected, |
| 349 base::Unretained(this), &event_channel_socket_)); | 344 base::Unretained(this), &event_channel_socket_)); |
| 350 } | 345 } |
| 351 | 346 |
| 352 void PepperSession::OnChannelConnected( | 347 void PepperSession::OnChannelConnected( |
| 353 scoped_ptr<net::Socket>* socket_container, | 348 scoped_ptr<net::Socket>* socket_container, |
| 354 net::StreamSocket* socket) { | 349 net::StreamSocket* socket) { |
| 355 if (!socket) { | 350 if (!socket) { |
| 356 LOG(ERROR) << "Failed to connect control or events channel. " | 351 LOG(ERROR) << "Failed to connect control or events channel. " |
| 357 << "Terminating connection"; | 352 << "Terminating connection"; |
| 358 OnError(ERROR_CHANNEL_CONNECTION_FAILURE); | 353 OnError(CHANNEL_CONNECTION_ERROR); |
| 359 return; | 354 return; |
| 360 } | 355 } |
| 361 | 356 |
| 362 socket_container->reset(socket); | 357 socket_container->reset(socket); |
| 363 | 358 |
| 364 if (control_channel_socket_.get() && event_channel_socket_.get()) | 359 if (control_channel_socket_.get() && event_channel_socket_.get()) |
| 365 SetState(CONNECTED_CHANNELS); | 360 SetState(CONNECTED_CHANNELS); |
| 366 } | 361 } |
| 367 | 362 |
| 368 void PepperSession::CloseInternal(bool failed) { | 363 void PepperSession::CloseInternal(bool failed) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 387 DCHECK_NE(state_, FAILED); | 382 DCHECK_NE(state_, FAILED); |
| 388 | 383 |
| 389 state_ = new_state; | 384 state_ = new_state; |
| 390 if (state_change_callback_.get()) | 385 if (state_change_callback_.get()) |
| 391 state_change_callback_->Run(new_state); | 386 state_change_callback_->Run(new_state); |
| 392 } | 387 } |
| 393 } | 388 } |
| 394 | 389 |
| 395 } // namespace protocol | 390 } // namespace protocol |
| 396 } // namespace remoting | 391 } // namespace remoting |
| OLD | NEW |