Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: net/reporting/reporting_uploader.cc

Issue 2249213002: [OBSOLETE] Reporting: Initial implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Try fixing unittest compile error on Android? Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/reporting/reporting_uploader.h ('k') | net/reporting/reporting_uploader_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/reporting/reporting_uploader.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/metrics/histogram_macros.h"
10 #include "net/base/elements_upload_data_stream.h"
11 #include "net/base/load_flags.h"
12 #include "net/base/upload_bytes_element_reader.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/url_request/redirect_info.h"
15 #include "net/url_request/url_request_context.h"
16
17 namespace net {
18
19 namespace {
20
21 ReportingUploader::Outcome ResponseCodeToOutcome(int response_code) {
22 if (response_code >= 200 && response_code <= 299)
23 return ReportingUploader::SUCCESS;
24 if (response_code == 410)
25 return ReportingUploader::REMOVE_ENDPOINT;
26 return ReportingUploader::FAILURE;
27 }
28
29 class ReportingUploaderImpl : public ReportingUploader, URLRequest::Delegate {
30 public:
31 ReportingUploaderImpl(const URLRequestContext* context) : context_(context) {
32 DCHECK(context_);
33 }
34
35 ~ReportingUploaderImpl() override {
36 for (auto& it : uploads_) {
37 base::ResetAndReturn(&it.second->second).Run(FAILURE);
38 it.second->first->Cancel();
39 }
40 uploads_.clear();
41 }
42
43 void AttemptDelivery(const GURL& url,
44 const std::string& json,
45 const Callback& callback) override {
46 std::unique_ptr<URLRequest> request =
47 context_->CreateRequest(url, IDLE, this);
48
49 request->set_method("POST");
50
51 request->SetLoadFlags(LOAD_DISABLE_CACHE | LOAD_DO_NOT_SAVE_COOKIES |
52 LOAD_DO_NOT_SEND_COOKIES);
53
54 request->SetExtraRequestHeaderByName(HttpRequestHeaders::kContentType,
55 kUploadContentType, true);
56
57 std::vector<char> json_data(json.begin(), json.end());
58 std::unique_ptr<UploadElementReader> reader(
59 new UploadOwnedBytesElementReader(&json_data));
60 request->set_upload(
61 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0));
62
63 // This inherently sets mode = "no-cors", but that doesn't matter, because
64 // the origins that are included in the upload don't actually get to see
65 // the response.
66 //
67 // This inherently skips Service Worker, too.
68 request->Start();
69
70 // Have to grab this as a reference to ensure request.get() happens before
71 // std::move(request).
72 std::unique_ptr<Upload>& upload = uploads_[request.get()];
73 upload = base::MakeUnique<Upload>(std::move(request), callback);
74 }
75
76 // URLRequest::Delegate implementation:
77 void OnReceivedRedirect(URLRequest* request,
78 const RedirectInfo& redirect_info,
79 bool* defer_redirect) override {
80 if (!redirect_info.new_url.SchemeIsCryptographic()) {
81 LOG(WARNING) << "Canceling Reporting upload redirected to non-HTTPS URL.";
82 request->Cancel();
83 return;
84 }
85 }
86
87 void OnAuthRequired(URLRequest* request,
88 AuthChallengeInfo* auth_info) override {
89 LOG(WARNING) << "Canceling Reporting upload prompted for authentication.";
90 request->Cancel();
91 }
92
93 void OnCertificateRequested(URLRequest* request,
94 SSLCertRequestInfo* cert_request_info) override {
95 LOG(WARNING) << "Canceling Reporting upload prompted for certificate.";
96 request->Cancel();
97 }
98
99 void OnResponseStarted(URLRequest* request, int net_error) override {
100 UploadMap::iterator it = uploads_.find(request);
101 DCHECK(it != uploads_.end());
102 std::unique_ptr<Upload> upload = std::move(it->second);
103 uploads_.erase(it);
104
105 // request->GetResponseCode() should work, but doesn't in the cases above
106 // where the request was canceled, so get the response code by hand.
107 HttpResponseHeaders* headers = request->response_headers();
108 int response_code = headers ? headers->response_code() : 0;
109 Outcome outcome = ResponseCodeToOutcome(response_code);
110
111 upload->second.Run(outcome);
112
113 request->Cancel();
114
115 // Deletes request (upload->first) when upload goes out of scope and is
116 // deleted.
117 }
118
119 void OnReadCompleted(URLRequest* request, int bytes_read) override {
120 // Reporting doesn't need anything in the body of the response, so it
121 // doesn't read it, so it should never get OnReadCompleted calls.
122 NOTREACHED();
123 }
124
125 private:
126 using Upload = std::pair<std::unique_ptr<URLRequest>, Callback>;
127 using UploadMap = std::map<const URLRequest*, std::unique_ptr<Upload>>;
128
129 const URLRequestContext* context_;
130 UploadMap uploads_;
131 };
132
133 } // namespace
134
135 // static
136 const char ReportingUploader::kUploadContentType[] = "application/report";
137
138 ReportingUploader::~ReportingUploader() {}
139
140 // static
141 std::unique_ptr<ReportingUploader> ReportingUploader::Create(
142 const URLRequestContext* context) {
143 return base::MakeUnique<ReportingUploaderImpl>(context);
144 }
145
146 } // namespace net
OLDNEW
« no previous file with comments | « net/reporting/reporting_uploader.h ('k') | net/reporting/reporting_uploader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698