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/session/assignment_source.h" | 5 #include "blimp/client/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/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/memory/ref_counted.h" | |
| 13 #include "base/numerics/safe_conversions.h" | 15 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/task_runner_util.h" | |
| 15 #include "base/values.h" | 18 #include "base/values.h" |
| 16 #include "blimp/client/app/blimp_client_switches.h" | 19 #include "blimp/client/app/blimp_client_switches.h" |
| 17 #include "blimp/common/protocol_version.h" | 20 #include "blimp/common/protocol_version.h" |
| 21 #include "components/safe_json/safe_json_parser.h" | |
| 18 #include "net/base/ip_address.h" | 22 #include "net/base/ip_address.h" |
| 19 #include "net/base/ip_endpoint.h" | 23 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
| 21 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| 22 #include "net/base/url_util.h" | 26 #include "net/base/url_util.h" |
|
Wez
2016/03/01 00:23:55
Do you need this include any more?
Kevin M
2016/03/01 18:23:17
Done.
| |
| 23 #include "net/http/http_status_code.h" | 27 #include "net/http/http_status_code.h" |
| 24 #include "net/proxy/proxy_config_service.h" | 28 #include "net/proxy/proxy_config_service.h" |
| 25 #include "net/proxy/proxy_service.h" | 29 #include "net/proxy/proxy_service.h" |
| 26 #include "net/url_request/url_fetcher.h" | 30 #include "net/url_request/url_fetcher.h" |
| 27 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
| 28 #include "net/url_request/url_request_context_builder.h" | 32 #include "net/url_request/url_request_context_builder.h" |
| 29 #include "net/url_request/url_request_context_getter.h" | 33 #include "net/url_request/url_request_context_getter.h" |
| 30 | 34 |
| 31 namespace blimp { | 35 namespace blimp { |
| 32 namespace client { | 36 namespace client { |
| 33 | 37 |
| 34 namespace { | 38 namespace { |
| 35 | 39 |
| 36 // Assignment request JSON keys. | 40 // Assignment request JSON keys. |
| 37 const char kProtocolVersionKey[] = "protocol_version"; | 41 const char kProtocolVersionKey[] = "protocol_version"; |
| 38 | 42 |
| 39 // Assignment response JSON keys. | 43 // Assignment response JSON keys. |
| 40 const char kClientTokenKey[] = "clientToken"; | 44 const char kClientTokenKey[] = "clientToken"; |
| 41 const char kHostKey[] = "host"; | 45 const char kHostKey[] = "host"; |
| 42 const char kPortKey[] = "port"; | 46 const char kPortKey[] = "port"; |
| 43 const char kCertificateFingerprintKey[] = "certificateFingerprint"; | |
| 44 const char kCertificateKey[] = "certificate"; | 47 const char kCertificateKey[] = "certificate"; |
| 45 | 48 |
| 46 // URL scheme constants for custom assignments. See the '--blimplet-endpoint' | 49 // URL scheme constants for custom assignments. See the '--blimplet-endpoint' |
| 47 // documentation in blimp_client_switches.cc for details. | 50 // documentation in blimp_client_switches.cc for details. |
|
Wez
2016/03/01 00:23:56
This comment is now out-of-date.
Kevin M
2016/03/01 18:23:17
Done.
| |
| 48 const char kCustomSSLScheme[] = "ssl"; | 51 const char kSSLTransport[] = "ssl"; |
| 49 const char kCustomTCPScheme[] = "tcp"; | 52 const char kTCPTransport[] = "tcp"; |
|
Wez
2016/03/01 00:23:55
nit: Suggest kSslTransportValue and kTcpTransportV
Kevin M
2016/03/01 18:23:17
Done.
| |
| 50 const char kCustomQUICScheme[] = "quic"; | |
| 51 | |
| 52 Assignment GetCustomBlimpletAssignment() { | |
| 53 GURL url(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 54 switches::kBlimpletEndpoint)); | |
| 55 | |
| 56 std::string host; | |
| 57 int port; | |
| 58 if (url.is_empty() || !url.is_valid() || !url.has_scheme() || | |
| 59 !net::ParseHostAndPort(url.path(), &host, &port)) { | |
| 60 return Assignment(); | |
| 61 } | |
| 62 | |
| 63 net::IPAddress ip_address; | |
| 64 if (!ip_address.AssignFromIPLiteral(host)) { | |
| 65 CHECK(false) << "Invalid BlimpletAssignment host " << host; | |
| 66 } | |
| 67 | |
| 68 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { | |
| 69 CHECK(false) << "Invalid BlimpletAssignment port " << port; | |
| 70 } | |
| 71 | |
| 72 Assignment::TransportProtocol protocol = | |
| 73 Assignment::TransportProtocol::UNKNOWN; | |
| 74 if (url.has_scheme()) { | |
| 75 if (url.SchemeIs(kCustomSSLScheme)) { | |
| 76 protocol = Assignment::TransportProtocol::SSL; | |
| 77 } else if (url.SchemeIs(kCustomTCPScheme)) { | |
| 78 protocol = Assignment::TransportProtocol::TCP; | |
| 79 } else if (url.SchemeIs(kCustomQUICScheme)) { | |
| 80 protocol = Assignment::TransportProtocol::QUIC; | |
| 81 } else { | |
| 82 CHECK(false) << "Invalid BlimpletAssignment scheme " << url.scheme(); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 Assignment assignment; | |
| 87 assignment.transport_protocol = protocol; | |
| 88 assignment.ip_endpoint = net::IPEndPoint(ip_address, port); | |
| 89 assignment.client_token = kDummyClientToken; | |
| 90 return assignment; | |
| 91 } | |
| 92 | 53 |
| 93 GURL GetBlimpAssignerURL() { | 54 GURL GetBlimpAssignerURL() { |
| 94 // TODO(dtrainor): Add a way to specify another assigner. | 55 // TODO(dtrainor): Add a way to specify another assigner. |
| 95 return GURL(kDefaultAssignerURL); | 56 return GURL(kDefaultAssignerURL); |
| 96 } | 57 } |
| 97 | 58 |
| 98 class SimpleURLRequestContextGetter : public net::URLRequestContextGetter { | 59 class SimpleURLRequestContextGetter : public net::URLRequestContextGetter { |
| 99 public: | 60 public: |
| 100 SimpleURLRequestContextGetter( | 61 SimpleURLRequestContextGetter( |
| 101 const scoped_refptr<base::SingleThreadTaskRunner>& io_loop_task_runner) | 62 scoped_refptr<base::SingleThreadTaskRunner> io_loop_task_runner) |
| 102 : io_loop_task_runner_(io_loop_task_runner), | 63 : io_loop_task_runner_(std::move(io_loop_task_runner)), |
| 103 proxy_config_service_(net::ProxyService::CreateSystemProxyConfigService( | 64 proxy_config_service_(net::ProxyService::CreateSystemProxyConfigService( |
| 104 io_loop_task_runner_, | 65 io_loop_task_runner_, |
| 105 io_loop_task_runner_)) {} | 66 io_loop_task_runner_)) {} |
| 106 | 67 |
| 107 // net::URLRequestContextGetter implementation. | 68 // net::URLRequestContextGetter implementation. |
| 108 net::URLRequestContext* GetURLRequestContext() override { | 69 net::URLRequestContext* GetURLRequestContext() override { |
| 109 if (!url_request_context_) { | 70 if (!url_request_context_) { |
| 110 net::URLRequestContextBuilder builder; | 71 net::URLRequestContextBuilder builder; |
| 111 builder.set_proxy_config_service(std::move(proxy_config_service_)); | 72 builder.set_proxy_config_service(std::move(proxy_config_service_)); |
| 112 builder.DisableHttpCache(); | 73 builder.DisableHttpCache(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 129 | 90 |
| 130 // Temporary storage for the ProxyConfigService, which needs to be created on | 91 // Temporary storage for the ProxyConfigService, which needs to be created on |
| 131 // the main thread but cleared on the IO thread. This will be built in the | 92 // the main thread but cleared on the IO thread. This will be built in the |
| 132 // constructor and cleared on the IO thread. Due to the usage of this class | 93 // constructor and cleared on the IO thread. Due to the usage of this class |
| 133 // this is safe. | 94 // this is safe. |
| 134 scoped_ptr<net::ProxyConfigService> proxy_config_service_; | 95 scoped_ptr<net::ProxyConfigService> proxy_config_service_; |
| 135 | 96 |
| 136 DISALLOW_COPY_AND_ASSIGN(SimpleURLRequestContextGetter); | 97 DISALLOW_COPY_AND_ASSIGN(SimpleURLRequestContextGetter); |
| 137 }; | 98 }; |
| 138 | 99 |
| 100 // Populates an Assignment using command-line parameters, if provided. | |
| 101 // Returns a null Assignment if no parameters were set. | |
| 102 // Must be called on an IO thread. | |
|
Wez
2016/03/01 00:23:55
nit: Suggest being explicit as to why e.g. "Must b
Kevin M
2016/03/01 18:23:17
Done.
Kevin M
2016/03/01 18:23:17
Done.
| |
| 103 Assignment GetAssignmentFromCommandLine() { | |
| 104 std::string ip_str = | |
| 105 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 106 switches::kEngineIP); | |
| 107 std::string port_str = | |
| 108 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 109 switches::kEnginePort); | |
| 110 std::string transport_str = | |
| 111 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 112 switches::kEngineTransport); | |
|
Wez
2016/03/01 00:23:55
Don't bother pulling these out of the command-line
Kevin M
2016/03/01 18:23:17
Done.
| |
| 113 | |
| 114 if (ip_str.empty() && port_str.empty() && transport_str.empty()) { | |
| 115 return Assignment(); | |
|
Wez
2016/03/01 00:23:55
This early-exit and the subsequent test to see tha
Kevin M
2016/03/01 18:23:17
Done.
| |
| 116 } | |
| 117 | |
| 118 if (ip_str.empty() != port_str.empty() || | |
| 119 ip_str.empty() != transport_str.empty()) { | |
|
Wez
2016/03/01 00:23:56
nit: If you keep the early-exit, above, then this
Kevin M
2016/03/01 18:23:17
Done.
| |
| 120 DLOG(FATAL) | |
| 121 << "--engine-ip, --engine-port, and --engine-transport must be set " | |
| 122 "together."; | |
| 123 return Assignment(); | |
| 124 } | |
| 125 | |
| 126 unsigned port_parsed; | |
| 127 uint16_t port; | |
|
Wez
2016/03/01 00:23:55
nit: Always initialize PoD types, even if they're
Kevin M
2016/03/01 18:23:17
Done.
| |
| 128 if (!base::StringToUint(port_str, &port_parsed) || port_parsed <= 0 || | |
| 129 port_parsed > 65535) { | |
| 130 DLOG(FATAL) << "--engine-port must be a value between 0 and 65535."; | |
|
Wez
2016/03/01 00:23:55
It's not valid for the port # to be zero, so this
Kevin M
2016/03/01 18:23:17
Done.
Kevin M
2016/03/01 18:23:17
Done.
| |
| 131 return Assignment(); | |
| 132 } | |
| 133 port = base::checked_cast<uint16_t>(port_parsed); | |
| 134 | |
| 135 net::IPAddress ip_address; | |
| 136 if (!ip_address.AssignFromIPLiteral(ip_str)) { | |
| 137 DLOG(FATAL) << "Invalid engine IP " << ip_str; | |
| 138 return Assignment(); | |
| 139 } | |
| 140 | |
| 141 Assignment::TransportProtocol transport = | |
| 142 Assignment::TransportProtocol::UNKNOWN; | |
| 143 if (transport_str == kSSLTransport) { | |
| 144 transport = Assignment::TransportProtocol::SSL; | |
| 145 } else if (transport_str == kTCPTransport) { | |
| 146 transport = Assignment::TransportProtocol::TCP; | |
| 147 } else { | |
| 148 DLOG(FATAL) << "Invalid engine transport " << transport; | |
| 149 return Assignment(); | |
| 150 } | |
| 151 | |
| 152 scoped_refptr<net::X509Certificate> cert; | |
| 153 if (transport == Assignment::TransportProtocol::SSL) { | |
| 154 base::FilePath cert_path = | |
| 155 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( | |
| 156 switches::kEngineCertPath); | |
| 157 if (cert_path.empty()) { | |
| 158 DLOG(FATAL) << "Missing required parameter --" | |
| 159 << switches::kEngineCertPath << "."; | |
| 160 return Assignment(); | |
| 161 } | |
| 162 std::string cert_str; | |
| 163 if (!base::ReadFileToString(cert_path, &cert_str)) { | |
| 164 DLOG(FATAL) << "Couldn't read from file: " | |
| 165 << cert_path.LossyDisplayName(); | |
| 166 return Assignment(); | |
| 167 } | |
| 168 net::CertificateList cert_list = | |
| 169 net::X509Certificate::CreateCertificateListFromBytes( | |
| 170 cert_str.data(), cert_str.size(), | |
| 171 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE); | |
| 172 DLOG_IF(FATAL, (cert_list.size() != 1u)) | |
| 173 << "Only one cert is allowed in PEM cert list."; | |
| 174 cert = std::move(cert_list[0]); | |
| 175 } | |
| 176 | |
| 177 Assignment assignment; | |
| 178 assignment.transport_protocol = transport; | |
| 179 assignment.engine_endpoint = | |
| 180 net::IPEndPoint(ip_address, base::checked_cast<uint16_t>(port)); | |
|
Wez
2016/03/01 00:23:56
You have already checked_cast<> when assigning to
Kevin M
2016/03/01 18:23:17
Done.
| |
| 181 assignment.client_token = kDummyClientToken; | |
| 182 assignment.cert = std::move(cert); | |
| 183 return assignment; | |
| 184 } | |
| 185 | |
| 139 } // namespace | 186 } // namespace |
| 140 | 187 |
| 141 Assignment::Assignment() : transport_protocol(TransportProtocol::UNKNOWN) {} | 188 Assignment::Assignment() : transport_protocol(TransportProtocol::UNKNOWN) {} |
| 142 | 189 |
| 143 Assignment::~Assignment() {} | 190 Assignment::~Assignment() {} |
| 144 | 191 |
| 145 bool Assignment::is_null() const { | 192 bool Assignment::IsValid() const { |
| 146 return ip_endpoint.address().empty() || ip_endpoint.port() == 0 || | 193 return !engine_endpoint.address().empty() && engine_endpoint.port() != 0 && |
| 147 transport_protocol == TransportProtocol::UNKNOWN; | 194 transport_protocol != TransportProtocol::UNKNOWN; |
| 148 } | 195 } |
| 149 | 196 |
| 150 AssignmentSource::AssignmentSource( | 197 AssignmentSource::AssignmentSource( |
| 151 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, | 198 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
| 152 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 199 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
| 153 : main_task_runner_(main_task_runner), | 200 : file_task_runner_(std::move(file_task_runner)), |
| 154 url_request_context_(new SimpleURLRequestContextGetter(io_task_runner)) {} | 201 url_request_context_( |
| 202 new SimpleURLRequestContextGetter(network_task_runner)), | |
| 203 weak_factory_(this) {} | |
| 155 | 204 |
| 156 AssignmentSource::~AssignmentSource() {} | 205 AssignmentSource::~AssignmentSource() {} |
| 157 | 206 |
| 158 void AssignmentSource::GetAssignment(const std::string& client_auth_token, | 207 void AssignmentSource::GetAssignment(const std::string& client_auth_token, |
| 159 const AssignmentCallback& callback) { | 208 const AssignmentCallback& callback) { |
| 160 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 161 | |
| 162 // Cancel any outstanding callback. | 209 // Cancel any outstanding callback. |
| 163 if (!callback_.is_null()) { | 210 if (!callback_.is_null()) { |
| 164 base::ResetAndReturn(&callback_) | 211 base::ResetAndReturn(&callback_) |
| 165 .Run(AssignmentSource::Result::RESULT_SERVER_INTERRUPTED, Assignment()); | 212 .Run(AssignmentSource::Result::RESULT_SERVER_INTERRUPTED, Assignment()); |
| 166 } | 213 } |
|
Wez
2016/03/01 00:23:55
Follow-up: This looks dubious:
1. the caller shou
Kevin M
2016/03/01 18:23:17
Hey +dtrainor@chromium.org, is there a specific re
Wez
2016/03/02 02:26:44
Acknowledged.
| |
| 167 callback_ = AssignmentCallback(callback); | 214 callback_ = AssignmentCallback(callback); |
| 168 | 215 |
| 169 Assignment assignment = GetCustomBlimpletAssignment(); | 216 // Try to get a custom assignment from command lines first. |
| 170 if (!assignment.is_null()) { | 217 PostTaskAndReplyWithResult( |
| 171 // Post the result so that the behavior of this function is consistent. | 218 file_task_runner_.get(), FROM_HERE, |
| 172 main_task_runner_->PostTask( | 219 base::Bind(&GetAssignmentFromCommandLine), |
|
Wez
2016/03/01 00:23:55
Wouldn't it be cleaner to only GetAssignmentFromCo
Kevin M
2016/03/02 18:05:33
(Done on previous patch)
| |
| 173 FROM_HERE, base::Bind(base::ResetAndReturn(&callback_), | 220 base::Bind(&AssignmentSource::OnGetAssignmentFromCommandLineDone, |
| 174 AssignmentSource::Result::RESULT_OK, assignment)); | 221 weak_factory_.GetWeakPtr(), client_auth_token)); |
| 222 } | |
| 223 | |
| 224 void AssignmentSource::OnGetAssignmentFromCommandLineDone( | |
| 225 const std::string& client_auth_token, | |
| 226 Assignment parsed_assignment) { | |
| 227 // If GetAssignmentFromCommandLine succeeded, then return the custom | |
|
Wez
2016/03/01 00:23:56
nit: Not "custom" assignment any more.
Wez
2016/03/02 02:26:44
nit: Ping!
Kevin M
2016/03/02 18:05:33
Done.
| |
| 228 // assignment | |
| 229 // directly. | |
| 230 if (parsed_assignment.IsValid()) { | |
| 231 base::ResetAndReturn(&callback_) | |
| 232 .Run(AssignmentSource::RESULT_OK, parsed_assignment); | |
| 175 return; | 233 return; |
| 176 } | 234 } |
| 177 | 235 |
| 178 // Call out to the network for a real assignment. Build the network request | 236 // Call out to the network for a real assignment. Build the network request |
|
Wez
2016/03/01 00:23:55
nit: Suggest keeping the network-assignment code a
Kevin M
2016/03/02 18:05:33
Done.
| |
| 179 // to hit the assigner. | 237 // to hit the assigner. |
| 180 url_fetcher_ = net::URLFetcher::Create(GetBlimpAssignerURL(), | 238 url_fetcher_ = net::URLFetcher::Create(GetBlimpAssignerURL(), |
| 181 net::URLFetcher::POST, this); | 239 net::URLFetcher::POST, this); |
| 182 url_fetcher_->SetRequestContext(url_request_context_.get()); | 240 url_fetcher_->SetRequestContext(url_request_context_.get()); |
| 183 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | 241 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
| 184 net::LOAD_DO_NOT_SEND_COOKIES); | 242 net::LOAD_DO_NOT_SEND_COOKIES); |
| 185 url_fetcher_->AddExtraRequestHeader("Authorization: Bearer " + | 243 url_fetcher_->AddExtraRequestHeader("Authorization: Bearer " + |
| 186 client_auth_token); | 244 client_auth_token); |
| 187 | 245 |
| 188 // Write the JSON for the request data. | 246 // Write the JSON for the request data. |
| 189 base::DictionaryValue dictionary; | 247 base::DictionaryValue dictionary; |
| 190 dictionary.SetString(kProtocolVersionKey, blimp::kEngineVersion); | 248 dictionary.SetString(kProtocolVersionKey, blimp::kEngineVersion); |
| 191 std::string json; | 249 std::string json; |
| 192 base::JSONWriter::Write(dictionary, &json); | 250 base::JSONWriter::Write(dictionary, &json); |
| 193 url_fetcher_->SetUploadData("application/json", json); | 251 url_fetcher_->SetUploadData("application/json", json); |
| 194 | |
| 195 url_fetcher_->Start(); | 252 url_fetcher_->Start(); |
| 196 } | 253 } |
| 197 | 254 |
| 198 void AssignmentSource::OnURLFetchComplete(const net::URLFetcher* source) { | 255 void AssignmentSource::OnURLFetchComplete(const net::URLFetcher* source) { |
| 199 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 200 DCHECK(!callback_.is_null()); | 256 DCHECK(!callback_.is_null()); |
| 201 DCHECK_EQ(url_fetcher_.get(), source); | 257 DCHECK_EQ(url_fetcher_.get(), source); |
| 202 | 258 |
| 203 if (!source->GetStatus().is_success()) { | 259 if (!source->GetStatus().is_success()) { |
| 204 DVLOG(1) << "Assignment request failed due to network error: " | 260 DVLOG(1) << "Assignment request failed due to network error: " |
| 205 << net::ErrorToString(source->GetStatus().error()); | 261 << net::ErrorToString(source->GetStatus().error()); |
| 206 base::ResetAndReturn(&callback_) | 262 base::ResetAndReturn(&callback_) |
| 207 .Run(AssignmentSource::Result::RESULT_NETWORK_FAILURE, Assignment()); | 263 .Run(AssignmentSource::Result::RESULT_NETWORK_FAILURE, Assignment()); |
| 208 return; | 264 return; |
| 209 } | 265 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 DCHECK_EQ(net::HTTP_OK, url_fetcher_->GetResponseCode()); | 302 DCHECK_EQ(net::HTTP_OK, url_fetcher_->GetResponseCode()); |
| 247 | 303 |
| 248 // Grab the response from the assigner request. | 304 // Grab the response from the assigner request. |
| 249 std::string response; | 305 std::string response; |
| 250 if (!url_fetcher_->GetResponseAsString(&response)) { | 306 if (!url_fetcher_->GetResponseAsString(&response)) { |
| 251 base::ResetAndReturn(&callback_) | 307 base::ResetAndReturn(&callback_) |
| 252 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | 308 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 253 return; | 309 return; |
| 254 } | 310 } |
| 255 | 311 |
| 256 // Attempt to interpret the response as JSON and treat it as a dictionary. | 312 safe_json::SafeJsonParser::Parse( |
| 257 scoped_ptr<base::Value> json = base::JSONReader::Read(response); | 313 response, |
| 258 if (!json) { | 314 base::Bind(&AssignmentSource::OnJSONParsed, weak_factory_.GetWeakPtr()), |
| 259 base::ResetAndReturn(&callback_) | 315 base::Bind(&AssignmentSource::OnJSONParseError, |
| 260 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | 316 weak_factory_.GetWeakPtr())); |
| 261 return; | 317 } |
| 262 } | |
| 263 | 318 |
| 319 void AssignmentSource::OnJSONParsed(scoped_ptr<base::Value> json) { | |
| 264 const base::DictionaryValue* dict; | 320 const base::DictionaryValue* dict; |
| 265 if (!json->GetAsDictionary(&dict)) { | 321 if (!json->GetAsDictionary(&dict)) { |
| 266 base::ResetAndReturn(&callback_) | 322 base::ResetAndReturn(&callback_) |
| 267 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | 323 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 268 return; | 324 return; |
| 269 } | 325 } |
| 270 | 326 |
| 271 // Validate that all the expected fields are present. | 327 // Validate that all the expected fields are present. |
| 272 std::string client_token; | 328 std::string client_token; |
| 273 std::string host; | 329 std::string host; |
| 274 int port; | 330 int port; |
| 275 std::string cert_fingerprint; | 331 std::string cert_str; |
| 276 std::string cert; | |
| 277 if (!(dict->GetString(kClientTokenKey, &client_token) && | 332 if (!(dict->GetString(kClientTokenKey, &client_token) && |
| 278 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && | 333 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && |
| 279 dict->GetString(kCertificateFingerprintKey, &cert_fingerprint) && | 334 dict->GetString(kCertificateKey, &cert_str))) { |
| 280 dict->GetString(kCertificateKey, &cert))) { | |
| 281 base::ResetAndReturn(&callback_) | 335 base::ResetAndReturn(&callback_) |
| 282 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | 336 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 283 return; | 337 return; |
| 284 } | 338 } |
| 285 | 339 |
| 286 net::IPAddress ip_address; | 340 net::IPAddress ip_address; |
| 287 if (!ip_address.AssignFromIPLiteral(host)) { | 341 if (!ip_address.AssignFromIPLiteral(host)) { |
| 288 base::ResetAndReturn(&callback_) | 342 base::ResetAndReturn(&callback_) |
| 289 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | 343 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 290 return; | 344 return; |
| 291 } | 345 } |
| 292 | 346 |
| 293 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { | 347 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { |
| 294 base::ResetAndReturn(&callback_) | 348 base::ResetAndReturn(&callback_) |
| 295 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | 349 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 296 return; | 350 return; |
| 297 } | 351 } |
| 298 | 352 |
| 299 Assignment assignment; | 353 net::CertificateList cert_list = |
| 354 net::X509Certificate::CreateCertificateListFromBytes( | |
| 355 cert_str.data(), cert_str.size(), | |
| 356 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE); | |
| 357 if (cert_list.size() != 1) { | |
| 358 base::ResetAndReturn(&callback_) | |
| 359 .Run(AssignmentSource::Result::RESULT_INVALID_CERT, Assignment()); | |
| 360 return; | |
| 361 } | |
| 362 | |
| 300 // The assigner assumes SSL-only and all engines it assigns only communicate | 363 // The assigner assumes SSL-only and all engines it assigns only communicate |
| 301 // over SSL. | 364 // over SSL. |
| 365 Assignment assignment; | |
| 302 assignment.transport_protocol = Assignment::TransportProtocol::SSL; | 366 assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
| 303 assignment.ip_endpoint = net::IPEndPoint(ip_address, port); | 367 assignment.engine_endpoint = net::IPEndPoint(ip_address, port); |
| 304 assignment.client_token = client_token; | 368 assignment.client_token = client_token; |
| 305 assignment.certificate = cert; | 369 assignment.cert = std::move(cert_list[0]); |
| 306 assignment.certificate_fingerprint = cert_fingerprint; | |
| 307 | 370 |
| 308 base::ResetAndReturn(&callback_) | 371 base::ResetAndReturn(&callback_) |
| 309 .Run(AssignmentSource::Result::RESULT_OK, assignment); | 372 .Run(AssignmentSource::Result::RESULT_OK, assignment); |
| 310 } | 373 } |
| 311 | 374 |
| 375 void AssignmentSource::OnJSONParseError(const std::string& error) { | |
| 376 DLOG(ERROR) << "Error while parsing assigner JSON: " << error; | |
| 377 base::ResetAndReturn(&callback_) | |
| 378 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | |
| 379 } | |
| 380 | |
| 312 } // namespace client | 381 } // namespace client |
| 313 } // namespace blimp | 382 } // namespace blimp |
| OLD | NEW |