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/libjingle_transport_factory.h" | 5 #include "remoting/protocol/libjingle_transport_factory.h" |
6 | 6 |
7 #include "base/base64.h" | |
8 #include "base/callback.h" | 7 #include "base/callback.h" |
9 #include "base/rand_util.h" | |
10 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
11 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
12 #include "base/timer/timer.h" | 10 #include "base/timer/timer.h" |
13 #include "jingle/glue/channel_socket_adapter.h" | 11 #include "jingle/glue/channel_socket_adapter.h" |
14 #include "jingle/glue/pseudotcp_adapter.h" | 12 #include "jingle/glue/pseudotcp_adapter.h" |
15 #include "jingle/glue/utils.h" | 13 #include "jingle/glue/utils.h" |
16 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
17 #include "remoting/base/constants.h" | 15 #include "remoting/base/constants.h" |
18 #include "remoting/jingle_glue/jingle_info_request.h" | 16 #include "remoting/jingle_glue/jingle_info_request.h" |
19 #include "remoting/jingle_glue/network_settings.h" | 17 #include "remoting/jingle_glue/network_settings.h" |
(...skipping 18 matching lines...) Expand all Loading... |
38 const int kTcpReceiveBufferSize = 256 * 1024; | 36 const int kTcpReceiveBufferSize = 256 * 1024; |
39 const int kTcpSendBufferSize = kTcpReceiveBufferSize + 30 * 1024; | 37 const int kTcpSendBufferSize = kTcpReceiveBufferSize + 30 * 1024; |
40 | 38 |
41 // Try connecting ICE twice with timeout of 15 seconds for each attempt. | 39 // Try connecting ICE twice with timeout of 15 seconds for each attempt. |
42 const int kMaxReconnectAttempts = 2; | 40 const int kMaxReconnectAttempts = 2; |
43 const int kReconnectDelaySeconds = 15; | 41 const int kReconnectDelaySeconds = 15; |
44 | 42 |
45 // Get fresh STUN/Relay configuration every hour. | 43 // Get fresh STUN/Relay configuration every hour. |
46 const int kJingleInfoUpdatePeriodSeconds = 3600; | 44 const int kJingleInfoUpdatePeriodSeconds = 3600; |
47 | 45 |
48 // TODO(sergeyu): Remove this function and use talk_base::CreateRandomString() | |
49 // when it's fixed to work reliably. See crbug.com/364689 . | |
50 std::string CreateRandomString(int length) { | |
51 // Number of random bytes to generate base64 string at least |length| | |
52 // characters long. | |
53 int raw_length = (length + 1) * 3 / 4; | |
54 std::string base64; | |
55 base::Base64Encode(base::RandBytesAsString(raw_length), &base64); | |
56 DCHECK(static_cast<int>(base64.size()) == length || | |
57 static_cast<int>(base64.size()) == length + 1); | |
58 base64.resize(length); | |
59 return base64; | |
60 } | |
61 | |
62 class LibjingleStreamTransport | 46 class LibjingleStreamTransport |
63 : public StreamTransport, | 47 : public StreamTransport, |
64 public base::SupportsWeakPtr<LibjingleStreamTransport>, | 48 public base::SupportsWeakPtr<LibjingleStreamTransport>, |
65 public sigslot::has_slots<> { | 49 public sigslot::has_slots<> { |
66 public: | 50 public: |
67 LibjingleStreamTransport(cricket::PortAllocator* port_allocator, | 51 LibjingleStreamTransport(cricket::PortAllocator* port_allocator, |
68 const NetworkSettings& network_settings); | 52 const NetworkSettings& network_settings); |
69 virtual ~LibjingleStreamTransport(); | 53 virtual ~LibjingleStreamTransport(); |
70 | 54 |
71 // Called by JingleTransportFactory when it has fresh Jingle info. | 55 // Called by JingleTransportFactory when it has fresh Jingle info. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 118 |
135 DISALLOW_COPY_AND_ASSIGN(LibjingleStreamTransport); | 119 DISALLOW_COPY_AND_ASSIGN(LibjingleStreamTransport); |
136 }; | 120 }; |
137 | 121 |
138 LibjingleStreamTransport::LibjingleStreamTransport( | 122 LibjingleStreamTransport::LibjingleStreamTransport( |
139 cricket::PortAllocator* port_allocator, | 123 cricket::PortAllocator* port_allocator, |
140 const NetworkSettings& network_settings) | 124 const NetworkSettings& network_settings) |
141 : port_allocator_(port_allocator), | 125 : port_allocator_(port_allocator), |
142 network_settings_(network_settings), | 126 network_settings_(network_settings), |
143 event_handler_(NULL), | 127 event_handler_(NULL), |
144 ice_username_fragment_(CreateRandomString(cricket::ICE_UFRAG_LENGTH)), | 128 ice_username_fragment_( |
145 ice_password_(CreateRandomString(cricket::ICE_PWD_LENGTH)), | 129 talk_base::CreateRandomString(cricket::ICE_UFRAG_LENGTH)), |
| 130 ice_password_(talk_base::CreateRandomString(cricket::ICE_PWD_LENGTH)), |
146 can_start_(false), | 131 can_start_(false), |
147 channel_was_writable_(false), | 132 channel_was_writable_(false), |
148 connect_attempts_left_(kMaxReconnectAttempts) { | 133 connect_attempts_left_(kMaxReconnectAttempts) { |
149 DCHECK(!ice_username_fragment_.empty()); | 134 DCHECK(!ice_username_fragment_.empty()); |
150 DCHECK(!ice_password_.empty()); | 135 DCHECK(!ice_password_.empty()); |
151 } | 136 } |
152 | 137 |
153 LibjingleStreamTransport::~LibjingleStreamTransport() { | 138 LibjingleStreamTransport::~LibjingleStreamTransport() { |
154 DCHECK(event_handler_); | 139 DCHECK(event_handler_); |
155 event_handler_->OnTransportDeleted(this); | 140 event_handler_->OnTransportDeleted(this); |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 reconnect_timer_.Stop(); | 363 reconnect_timer_.Stop(); |
379 | 364 |
380 // Notify the caller that ICE connection has failed - normally that will | 365 // Notify the caller that ICE connection has failed - normally that will |
381 // terminate Jingle connection (i.e. the transport will be destroyed). | 366 // terminate Jingle connection (i.e. the transport will be destroyed). |
382 event_handler_->OnTransportFailed(this); | 367 event_handler_->OnTransportFailed(this); |
383 return; | 368 return; |
384 } | 369 } |
385 --connect_attempts_left_; | 370 --connect_attempts_left_; |
386 | 371 |
387 // Restart ICE by resetting ICE password. | 372 // Restart ICE by resetting ICE password. |
388 ice_password_ = CreateRandomString(cricket::ICE_PWD_LENGTH); | 373 ice_password_ = talk_base::CreateRandomString(cricket::ICE_PWD_LENGTH); |
389 channel_->SetIceCredentials(ice_username_fragment_, ice_password_); | 374 channel_->SetIceCredentials(ice_username_fragment_, ice_password_); |
390 } | 375 } |
391 | 376 |
392 void LibjingleStreamTransport::NotifyConnected( | 377 void LibjingleStreamTransport::NotifyConnected( |
393 scoped_ptr<net::StreamSocket> socket) { | 378 scoped_ptr<net::StreamSocket> socket) { |
394 DCHECK(!is_connected()); | 379 DCHECK(!is_connected()); |
395 StreamTransport::ConnectedCallback callback = callback_; | 380 StreamTransport::ConnectedCallback callback = callback_; |
396 callback_.Reset(); | 381 callback_.Reset(); |
397 callback.Run(socket.Pass()); | 382 callback.Run(socket.Pass()); |
398 } | 383 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 last_jingle_info_update_time_ = base::TimeTicks::Now(); | 480 last_jingle_info_update_time_ = base::TimeTicks::Now(); |
496 | 481 |
497 while (!on_jingle_info_callbacks_.empty()) { | 482 while (!on_jingle_info_callbacks_.empty()) { |
498 on_jingle_info_callbacks_.begin()->Run(); | 483 on_jingle_info_callbacks_.begin()->Run(); |
499 on_jingle_info_callbacks_.pop_front(); | 484 on_jingle_info_callbacks_.pop_front(); |
500 } | 485 } |
501 } | 486 } |
502 | 487 |
503 } // namespace protocol | 488 } // namespace protocol |
504 } // namespace remoting | 489 } // namespace remoting |
OLD | NEW |