Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 15 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
| 16 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
| 21 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" |
| 22 #include "base/values.h" | 22 #include "base/values.h" |
| 23 #include "blimp/client/core/blimp_client_switches.h" | 23 #include "blimp/client/core/blimp_client_switches.h" |
| 24 #include "blimp/common/get_client_token.h" | 24 #include "blimp/common/get_client_auth_token.h" |
| 25 #include "blimp/common/protocol_version.h" | 25 #include "blimp/common/protocol_version.h" |
| 26 #include "components/safe_json/safe_json_parser.h" | 26 #include "components/safe_json/safe_json_parser.h" |
| 27 #include "net/base/ip_address.h" | 27 #include "net/base/ip_address.h" |
| 28 #include "net/base/ip_endpoint.h" | 28 #include "net/base/ip_endpoint.h" |
| 29 #include "net/base/load_flags.h" | 29 #include "net/base/load_flags.h" |
| 30 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
| 31 #include "net/http/http_status_code.h" | 31 #include "net/http/http_status_code.h" |
| 32 #include "net/proxy/proxy_config_service.h" | 32 #include "net/proxy/proxy_config_service.h" |
| 33 #include "net/proxy/proxy_service.h" | 33 #include "net/proxy/proxy_service.h" |
| 34 #include "net/url_request/url_fetcher.h" | 34 #include "net/url_request/url_fetcher.h" |
| 35 #include "net/url_request/url_request_context.h" | 35 #include "net/url_request/url_request_context.h" |
| 36 #include "net/url_request/url_request_context_builder.h" | 36 #include "net/url_request/url_request_context_builder.h" |
| 37 #include "net/url_request/url_request_context_getter.h" | 37 #include "net/url_request/url_request_context_getter.h" |
| 38 | 38 |
| 39 namespace blimp { | 39 namespace blimp { |
| 40 namespace client { | 40 namespace client { |
| 41 | 41 |
| 42 namespace { | 42 namespace { |
| 43 | 43 |
| 44 // Assignment request JSON keys. | 44 // Assignment request JSON keys. |
| 45 const char kProtocolVersionKey[] = "protocol_version"; | 45 const char kProtocolVersionKey[] = "protocol_version"; |
| 46 | 46 |
| 47 // Assignment response JSON keys. | 47 // Assignment response JSON keys. |
| 48 const char kClientTokenKey[] = "clientToken"; | 48 const char kClientAuthTokenKey[] = "clientToken"; |
| 49 const char kHostKey[] = "host"; | 49 const char kHostKey[] = "host"; |
| 50 const char kPortKey[] = "port"; | 50 const char kPortKey[] = "port"; |
| 51 const char kCertificateKey[] = "certificate"; | 51 const char kCertificateKey[] = "certificate"; |
| 52 | 52 |
| 53 // Possible arguments for the "--engine-transport" command line parameter. | 53 // Possible arguments for the "--engine-transport" command line parameter. |
| 54 const char kSSLTransportValue[] = "ssl"; | 54 const char kSSLTransportValue[] = "ssl"; |
| 55 const char kTCPTransportValue[] = "tcp"; | 55 const char kTCPTransportValue[] = "tcp"; |
| 56 | 56 |
| 57 class SimpleURLRequestContextGetter : public net::URLRequestContextGetter { | 57 class SimpleURLRequestContextGetter : public net::URLRequestContextGetter { |
| 58 public: | 58 public: |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 return port > 0 && port <= 65535; | 104 return port > 0 && port <= 65535; |
| 105 } | 105 } |
| 106 | 106 |
| 107 // Populates an Assignment using command-line parameters, if provided. | 107 // Populates an Assignment using command-line parameters, if provided. |
| 108 // Returns a null Assignment if no parameters were set. | 108 // Returns a null Assignment if no parameters were set. |
| 109 // Must be called on a thread suitable for file IO. | 109 // Must be called on a thread suitable for file IO. |
| 110 Assignment GetAssignmentFromCommandLine() { | 110 Assignment GetAssignmentFromCommandLine() { |
| 111 Assignment assignment; | 111 Assignment assignment; |
| 112 | 112 |
| 113 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 113 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 114 assignment.client_token = GetClientToken(*cmd_line); | 114 assignment.client_auth_token = GetClientAuthToken(*cmd_line); |
| 115 CHECK(!assignment.client_token.empty()) << "No client token provided."; | 115 CHECK(!assignment.client_auth_token.empty()) << "No client token provided."; |
|
maniscalco
2016/08/30 18:05:09
"No client token provided." -> "No client auth tok
CJ
2016/09/01 23:18:22
Done.
| |
| 116 | 116 |
| 117 unsigned port_parsed = 0; | 117 unsigned port_parsed = 0; |
| 118 if (!base::StringToUint( | 118 if (!base::StringToUint( |
| 119 cmd_line->GetSwitchValueASCII(switches::kEnginePort), | 119 cmd_line->GetSwitchValueASCII(switches::kEnginePort), |
| 120 &port_parsed) || !IsValidIpPortNumber(port_parsed)) { | 120 &port_parsed) || !IsValidIpPortNumber(port_parsed)) { |
| 121 DLOG(FATAL) << "--engine-port must be a value between 1 and 65535."; | 121 DLOG(FATAL) << "--engine-port must be a value between 1 and 65535."; |
| 122 return Assignment(); | 122 return Assignment(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 net::IPAddress ip_address; | 125 net::IPAddress ip_address; |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 312 void AssignmentSource::OnJsonParsed(std::unique_ptr<base::Value> json) { | 312 void AssignmentSource::OnJsonParsed(std::unique_ptr<base::Value> json) { |
| 313 const base::DictionaryValue* dict; | 313 const base::DictionaryValue* dict; |
| 314 if (!json->GetAsDictionary(&dict)) { | 314 if (!json->GetAsDictionary(&dict)) { |
| 315 LOG(WARNING) << "Unable to parse JSON data as a dictionary."; | 315 LOG(WARNING) << "Unable to parse JSON data as a dictionary."; |
| 316 base::ResetAndReturn(&callback_) | 316 base::ResetAndReturn(&callback_) |
| 317 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment()); | 317 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment()); |
| 318 return; | 318 return; |
| 319 } | 319 } |
| 320 | 320 |
| 321 // Validate that all the expected fields are present. | 321 // Validate that all the expected fields are present. |
| 322 std::string client_token; | 322 std::string client_auth_token; |
| 323 std::string host; | 323 std::string host; |
| 324 int port; | 324 int port; |
| 325 std::string cert_str; | 325 std::string cert_str; |
| 326 if (!(dict->GetString(kClientTokenKey, &client_token) && | 326 if (!(dict->GetString(kClientAuthTokenKey, &client_auth_token) && |
| 327 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && | 327 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && |
| 328 dict->GetString(kCertificateKey, &cert_str))) { | 328 dict->GetString(kCertificateKey, &cert_str))) { |
| 329 LOG(WARNING) << "Not all fields present in assignment JSON data."; | 329 LOG(WARNING) << "Not all fields present in assignment JSON data."; |
| 330 base::ResetAndReturn(&callback_) | 330 base::ResetAndReturn(&callback_) |
| 331 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment()); | 331 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment()); |
| 332 return; | 332 return; |
| 333 } | 333 } |
| 334 | 334 |
| 335 net::IPAddress ip_address; | 335 net::IPAddress ip_address; |
| 336 if (!ip_address.AssignFromIPLiteral(host)) { | 336 if (!ip_address.AssignFromIPLiteral(host)) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 355 base::ResetAndReturn(&callback_) | 355 base::ResetAndReturn(&callback_) |
| 356 .Run(ASSIGNMENT_REQUEST_RESULT_INVALID_CERT, Assignment()); | 356 .Run(ASSIGNMENT_REQUEST_RESULT_INVALID_CERT, Assignment()); |
| 357 return; | 357 return; |
| 358 } | 358 } |
| 359 | 359 |
| 360 // The assigner assumes SSL-only and all engines it assigns only communicate | 360 // The assigner assumes SSL-only and all engines it assigns only communicate |
| 361 // over SSL. | 361 // over SSL. |
| 362 Assignment assignment; | 362 Assignment assignment; |
| 363 assignment.transport_protocol = Assignment::TransportProtocol::SSL; | 363 assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
| 364 assignment.engine_endpoint = net::IPEndPoint(ip_address, port); | 364 assignment.engine_endpoint = net::IPEndPoint(ip_address, port); |
| 365 assignment.client_token = client_token; | 365 assignment.client_auth_token = client_auth_token; |
| 366 assignment.cert = std::move(cert_list[0]); | 366 assignment.cert = std::move(cert_list[0]); |
| 367 | 367 |
| 368 base::ResetAndReturn(&callback_) | 368 base::ResetAndReturn(&callback_) |
| 369 .Run(ASSIGNMENT_REQUEST_RESULT_OK, assignment); | 369 .Run(ASSIGNMENT_REQUEST_RESULT_OK, assignment); |
| 370 } | 370 } |
| 371 | 371 |
| 372 void AssignmentSource::OnJsonParseError(const std::string& error) { | 372 void AssignmentSource::OnJsonParseError(const std::string& error) { |
| 373 DLOG(ERROR) << "Error while parsing assigner JSON: " << error; | 373 DLOG(ERROR) << "Error while parsing assigner JSON: " << error; |
| 374 base::ResetAndReturn(&callback_) | 374 base::ResetAndReturn(&callback_) |
| 375 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment()); | 375 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment()); |
| 376 } | 376 } |
| 377 | 377 |
| 378 } // namespace client | 378 } // namespace client |
| 379 } // namespace blimp | 379 } // namespace blimp |
| OLD | NEW |