| 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 | 9 |
| 9 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 10 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/numerics/safe_conversions.h" | 14 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 17 #include "components/safe_json/safe_json_parser.h" | 18 #include "components/safe_json/safe_json_parser.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 void StartNextReadLoop(); | 65 void StartNextReadLoop(); |
| 65 | 66 |
| 66 // Invokes the success callback. After this method is called, the LogFetcher | 67 // Invokes the success callback. After this method is called, the LogFetcher |
| 67 // is deleted and no longer safe to call. | 68 // is deleted and no longer safe to call. |
| 68 void RequestComplete(); | 69 void RequestComplete(); |
| 69 | 70 |
| 70 // Invokes the failure callback with the supplied error information. | 71 // Invokes the failure callback with the supplied error information. |
| 71 // After this method the LogFetcher is deleted and no longer safe to call. | 72 // After this method the LogFetcher is deleted and no longer safe to call. |
| 72 void InvokeFailureCallback(int net_error, int http_response_code); | 73 void InvokeFailureCallback(int net_error, int http_response_code); |
| 73 | 74 |
| 74 scoped_ptr<net::URLRequest> url_request_; | 75 std::unique_ptr<net::URLRequest> url_request_; |
| 75 const GURL request_url_; | 76 const GURL request_url_; |
| 76 base::Closure success_callback_; | 77 base::Closure success_callback_; |
| 77 FailureCallback failure_callback_; | 78 FailureCallback failure_callback_; |
| 78 scoped_refptr<net::IOBufferWithSize> response_buffer_; | 79 scoped_refptr<net::IOBufferWithSize> response_buffer_; |
| 79 std::string assembled_response_; | 80 std::string assembled_response_; |
| 80 | 81 |
| 81 DISALLOW_COPY_AND_ASSIGN(LogFetcher); | 82 DISALLOW_COPY_AND_ASSIGN(LogFetcher); |
| 82 }; | 83 }; |
| 83 | 84 |
| 84 LogFetcher::LogFetcher(net::URLRequestContext* request_context, | 85 LogFetcher::LogFetcher(net::URLRequestContext* request_context, |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 // handing the parsed JSON to HandleParsedJson, which is request-specific). | 229 // handing the parsed JSON to HandleParsedJson, which is request-specific). |
| 229 void HandleFetchCompletion(); | 230 void HandleFetchCompletion(); |
| 230 | 231 |
| 231 // Handle network failure to complete the request to the log, by invoking | 232 // Handle network failure to complete the request to the log, by invoking |
| 232 // the |done_callback_|. | 233 // the |done_callback_|. |
| 233 virtual void HandleNetFailure(int net_error, int http_response_code); | 234 virtual void HandleNetFailure(int net_error, int http_response_code); |
| 234 | 235 |
| 235 protected: | 236 protected: |
| 236 // Handle successful parsing of JSON by invoking HandleParsedJson, then | 237 // Handle successful parsing of JSON by invoking HandleParsedJson, then |
| 237 // invoking the |done_callback_| with the returned Closure. | 238 // invoking the |done_callback_| with the returned Closure. |
| 238 void OnJsonParseSuccess(scoped_ptr<base::Value> parsed_json); | 239 void OnJsonParseSuccess(std::unique_ptr<base::Value> parsed_json); |
| 239 | 240 |
| 240 // Handle failure to parse the JSON by invoking HandleJsonParseFailure, then | 241 // Handle failure to parse the JSON by invoking HandleJsonParseFailure, then |
| 241 // invoking the |done_callback_| with the returned Closure. | 242 // invoking the |done_callback_| with the returned Closure. |
| 242 void OnJsonParseError(const std::string& error); | 243 void OnJsonParseError(const std::string& error); |
| 243 | 244 |
| 244 // Handle respones JSON that parsed successfully, usually by | 245 // Handle respones JSON that parsed successfully, usually by |
| 245 // returning the success callback bound to parsed values as a Closure. | 246 // returning the success callback bound to parsed values as a Closure. |
| 246 virtual base::Closure HandleParsedJson(const base::Value& parsed_json) = 0; | 247 virtual base::Closure HandleParsedJson(const base::Value& parsed_json) = 0; |
| 247 | 248 |
| 248 // Handle failure to parse response JSON, usually by returning the failure | 249 // Handle failure to parse response JSON, usually by returning the failure |
| 249 // callback bound to a request-specific net error code. | 250 // callback bound to a request-specific net error code. |
| 250 virtual base::Closure HandleJsonParseFailure( | 251 virtual base::Closure HandleJsonParseFailure( |
| 251 const std::string& json_error) = 0; | 252 const std::string& json_error) = 0; |
| 252 | 253 |
| 253 const std::string log_id_; | 254 const std::string log_id_; |
| 254 LogProofFetcher::FetchFailedCallback failure_callback_; | 255 LogProofFetcher::FetchFailedCallback failure_callback_; |
| 255 scoped_ptr<LogFetcher> fetcher_; | 256 std::unique_ptr<LogFetcher> fetcher_; |
| 256 DoneCallback done_callback_; | 257 DoneCallback done_callback_; |
| 257 | 258 |
| 258 base::WeakPtrFactory<LogResponseHandler> weak_factory_; | 259 base::WeakPtrFactory<LogResponseHandler> weak_factory_; |
| 259 }; | 260 }; |
| 260 | 261 |
| 261 LogResponseHandler::LogResponseHandler( | 262 LogResponseHandler::LogResponseHandler( |
| 262 const std::string& log_id, | 263 const std::string& log_id, |
| 263 const LogProofFetcher::FetchFailedCallback& failure_callback) | 264 const LogProofFetcher::FetchFailedCallback& failure_callback) |
| 264 : log_id_(log_id), | 265 : log_id_(log_id), |
| 265 failure_callback_(failure_callback), | 266 failure_callback_(failure_callback), |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 LogProofFetcher::FetchFailedCallback failure_callback = | 302 LogProofFetcher::FetchFailedCallback failure_callback = |
| 302 base::ResetAndReturn(&failure_callback_); | 303 base::ResetAndReturn(&failure_callback_); |
| 303 | 304 |
| 304 base::ResetAndReturn(&done_callback_) | 305 base::ResetAndReturn(&done_callback_) |
| 305 .Run( | 306 .Run( |
| 306 base::Bind(failure_callback, log_id_, net_error, http_response_code)); | 307 base::Bind(failure_callback, log_id_, net_error, http_response_code)); |
| 307 // NOTE: |this| is not valid after the |done_callback_| is invoked. | 308 // NOTE: |this| is not valid after the |done_callback_| is invoked. |
| 308 } | 309 } |
| 309 | 310 |
| 310 void LogResponseHandler::OnJsonParseSuccess( | 311 void LogResponseHandler::OnJsonParseSuccess( |
| 311 scoped_ptr<base::Value> parsed_json) { | 312 std::unique_ptr<base::Value> parsed_json) { |
| 312 base::ResetAndReturn(&done_callback_).Run(HandleParsedJson(*parsed_json)); | 313 base::ResetAndReturn(&done_callback_).Run(HandleParsedJson(*parsed_json)); |
| 313 // NOTE: |this| is not valid after the |done_callback_| is invoked. | 314 // NOTE: |this| is not valid after the |done_callback_| is invoked. |
| 314 } | 315 } |
| 315 | 316 |
| 316 void LogResponseHandler::OnJsonParseError(const std::string& error) { | 317 void LogResponseHandler::OnJsonParseError(const std::string& error) { |
| 317 base::ResetAndReturn(&done_callback_).Run(HandleJsonParseFailure(error)); | 318 base::ResetAndReturn(&done_callback_).Run(HandleJsonParseFailure(error)); |
| 318 // NOTE: |this| is not valid after the |done_callback_| is invoked. | 319 // NOTE: |this| is not valid after the |done_callback_| is invoked. |
| 319 } | 320 } |
| 320 | 321 |
| 321 class GetSTHLogResponseHandler : public LogResponseHandler { | 322 class GetSTHLogResponseHandler : public LogResponseHandler { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 const base::Closure& requestor_callback) { | 437 const base::Closure& requestor_callback) { |
| 437 auto it = inflight_fetches_.find(log_handler); | 438 auto it = inflight_fetches_.find(log_handler); |
| 438 DCHECK(it != inflight_fetches_.end()); | 439 DCHECK(it != inflight_fetches_.end()); |
| 439 | 440 |
| 440 delete *it; | 441 delete *it; |
| 441 inflight_fetches_.erase(it); | 442 inflight_fetches_.erase(it); |
| 442 requestor_callback.Run(); | 443 requestor_callback.Run(); |
| 443 } | 444 } |
| 444 | 445 |
| 445 } // namespace certificate_transparency | 446 } // namespace certificate_transparency |
| OLD | NEW |