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 27 matching lines...) Expand all Loading... |
38 | 38 |
39 int load_flags = | 39 int load_flags = |
40 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; |
41 if (cookies_preference_ != SEND_COOKIES) { | 41 if (cookies_preference_ != SEND_COOKIES) { |
42 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; |
43 } | 43 } |
44 url_request->SetLoadFlags(load_flags); | 44 url_request->SetLoadFlags(load_flags); |
45 | 45 |
46 url_request->set_method("POST"); | 46 url_request->set_method("POST"); |
47 | 47 |
| 48 if (!content_type_.empty()) { |
| 49 url_request->SetExtraRequestHeaderByName(HttpRequestHeaders::kContentType, |
| 50 content_type_, true); |
| 51 } |
| 52 |
48 std::unique_ptr<UploadElementReader> reader( | 53 std::unique_ptr<UploadElementReader> reader( |
49 UploadOwnedBytesElementReader::CreateWithString(report)); | 54 UploadOwnedBytesElementReader::CreateWithString(report)); |
50 url_request->set_upload( | 55 url_request->set_upload( |
51 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0)); | 56 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0)); |
52 | 57 |
53 URLRequest* raw_url_request = url_request.get(); | 58 URLRequest* raw_url_request = url_request.get(); |
54 inflight_requests_.insert(url_request.release()); | 59 inflight_requests_.insert(url_request.release()); |
55 raw_url_request->Start(); | 60 raw_url_request->Start(); |
56 } | 61 } |
57 | 62 |
| 63 void ReportSender::SetContentTypeHeader(const std::string& content_type) { |
| 64 content_type_ = content_type; |
| 65 } |
| 66 |
58 void ReportSender::SetErrorCallback(const ErrorCallback& error_callback) { | 67 void ReportSender::SetErrorCallback(const ErrorCallback& error_callback) { |
59 error_callback_ = error_callback; | 68 error_callback_ = error_callback; |
60 } | 69 } |
61 | 70 |
62 void ReportSender::OnResponseStarted(URLRequest* request, int net_error) { | 71 void ReportSender::OnResponseStarted(URLRequest* request, int net_error) { |
63 DCHECK_NE(ERR_IO_PENDING, net_error); | 72 DCHECK_NE(ERR_IO_PENDING, net_error); |
64 | 73 |
65 if (net_error != OK) { | 74 if (net_error != OK) { |
66 DVLOG(1) << "Failed to send report for " << request->url().host(); | 75 DVLOG(1) << "Failed to send report for " << request->url().host(); |
67 if (!error_callback_.is_null()) | 76 if (!error_callback_.is_null()) |
68 error_callback_.Run(request->url(), net_error); | 77 error_callback_.Run(request->url(), net_error); |
69 } | 78 } |
70 | 79 |
71 CHECK_GT(inflight_requests_.erase(request), 0u); | 80 CHECK_GT(inflight_requests_.erase(request), 0u); |
72 // Clean up the request, which cancels it. | 81 // Clean up the request, which cancels it. |
73 delete request; | 82 delete request; |
74 } | 83 } |
75 | 84 |
76 void ReportSender::OnReadCompleted(URLRequest* request, int bytes_read) { | 85 void ReportSender::OnReadCompleted(URLRequest* request, int bytes_read) { |
77 NOTREACHED(); | 86 NOTREACHED(); |
78 } | 87 } |
79 | 88 |
80 } // namespace net | 89 } // namespace net |
OLD | NEW |