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