Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: remoting/protocol/pepper_transport_factory.cc

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

Powered by Google App Engine
This is Rietveld 408576698