| 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/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" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 const ErrorCallback& error_callback) | 25 const ErrorCallback& error_callback) |
| 26 : request_context_(request_context), | 26 : request_context_(request_context), |
| 27 cookies_preference_(cookies_preference), | 27 cookies_preference_(cookies_preference), |
| 28 error_callback_(error_callback) {} | 28 error_callback_(error_callback) {} |
| 29 | 29 |
| 30 ReportSender::~ReportSender() { | 30 ReportSender::~ReportSender() { |
| 31 // Cancel all of the uncompleted requests. | 31 // Cancel all of the uncompleted requests. |
| 32 base::STLDeleteElements(&inflight_requests_); | 32 base::STLDeleteElements(&inflight_requests_); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void ReportSender::Send(const GURL& report_uri, const std::string& report) { | 35 void ReportSender::Send(const GURL& report_uri, |
| 36 base::StringPiece content_type, |
| 37 base::StringPiece report) { |
| 38 DCHECK(!content_type.empty()); |
| 36 std::unique_ptr<URLRequest> url_request = | 39 std::unique_ptr<URLRequest> url_request = |
| 37 request_context_->CreateRequest(report_uri, DEFAULT_PRIORITY, this); | 40 request_context_->CreateRequest(report_uri, DEFAULT_PRIORITY, this); |
| 38 | 41 |
| 39 int load_flags = | 42 int load_flags = |
| 40 LOAD_BYPASS_CACHE | LOAD_DISABLE_CACHE | LOAD_DO_NOT_SEND_AUTH_DATA; | 43 LOAD_BYPASS_CACHE | LOAD_DISABLE_CACHE | LOAD_DO_NOT_SEND_AUTH_DATA; |
| 41 if (cookies_preference_ != SEND_COOKIES) { | 44 if (cookies_preference_ != SEND_COOKIES) { |
| 42 load_flags |= LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES; | 45 load_flags |= LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES; |
| 43 } | 46 } |
| 44 url_request->SetLoadFlags(load_flags); | 47 url_request->SetLoadFlags(load_flags); |
| 45 | 48 |
| 49 HttpRequestHeaders extra_headers; |
| 50 extra_headers.SetHeader(HttpRequestHeaders::kContentType, content_type); |
| 51 url_request->SetExtraRequestHeaders(extra_headers); |
| 52 |
| 46 url_request->set_method("POST"); | 53 url_request->set_method("POST"); |
| 47 | 54 |
| 55 std::vector<char> report_data(report.begin(), report.end()); |
| 48 std::unique_ptr<UploadElementReader> reader( | 56 std::unique_ptr<UploadElementReader> reader( |
| 49 UploadOwnedBytesElementReader::CreateWithString(report)); | 57 new UploadOwnedBytesElementReader(&report_data)); |
| 50 url_request->set_upload( | 58 url_request->set_upload( |
| 51 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0)); | 59 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0)); |
| 52 | 60 |
| 53 URLRequest* raw_url_request = url_request.get(); | 61 URLRequest* raw_url_request = url_request.get(); |
| 54 inflight_requests_.insert(url_request.release()); | 62 inflight_requests_.insert(url_request.release()); |
| 55 raw_url_request->Start(); | 63 raw_url_request->Start(); |
| 56 } | 64 } |
| 57 | 65 |
| 58 void ReportSender::SetErrorCallback(const ErrorCallback& error_callback) { | 66 void ReportSender::SetErrorCallback(const ErrorCallback& error_callback) { |
| 59 error_callback_ = error_callback; | 67 error_callback_ = error_callback; |
| 60 } | 68 } |
| 61 | 69 |
| 62 void ReportSender::OnResponseStarted(URLRequest* request) { | 70 void ReportSender::OnResponseStarted(URLRequest* request) { |
| 63 if (!request->status().is_success()) { | 71 if (!request->status().is_success()) { |
| 64 DVLOG(1) << "Failed to send report for " << request->url().host(); | 72 DVLOG(1) << "Failed to send report for " << request->url().host(); |
| 65 if (!error_callback_.is_null()) | 73 if (!error_callback_.is_null()) |
| 66 error_callback_.Run(request->url(), request->status().error()); | 74 error_callback_.Run(request->url(), request->status().error()); |
| 67 } | 75 } |
| 68 | 76 |
| 69 CHECK_GT(inflight_requests_.erase(request), 0u); | 77 CHECK_GT(inflight_requests_.erase(request), 0u); |
| 70 // Clean up the request, which cancels it. | 78 // Clean up the request, which cancels it. |
| 71 delete request; | 79 delete request; |
| 72 } | 80 } |
| 73 | 81 |
| 74 void ReportSender::OnReadCompleted(URLRequest* request, int bytes_read) { | 82 void ReportSender::OnReadCompleted(URLRequest* request, int bytes_read) { |
| 75 NOTREACHED(); | 83 NOTREACHED(); |
| 76 } | 84 } |
| 77 | 85 |
| 78 } // namespace net | 86 } // namespace net |
| OLD | NEW |