OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/certificate_transparency/log_proof_fetcher.h" | 5 #include "components/certificate_transparency/log_proof_fetcher.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/numerics/safe_conversions.h" | 15 #include "base/numerics/safe_conversions.h" |
15 #include "base/stl_util.h" | |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "components/safe_json/safe_json_parser.h" | 18 #include "components/safe_json/safe_json_parser.h" |
19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
20 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
22 #include "net/base/request_priority.h" | 22 #include "net/base/request_priority.h" |
23 #include "net/cert/ct_log_response_parser.h" | 23 #include "net/cert/ct_log_response_parser.h" |
24 #include "net/cert/signed_tree_head.h" | 24 #include "net/cert/signed_tree_head.h" |
25 #include "net/http/http_status_code.h" | 25 #include "net/http/http_status_code.h" |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 private: | 390 private: |
391 LogProofFetcher::ConsistencyProofFetchedCallback proof_fetched_; | 391 LogProofFetcher::ConsistencyProofFetchedCallback proof_fetched_; |
392 }; | 392 }; |
393 | 393 |
394 LogProofFetcher::LogProofFetcher(net::URLRequestContext* request_context) | 394 LogProofFetcher::LogProofFetcher(net::URLRequestContext* request_context) |
395 : request_context_(request_context), weak_factory_(this) { | 395 : request_context_(request_context), weak_factory_(this) { |
396 DCHECK(request_context); | 396 DCHECK(request_context); |
397 } | 397 } |
398 | 398 |
399 LogProofFetcher::~LogProofFetcher() { | 399 LogProofFetcher::~LogProofFetcher() { |
400 base::STLDeleteContainerPointers(inflight_fetches_.begin(), | |
401 inflight_fetches_.end()); | |
402 } | 400 } |
403 | 401 |
404 void LogProofFetcher::FetchSignedTreeHead( | 402 void LogProofFetcher::FetchSignedTreeHead( |
405 const GURL& base_log_url, | 403 const GURL& base_log_url, |
406 const std::string& log_id, | 404 const std::string& log_id, |
407 const SignedTreeHeadFetchedCallback& fetched_callback, | 405 const SignedTreeHeadFetchedCallback& fetched_callback, |
408 const FetchFailedCallback& failed_callback) { | 406 const FetchFailedCallback& failed_callback) { |
409 GURL request_url = base_log_url.Resolve("ct/v1/get-sth"); | 407 GURL request_url = base_log_url.Resolve("ct/v1/get-sth"); |
410 StartFetch(request_url, new GetSTHLogResponseHandler(log_id, fetched_callback, | 408 StartFetch(request_url, base::MakeUnique<GetSTHLogResponseHandler>( |
411 failed_callback)); | 409 log_id, fetched_callback, failed_callback)); |
412 } | 410 } |
413 | 411 |
414 void LogProofFetcher::FetchConsistencyProof( | 412 void LogProofFetcher::FetchConsistencyProof( |
415 const GURL& base_log_url, | 413 const GURL& base_log_url, |
416 const std::string& log_id, | 414 const std::string& log_id, |
417 uint64_t old_tree_size, | 415 uint64_t old_tree_size, |
418 uint64_t new_tree_size, | 416 uint64_t new_tree_size, |
419 const ConsistencyProofFetchedCallback& fetched_callback, | 417 const ConsistencyProofFetchedCallback& fetched_callback, |
420 const FetchFailedCallback& failed_callback) { | 418 const FetchFailedCallback& failed_callback) { |
421 GURL request_url = base_log_url.Resolve(base::StringPrintf( | 419 GURL request_url = base_log_url.Resolve(base::StringPrintf( |
422 "ct/v1/get-sth-consistency?first=%" PRIu64 "&second=%" PRIu64, | 420 "ct/v1/get-sth-consistency?first=%" PRIu64 "&second=%" PRIu64, |
423 old_tree_size, new_tree_size)); | 421 old_tree_size, new_tree_size)); |
424 StartFetch(request_url, new GetConsistencyProofLogResponseHandler( | 422 StartFetch(request_url, |
425 log_id, fetched_callback, failed_callback)); | 423 base::MakeUnique<GetConsistencyProofLogResponseHandler>( |
| 424 log_id, fetched_callback, failed_callback)); |
426 } | 425 } |
427 | 426 |
428 void LogProofFetcher::StartFetch(const GURL& request_url, | 427 void LogProofFetcher::StartFetch( |
429 LogResponseHandler* log_request) { | 428 const GURL& request_url, |
430 log_request->StartFetch(request_context_, request_url, | 429 std::unique_ptr<LogResponseHandler> log_request) { |
431 base::Bind(&LogProofFetcher::OnFetchDone, | 430 log_request->StartFetch( |
432 weak_factory_.GetWeakPtr(), log_request)); | 431 request_context_, request_url, |
433 inflight_fetches_.insert(log_request); | 432 base::Bind(&LogProofFetcher::OnFetchDone, weak_factory_.GetWeakPtr(), |
| 433 log_request.get())); |
| 434 inflight_fetches_.insert(std::move(log_request)); |
434 } | 435 } |
435 | 436 |
436 void LogProofFetcher::OnFetchDone(LogResponseHandler* log_handler, | 437 void LogProofFetcher::OnFetchDone(LogResponseHandler* log_handler, |
437 const base::Closure& requestor_callback) { | 438 const base::Closure& requestor_callback) { |
438 auto it = inflight_fetches_.find(log_handler); | 439 auto it = std::find_if( |
| 440 inflight_fetches_.begin(), inflight_fetches_.end(), |
| 441 [log_handler](const std::unique_ptr<LogResponseHandler>& ptr) { |
| 442 return ptr.get() == log_handler; |
| 443 }); |
439 DCHECK(it != inflight_fetches_.end()); | 444 DCHECK(it != inflight_fetches_.end()); |
440 | 445 |
441 delete *it; | |
442 inflight_fetches_.erase(it); | 446 inflight_fetches_.erase(it); |
443 requestor_callback.Run(); | 447 requestor_callback.Run(); |
444 } | 448 } |
445 | 449 |
446 } // namespace certificate_transparency | 450 } // namespace certificate_transparency |
OLD | NEW |