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

Side by Side Diff: blimp/client/core/session/assignment_source.cc

Issue 2211613002: Add AssignmentSource to BlimpClientContextImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge origin/master Created 4 years, 4 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "blimp/client/core/session/assignment_source.h" 5 #include "blimp/client/core/session/assignment_source.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback_helpers.h" 8 #include "base/callback_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 if (!assignment.IsValid()) { 161 if (!assignment.IsValid()) {
162 DLOG(FATAL) << "Invalid command-line assignment."; 162 DLOG(FATAL) << "Invalid command-line assignment.";
163 return Assignment(); 163 return Assignment();
164 } 164 }
165 165
166 return assignment; 166 return assignment;
167 } 167 }
168 168
169 } // namespace 169 } // namespace
170 170
171 Assignment::Assignment() : transport_protocol(TransportProtocol::UNKNOWN) {}
172
173 Assignment::Assignment(const Assignment& other) = default;
174
175 Assignment::~Assignment() {}
176
177 bool Assignment::IsValid() const {
178 if (engine_endpoint.address().empty() || engine_endpoint.port() == 0 ||
179 transport_protocol == TransportProtocol::UNKNOWN) {
180 return false;
181 }
182 if (transport_protocol == TransportProtocol::SSL && !cert) {
183 return false;
184 }
185 return true;
186 }
187
188 AssignmentSource::AssignmentSource( 171 AssignmentSource::AssignmentSource(
189 const GURL& assigner_endpoint, 172 const GURL& assigner_endpoint,
190 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner, 173 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
191 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) 174 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
192 : assigner_endpoint_(assigner_endpoint), 175 : assigner_endpoint_(assigner_endpoint),
193 file_task_runner_(std::move(file_task_runner)), 176 file_task_runner_(std::move(file_task_runner)),
194 url_request_context_( 177 url_request_context_(
195 new SimpleURLRequestContextGetter(network_task_runner)), 178 new SimpleURLRequestContextGetter(network_task_runner)),
196 weak_factory_(this) { 179 weak_factory_(this) {
197 DCHECK(assigner_endpoint_.is_valid()); 180 DCHECK(assigner_endpoint_.is_valid());
(...skipping 16 matching lines...) Expand all
214 QueryAssigner(client_auth_token); 197 QueryAssigner(client_auth_token);
215 } 198 }
216 } 199 }
217 200
218 void AssignmentSource::OnGetAssignmentFromCommandLineDone( 201 void AssignmentSource::OnGetAssignmentFromCommandLineDone(
219 const std::string& client_auth_token, 202 const std::string& client_auth_token,
220 Assignment parsed_assignment) { 203 Assignment parsed_assignment) {
221 // If GetAssignmentFromCommandLine succeeded, then return its output. 204 // If GetAssignmentFromCommandLine succeeded, then return its output.
222 if (parsed_assignment.IsValid()) { 205 if (parsed_assignment.IsValid()) {
223 base::ResetAndReturn(&callback_) 206 base::ResetAndReturn(&callback_)
224 .Run(AssignmentSource::RESULT_OK, parsed_assignment); 207 .Run(ASSIGNMENT_REQUEST_RESULT_OK, parsed_assignment);
225 return; 208 return;
226 } 209 }
227 210
228 // If no assignment was passed via the command line, then fall back on 211 // If no assignment was passed via the command line, then fall back on
229 // querying the Assigner service. 212 // querying the Assigner service.
230 QueryAssigner(client_auth_token); 213 QueryAssigner(client_auth_token);
231 } 214 }
232 215
233 void AssignmentSource::QueryAssigner(const std::string& client_auth_token) { 216 void AssignmentSource::QueryAssigner(const std::string& client_auth_token) {
234 // Call out to the network for a real assignment. Build the network request 217 // Call out to the network for a real assignment. Build the network request
(...skipping 17 matching lines...) Expand all
252 } 235 }
253 236
254 void AssignmentSource::OnURLFetchComplete(const net::URLFetcher* source) { 237 void AssignmentSource::OnURLFetchComplete(const net::URLFetcher* source) {
255 DCHECK(!callback_.is_null()); 238 DCHECK(!callback_.is_null());
256 DCHECK_EQ(url_fetcher_.get(), source); 239 DCHECK_EQ(url_fetcher_.get(), source);
257 240
258 if (!source->GetStatus().is_success()) { 241 if (!source->GetStatus().is_success()) {
259 DVLOG(1) << "Assignment request failed due to network error: " 242 DVLOG(1) << "Assignment request failed due to network error: "
260 << net::ErrorToString(source->GetStatus().error()); 243 << net::ErrorToString(source->GetStatus().error());
261 base::ResetAndReturn(&callback_) 244 base::ResetAndReturn(&callback_)
262 .Run(AssignmentSource::Result::RESULT_NETWORK_FAILURE, Assignment()); 245 .Run(ASSIGNMENT_REQUEST_RESULT_NETWORK_FAILURE, Assignment());
263 return; 246 return;
264 } 247 }
265 248
266 switch (source->GetResponseCode()) { 249 switch (source->GetResponseCode()) {
267 case net::HTTP_OK: 250 case net::HTTP_OK:
268 ParseAssignerResponse(); 251 ParseAssignerResponse();
269 break; 252 break;
270 case net::HTTP_BAD_REQUEST: 253 case net::HTTP_BAD_REQUEST:
271 base::ResetAndReturn(&callback_) 254 base::ResetAndReturn(&callback_)
272 .Run(AssignmentSource::Result::RESULT_BAD_REQUEST, Assignment()); 255 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_REQUEST, Assignment());
273 break; 256 break;
274 case net::HTTP_UNAUTHORIZED: 257 case net::HTTP_UNAUTHORIZED:
275 base::ResetAndReturn(&callback_) 258 base::ResetAndReturn(&callback_)
276 .Run(AssignmentSource::Result::RESULT_EXPIRED_ACCESS_TOKEN, 259 .Run(ASSIGNMENT_REQUEST_RESULT_EXPIRED_ACCESS_TOKEN, Assignment());
277 Assignment());
278 break; 260 break;
279 case net::HTTP_FORBIDDEN: 261 case net::HTTP_FORBIDDEN:
280 base::ResetAndReturn(&callback_) 262 base::ResetAndReturn(&callback_)
281 .Run(AssignmentSource::Result::RESULT_USER_INVALID, Assignment()); 263 .Run(ASSIGNMENT_REQUEST_RESULT_USER_INVALID, Assignment());
282 break; 264 break;
283 case 429: // Too Many Requests 265 case 429: // Too Many Requests
284 base::ResetAndReturn(&callback_) 266 base::ResetAndReturn(&callback_)
285 .Run(AssignmentSource::Result::RESULT_OUT_OF_VMS, Assignment()); 267 .Run(ASSIGNMENT_REQUEST_RESULT_OUT_OF_VMS, Assignment());
286 break; 268 break;
287 case net::HTTP_INTERNAL_SERVER_ERROR: 269 case net::HTTP_INTERNAL_SERVER_ERROR:
288 base::ResetAndReturn(&callback_) 270 base::ResetAndReturn(&callback_)
289 .Run(AssignmentSource::Result::RESULT_SERVER_ERROR, Assignment()); 271 .Run(ASSIGNMENT_REQUEST_RESULT_SERVER_ERROR, Assignment());
290 break; 272 break;
291 default: 273 default:
292 base::ResetAndReturn(&callback_) 274 base::ResetAndReturn(&callback_)
293 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 275 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
294 break; 276 break;
295 } 277 }
296 } 278 }
297 279
298 void AssignmentSource::ParseAssignerResponse() { 280 void AssignmentSource::ParseAssignerResponse() {
299 DCHECK(url_fetcher_.get()); 281 DCHECK(url_fetcher_.get());
300 DCHECK(url_fetcher_->GetStatus().is_success()); 282 DCHECK(url_fetcher_->GetStatus().is_success());
301 DCHECK_EQ(net::HTTP_OK, url_fetcher_->GetResponseCode()); 283 DCHECK_EQ(net::HTTP_OK, url_fetcher_->GetResponseCode());
302 284
303 // Grab the response from the assigner request. 285 // Grab the response from the assigner request.
304 std::string response; 286 std::string response;
305 if (!url_fetcher_->GetResponseAsString(&response)) { 287 if (!url_fetcher_->GetResponseAsString(&response)) {
306 base::ResetAndReturn(&callback_) 288 base::ResetAndReturn(&callback_)
307 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 289 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
308 return; 290 return;
309 } 291 }
310 292
311 safe_json::SafeJsonParser::Parse( 293 safe_json::SafeJsonParser::Parse(
312 response, 294 response,
313 base::Bind(&AssignmentSource::OnJsonParsed, weak_factory_.GetWeakPtr()), 295 base::Bind(&AssignmentSource::OnJsonParsed, weak_factory_.GetWeakPtr()),
314 base::Bind(&AssignmentSource::OnJsonParseError, 296 base::Bind(&AssignmentSource::OnJsonParseError,
315 weak_factory_.GetWeakPtr())); 297 weak_factory_.GetWeakPtr()));
316 } 298 }
317 299
318 void AssignmentSource::OnJsonParsed(std::unique_ptr<base::Value> json) { 300 void AssignmentSource::OnJsonParsed(std::unique_ptr<base::Value> json) {
319 const base::DictionaryValue* dict; 301 const base::DictionaryValue* dict;
320 if (!json->GetAsDictionary(&dict)) { 302 if (!json->GetAsDictionary(&dict)) {
Kevin M 2016/08/09 21:00:34 nit: Add LOG(WARNING)s here and wherever BAD_RESPO
321 base::ResetAndReturn(&callback_) 303 base::ResetAndReturn(&callback_)
322 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 304 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
323 return; 305 return;
324 } 306 }
325 307
326 // Validate that all the expected fields are present. 308 // Validate that all the expected fields are present.
327 std::string client_token; 309 std::string client_token;
328 std::string host; 310 std::string host;
329 int port; 311 int port;
330 std::string cert_str; 312 std::string cert_str;
331 if (!(dict->GetString(kClientTokenKey, &client_token) && 313 if (!(dict->GetString(kClientTokenKey, &client_token) &&
332 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && 314 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) &&
333 dict->GetString(kCertificateKey, &cert_str))) { 315 dict->GetString(kCertificateKey, &cert_str))) {
334 base::ResetAndReturn(&callback_) 316 base::ResetAndReturn(&callback_)
335 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 317 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
336 return; 318 return;
337 } 319 }
338 320
339 net::IPAddress ip_address; 321 net::IPAddress ip_address;
340 if (!ip_address.AssignFromIPLiteral(host)) { 322 if (!ip_address.AssignFromIPLiteral(host)) {
341 base::ResetAndReturn(&callback_) 323 base::ResetAndReturn(&callback_)
342 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 324 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
343 return; 325 return;
344 } 326 }
345 327
346 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { 328 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) {
347 base::ResetAndReturn(&callback_) 329 base::ResetAndReturn(&callback_)
348 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 330 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
349 return; 331 return;
350 } 332 }
351 333
352 net::CertificateList cert_list = 334 net::CertificateList cert_list =
353 net::X509Certificate::CreateCertificateListFromBytes( 335 net::X509Certificate::CreateCertificateListFromBytes(
354 cert_str.data(), cert_str.size(), 336 cert_str.data(), cert_str.size(),
355 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE); 337 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE);
356 if (cert_list.size() != 1) { 338 if (cert_list.size() != 1) {
357 base::ResetAndReturn(&callback_) 339 base::ResetAndReturn(&callback_)
358 .Run(AssignmentSource::Result::RESULT_INVALID_CERT, Assignment()); 340 .Run(ASSIGNMENT_REQUEST_RESULT_INVALID_CERT, Assignment());
359 return; 341 return;
360 } 342 }
361 343
362 // The assigner assumes SSL-only and all engines it assigns only communicate 344 // The assigner assumes SSL-only and all engines it assigns only communicate
363 // over SSL. 345 // over SSL.
364 Assignment assignment; 346 Assignment assignment;
365 assignment.transport_protocol = Assignment::TransportProtocol::SSL; 347 assignment.transport_protocol = Assignment::TransportProtocol::SSL;
366 assignment.engine_endpoint = net::IPEndPoint(ip_address, port); 348 assignment.engine_endpoint = net::IPEndPoint(ip_address, port);
367 assignment.client_token = client_token; 349 assignment.client_token = client_token;
368 assignment.cert = std::move(cert_list[0]); 350 assignment.cert = std::move(cert_list[0]);
369 351
370 base::ResetAndReturn(&callback_) 352 base::ResetAndReturn(&callback_)
371 .Run(AssignmentSource::Result::RESULT_OK, assignment); 353 .Run(ASSIGNMENT_REQUEST_RESULT_OK, assignment);
372 } 354 }
373 355
374 void AssignmentSource::OnJsonParseError(const std::string& error) { 356 void AssignmentSource::OnJsonParseError(const std::string& error) {
375 DLOG(ERROR) << "Error while parsing assigner JSON: " << error; 357 DLOG(ERROR) << "Error while parsing assigner JSON: " << error;
376 base::ResetAndReturn(&callback_) 358 base::ResetAndReturn(&callback_)
377 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 359 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
378 } 360 }
379 361
380 } // namespace client 362 } // namespace client
381 } // namespace blimp 363 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698