OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/client/plugin/pepper_port_allocator.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <utility> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/macros.h" | |
13 #include "base/strings/string_number_conversions.h" | |
14 #include "net/base/net_util.h" | |
15 #include "ppapi/c/pp_errors.h" | |
16 #include "ppapi/cpp/url_loader.h" | |
17 #include "ppapi/cpp/url_request_info.h" | |
18 #include "ppapi/cpp/url_response_info.h" | |
19 #include "ppapi/utility/completion_callback_factory.h" | |
20 #include "remoting/client/plugin/pepper_network_manager.h" | |
21 #include "remoting/client/plugin/pepper_packet_socket_factory.h" | |
22 #include "remoting/client/plugin/pepper_util.h" | |
23 #include "remoting/protocol/transport_context.h" | |
24 | |
25 namespace remoting { | |
26 | |
27 namespace { | |
28 | |
29 // Read buffer we allocate per read when reading response from | |
30 // URLLoader. Normally the response from URL loader is smaller than 1kB. | |
31 const int kReadSize = 1024; | |
32 | |
33 class PepperPortAllocatorSession : public protocol::PortAllocatorSessionBase { | |
34 public: | |
35 PepperPortAllocatorSession( | |
36 PepperPortAllocator* allocator, | |
37 const std::string& content_name, | |
38 int component, | |
39 const std::string& ice_username_fragment, | |
40 const std::string& ice_password); | |
41 ~PepperPortAllocatorSession() override; | |
42 | |
43 // PortAllocatorBase overrides. | |
44 void SendSessionRequest(const std::string& host) override; | |
45 | |
46 private: | |
47 void OnUrlOpened(int32_t result); | |
48 void ReadResponseBody(); | |
49 void OnResponseBodyRead(int32_t result); | |
50 | |
51 pp::InstanceHandle pp_instance_; | |
52 | |
53 cricket::ServerAddresses stun_hosts_; | |
54 | |
55 scoped_ptr<pp::URLLoader> relay_url_loader_; | |
56 std::vector<char> relay_response_body_; | |
57 bool relay_response_received_ = false; | |
58 | |
59 pp::CompletionCallbackFactory<PepperPortAllocatorSession> callback_factory_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(PepperPortAllocatorSession); | |
62 }; | |
63 | |
64 PepperPortAllocatorSession::PepperPortAllocatorSession( | |
65 PepperPortAllocator* allocator, | |
66 const std::string& content_name, | |
67 int component, | |
68 const std::string& ice_username_fragment, | |
69 const std::string& ice_password) | |
70 : PortAllocatorSessionBase(allocator, | |
71 content_name, | |
72 component, | |
73 ice_username_fragment, | |
74 ice_password), | |
75 pp_instance_(allocator->pp_instance()), | |
76 callback_factory_(this) {} | |
77 | |
78 PepperPortAllocatorSession::~PepperPortAllocatorSession() {} | |
79 | |
80 void PepperPortAllocatorSession::SendSessionRequest(const std::string& host) { | |
81 relay_url_loader_.reset(new pp::URLLoader(pp_instance_)); | |
82 pp::URLRequestInfo request_info(pp_instance_); | |
83 std::string url = "https://" + host + GetSessionRequestUrl() + "&sn=1"; | |
84 request_info.SetURL(url); | |
85 request_info.SetMethod("GET"); | |
86 std::stringstream headers; | |
87 headers << "X-Talk-Google-Relay-Auth: " << relay_token() << "\n\r"; | |
88 headers << "X-Google-Relay-Auth: " << relay_token() << "\n\r"; | |
89 headers << "X-Stream-Type: " << "chromoting" << "\n\r"; | |
90 request_info.SetHeaders(headers.str()); | |
91 | |
92 pp::CompletionCallback callback = | |
93 callback_factory_.NewCallback(&PepperPortAllocatorSession::OnUrlOpened); | |
94 int result = relay_url_loader_->Open(request_info, callback); | |
95 DCHECK_EQ(result, PP_OK_COMPLETIONPENDING); | |
96 } | |
97 | |
98 void PepperPortAllocatorSession::OnUrlOpened(int32_t result) { | |
99 if (result == PP_ERROR_ABORTED) { | |
100 return; | |
101 } | |
102 | |
103 if (result < 0) { | |
104 LOG(WARNING) << "URLLoader failed: " << result; | |
105 // Retry creating session. | |
106 TryCreateRelaySession(); | |
107 return; | |
108 } | |
109 | |
110 pp::URLResponseInfo response = relay_url_loader_->GetResponseInfo(); | |
111 DCHECK(!response.is_null()); | |
112 if (response.GetStatusCode() != 200) { | |
113 LOG(WARNING) << "Received HTTP status code " << response.GetStatusCode(); | |
114 // Retry creating session. | |
115 TryCreateRelaySession(); | |
116 return; | |
117 } | |
118 | |
119 relay_response_body_.clear(); | |
120 ReadResponseBody(); | |
121 } | |
122 | |
123 void PepperPortAllocatorSession::ReadResponseBody() { | |
124 int pos = relay_response_body_.size(); | |
125 relay_response_body_.resize(pos + kReadSize); | |
126 pp::CompletionCallback callback = callback_factory_.NewCallback( | |
127 &PepperPortAllocatorSession::OnResponseBodyRead); | |
128 int result = relay_url_loader_->ReadResponseBody(&relay_response_body_[pos], | |
129 kReadSize, | |
130 callback); | |
131 DCHECK_EQ(result, PP_OK_COMPLETIONPENDING); | |
132 } | |
133 | |
134 void PepperPortAllocatorSession::OnResponseBodyRead(int32_t result) { | |
135 if (result == PP_ERROR_ABORTED) { | |
136 return; | |
137 } | |
138 | |
139 if (result < 0) { | |
140 LOG(WARNING) << "Failed to read HTTP response body when " | |
141 "creating relay session: " << result; | |
142 // Retry creating session. | |
143 TryCreateRelaySession(); | |
144 return; | |
145 } | |
146 | |
147 // Resize the buffer in case we've read less than was requested. | |
148 CHECK_LE(result, kReadSize); | |
149 CHECK_GE(static_cast<int>(relay_response_body_.size()), kReadSize); | |
150 relay_response_body_.resize(relay_response_body_.size() - kReadSize + result); | |
151 | |
152 if (result == 0) { | |
153 relay_response_received_ = true; | |
154 ReceiveSessionResponse(std::string(relay_response_body_.begin(), | |
155 relay_response_body_.end())); | |
156 return; | |
157 } | |
158 | |
159 ReadResponseBody(); | |
160 } | |
161 | |
162 } // namespace | |
163 | |
164 PepperPortAllocator::PepperPortAllocator( | |
165 scoped_refptr<protocol::TransportContext> transport_context, | |
166 pp::InstanceHandle pp_instance) | |
167 : PortAllocatorBase( | |
168 make_scoped_ptr(new PepperNetworkManager(pp_instance)), | |
169 make_scoped_ptr(new PepperPacketSocketFactory(pp_instance)), | |
170 transport_context), | |
171 pp_instance_(pp_instance) {} | |
172 | |
173 PepperPortAllocator::~PepperPortAllocator() {} | |
174 | |
175 cricket::PortAllocatorSession* PepperPortAllocator::CreateSessionInternal( | |
176 const std::string& content_name, | |
177 int component, | |
178 const std::string& ice_username_fragment, | |
179 const std::string& ice_password) { | |
180 return new PepperPortAllocatorSession(this, content_name, component, | |
181 ice_username_fragment, ice_password); | |
182 } | |
183 | |
184 PepperPortAllocatorFactory::PepperPortAllocatorFactory( | |
185 pp::InstanceHandle pp_instance) | |
186 : pp_instance_(pp_instance) {} | |
187 | |
188 PepperPortAllocatorFactory::~PepperPortAllocatorFactory() {} | |
189 | |
190 scoped_ptr<cricket::PortAllocator> | |
191 PepperPortAllocatorFactory::CreatePortAllocator( | |
192 scoped_refptr<protocol::TransportContext> transport_context) { | |
193 return make_scoped_ptr( | |
194 new PepperPortAllocator(transport_context, pp_instance_)); | |
195 } | |
196 | |
197 } // namespace remoting | |
OLD | NEW |