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