| 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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 232 } |
| 233 | 233 |
| 234 void PepperSession::OnAccept(const JingleMessage& message, | 234 void PepperSession::OnAccept(const JingleMessage& message, |
| 235 JingleMessageReply* reply) { | 235 JingleMessageReply* reply) { |
| 236 if (state_ != CONNECTING) { | 236 if (state_ != CONNECTING) { |
| 237 *reply = JingleMessageReply(JingleMessageReply::UNEXPECTED_REQUEST); | 237 *reply = JingleMessageReply(JingleMessageReply::UNEXPECTED_REQUEST); |
| 238 return; | 238 return; |
| 239 } | 239 } |
| 240 | 240 |
| 241 if (!InitializeConfigFromDescription(message.description.get())) { | 241 if (!InitializeConfigFromDescription(message.description.get())) { |
| 242 OnError(ERROR_INCOMPATIBLE_PROTOCOL); | 242 OnError(INCOMPATIBLE_PROTOCOL); |
| 243 return; | 243 return; |
| 244 } | 244 } |
| 245 | 245 |
| 246 CreateChannels(); | 246 CreateChannels(); |
| 247 SetState(CONNECTED); | 247 SetState(CONNECTED); |
| 248 | 248 |
| 249 // In case there is transport information in the accept message. | 249 // In case there is transport information in the accept message. |
| 250 ProcessTransportInfo(message); | 250 ProcessTransportInfo(message); |
| 251 } | 251 } |
| 252 | 252 |
| 253 void PepperSession::ProcessTransportInfo(const JingleMessage& message) { | 253 void PepperSession::ProcessTransportInfo(const JingleMessage& message) { |
| 254 for (std::list<cricket::Candidate>::const_iterator it = | 254 for (std::list<cricket::Candidate>::const_iterator it = |
| 255 message.candidates.begin(); | 255 message.candidates.begin(); |
| 256 it != message.candidates.end(); ++it) { | 256 it != message.candidates.end(); ++it) { |
| 257 ChannelsMap::iterator channel = channels_.find(it->name()); | 257 ChannelsMap::iterator channel = channels_.find(it->name()); |
| 258 if (channel == channels_.end()) { | 258 if (channel == channels_.end()) { |
| 259 LOG(WARNING) << "Received candidate for unknown channel " << it->name(); | 259 LOG(WARNING) << "Received candidate for unknown channel " << it->name(); |
| 260 continue; | 260 continue; |
| 261 } | 261 } |
| 262 channel->second->AddRemoveCandidate(*it); | 262 channel->second->AddRemoveCandidate(*it); |
| 263 } | 263 } |
| 264 } | 264 } |
| 265 | 265 |
| 266 void PepperSession::OnTerminate(const JingleMessage& message, | 266 void PepperSession::OnTerminate(const JingleMessage& message, |
| 267 JingleMessageReply* reply) { | 267 JingleMessageReply* reply) { |
| 268 if (state_ == CONNECTING) { | 268 if (state_ == CONNECTING) { |
| 269 OnError(ERROR_SESSION_REJECTED); | 269 switch (message.reason) { |
| 270 case JingleMessage::DECLINE: |
| 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 } |
| 270 return; | 283 return; |
| 271 } | 284 } |
| 272 | 285 |
| 273 CloseInternal(false); | 286 CloseInternal(false); |
| 274 } | 287 } |
| 275 | 288 |
| 276 bool PepperSession::InitializeConfigFromDescription( | 289 bool PepperSession::InitializeConfigFromDescription( |
| 277 const ContentDescription* description) { | 290 const ContentDescription* description) { |
| 278 DCHECK(description); | 291 DCHECK(description); |
| 279 | 292 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 base::Bind(&PepperSession::OnChannelConnected, | 343 base::Bind(&PepperSession::OnChannelConnected, |
| 331 base::Unretained(this), &event_channel_socket_)); | 344 base::Unretained(this), &event_channel_socket_)); |
| 332 } | 345 } |
| 333 | 346 |
| 334 void PepperSession::OnChannelConnected( | 347 void PepperSession::OnChannelConnected( |
| 335 scoped_ptr<net::Socket>* socket_container, | 348 scoped_ptr<net::Socket>* socket_container, |
| 336 net::StreamSocket* socket) { | 349 net::StreamSocket* socket) { |
| 337 if (!socket) { | 350 if (!socket) { |
| 338 LOG(ERROR) << "Failed to connect control or events channel. " | 351 LOG(ERROR) << "Failed to connect control or events channel. " |
| 339 << "Terminating connection"; | 352 << "Terminating connection"; |
| 340 OnError(ERROR_CHANNEL_CONNECTION_FAILURE); | 353 OnError(CHANNEL_CONNECTION_ERROR); |
| 341 return; | 354 return; |
| 342 } | 355 } |
| 343 | 356 |
| 344 socket_container->reset(socket); | 357 socket_container->reset(socket); |
| 345 | 358 |
| 346 if (control_channel_socket_.get() && event_channel_socket_.get()) | 359 if (control_channel_socket_.get() && event_channel_socket_.get()) |
| 347 SetState(CONNECTED_CHANNELS); | 360 SetState(CONNECTED_CHANNELS); |
| 348 } | 361 } |
| 349 | 362 |
| 350 void PepperSession::CloseInternal(bool failed) { | 363 void PepperSession::CloseInternal(bool failed) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 369 DCHECK_NE(state_, FAILED); | 382 DCHECK_NE(state_, FAILED); |
| 370 | 383 |
| 371 state_ = new_state; | 384 state_ = new_state; |
| 372 if (state_change_callback_.get()) | 385 if (state_change_callback_.get()) |
| 373 state_change_callback_->Run(new_state); | 386 state_change_callback_->Run(new_state); |
| 374 } | 387 } |
| 375 } | 388 } |
| 376 | 389 |
| 377 } // namespace protocol | 390 } // namespace protocol |
| 378 } // namespace remoting | 391 } // namespace remoting |
| OLD | NEW |