| 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 #include "net/url_request/certificate_report_sender.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/stl_util.h" | |
| 10 #include "net/base/elements_upload_data_stream.h" | |
| 11 #include "net/base/load_flags.h" | |
| 12 #include "net/base/request_priority.h" | |
| 13 #include "net/base/upload_bytes_element_reader.h" | |
| 14 #include "net/url_request/url_request_context.h" | |
| 15 #include "net/url_request/url_request_status.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 CertificateReportSender::CertificateReportSender( | |
| 20 URLRequestContext* request_context, | |
| 21 CookiesPreference cookies_preference) | |
| 22 : CertificateReportSender(request_context, | |
| 23 cookies_preference, | |
| 24 ErrorCallback()) {} | |
| 25 | |
| 26 CertificateReportSender::CertificateReportSender( | |
| 27 URLRequestContext* request_context, | |
| 28 CookiesPreference cookies_preference, | |
| 29 const ErrorCallback& error_callback) | |
| 30 : request_context_(request_context), | |
| 31 cookies_preference_(cookies_preference), | |
| 32 error_callback_(error_callback) {} | |
| 33 | |
| 34 CertificateReportSender::~CertificateReportSender() { | |
| 35 // Cancel all of the uncompleted requests. | |
| 36 STLDeleteElements(&inflight_requests_); | |
| 37 } | |
| 38 | |
| 39 void CertificateReportSender::Send(const GURL& report_uri, | |
| 40 const std::string& report) { | |
| 41 std::unique_ptr<URLRequest> url_request = | |
| 42 request_context_->CreateRequest(report_uri, DEFAULT_PRIORITY, this); | |
| 43 | |
| 44 int load_flags = | |
| 45 LOAD_BYPASS_CACHE | LOAD_DISABLE_CACHE | LOAD_DO_NOT_SEND_AUTH_DATA; | |
| 46 if (cookies_preference_ != SEND_COOKIES) { | |
| 47 load_flags |= LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES; | |
| 48 } | |
| 49 url_request->SetLoadFlags(load_flags); | |
| 50 | |
| 51 url_request->set_method("POST"); | |
| 52 | |
| 53 std::unique_ptr<UploadElementReader> reader( | |
| 54 UploadOwnedBytesElementReader::CreateWithString(report)); | |
| 55 url_request->set_upload( | |
| 56 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0)); | |
| 57 | |
| 58 URLRequest* raw_url_request = url_request.get(); | |
| 59 inflight_requests_.insert(url_request.release()); | |
| 60 raw_url_request->Start(); | |
| 61 } | |
| 62 | |
| 63 void CertificateReportSender::SetErrorCallback( | |
| 64 const ErrorCallback& error_callback) { | |
| 65 error_callback_ = error_callback; | |
| 66 } | |
| 67 | |
| 68 void CertificateReportSender::OnResponseStarted(URLRequest* request) { | |
| 69 if (!request->status().is_success()) { | |
| 70 DVLOG(1) << "Failed to send certificate report for " | |
| 71 << request->url().host(); | |
| 72 if (!error_callback_.is_null()) | |
| 73 error_callback_.Run(request->url(), request->status().error()); | |
| 74 } | |
| 75 | |
| 76 CHECK_GT(inflight_requests_.erase(request), 0u); | |
| 77 // Clean up the request, which cancels it. | |
| 78 delete request; | |
| 79 } | |
| 80 | |
| 81 void CertificateReportSender::OnReadCompleted(URLRequest* request, | |
| 82 int bytes_read) { | |
| 83 NOTREACHED(); | |
| 84 } | |
| 85 | |
| 86 } // namespace net | |
| OLD | NEW |