| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_ |
| 6 #define COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "net/url_request/url_request.h" |
| 16 |
| 17 namespace base { |
| 18 class Value; |
| 19 } // namespace base |
| 20 |
| 21 namespace net { |
| 22 |
| 23 class URLRequestContext; |
| 24 |
| 25 namespace ct { |
| 26 struct SignedTreeHead; |
| 27 } // namespace ct |
| 28 |
| 29 } // namespace net |
| 30 |
| 31 class GURL; |
| 32 |
| 33 namespace certificate_transparency { |
| 34 |
| 35 // Fetches Signed Tree Heads (STHs) and consistency proofs from Certificate |
| 36 // Transparency logs using the URLRequestContext provided during the instance |
| 37 // construction. |
| 38 class LogProofFetcher : public net::URLRequest::Delegate { |
| 39 public: |
| 40 // Callback for successful retrieval of Signed Tree Heads. Called |
| 41 // with the log_id of the log the STH belogs to (as supplied by the caller |
| 42 // to FetchSignedTreeHead) and the STH itself. |
| 43 using SignedTreeHeadFetchedCallback = |
| 44 base::Callback<void(const std::string& log_id, |
| 45 const net::ct::SignedTreeHead& signed_tree_head)>; |
| 46 |
| 47 // Callback for failure of Signed Tree Head retrieval. Called with the log_id |
| 48 // that the log fetching was requested for and a net error code of the |
| 49 // failure. |
| 50 using FetchFailedCallback = base::Callback< |
| 51 void(const std::string& log_id, int net_error, int http_response_code)>; |
| 52 |
| 53 explicit LogProofFetcher(net::URLRequestContext* request_context); |
| 54 ~LogProofFetcher() override; |
| 55 |
| 56 // Fetch the latest Signed Tree Head from the log identified by |log_id| |
| 57 // from |base_log_url|. The |log_id| will be passed into the callbacks to |
| 58 // identify the log the retrieved Signed Tree Head belongs to. |
| 59 // It is possible, but does not make a lot of sense, to have multiple |
| 60 // Signed Tree Head fetching requests going out to the same log, since |
| 61 // they are likely to return the same result. |
| 62 // TODO(eranm): Think further about whether multiple requests to the same |
| 63 // log imply cancellation of previous requests, should be coalesced or handled |
| 64 // independently. |
| 65 void FetchSignedTreeHead( |
| 66 const GURL& base_log_url, |
| 67 const std::string& log_id, |
| 68 const SignedTreeHeadFetchedCallback& fetched_callback, |
| 69 const FetchFailedCallback& failed_callback); |
| 70 |
| 71 // net::URLRequest::Delegate |
| 72 void OnResponseStarted(net::URLRequest* request) override; |
| 73 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
| 74 |
| 75 private: |
| 76 struct FetchState { |
| 77 FetchState(const std::string& id, |
| 78 const SignedTreeHeadFetchedCallback& fetched_callback, |
| 79 const FetchFailedCallback& failed_callback); |
| 80 ~FetchState(); |
| 81 |
| 82 std::string log_id; |
| 83 SignedTreeHeadFetchedCallback fetched_callback; |
| 84 FetchFailedCallback failed_callback; |
| 85 scoped_refptr<net::IOBufferWithSize> response_buffer; |
| 86 std::string assembled_response; |
| 87 }; |
| 88 |
| 89 // Handles the result of a URLRequest::Read call on |request|. |
| 90 // Returns true if another read should be started, false if the read |
| 91 // failed completely or we have to wait for OnResponseStarted to |
| 92 // be called. |
| 93 bool HandleReadResult(net::URLRequest* request, |
| 94 FetchState* params, |
| 95 int bytes_read); |
| 96 |
| 97 // Calls URLRequest::Read on |request| repeatedly, until HandleReadResult |
| 98 // indicates it should no longer be called. Usually this would be when there |
| 99 // is pending IO that requires waiting for OnResponseStarted to be called. |
| 100 void StartNextRead(net::URLRequest* request, FetchState* params); |
| 101 |
| 102 // Performs post-report cleanup. |
| 103 void RequestComplete(net::URLRequest* request); |
| 104 // Deletes the request and associated FetchState from the internal map. |
| 105 void CleanupRequest(net::URLRequest* request); |
| 106 |
| 107 // Actually create the request |
| 108 scoped_ptr<net::URLRequest> CreateURLRequest(const GURL& fetch_sth_url); |
| 109 |
| 110 // Callbacks for parsing the STH's JSON by the SafeJsonParser |
| 111 void OnSTHJsonParseSuccess(FetchState params, |
| 112 scoped_ptr<base::Value> parsed_json); |
| 113 void OnSTHJsonParseError(FetchState params, const std::string& error); |
| 114 |
| 115 net::URLRequestContext* const request_context_; |
| 116 |
| 117 // Owns the contained requests, as well as FetchState. |
| 118 std::map<net::URLRequest*, FetchState*> inflight_requests_; |
| 119 |
| 120 base::WeakPtrFactory<LogProofFetcher> weak_factory_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(LogProofFetcher); |
| 123 }; |
| 124 |
| 125 } // namespace certificate_transparency |
| 126 |
| 127 #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_LOG_PROOF_FETCHER_H_ |
| OLD | NEW |