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 protocol::PortAllocatorBase* 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: |
47 PepperPortAllocator* allocator() override { | |
48 return static_cast<PepperPortAllocator*>( | |
49 BasicPortAllocatorSession::allocator()); | |
Jamie
2016/01/09 00:12:07
Optional: I'm not a big fan of this cast either, t
Sergey Ulanov
2016/01/11 20:01:04
Done.
| |
50 } | |
51 | |
52 void OnUrlOpened(int32_t result); | 52 void OnUrlOpened(int32_t result); |
53 void ReadResponseBody(); | 53 void ReadResponseBody(); |
54 void OnResponseBodyRead(int32_t result); | 54 void OnResponseBodyRead(int32_t result); |
55 | 55 |
56 pp::InstanceHandle instance_; | |
57 | |
58 cricket::ServerAddresses stun_hosts_; | 56 cricket::ServerAddresses stun_hosts_; |
59 | 57 |
60 scoped_ptr<pp::URLLoader> relay_url_loader_; | 58 scoped_ptr<pp::URLLoader> relay_url_loader_; |
61 std::vector<char> relay_response_body_; | 59 std::vector<char> relay_response_body_; |
62 bool relay_response_received_ = false; | 60 bool relay_response_received_ = false; |
63 | 61 |
64 pp::CompletionCallbackFactory<PepperPortAllocatorSession> callback_factory_; | 62 pp::CompletionCallbackFactory<PepperPortAllocatorSession> callback_factory_; |
65 | 63 |
66 DISALLOW_COPY_AND_ASSIGN(PepperPortAllocatorSession); | 64 DISALLOW_COPY_AND_ASSIGN(PepperPortAllocatorSession); |
67 }; | 65 }; |
68 | 66 |
69 PepperPortAllocatorSession::PepperPortAllocatorSession( | 67 PepperPortAllocatorSession::PepperPortAllocatorSession( |
70 protocol::PortAllocatorBase* allocator, | 68 protocol::PortAllocatorBase* allocator, |
71 const std::string& content_name, | 69 const std::string& content_name, |
72 int component, | 70 int component, |
73 const std::string& ice_username_fragment, | 71 const std::string& ice_username_fragment, |
74 const std::string& ice_password, | 72 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, | 73 : PortAllocatorSessionBase(allocator, |
80 content_name, | 74 content_name, |
81 component, | 75 component, |
82 ice_username_fragment, | 76 ice_username_fragment, |
83 ice_password, | 77 ice_password) {} |
84 stun_hosts, | |
85 relay_hosts, | |
86 relay_token), | |
87 instance_(instance), | |
88 stun_hosts_(stun_hosts.begin(), stun_hosts.end()), | |
89 callback_factory_(this) {} | |
90 | 78 |
91 PepperPortAllocatorSession::~PepperPortAllocatorSession() {} | 79 PepperPortAllocatorSession::~PepperPortAllocatorSession() {} |
92 | 80 |
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) { | 81 void PepperPortAllocatorSession::SendSessionRequest(const std::string& host) { |
120 relay_url_loader_.reset(new pp::URLLoader(instance_)); | 82 relay_url_loader_.reset(new pp::URLLoader(allocator()->instance())); |
121 pp::URLRequestInfo request_info(instance_); | 83 pp::URLRequestInfo request_info(allocator()->instance()); |
122 std::string url = "https://" + host + GetSessionRequestUrl() + "&sn=1"; | 84 std::string url = "https://" + host + GetSessionRequestUrl() + "&sn=1"; |
123 request_info.SetURL(url); | 85 request_info.SetURL(url); |
124 request_info.SetMethod("GET"); | 86 request_info.SetMethod("GET"); |
125 std::stringstream headers; | 87 std::stringstream headers; |
126 headers << "X-Talk-Google-Relay-Auth: " << relay_token() << "\n\r"; | 88 headers << "X-Talk-Google-Relay-Auth: " << relay_token() << "\n\r"; |
127 headers << "X-Google-Relay-Auth: " << relay_token() << "\n\r"; | 89 headers << "X-Google-Relay-Auth: " << relay_token() << "\n\r"; |
128 headers << "X-Stream-Type: " << "chromoting" << "\n\r"; | 90 headers << "X-Stream-Type: " << "chromoting" << "\n\r"; |
129 request_info.SetHeaders(headers.str()); | 91 request_info.SetHeaders(headers.str()); |
130 | 92 |
131 pp::CompletionCallback callback = | 93 pp::CompletionCallback callback = |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 ReceiveSessionResponse(std::string(relay_response_body_.begin(), | 155 ReceiveSessionResponse(std::string(relay_response_body_.begin(), |
194 relay_response_body_.end())); | 156 relay_response_body_.end())); |
195 return; | 157 return; |
196 } | 158 } |
197 | 159 |
198 ReadResponseBody(); | 160 ReadResponseBody(); |
199 } | 161 } |
200 | 162 |
201 } // namespace | 163 } // namespace |
202 | 164 |
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( | 165 PepperPortAllocator::PepperPortAllocator( |
212 const pp::InstanceHandle& instance, | 166 scoped_refptr<protocol::TransportContext> transport_context, |
213 scoped_ptr<rtc::NetworkManager> network_manager, | 167 pp::InstanceHandle instance) |
214 scoped_ptr<rtc::PacketSocketFactory> socket_factory) | 168 : PortAllocatorBase( |
215 : PortAllocatorBase(network_manager.get(), socket_factory.get()), | 169 make_scoped_ptr(new PepperNetworkManager(instance)), |
216 instance_(instance), | 170 make_scoped_ptr(new PepperPacketSocketFactory(instance)), |
217 network_manager_(std::move(network_manager)), | 171 transport_context), |
218 socket_factory_(std::move(socket_factory)) {} | 172 instance_(instance) {} |
219 | 173 |
220 PepperPortAllocator::~PepperPortAllocator() {} | 174 PepperPortAllocator::~PepperPortAllocator() {} |
221 | 175 |
222 cricket::PortAllocatorSession* PepperPortAllocator::CreateSessionInternal( | 176 cricket::PortAllocatorSession* PepperPortAllocator::CreateSessionInternal( |
223 const std::string& content_name, | 177 const std::string& content_name, |
224 int component, | 178 int component, |
225 const std::string& ice_username_fragment, | 179 const std::string& ice_username_fragment, |
226 const std::string& ice_password) { | 180 const std::string& ice_password) { |
227 return new PepperPortAllocatorSession( | 181 return new PepperPortAllocatorSession(this, content_name, component, |
228 this, content_name, component, ice_username_fragment, ice_password, | 182 ice_username_fragment, ice_password); |
229 stun_hosts(), relay_hosts(), relay_token(), instance_); | |
230 } | 183 } |
231 | 184 |
232 PepperPortAllocatorFactory::PepperPortAllocatorFactory( | 185 PepperPortAllocatorFactory::PepperPortAllocatorFactory( |
233 const pp::InstanceHandle& instance) | 186 pp::InstanceHandle instance) |
234 : instance_(instance) {} | 187 : instance_(instance) {} |
235 | 188 |
236 PepperPortAllocatorFactory::~PepperPortAllocatorFactory() {} | 189 PepperPortAllocatorFactory::~PepperPortAllocatorFactory() {} |
237 | 190 |
238 scoped_ptr<protocol::PortAllocatorBase> | 191 scoped_ptr<cricket::PortAllocator> |
239 PepperPortAllocatorFactory::CreatePortAllocator() { | 192 PepperPortAllocatorFactory::CreatePortAllocator( |
240 return PepperPortAllocator::Create(instance_); | 193 scoped_refptr<protocol::TransportContext> transport_context) { |
194 return make_scoped_ptr(new PepperPortAllocator(transport_context, instance_)); | |
241 } | 195 } |
242 | 196 |
243 } // namespace remoting | 197 } // namespace remoting |
OLD | NEW |