| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_stream_channel.h" | 5 #include "remoting/protocol/pepper_transport_factory.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "crypto/hmac.h" | 8 #include "crypto/hmac.h" |
| 9 #include "jingle/glue/utils.h" | 9 #include "jingle/glue/utils.h" |
| 10 #include "net/base/cert_status_flags.h" | 10 #include "net/base/cert_status_flags.h" |
| 11 #include "net/base/cert_verifier.h" | 11 #include "net/base/cert_verifier.h" |
| 12 #include "net/base/host_port_pair.h" | 12 #include "net/base/host_port_pair.h" |
| 13 #include "net/base/ssl_config_service.h" | 13 #include "net/base/ssl_config_service.h" |
| 14 #include "net/socket/ssl_client_socket.h" | 14 #include "net/socket/ssl_client_socket.h" |
| 15 #include "net/socket/client_socket_factory.h" | 15 #include "net/socket/client_socket_factory.h" |
| 16 #include "ppapi/c/pp_errors.h" | 16 #include "ppapi/c/pp_errors.h" |
| 17 #include "ppapi/cpp/dev/transport_dev.h" | 17 #include "ppapi/cpp/dev/transport_dev.h" |
| 18 #include "ppapi/cpp/var.h" | 18 #include "ppapi/cpp/var.h" |
| 19 #include "remoting/protocol/channel_authenticator.h" | 19 #include "remoting/protocol/channel_authenticator.h" |
| 20 #include "remoting/protocol/pepper_session.h" | 20 #include "remoting/protocol/pepper_transport_socket_adapter.h" |
| 21 #include "remoting/protocol/transport_config.h" | 21 #include "remoting/protocol/transport_config.h" |
| 22 #include "third_party/libjingle/source/talk/p2p/base/candidate.h" | 22 #include "third_party/libjingle/source/talk/p2p/base/candidate.h" |
| 23 | 23 |
| 24 namespace remoting { | 24 namespace remoting { |
| 25 namespace protocol { | 25 namespace protocol { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // Value is choosen to balance the extra latency against the reduced | 29 // Value is choosen to balance the extra latency against the reduced |
| 30 // load due to ACK traffic. | 30 // load due to ACK traffic. |
| 31 const int kTcpAckDelayMilliseconds = 10; | 31 const int kTcpAckDelayMilliseconds = 10; |
| 32 | 32 |
| 33 // Values for the TCP send and receive buffer size. This should be tuned to | 33 // Values for the TCP send and receive buffer size. This should be tuned to |
| 34 // accomodate high latency network but not backlog the decoding pipeline. | 34 // accomodate high latency network but not backlog the decoding pipeline. |
| 35 const int kTcpReceiveBufferSize = 256 * 1024; | 35 const int kTcpReceiveBufferSize = 256 * 1024; |
| 36 const int kTcpSendBufferSize = kTcpReceiveBufferSize + 30 * 1024; | 36 const int kTcpSendBufferSize = kTcpReceiveBufferSize + 30 * 1024; |
| 37 | 37 |
| 38 } // namespace | 38 class PepperStreamTransport : public StreamTransport, |
| 39 public PepperTransportSocketAdapter::Observer { |
| 40 public: |
| 41 PepperStreamTransport(pp::Instance* pp_instance); |
| 42 virtual ~PepperStreamTransport(); |
| 39 | 43 |
| 40 PepperStreamChannel::PepperStreamChannel( | 44 // StreamTransport interface. |
| 41 PepperSession* session, | 45 virtual void Initialize( |
| 42 const std::string& name, | 46 const std::string& name, |
| 43 const Session::StreamChannelCallback& callback) | 47 const TransportConfig& config, |
| 44 : session_(session), | 48 Transport::EventHandler* event_handler, |
| 45 name_(name), | 49 scoped_ptr<ChannelAuthenticator> authenticator) OVERRIDE; |
| 46 callback_(callback), | 50 virtual void Connect( |
| 51 const StreamTransport::ConnectedCallback& callback) OVERRIDE; |
| 52 virtual void AddRemoteCandidate(const cricket::Candidate& candidate) OVERRIDE; |
| 53 virtual const std::string& name() const OVERRIDE; |
| 54 virtual bool is_connected() const OVERRIDE; |
| 55 |
| 56 // PepperTransportSocketAdapter::Observer interface. |
| 57 virtual void OnChannelDeleted() OVERRIDE; |
| 58 virtual void OnChannelNewLocalCandidate( |
| 59 const std::string& candidate) OVERRIDE; |
| 60 |
| 61 private: |
| 62 void OnP2PConnect(int result); |
| 63 void OnAuthenticationDone(net::Error error, |
| 64 scoped_ptr<net::StreamSocket> socket); |
| 65 |
| 66 void NotifyConnected(scoped_ptr<net::StreamSocket> socket); |
| 67 void NotifyConnectFailed(); |
| 68 |
| 69 pp::Instance* pp_instance_; |
| 70 std::string name_; |
| 71 TransportConfig config_; |
| 72 EventHandler* event_handler_; |
| 73 StreamTransport::ConnectedCallback callback_; |
| 74 scoped_ptr<ChannelAuthenticator> authenticator_; |
| 75 |
| 76 // We own |channel_| until it is connected. After that |
| 77 // |authenticator_| owns it. |
| 78 scoped_ptr<PepperTransportSocketAdapter> owned_channel_; |
| 79 PepperTransportSocketAdapter* channel_; |
| 80 |
| 81 // Indicates that we've finished connecting. |
| 82 bool connected_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(PepperStreamTransport); |
| 85 }; |
| 86 |
| 87 PepperStreamTransport::PepperStreamTransport(pp::Instance* pp_instance) |
| 88 : pp_instance_(pp_instance), |
| 89 event_handler_(NULL), |
| 47 channel_(NULL), | 90 channel_(NULL), |
| 48 connected_(false) { | 91 connected_(false) { |
| 49 } | 92 } |
| 50 | 93 |
| 51 PepperStreamChannel::~PepperStreamChannel() { | 94 PepperStreamTransport::~PepperStreamTransport() { |
| 52 session_->OnDeleteChannel(this); | 95 DCHECK(event_handler_); |
| 96 event_handler_->OnTransportDeleted(this); |
| 53 // Channel should be already destroyed if we were connected. | 97 // Channel should be already destroyed if we were connected. |
| 54 DCHECK(!connected_ || channel_ == NULL); | 98 DCHECK(!connected_ || channel_ == NULL); |
| 55 } | 99 } |
| 56 | 100 |
| 57 void PepperStreamChannel::Connect( | 101 void PepperStreamTransport::Initialize( |
| 58 pp::Instance* pp_instance, | 102 const std::string& name, |
| 59 const TransportConfig& transport_config, | 103 const TransportConfig& config, |
| 104 Transport::EventHandler* event_handler, |
| 60 scoped_ptr<ChannelAuthenticator> authenticator) { | 105 scoped_ptr<ChannelAuthenticator> authenticator) { |
| 61 DCHECK(CalledOnValidThread()); | 106 DCHECK(CalledOnValidThread()); |
| 62 | 107 |
| 108 DCHECK(!name.empty()); |
| 109 DCHECK(event_handler); |
| 110 |
| 111 // Can be initialized only once. |
| 112 DCHECK(name_.empty()); |
| 113 |
| 114 name_ = name; |
| 115 config_ = config; |
| 116 event_handler_ = event_handler; |
| 63 authenticator_ = authenticator.Pass(); | 117 authenticator_ = authenticator.Pass(); |
| 118 } |
| 119 |
| 120 void PepperStreamTransport::Connect( |
| 121 const StreamTransport::ConnectedCallback& callback) { |
| 122 DCHECK(CalledOnValidThread()); |
| 123 |
| 124 // Initialize() must be called first. |
| 125 DCHECK(!name_.empty()); |
| 126 |
| 127 callback_ = callback; |
| 64 | 128 |
| 65 pp::Transport_Dev* transport = | 129 pp::Transport_Dev* transport = |
| 66 new pp::Transport_Dev(pp_instance, name_.c_str(), | 130 new pp::Transport_Dev(pp_instance_, name_.c_str(), |
| 67 PP_TRANSPORTTYPE_STREAM); | 131 PP_TRANSPORTTYPE_STREAM); |
| 68 | 132 |
| 69 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_RECEIVE_WINDOW, | 133 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_RECEIVE_WINDOW, |
| 70 pp::Var(kTcpReceiveBufferSize)) != PP_OK) { | 134 pp::Var(kTcpReceiveBufferSize)) != PP_OK) { |
| 71 LOG(ERROR) << "Failed to set TCP receive window"; | 135 LOG(ERROR) << "Failed to set TCP receive window"; |
| 72 } | 136 } |
| 73 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_SEND_WINDOW, | 137 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_SEND_WINDOW, |
| 74 pp::Var(kTcpSendBufferSize)) != PP_OK) { | 138 pp::Var(kTcpSendBufferSize)) != PP_OK) { |
| 75 LOG(ERROR) << "Failed to set TCP send window"; | 139 LOG(ERROR) << "Failed to set TCP send window"; |
| 76 } | 140 } |
| 77 | 141 |
| 78 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_NO_DELAY, | 142 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_NO_DELAY, |
| 79 pp::Var(true)) != PP_OK) { | 143 pp::Var(true)) != PP_OK) { |
| 80 LOG(ERROR) << "Failed to set TCP_NODELAY"; | 144 LOG(ERROR) << "Failed to set TCP_NODELAY"; |
| 81 } | 145 } |
| 82 | 146 |
| 83 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_ACK_DELAY, | 147 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_ACK_DELAY, |
| 84 pp::Var(kTcpAckDelayMilliseconds)) != PP_OK) { | 148 pp::Var(kTcpAckDelayMilliseconds)) != PP_OK) { |
| 85 LOG(ERROR) << "Failed to set TCP ACK delay."; | 149 LOG(ERROR) << "Failed to set TCP ACK delay."; |
| 86 } | 150 } |
| 87 | 151 |
| 88 if (transport_config.nat_traversal) { | 152 if (config_.nat_traversal) { |
| 89 if (transport->SetProperty( | 153 if (transport->SetProperty( |
| 90 PP_TRANSPORTPROPERTY_STUN_SERVER, | 154 PP_TRANSPORTPROPERTY_STUN_SERVER, |
| 91 pp::Var(transport_config.stun_server)) != PP_OK) { | 155 pp::Var(config_.stun_server)) != PP_OK) { |
| 92 LOG(ERROR) << "Failed to set STUN server."; | 156 LOG(ERROR) << "Failed to set STUN server."; |
| 93 } | 157 } |
| 94 | 158 |
| 95 if (transport->SetProperty( | 159 if (transport->SetProperty( |
| 96 PP_TRANSPORTPROPERTY_RELAY_SERVER, | 160 PP_TRANSPORTPROPERTY_RELAY_SERVER, |
| 97 pp::Var(transport_config.relay_server)) != PP_OK) { | 161 pp::Var(config_.relay_server)) != PP_OK) { |
| 98 LOG(ERROR) << "Failed to set relay server."; | 162 LOG(ERROR) << "Failed to set relay server."; |
| 99 } | 163 } |
| 100 | 164 |
| 101 if (transport->SetProperty( | 165 if (transport->SetProperty( |
| 102 PP_TRANSPORTPROPERTY_RELAY_PASSWORD, | 166 PP_TRANSPORTPROPERTY_RELAY_PASSWORD, |
| 103 pp::Var(transport_config.relay_token)) != PP_OK) { | 167 pp::Var(config_.relay_token)) != PP_OK) { |
| 104 LOG(ERROR) << "Failed to set relay token."; | 168 LOG(ERROR) << "Failed to set relay token."; |
| 105 } | 169 } |
| 106 | 170 |
| 107 if (transport->SetProperty( | 171 if (transport->SetProperty( |
| 108 PP_TRANSPORTPROPERTY_RELAY_MODE, | 172 PP_TRANSPORTPROPERTY_RELAY_MODE, |
| 109 pp::Var(PP_TRANSPORTRELAYMODE_GOOGLE)) != PP_OK) { | 173 pp::Var(PP_TRANSPORTRELAYMODE_GOOGLE)) != PP_OK) { |
| 110 LOG(ERROR) << "Failed to set relay mode."; | 174 LOG(ERROR) << "Failed to set relay mode."; |
| 111 } | 175 } |
| 112 } | 176 } |
| 113 | 177 |
| 114 if (transport->SetProperty(PP_TRANSPORTPROPERTY_DISABLE_TCP_TRANSPORT, | 178 if (transport->SetProperty(PP_TRANSPORTPROPERTY_DISABLE_TCP_TRANSPORT, |
| 115 pp::Var(true)) != PP_OK) { | 179 pp::Var(true)) != PP_OK) { |
| 116 LOG(ERROR) << "Failed to set DISABLE_TCP_TRANSPORT flag."; | 180 LOG(ERROR) << "Failed to set DISABLE_TCP_TRANSPORT flag."; |
| 117 } | 181 } |
| 118 | 182 |
| 119 channel_ = new PepperTransportSocketAdapter(transport, name_, this); | 183 channel_ = new PepperTransportSocketAdapter(transport, name_, this); |
| 120 owned_channel_.reset(channel_); | 184 owned_channel_.reset(channel_); |
| 121 | 185 |
| 122 int result = channel_->Connect(base::Bind(&PepperStreamChannel::OnP2PConnect, | 186 int result = channel_->Connect( |
| 123 base::Unretained(this))); | 187 base::Bind(&PepperStreamTransport::OnP2PConnect, base::Unretained(this))); |
| 124 if (result != net::ERR_IO_PENDING) | 188 if (result != net::ERR_IO_PENDING) |
| 125 OnP2PConnect(result); | 189 OnP2PConnect(result); |
| 126 } | 190 } |
| 127 | 191 |
| 128 void PepperStreamChannel::AddRemoteCandidate( | 192 void PepperStreamTransport::AddRemoteCandidate( |
| 129 const cricket::Candidate& candidate) { | 193 const cricket::Candidate& candidate) { |
| 130 DCHECK(CalledOnValidThread()); | 194 DCHECK(CalledOnValidThread()); |
| 131 if (channel_) | 195 if (channel_) |
| 132 channel_->AddRemoteCandidate(jingle_glue::SerializeP2PCandidate(candidate)); | 196 channel_->AddRemoteCandidate(jingle_glue::SerializeP2PCandidate(candidate)); |
| 133 } | 197 } |
| 134 | 198 |
| 135 const std::string& PepperStreamChannel::name() const { | 199 const std::string& PepperStreamTransport::name() const { |
| 136 DCHECK(CalledOnValidThread()); | 200 DCHECK(CalledOnValidThread()); |
| 137 return name_; | 201 return name_; |
| 138 } | 202 } |
| 139 | 203 |
| 140 bool PepperStreamChannel::is_connected() const { | 204 bool PepperStreamTransport::is_connected() const { |
| 141 DCHECK(CalledOnValidThread()); | 205 DCHECK(CalledOnValidThread()); |
| 142 return connected_; | 206 return connected_; |
| 143 } | 207 } |
| 144 | 208 |
| 145 void PepperStreamChannel::OnChannelDeleted() { | 209 void PepperStreamTransport::OnChannelDeleted() { |
| 146 if (connected_) { | 210 if (connected_) { |
| 147 channel_ = NULL; | 211 channel_ = NULL; |
| 148 // The PepperTransportSocketAdapter is being deleted, so delete | 212 // The PepperTransportSocketAdapter is being deleted, so delete |
| 149 // the channel too. | 213 // the channel too. |
| 150 delete this; | 214 delete this; |
| 151 } | 215 } |
| 152 } | 216 } |
| 153 | 217 |
| 154 void PepperStreamChannel::OnChannelNewLocalCandidate( | 218 void PepperStreamTransport::OnChannelNewLocalCandidate( |
| 155 const std::string& candidate) { | 219 const std::string& candidate) { |
| 156 DCHECK(CalledOnValidThread()); | 220 DCHECK(CalledOnValidThread()); |
| 157 | 221 |
| 158 cricket::Candidate candidate_value; | 222 cricket::Candidate candidate_value; |
| 159 if (!jingle_glue::DeserializeP2PCandidate(candidate, &candidate_value)) { | 223 if (!jingle_glue::DeserializeP2PCandidate(candidate, &candidate_value)) { |
| 160 LOG(ERROR) << "Failed to parse candidate " << candidate; | 224 LOG(ERROR) << "Failed to parse candidate " << candidate; |
| 161 } | 225 } |
| 162 session_->AddLocalCandidate(candidate_value); | 226 event_handler_->OnTransportCandidate(this, candidate_value); |
| 163 } | 227 } |
| 164 | 228 |
| 165 void PepperStreamChannel::OnP2PConnect(int result) { | 229 void PepperStreamTransport::OnP2PConnect(int result) { |
| 166 DCHECK(CalledOnValidThread()); | 230 DCHECK(CalledOnValidThread()); |
| 167 | 231 |
| 168 if (result != net::OK) | 232 if (result != net::OK) { |
| 169 NotifyConnectFailed(); | 233 NotifyConnectFailed(); |
| 234 return; |
| 235 } |
| 170 | 236 |
| 171 authenticator_->SecureAndAuthenticate( | 237 authenticator_->SecureAndAuthenticate( |
| 172 owned_channel_.PassAs<net::StreamSocket>(), | 238 owned_channel_.PassAs<net::StreamSocket>(), |
| 173 base::Bind(&PepperStreamChannel::OnAuthenticationDone, | 239 base::Bind(&PepperStreamTransport::OnAuthenticationDone, |
| 174 base::Unretained(this))); | 240 base::Unretained(this))); |
| 175 } | 241 } |
| 176 | 242 |
| 177 | 243 |
| 178 void PepperStreamChannel::OnAuthenticationDone( | 244 void PepperStreamTransport::OnAuthenticationDone( |
| 179 net::Error error, scoped_ptr<net::StreamSocket> socket) { | 245 net::Error error, scoped_ptr<net::StreamSocket> socket) { |
| 180 DCHECK(CalledOnValidThread()); | 246 DCHECK(CalledOnValidThread()); |
| 181 if (error != net::OK) { | 247 if (error != net::OK) { |
| 182 NotifyConnectFailed(); | 248 NotifyConnectFailed(); |
| 183 return; | 249 return; |
| 184 } | 250 } |
| 185 | 251 |
| 186 NotifyConnected(socket.Pass()); | 252 NotifyConnected(socket.Pass()); |
| 187 } | 253 } |
| 188 | 254 |
| 189 void PepperStreamChannel::NotifyConnected( | 255 void PepperStreamTransport::NotifyConnected( |
| 190 scoped_ptr<net::StreamSocket> socket) { | 256 scoped_ptr<net::StreamSocket> socket) { |
| 191 DCHECK(!connected_); | 257 DCHECK(!connected_); |
| 192 callback_.Run(socket.Pass()); | 258 callback_.Run(socket.Pass()); |
| 193 connected_ = true; | 259 connected_ = true; |
| 194 } | 260 } |
| 195 | 261 |
| 196 void PepperStreamChannel::NotifyConnectFailed() { | 262 void PepperStreamTransport::NotifyConnectFailed() { |
| 197 channel_ = NULL; | 263 channel_ = NULL; |
| 198 owned_channel_.reset(); | 264 owned_channel_.reset(); |
| 199 authenticator_.reset(); | 265 authenticator_.reset(); |
| 200 | 266 |
| 201 NotifyConnected(scoped_ptr<net::StreamSocket>(NULL)); | 267 NotifyConnected(scoped_ptr<net::StreamSocket>(NULL)); |
| 202 } | 268 } |
| 203 | 269 |
| 270 } // namespace |
| 271 |
| 272 PepperTransportFactory::PepperTransportFactory( |
| 273 pp::Instance* pp_instance) |
| 274 : pp_instance_(pp_instance) { |
| 275 } |
| 276 |
| 277 PepperTransportFactory::~PepperTransportFactory() { |
| 278 } |
| 279 |
| 280 scoped_ptr<StreamTransport> PepperTransportFactory::CreateStreamTransport() { |
| 281 return scoped_ptr<StreamTransport>(new PepperStreamTransport(pp_instance_)); |
| 282 } |
| 283 |
| 284 scoped_ptr<DatagramTransport> |
| 285 PepperTransportFactory::CreateDatagramTransport() { |
| 286 NOTIMPLEMENTED(); |
| 287 return scoped_ptr<DatagramTransport>(NULL); |
| 288 } |
| 289 |
| 204 } // namespace protocol | 290 } // namespace protocol |
| 205 } // namespace remoting | 291 } // namespace remoting |
| OLD | NEW |