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/client/plugin/pepper_port_allocator.h" | 5 #include "remoting/client/plugin/pepper_port_allocator.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
15 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
16 #include "ppapi/cpp/url_loader.h" | 16 #include "ppapi/cpp/url_loader.h" |
17 #include "ppapi/cpp/url_request_info.h" | 17 #include "ppapi/cpp/url_request_info.h" |
18 #include "ppapi/cpp/url_response_info.h" | 18 #include "ppapi/cpp/url_response_info.h" |
19 #include "ppapi/utility/completion_callback_factory.h" | 19 #include "ppapi/utility/completion_callback_factory.h" |
20 #include "remoting/client/plugin/pepper_network_manager.h" | 20 #include "remoting/client/plugin/pepper_network_manager.h" |
21 #include "remoting/client/plugin/pepper_packet_socket_factory.h" | 21 #include "remoting/client/plugin/pepper_packet_socket_factory.h" |
22 #include "remoting/client/plugin/pepper_util.h" | 22 #include "remoting/client/plugin/pepper_util.h" |
| 23 #include "remoting/protocol/transport_context.h" |
23 | 24 |
24 namespace remoting { | 25 namespace remoting { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 // Read buffer we allocate per read when reading response from | 29 // Read buffer we allocate per read when reading response from |
29 // URLLoader. Normally the response from URL loader is smaller than 1kB. | 30 // URLLoader. Normally the response from URL loader is smaller than 1kB. |
30 const int kReadSize = 1024; | 31 const int kReadSize = 1024; |
31 | 32 |
32 class PepperPortAllocatorSession : public protocol::PortAllocatorSessionBase { | 33 class PepperPortAllocatorSession : public protocol::PortAllocatorSessionBase { |
33 public: | 34 public: |
34 PepperPortAllocatorSession( | 35 PepperPortAllocatorSession( |
35 protocol::PortAllocatorBase* allocator, | 36 PepperPortAllocator* allocator, |
36 const std::string& content_name, | 37 const std::string& content_name, |
37 int component, | 38 int component, |
38 const std::string& ice_username_fragment, | 39 const std::string& ice_username_fragment, |
39 const std::string& ice_password, | 40 const std::string& ice_password); |
40 const std::vector<rtc::SocketAddress>& stun_hosts, | |
41 const std::vector<std::string>& relay_hosts, | |
42 const std::string& relay_token, | |
43 const pp::InstanceHandle& instance); | |
44 ~PepperPortAllocatorSession() override; | 41 ~PepperPortAllocatorSession() override; |
45 | 42 |
46 // PortAllocatorBase overrides. | 43 // PortAllocatorBase overrides. |
47 void ConfigReady(cricket::PortConfiguration* config) override; | |
48 void GetPortConfigurations() override; | |
49 void SendSessionRequest(const std::string& host) override; | 44 void SendSessionRequest(const std::string& host) override; |
50 | 45 |
51 private: | 46 private: |
52 void OnUrlOpened(int32_t result); | 47 void OnUrlOpened(int32_t result); |
53 void ReadResponseBody(); | 48 void ReadResponseBody(); |
54 void OnResponseBodyRead(int32_t result); | 49 void OnResponseBodyRead(int32_t result); |
55 | 50 |
56 pp::InstanceHandle instance_; | 51 pp::InstanceHandle pp_instance_; |
57 | 52 |
58 cricket::ServerAddresses stun_hosts_; | 53 cricket::ServerAddresses stun_hosts_; |
59 | 54 |
60 scoped_ptr<pp::URLLoader> relay_url_loader_; | 55 scoped_ptr<pp::URLLoader> relay_url_loader_; |
61 std::vector<char> relay_response_body_; | 56 std::vector<char> relay_response_body_; |
62 bool relay_response_received_ = false; | 57 bool relay_response_received_ = false; |
63 | 58 |
64 pp::CompletionCallbackFactory<PepperPortAllocatorSession> callback_factory_; | 59 pp::CompletionCallbackFactory<PepperPortAllocatorSession> callback_factory_; |
65 | 60 |
66 DISALLOW_COPY_AND_ASSIGN(PepperPortAllocatorSession); | 61 DISALLOW_COPY_AND_ASSIGN(PepperPortAllocatorSession); |
67 }; | 62 }; |
68 | 63 |
69 PepperPortAllocatorSession::PepperPortAllocatorSession( | 64 PepperPortAllocatorSession::PepperPortAllocatorSession( |
70 protocol::PortAllocatorBase* allocator, | 65 PepperPortAllocator* allocator, |
71 const std::string& content_name, | 66 const std::string& content_name, |
72 int component, | 67 int component, |
73 const std::string& ice_username_fragment, | 68 const std::string& ice_username_fragment, |
74 const std::string& ice_password, | 69 const std::string& ice_password) |
75 const std::vector<rtc::SocketAddress>& stun_hosts, | |
76 const std::vector<std::string>& relay_hosts, | |
77 const std::string& relay_token, | |
78 const pp::InstanceHandle& instance) | |
79 : PortAllocatorSessionBase(allocator, | 70 : PortAllocatorSessionBase(allocator, |
80 content_name, | 71 content_name, |
81 component, | 72 component, |
82 ice_username_fragment, | 73 ice_username_fragment, |
83 ice_password, | 74 ice_password), |
84 stun_hosts, | 75 pp_instance_(allocator->pp_instance()) {} |
85 relay_hosts, | |
86 relay_token), | |
87 instance_(instance), | |
88 stun_hosts_(stun_hosts.begin(), stun_hosts.end()), | |
89 callback_factory_(this) {} | |
90 | 76 |
91 PepperPortAllocatorSession::~PepperPortAllocatorSession() {} | 77 PepperPortAllocatorSession::~PepperPortAllocatorSession() {} |
92 | 78 |
93 void PepperPortAllocatorSession::ConfigReady( | |
94 cricket::PortConfiguration* config) { | |
95 // Filter out non-UDP relay ports, so that we don't try using TCP. | |
96 for (cricket::PortConfiguration::RelayList::iterator relay = | |
97 config->relays.begin(); relay != config->relays.end(); ++relay) { | |
98 cricket::PortList filtered_ports; | |
99 for (cricket::PortList::iterator port = | |
100 relay->ports.begin(); port != relay->ports.end(); ++port) { | |
101 if (port->proto == cricket::PROTO_UDP) { | |
102 filtered_ports.push_back(*port); | |
103 } | |
104 } | |
105 relay->ports = filtered_ports; | |
106 } | |
107 cricket::BasicPortAllocatorSession::ConfigReady(config); | |
108 } | |
109 | |
110 void PepperPortAllocatorSession::GetPortConfigurations() { | |
111 // Add a configuration without relay response first so local and STUN | |
112 // candidates can be allocated without waiting for the relay response. | |
113 ConfigReady(new cricket::PortConfiguration( | |
114 stun_hosts_, std::string(), std::string())); | |
115 | |
116 TryCreateRelaySession(); | |
117 } | |
118 | |
119 void PepperPortAllocatorSession::SendSessionRequest(const std::string& host) { | 79 void PepperPortAllocatorSession::SendSessionRequest(const std::string& host) { |
120 relay_url_loader_.reset(new pp::URLLoader(instance_)); | 80 relay_url_loader_.reset(new pp::URLLoader(pp_instance_)); |
121 pp::URLRequestInfo request_info(instance_); | 81 pp::URLRequestInfo request_info(pp_instance_); |
122 std::string url = "https://" + host + GetSessionRequestUrl() + "&sn=1"; | 82 std::string url = "https://" + host + GetSessionRequestUrl() + "&sn=1"; |
123 request_info.SetURL(url); | 83 request_info.SetURL(url); |
124 request_info.SetMethod("GET"); | 84 request_info.SetMethod("GET"); |
125 std::stringstream headers; | 85 std::stringstream headers; |
126 headers << "X-Talk-Google-Relay-Auth: " << relay_token() << "\n\r"; | 86 headers << "X-Talk-Google-Relay-Auth: " << relay_token() << "\n\r"; |
127 headers << "X-Google-Relay-Auth: " << relay_token() << "\n\r"; | 87 headers << "X-Google-Relay-Auth: " << relay_token() << "\n\r"; |
128 headers << "X-Stream-Type: " << "chromoting" << "\n\r"; | 88 headers << "X-Stream-Type: " << "chromoting" << "\n\r"; |
129 request_info.SetHeaders(headers.str()); | 89 request_info.SetHeaders(headers.str()); |
130 | 90 |
131 pp::CompletionCallback callback = | 91 pp::CompletionCallback callback = |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 ReceiveSessionResponse(std::string(relay_response_body_.begin(), | 153 ReceiveSessionResponse(std::string(relay_response_body_.begin(), |
194 relay_response_body_.end())); | 154 relay_response_body_.end())); |
195 return; | 155 return; |
196 } | 156 } |
197 | 157 |
198 ReadResponseBody(); | 158 ReadResponseBody(); |
199 } | 159 } |
200 | 160 |
201 } // namespace | 161 } // namespace |
202 | 162 |
203 // static | |
204 scoped_ptr<PepperPortAllocator> PepperPortAllocator::Create( | |
205 const pp::InstanceHandle& instance) { | |
206 return make_scoped_ptr(new PepperPortAllocator( | |
207 instance, make_scoped_ptr(new PepperNetworkManager(instance)), | |
208 make_scoped_ptr(new PepperPacketSocketFactory(instance)))); | |
209 } | |
210 | |
211 PepperPortAllocator::PepperPortAllocator( | 163 PepperPortAllocator::PepperPortAllocator( |
212 const pp::InstanceHandle& instance, | 164 scoped_refptr<protocol::TransportContext> transport_context, |
213 scoped_ptr<rtc::NetworkManager> network_manager, | 165 pp::InstanceHandle pp_instance) |
214 scoped_ptr<rtc::PacketSocketFactory> socket_factory) | 166 : PortAllocatorBase( |
215 : PortAllocatorBase(network_manager.get(), socket_factory.get()), | 167 make_scoped_ptr(new PepperNetworkManager(pp_instance)), |
216 instance_(instance), | 168 make_scoped_ptr(new PepperPacketSocketFactory(pp_instance)), |
217 network_manager_(std::move(network_manager)), | 169 transport_context), |
218 socket_factory_(std::move(socket_factory)) {} | 170 pp_instance_(pp_instance) {} |
219 | 171 |
220 PepperPortAllocator::~PepperPortAllocator() {} | 172 PepperPortAllocator::~PepperPortAllocator() {} |
221 | 173 |
222 cricket::PortAllocatorSession* PepperPortAllocator::CreateSessionInternal( | 174 cricket::PortAllocatorSession* PepperPortAllocator::CreateSessionInternal( |
223 const std::string& content_name, | 175 const std::string& content_name, |
224 int component, | 176 int component, |
225 const std::string& ice_username_fragment, | 177 const std::string& ice_username_fragment, |
226 const std::string& ice_password) { | 178 const std::string& ice_password) { |
227 return new PepperPortAllocatorSession( | 179 return new PepperPortAllocatorSession(this, content_name, component, |
228 this, content_name, component, ice_username_fragment, ice_password, | 180 ice_username_fragment, ice_password); |
229 stun_hosts(), relay_hosts(), relay_token(), instance_); | |
230 } | 181 } |
231 | 182 |
232 PepperPortAllocatorFactory::PepperPortAllocatorFactory( | 183 PepperPortAllocatorFactory::PepperPortAllocatorFactory( |
233 const pp::InstanceHandle& instance) | 184 pp::InstanceHandle pp_instance) |
234 : instance_(instance) {} | 185 : pp_instance_(pp_instance) {} |
235 | 186 |
236 PepperPortAllocatorFactory::~PepperPortAllocatorFactory() {} | 187 PepperPortAllocatorFactory::~PepperPortAllocatorFactory() {} |
237 | 188 |
238 scoped_ptr<protocol::PortAllocatorBase> | 189 scoped_ptr<cricket::PortAllocator> |
239 PepperPortAllocatorFactory::CreatePortAllocator() { | 190 PepperPortAllocatorFactory::CreatePortAllocator( |
240 return PepperPortAllocator::Create(instance_); | 191 scoped_refptr<protocol::TransportContext> transport_context) { |
| 192 return make_scoped_ptr( |
| 193 new PepperPortAllocator(transport_context, pp_instance_)); |
241 } | 194 } |
242 | 195 |
243 } // namespace remoting | 196 } // namespace remoting |
OLD | NEW |