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

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:
274 LOG(WARNING) << "Defaulting to BAD_RESPONSE for HTTP response code "
275 << source->GetResponseCode();
292 base::ResetAndReturn(&callback_) 276 base::ResetAndReturn(&callback_)
293 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 277 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
294 break; 278 break;
295 } 279 }
296 } 280 }
297 281
298 void AssignmentSource::ParseAssignerResponse() { 282 void AssignmentSource::ParseAssignerResponse() {
299 DCHECK(url_fetcher_.get()); 283 DCHECK(url_fetcher_.get());
300 DCHECK(url_fetcher_->GetStatus().is_success()); 284 DCHECK(url_fetcher_->GetStatus().is_success());
301 DCHECK_EQ(net::HTTP_OK, url_fetcher_->GetResponseCode()); 285 DCHECK_EQ(net::HTTP_OK, url_fetcher_->GetResponseCode());
302 286
303 // Grab the response from the assigner request. 287 // Grab the response from the assigner request.
304 std::string response; 288 std::string response;
305 if (!url_fetcher_->GetResponseAsString(&response)) { 289 if (!url_fetcher_->GetResponseAsString(&response)) {
290 LOG(WARNING) << "Unable to retrieve response as string from URLFetcher.";
306 base::ResetAndReturn(&callback_) 291 base::ResetAndReturn(&callback_)
307 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 292 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
308 return; 293 return;
309 } 294 }
310 295
311 safe_json::SafeJsonParser::Parse( 296 safe_json::SafeJsonParser::Parse(
312 response, 297 response,
313 base::Bind(&AssignmentSource::OnJsonParsed, weak_factory_.GetWeakPtr()), 298 base::Bind(&AssignmentSource::OnJsonParsed, weak_factory_.GetWeakPtr()),
314 base::Bind(&AssignmentSource::OnJsonParseError, 299 base::Bind(&AssignmentSource::OnJsonParseError,
315 weak_factory_.GetWeakPtr())); 300 weak_factory_.GetWeakPtr()));
316 } 301 }
317 302
318 void AssignmentSource::OnJsonParsed(std::unique_ptr<base::Value> json) { 303 void AssignmentSource::OnJsonParsed(std::unique_ptr<base::Value> json) {
319 const base::DictionaryValue* dict; 304 const base::DictionaryValue* dict;
320 if (!json->GetAsDictionary(&dict)) { 305 if (!json->GetAsDictionary(&dict)) {
306 LOG(WARNING) << "Unable to parse JSON data as a dictionary.";
321 base::ResetAndReturn(&callback_) 307 base::ResetAndReturn(&callback_)
322 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 308 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
323 return; 309 return;
324 } 310 }
325 311
326 // Validate that all the expected fields are present. 312 // Validate that all the expected fields are present.
327 std::string client_token; 313 std::string client_token;
328 std::string host; 314 std::string host;
329 int port; 315 int port;
330 std::string cert_str; 316 std::string cert_str;
331 if (!(dict->GetString(kClientTokenKey, &client_token) && 317 if (!(dict->GetString(kClientTokenKey, &client_token) &&
332 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && 318 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) &&
333 dict->GetString(kCertificateKey, &cert_str))) { 319 dict->GetString(kCertificateKey, &cert_str))) {
320 LOG(WARNING) << "Not all fields present in assignment JSON data.";
334 base::ResetAndReturn(&callback_) 321 base::ResetAndReturn(&callback_)
335 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 322 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
336 return; 323 return;
337 } 324 }
338 325
339 net::IPAddress ip_address; 326 net::IPAddress ip_address;
340 if (!ip_address.AssignFromIPLiteral(host)) { 327 if (!ip_address.AssignFromIPLiteral(host)) {
328 LOG(WARNING) << "Unable to assign IP address from string literal " << host;
341 base::ResetAndReturn(&callback_) 329 base::ResetAndReturn(&callback_)
342 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 330 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
343 return; 331 return;
344 } 332 }
345 333
346 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { 334 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) {
335 LOG(WARNING) << "Assignment port number not in range for uint16_t";
347 base::ResetAndReturn(&callback_) 336 base::ResetAndReturn(&callback_)
348 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 337 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
349 return; 338 return;
350 } 339 }
351 340
352 net::CertificateList cert_list = 341 net::CertificateList cert_list =
353 net::X509Certificate::CreateCertificateListFromBytes( 342 net::X509Certificate::CreateCertificateListFromBytes(
354 cert_str.data(), cert_str.size(), 343 cert_str.data(), cert_str.size(),
355 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE); 344 net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE);
356 if (cert_list.size() != 1) { 345 if (cert_list.size() != 1) {
357 base::ResetAndReturn(&callback_) 346 base::ResetAndReturn(&callback_)
358 .Run(AssignmentSource::Result::RESULT_INVALID_CERT, Assignment()); 347 .Run(ASSIGNMENT_REQUEST_RESULT_INVALID_CERT, Assignment());
359 return; 348 return;
360 } 349 }
361 350
362 // The assigner assumes SSL-only and all engines it assigns only communicate 351 // The assigner assumes SSL-only and all engines it assigns only communicate
363 // over SSL. 352 // over SSL.
364 Assignment assignment; 353 Assignment assignment;
365 assignment.transport_protocol = Assignment::TransportProtocol::SSL; 354 assignment.transport_protocol = Assignment::TransportProtocol::SSL;
366 assignment.engine_endpoint = net::IPEndPoint(ip_address, port); 355 assignment.engine_endpoint = net::IPEndPoint(ip_address, port);
367 assignment.client_token = client_token; 356 assignment.client_token = client_token;
368 assignment.cert = std::move(cert_list[0]); 357 assignment.cert = std::move(cert_list[0]);
369 358
370 base::ResetAndReturn(&callback_) 359 base::ResetAndReturn(&callback_)
371 .Run(AssignmentSource::Result::RESULT_OK, assignment); 360 .Run(ASSIGNMENT_REQUEST_RESULT_OK, assignment);
372 } 361 }
373 362
374 void AssignmentSource::OnJsonParseError(const std::string& error) { 363 void AssignmentSource::OnJsonParseError(const std::string& error) {
375 DLOG(ERROR) << "Error while parsing assigner JSON: " << error; 364 DLOG(ERROR) << "Error while parsing assigner JSON: " << error;
376 base::ResetAndReturn(&callback_) 365 base::ResetAndReturn(&callback_)
377 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); 366 .Run(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, Assignment());
378 } 367 }
379 368
380 } // namespace client 369 } // namespace client
381 } // namespace blimp 370 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/client/core/session/assignment_source.h ('k') | blimp/client/core/session/assignment_source_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698