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()); | |
Nathan Parker
2016/09/26 23:53:25
Can you DCHECK that content_type is a probably-val
eroman
2016/09/27 00:03:34
Or if JSON and application/octet-stream are going
estark
2016/09/27 04:05:20
I like the enum idea, but I can't figure out how t
| |
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 |
48 std::unique_ptr<UploadElementReader> reader( | 55 std::unique_ptr<UploadElementReader> reader(new UploadOwnedBytesElementReader( |
49 UploadOwnedBytesElementReader::CreateWithString(report)); | 56 new std::vector<char>(report.data(), report.data() + report.length()))); |
eroman
2016/09/26 23:40:55
nit: For terser code: (report.begin(), report.end(
estark
2016/09/27 04:05:20
Done.
| |
50 url_request->set_upload( | 57 url_request->set_upload( |
51 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0)); | 58 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0)); |
52 | 59 |
53 URLRequest* raw_url_request = url_request.get(); | 60 URLRequest* raw_url_request = url_request.get(); |
54 inflight_requests_.insert(url_request.release()); | 61 inflight_requests_.insert(url_request.release()); |
55 raw_url_request->Start(); | 62 raw_url_request->Start(); |
56 } | 63 } |
57 | 64 |
58 void ReportSender::SetErrorCallback(const ErrorCallback& error_callback) { | 65 void ReportSender::SetErrorCallback(const ErrorCallback& error_callback) { |
59 error_callback_ = error_callback; | 66 error_callback_ = error_callback; |
(...skipping 11 matching lines...) Expand all Loading... | |
71 CHECK_GT(inflight_requests_.erase(request), 0u); | 78 CHECK_GT(inflight_requests_.erase(request), 0u); |
72 // Clean up the request, which cancels it. | 79 // Clean up the request, which cancels it. |
73 delete request; | 80 delete request; |
74 } | 81 } |
75 | 82 |
76 void ReportSender::OnReadCompleted(URLRequest* request, int bytes_read) { | 83 void ReportSender::OnReadCompleted(URLRequest* request, int bytes_read) { |
77 NOTREACHED(); | 84 NOTREACHED(); |
78 } | 85 } |
79 | 86 |
80 } // namespace net | 87 } // namespace net |
OLD | NEW |