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

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) 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());
64 126
65 pp::Transport_Dev* transport = 127 pp::Transport_Dev* transport =
66 new pp::Transport_Dev(pp_instance, name_.c_str(), 128 new pp::Transport_Dev(pp_instance_, name_.c_str(),
67 PP_TRANSPORTTYPE_STREAM); 129 PP_TRANSPORTTYPE_STREAM);
68 130
69 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_RECEIVE_WINDOW, 131 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_RECEIVE_WINDOW,
70 pp::Var(kTcpReceiveBufferSize)) != PP_OK) { 132 pp::Var(kTcpReceiveBufferSize)) != PP_OK) {
71 LOG(ERROR) << "Failed to set TCP receive window"; 133 LOG(ERROR) << "Failed to set TCP receive window";
72 } 134 }
73 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_SEND_WINDOW, 135 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_SEND_WINDOW,
74 pp::Var(kTcpSendBufferSize)) != PP_OK) { 136 pp::Var(kTcpSendBufferSize)) != PP_OK) {
75 LOG(ERROR) << "Failed to set TCP send window"; 137 LOG(ERROR) << "Failed to set TCP send window";
76 } 138 }
77 139
78 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_NO_DELAY, 140 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_NO_DELAY,
79 pp::Var(true)) != PP_OK) { 141 pp::Var(true)) != PP_OK) {
80 LOG(ERROR) << "Failed to set TCP_NODELAY"; 142 LOG(ERROR) << "Failed to set TCP_NODELAY";
81 } 143 }
82 144
83 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_ACK_DELAY, 145 if (transport->SetProperty(PP_TRANSPORTPROPERTY_TCP_ACK_DELAY,
84 pp::Var(kTcpAckDelayMilliseconds)) != PP_OK) { 146 pp::Var(kTcpAckDelayMilliseconds)) != PP_OK) {
85 LOG(ERROR) << "Failed to set TCP ACK delay."; 147 LOG(ERROR) << "Failed to set TCP ACK delay.";
86 } 148 }
87 149
88 if (transport_config.nat_traversal) { 150 if (config_.nat_traversal) {
89 if (transport->SetProperty( 151 if (transport->SetProperty(
90 PP_TRANSPORTPROPERTY_STUN_SERVER, 152 PP_TRANSPORTPROPERTY_STUN_SERVER,
91 pp::Var(transport_config.stun_server)) != PP_OK) { 153 pp::Var(config_.stun_server)) != PP_OK) {
92 LOG(ERROR) << "Failed to set STUN server."; 154 LOG(ERROR) << "Failed to set STUN server.";
93 } 155 }
94 156
95 if (transport->SetProperty( 157 if (transport->SetProperty(
96 PP_TRANSPORTPROPERTY_RELAY_SERVER, 158 PP_TRANSPORTPROPERTY_RELAY_SERVER,
97 pp::Var(transport_config.relay_server)) != PP_OK) { 159 pp::Var(config_.relay_server)) != PP_OK) {
98 LOG(ERROR) << "Failed to set relay server."; 160 LOG(ERROR) << "Failed to set relay server.";
99 } 161 }
100 162
101 if (transport->SetProperty( 163 if (transport->SetProperty(
102 PP_TRANSPORTPROPERTY_RELAY_PASSWORD, 164 PP_TRANSPORTPROPERTY_RELAY_PASSWORD,
103 pp::Var(transport_config.relay_token)) != PP_OK) { 165 pp::Var(config_.relay_token)) != PP_OK) {
104 LOG(ERROR) << "Failed to set relay token."; 166 LOG(ERROR) << "Failed to set relay token.";
105 } 167 }
106 168
107 if (transport->SetProperty( 169 if (transport->SetProperty(
108 PP_TRANSPORTPROPERTY_RELAY_MODE, 170 PP_TRANSPORTPROPERTY_RELAY_MODE,
109 pp::Var(PP_TRANSPORTRELAYMODE_GOOGLE)) != PP_OK) { 171 pp::Var(PP_TRANSPORTRELAYMODE_GOOGLE)) != PP_OK) {
110 LOG(ERROR) << "Failed to set relay mode."; 172 LOG(ERROR) << "Failed to set relay mode.";
111 } 173 }
112 } 174 }
113 175
114 if (transport->SetProperty(PP_TRANSPORTPROPERTY_DISABLE_TCP_TRANSPORT, 176 if (transport->SetProperty(PP_TRANSPORTPROPERTY_DISABLE_TCP_TRANSPORT,
115 pp::Var(true)) != PP_OK) { 177 pp::Var(true)) != PP_OK) {
116 LOG(ERROR) << "Failed to set DISABLE_TCP_TRANSPORT flag."; 178 LOG(ERROR) << "Failed to set DISABLE_TCP_TRANSPORT flag.";
117 } 179 }
118 180
119 channel_ = new PepperTransportSocketAdapter(transport, name_, this); 181 channel_ = new PepperTransportSocketAdapter(transport, name_, this);
120 owned_channel_.reset(channel_); 182 owned_channel_.reset(channel_);
121 183
122 int result = channel_->Connect(base::Bind(&PepperStreamChannel::OnP2PConnect, 184 int result = channel_->Connect(
123 base::Unretained(this))); 185 base::Bind(&PepperStreamTransport::OnP2PConnect, base::Unretained(this)));
124 if (result != net::ERR_IO_PENDING) 186 if (result != net::ERR_IO_PENDING)
125 OnP2PConnect(result); 187 OnP2PConnect(result);
126 } 188 }
127 189
128 void PepperStreamChannel::AddRemoteCandidate( 190 void PepperStreamTransport::AddRemoteCandidate(
129 const cricket::Candidate& candidate) { 191 const cricket::Candidate& candidate) {
130 DCHECK(CalledOnValidThread()); 192 DCHECK(CalledOnValidThread());
131 if (channel_) 193 if (channel_)
132 channel_->AddRemoteCandidate(jingle_glue::SerializeP2PCandidate(candidate)); 194 channel_->AddRemoteCandidate(jingle_glue::SerializeP2PCandidate(candidate));
133 } 195 }
134 196
135 const std::string& PepperStreamChannel::name() const { 197 const std::string& PepperStreamTransport::name() const {
136 DCHECK(CalledOnValidThread()); 198 DCHECK(CalledOnValidThread());
137 return name_; 199 return name_;
138 } 200 }
139 201
140 bool PepperStreamChannel::is_connected() const { 202 bool PepperStreamTransport::is_connected() const {
141 DCHECK(CalledOnValidThread()); 203 DCHECK(CalledOnValidThread());
142 return connected_; 204 return connected_;
143 } 205 }
144 206
145 void PepperStreamChannel::OnChannelDeleted() { 207 void PepperStreamTransport::OnChannelDeleted() {
146 if (connected_) { 208 if (connected_) {
147 channel_ = NULL; 209 channel_ = NULL;
148 // The PepperTransportSocketAdapter is being deleted, so delete 210 // The PepperTransportSocketAdapter is being deleted, so delete
149 // the channel too. 211 // the channel too.
150 delete this; 212 delete this;
151 } 213 }
152 } 214 }
153 215
154 void PepperStreamChannel::OnChannelNewLocalCandidate( 216 void PepperStreamTransport::OnChannelNewLocalCandidate(
155 const std::string& candidate) { 217 const std::string& candidate) {
156 DCHECK(CalledOnValidThread()); 218 DCHECK(CalledOnValidThread());
157 219
158 cricket::Candidate candidate_value; 220 cricket::Candidate candidate_value;
159 if (!jingle_glue::DeserializeP2PCandidate(candidate, &candidate_value)) { 221 if (!jingle_glue::DeserializeP2PCandidate(candidate, &candidate_value)) {
160 LOG(ERROR) << "Failed to parse candidate " << candidate; 222 LOG(ERROR) << "Failed to parse candidate " << candidate;
161 } 223 }
162 session_->AddLocalCandidate(candidate_value); 224 event_handler_->OnTransportCandidate(this, candidate_value);
163 } 225 }
164 226
165 void PepperStreamChannel::OnP2PConnect(int result) { 227 void PepperStreamTransport::OnP2PConnect(int result) {
166 DCHECK(CalledOnValidThread()); 228 DCHECK(CalledOnValidThread());
167 229
168 if (result != net::OK) 230 if (result != net::OK) {
169 NotifyConnectFailed(); 231 NotifyConnectFailed();
232 return;
233 }
170 234
171 authenticator_->SecureAndAuthenticate( 235 authenticator_->SecureAndAuthenticate(
172 owned_channel_.PassAs<net::StreamSocket>(), 236 owned_channel_.PassAs<net::StreamSocket>(),
173 base::Bind(&PepperStreamChannel::OnAuthenticationDone, 237 base::Bind(&PepperStreamTransport::OnAuthenticationDone,
174 base::Unretained(this))); 238 base::Unretained(this)));
175 } 239 }
176 240
177 241
178 void PepperStreamChannel::OnAuthenticationDone( 242 void PepperStreamTransport::OnAuthenticationDone(
179 net::Error error, scoped_ptr<net::StreamSocket> socket) { 243 net::Error error, scoped_ptr<net::StreamSocket> socket) {
180 DCHECK(CalledOnValidThread()); 244 DCHECK(CalledOnValidThread());
181 if (error != net::OK) { 245 if (error != net::OK) {
182 NotifyConnectFailed(); 246 NotifyConnectFailed();
183 return; 247 return;
184 } 248 }
185 249
186 NotifyConnected(socket.Pass()); 250 NotifyConnected(socket.Pass());
187 } 251 }
188 252
189 void PepperStreamChannel::NotifyConnected( 253 void PepperStreamTransport::NotifyConnected(
190 scoped_ptr<net::StreamSocket> socket) { 254 scoped_ptr<net::StreamSocket> socket) {
191 DCHECK(!connected_); 255 DCHECK(!connected_);
192 callback_.Run(socket.Pass()); 256 callback_.Run(socket.Pass());
193 connected_ = true; 257 connected_ = true;
194 } 258 }
195 259
196 void PepperStreamChannel::NotifyConnectFailed() { 260 void PepperStreamTransport::NotifyConnectFailed() {
197 channel_ = NULL; 261 channel_ = NULL;
198 owned_channel_.reset(); 262 owned_channel_.reset();
199 authenticator_.reset(); 263 authenticator_.reset();
200 264
201 NotifyConnected(scoped_ptr<net::StreamSocket>(NULL)); 265 NotifyConnected(scoped_ptr<net::StreamSocket>(NULL));
202 } 266 }
203 267
268 } // namespace
269
270 PepperTransportFactory::PepperTransportFactory(
271 pp::Instance* pp_instance)
272 : pp_instance_(pp_instance) {
273 }
274
275 PepperTransportFactory::~PepperTransportFactory() {
276 }
277
278 scoped_ptr<StreamTransport> PepperTransportFactory::CreateStreamTransport() {
279 return scoped_ptr<StreamTransport>(new PepperStreamTransport(pp_instance_));
280 }
281
282 scoped_ptr<DatagramTransport>
283 PepperTransportFactory::CreateDatagramTransport() {
284 NOTIMPLEMENTED();
285 return scoped_ptr<DatagramTransport>(NULL);
286 }
287
204 } // namespace protocol 288 } // namespace protocol
205 } // namespace remoting 289 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698