Make castv2 performance test work.
[chromium-blink-merge.git] / components / domain_reliability / uploader.cc
blob96ec845cb244b368f3d35f41e171f4b150b36826
1 // Copyright 2014 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.
5 #include "components/domain_reliability/uploader.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/metrics/sparse_histogram.h"
11 #include "base/stl_util.h"
12 #include "base/supports_user_data.h"
13 #include "components/domain_reliability/util.h"
14 #include "net/base/load_flags.h"
15 #include "net/base/net_errors.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/http/http_util.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_fetcher_delegate.h"
20 #include "net/url_request/url_request_context_getter.h"
22 namespace domain_reliability {
24 namespace {
26 const char* kJsonMimeType = "application/json; charset=utf-8";
28 class UploadUserData : public base::SupportsUserData::Data {
29 public:
30 static net::URLFetcher::CreateDataCallback CreateCreateDataCallback() {
31 return base::Bind(&UploadUserData::CreateUploadUserData);
34 static const void* kUserDataKey;
36 private:
37 static base::SupportsUserData::Data* CreateUploadUserData() {
38 return new UploadUserData();
42 const void* UploadUserData::kUserDataKey =
43 static_cast<const void*>(&UploadUserData::kUserDataKey);
45 class DomainReliabilityUploaderImpl
46 : public DomainReliabilityUploader, net::URLFetcherDelegate {
47 public:
48 DomainReliabilityUploaderImpl(
49 MockableTime* time,
50 const scoped_refptr<
51 net::URLRequestContextGetter>& url_request_context_getter)
52 : time_(time),
53 url_request_context_getter_(url_request_context_getter),
54 discard_uploads_(true) {}
56 ~DomainReliabilityUploaderImpl() override {
57 // Delete any in-flight URLFetchers.
58 STLDeleteContainerPairFirstPointers(
59 upload_callbacks_.begin(), upload_callbacks_.end());
62 // DomainReliabilityUploader implementation:
63 void UploadReport(
64 const std::string& report_json,
65 const GURL& upload_url,
66 const DomainReliabilityUploader::UploadCallback& callback) override {
67 VLOG(1) << "Uploading report to " << upload_url;
68 VLOG(2) << "Report JSON: " << report_json;
70 if (discard_uploads_) {
71 VLOG(1) << "Discarding report instead of uploading.";
72 UploadResult result;
73 result.status = UploadResult::SUCCESS;
74 callback.Run(result);
75 return;
78 net::URLFetcher* fetcher =
79 net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this);
80 fetcher->SetRequestContext(url_request_context_getter_.get());
81 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
82 net::LOAD_DO_NOT_SAVE_COOKIES);
83 fetcher->SetUploadData(kJsonMimeType, report_json);
84 fetcher->SetAutomaticallyRetryOn5xx(false);
85 fetcher->SetURLRequestUserData(
86 UploadUserData::kUserDataKey,
87 UploadUserData::CreateCreateDataCallback());
88 fetcher->Start();
90 upload_callbacks_[fetcher] = callback;
93 void set_discard_uploads(bool discard_uploads) override {
94 discard_uploads_ = discard_uploads;
95 VLOG(1) << "Setting discard_uploads to " << discard_uploads;
98 // net::URLFetcherDelegate implementation:
99 void OnURLFetchComplete(const net::URLFetcher* fetcher) override {
100 DCHECK(fetcher);
102 UploadCallbackMap::iterator callback_it = upload_callbacks_.find(fetcher);
103 DCHECK(callback_it != upload_callbacks_.end());
105 int net_error = GetNetErrorFromURLRequestStatus(fetcher->GetStatus());
106 int http_response_code = fetcher->GetResponseCode();
107 base::TimeDelta retry_after;
109 std::string retry_after_string;
110 if (fetcher->GetResponseHeaders() &&
111 fetcher->GetResponseHeaders()->EnumerateHeader(nullptr,
112 "Retry-After",
113 &retry_after_string)) {
114 net::HttpUtil::ParseRetryAfterHeader(retry_after_string,
115 time_->Now(),
116 &retry_after);
120 VLOG(1) << "Upload finished with net error " << net_error
121 << ", response code " << http_response_code
122 << ", retry after " << retry_after;
124 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadResponseCode",
125 http_response_code);
126 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadNetError",
127 -net_error);
129 UploadResult result;
130 GetUploadResultFromResponseDetails(net_error,
131 http_response_code,
132 retry_after,
133 &result);
134 callback_it->second.Run(result);
136 delete callback_it->first;
137 upload_callbacks_.erase(callback_it);
140 private:
141 using DomainReliabilityUploader::UploadCallback;
142 typedef std::map<const net::URLFetcher*, UploadCallback> UploadCallbackMap;
144 MockableTime* time_;
145 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
146 UploadCallbackMap upload_callbacks_;
147 bool discard_uploads_;
150 } // namespace
152 DomainReliabilityUploader::DomainReliabilityUploader() {}
153 DomainReliabilityUploader::~DomainReliabilityUploader() {}
155 // static
156 scoped_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create(
157 MockableTime* time,
158 const scoped_refptr<net::URLRequestContextGetter>&
159 url_request_context_getter) {
160 return scoped_ptr<DomainReliabilityUploader>(
161 new DomainReliabilityUploaderImpl(time, url_request_context_getter));
164 // static
165 bool DomainReliabilityUploader::URLRequestIsUpload(
166 const net::URLRequest& request) {
167 return request.GetUserData(UploadUserData::kUserDataKey) != nullptr;
170 } // namespace domain_reliability