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

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: rebase Created 4 years 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
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 "net/base/elements_upload_data_stream.h"
10 #include "net/base/upload_bytes_element_reader.h"
11 #include "net/reporting/reporting_metrics.h"
12 #include "net/url_request/url_request_context.h"
13
14 namespace {
15 const char kUploadContentType[] = "application/report";
16 } // namespace
17
18 namespace net {
19
20 namespace {
21
22 ReportingUploader::Outcome ResponseCodeToOutcome(int response_code) {
23 if (response_code >= 200 && response_code <= 299)
24 return ReportingUploader::SUCCESS;
25 if (response_code == 410)
26 return ReportingUploader::REMOVE_ENDPOINT;
27 return ReportingUploader::FAILURE;
28 }
29
30 DeliveryFate OutcomeToFate(ReportingUploader::Outcome outcome) {
31 return (outcome == ReportingUploader::SUCCESS) ? DELIVERY_FATE_SUCCESS
32 : DELIVERY_FATE_FAILURE;
33 }
34
35 class ReportingUploaderImpl : public ReportingUploader, URLRequest::Delegate {
36 public:
37 ReportingUploaderImpl(const URLRequestContext* context) : context_(context) {
38 DCHECK(context_);
39 }
40
41 ~ReportingUploaderImpl() override {
42 for (auto& it : uploads_) {
43 base::ResetAndReturn(&it.second->second).Run(FAILURE);
44 it.second->first->Cancel();
45 HistogramDeliveryOutcome(DELIVERY_FATE_SHUTDOWN, 0, 0);
46 }
47 uploads_.clear();
48 }
49
50 void AttemptDelivery(const GURL& url,
51 const std::string& json,
52 const Callback& callback) override {
53 std::unique_ptr<URLRequest> request =
54 context_->CreateRequest(url, IDLE, this);
55
56 request->set_method("POST");
57
58 request->SetExtraRequestHeaderByName(HttpRequestHeaders::kContentType,
59 kUploadContentType, true);
60
61 std::vector<char> json_data(json.begin(), json.end());
62 std::unique_ptr<UploadElementReader> reader(
63 new UploadOwnedBytesElementReader(&json_data));
64 request->set_upload(
65 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0));
66
67 // This inherently sets mode = "no-cors", but that doesn't matter, because
68 // the origins that are included in the upload don't actually get to see
69 // the response.
70 //
71 // This inherently skips Service Worker, too.
72 request->Start();
73
74 // Have to grab this as a reference to ensure request.get() happens before
75 // std::move(request).
76 std::unique_ptr<Upload>& upload = uploads_[request.get()];
77 upload = base::MakeUnique<Upload>(std::move(request), callback);
78 }
79
80 // URLRequest::Delegate implementation:
81 void OnResponseStarted(URLRequest* request, int net_error) override {
82 UploadMap::iterator it = uploads_.find(request);
83 std::unique_ptr<Upload> upload = std::move(it->second);
84 uploads_.erase(it);
85
86 int response_code = request->GetResponseCode();
87
88 Outcome outcome = ResponseCodeToOutcome(response_code);
89
90 HistogramDeliveryOutcome(OutcomeToFate(outcome),
91 request->status().ToNetError(), response_code);
92
93 upload->second.Run(outcome);
94
95 request->Cancel();
96
97 // Deletes request (upload->first) when upload goes out of scope and is
98 // deleted.
99 }
100
101 void OnReadCompleted(URLRequest* request, int bytes_read) override {
102 // Reporting doesn't need anything in the body of the response, so it
103 // doesn't read it, so it should never get OnReadCompleted calls.
104 NOTREACHED();
105 }
106
107 private:
108 using Upload = std::pair<std::unique_ptr<URLRequest>, Callback>;
109 using UploadMap = std::map<const URLRequest*, std::unique_ptr<Upload>>;
110
111 const URLRequestContext* context_;
112 UploadMap uploads_;
113 };
114
115 } // namespace
116
117 ReportingUploader::~ReportingUploader() {}
118
119 // static
120 std::unique_ptr<ReportingUploader> ReportingUploader::Create(
121 const URLRequestContext* context) {
122 return base::MakeUnique<ReportingUploaderImpl>(context);
123 }
124
125 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698