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

Side by Side Diff: components/domain_reliability/uploader.cc

Issue 2289763003: Remove stl_util use from domain_reliability. (Closed)
Patch Set: win Created 4 years, 3 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "components/domain_reliability/uploader.h" 5 #include "components/domain_reliability/uploader.h"
6 6
7 #include <utility>
8
7 #include "base/bind.h" 9 #include "base/bind.h"
8 #include "base/callback.h" 10 #include "base/callback.h"
9 #include "base/metrics/sparse_histogram.h" 11 #include "base/metrics/sparse_histogram.h"
10 #include "base/stl_util.h"
11 #include "base/supports_user_data.h" 12 #include "base/supports_user_data.h"
12 #include "components/data_use_measurement/core/data_use_user_data.h" 13 #include "components/data_use_measurement/core/data_use_user_data.h"
13 #include "components/domain_reliability/util.h" 14 #include "components/domain_reliability/util.h"
14 #include "net/base/load_flags.h" 15 #include "net/base/load_flags.h"
15 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
16 #include "net/http/http_response_headers.h" 17 #include "net/http/http_response_headers.h"
17 #include "net/http/http_util.h" 18 #include "net/http/http_util.h"
18 #include "net/url_request/url_fetcher.h" 19 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_fetcher_delegate.h" 20 #include "net/url_request/url_fetcher_delegate.h"
20 #include "net/url_request/url_request_context_getter.h" 21 #include "net/url_request/url_request_context_getter.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 : public DomainReliabilityUploader, net::URLFetcherDelegate { 54 : public DomainReliabilityUploader, net::URLFetcherDelegate {
54 public: 55 public:
55 DomainReliabilityUploaderImpl( 56 DomainReliabilityUploaderImpl(
56 MockableTime* time, 57 MockableTime* time,
57 const scoped_refptr< 58 const scoped_refptr<
58 net::URLRequestContextGetter>& url_request_context_getter) 59 net::URLRequestContextGetter>& url_request_context_getter)
59 : time_(time), 60 : time_(time),
60 url_request_context_getter_(url_request_context_getter), 61 url_request_context_getter_(url_request_context_getter),
61 discard_uploads_(true) {} 62 discard_uploads_(true) {}
62 63
63 ~DomainReliabilityUploaderImpl() override { 64 ~DomainReliabilityUploaderImpl() override {}
64 // Delete any in-flight URLFetchers.
65 base::STLDeleteContainerPairFirstPointers(upload_callbacks_.begin(),
66 upload_callbacks_.end());
67 }
68 65
69 // DomainReliabilityUploader implementation: 66 // DomainReliabilityUploader implementation:
70 void UploadReport( 67 void UploadReport(
71 const std::string& report_json, 68 const std::string& report_json,
72 int max_upload_depth, 69 int max_upload_depth,
73 const GURL& upload_url, 70 const GURL& upload_url,
74 const DomainReliabilityUploader::UploadCallback& callback) override { 71 const DomainReliabilityUploader::UploadCallback& callback) override {
75 VLOG(1) << "Uploading report to " << upload_url; 72 VLOG(1) << "Uploading report to " << upload_url;
76 VLOG(2) << "Report JSON: " << report_json; 73 VLOG(2) << "Report JSON: " << report_json;
77 74
78 if (discard_uploads_) { 75 if (discard_uploads_) {
79 VLOG(1) << "Discarding report instead of uploading."; 76 VLOG(1) << "Discarding report instead of uploading.";
80 UploadResult result; 77 UploadResult result;
81 result.status = UploadResult::SUCCESS; 78 result.status = UploadResult::SUCCESS;
82 callback.Run(result); 79 callback.Run(result);
83 return; 80 return;
84 } 81 }
85 82
86 net::URLFetcher* fetcher = 83 std::unique_ptr<net::URLFetcher> owned_fetcher =
87 net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this) 84 net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this);
88 .release(); 85 net::URLFetcher* fetcher = owned_fetcher.get();
89 data_use_measurement::DataUseUserData::AttachToFetcher( 86 data_use_measurement::DataUseUserData::AttachToFetcher(
90 fetcher, data_use_measurement::DataUseUserData::DOMAIN_RELIABILITY); 87 fetcher, data_use_measurement::DataUseUserData::DOMAIN_RELIABILITY);
91 fetcher->SetRequestContext(url_request_context_getter_.get()); 88 fetcher->SetRequestContext(url_request_context_getter_.get());
92 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 89 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
93 net::LOAD_DO_NOT_SAVE_COOKIES); 90 net::LOAD_DO_NOT_SAVE_COOKIES);
94 fetcher->SetUploadData(kJsonMimeType, report_json); 91 fetcher->SetUploadData(kJsonMimeType, report_json);
95 fetcher->SetAutomaticallyRetryOn5xx(false); 92 fetcher->SetAutomaticallyRetryOn5xx(false);
96 fetcher->SetURLRequestUserData( 93 fetcher->SetURLRequestUserData(
97 UploadUserData::kUserDataKey, 94 UploadUserData::kUserDataKey,
98 UploadUserData::CreateCreateDataCallback(max_upload_depth + 1)); 95 UploadUserData::CreateCreateDataCallback(max_upload_depth + 1));
99 fetcher->Start(); 96 fetcher->Start();
100 97
101 upload_callbacks_[fetcher] = callback; 98 upload_callbacks_[fetcher] = {std::move(owned_fetcher), callback};
102 } 99 }
103 100
104 void set_discard_uploads(bool discard_uploads) override { 101 void set_discard_uploads(bool discard_uploads) override {
105 discard_uploads_ = discard_uploads; 102 discard_uploads_ = discard_uploads;
106 VLOG(1) << "Setting discard_uploads to " << discard_uploads; 103 VLOG(1) << "Setting discard_uploads to " << discard_uploads;
107 } 104 }
108 105
109 // net::URLFetcherDelegate implementation: 106 // net::URLFetcherDelegate implementation:
110 void OnURLFetchComplete(const net::URLFetcher* fetcher) override { 107 void OnURLFetchComplete(const net::URLFetcher* fetcher) override {
111 DCHECK(fetcher); 108 DCHECK(fetcher);
112 109
113 UploadCallbackMap::iterator callback_it = upload_callbacks_.find(fetcher); 110 auto callback_it = upload_callbacks_.find(fetcher);
114 DCHECK(callback_it != upload_callbacks_.end()); 111 DCHECK(callback_it != upload_callbacks_.end());
115 112
116 int net_error = GetNetErrorFromURLRequestStatus(fetcher->GetStatus()); 113 int net_error = GetNetErrorFromURLRequestStatus(fetcher->GetStatus());
117 int http_response_code = fetcher->GetResponseCode(); 114 int http_response_code = fetcher->GetResponseCode();
118 base::TimeDelta retry_after; 115 base::TimeDelta retry_after;
119 { 116 {
120 std::string retry_after_string; 117 std::string retry_after_string;
121 if (fetcher->GetResponseHeaders() && 118 if (fetcher->GetResponseHeaders() &&
122 fetcher->GetResponseHeaders()->EnumerateHeader(nullptr, 119 fetcher->GetResponseHeaders()->EnumerateHeader(nullptr,
123 "Retry-After", 120 "Retry-After",
(...skipping 11 matching lines...) Expand all
135 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadResponseCode", 132 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadResponseCode",
136 http_response_code); 133 http_response_code);
137 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadNetError", 134 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadNetError",
138 -net_error); 135 -net_error);
139 136
140 UploadResult result; 137 UploadResult result;
141 GetUploadResultFromResponseDetails(net_error, 138 GetUploadResultFromResponseDetails(net_error,
142 http_response_code, 139 http_response_code,
143 retry_after, 140 retry_after,
144 &result); 141 &result);
145 callback_it->second.Run(result); 142 callback_it->second.second.Run(result);
146 143
147 delete callback_it->first;
148 upload_callbacks_.erase(callback_it); 144 upload_callbacks_.erase(callback_it);
149 } 145 }
150 146
151 private: 147 private:
152 using DomainReliabilityUploader::UploadCallback; 148 using DomainReliabilityUploader::UploadCallback;
153 typedef std::map<const net::URLFetcher*, UploadCallback> UploadCallbackMap;
154 149
155 MockableTime* time_; 150 MockableTime* time_;
156 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 151 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
157 UploadCallbackMap upload_callbacks_; 152 std::map<const net::URLFetcher*,
153 std::pair<std::unique_ptr<net::URLFetcher>, UploadCallback>>
154 upload_callbacks_;
158 bool discard_uploads_; 155 bool discard_uploads_;
159 }; 156 };
160 157
161 } // namespace 158 } // namespace
162 159
163 DomainReliabilityUploader::DomainReliabilityUploader() {} 160 DomainReliabilityUploader::DomainReliabilityUploader() {}
164 DomainReliabilityUploader::~DomainReliabilityUploader() {} 161 DomainReliabilityUploader::~DomainReliabilityUploader() {}
165 162
166 // static 163 // static
167 std::unique_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create( 164 std::unique_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create(
168 MockableTime* time, 165 MockableTime* time,
169 const scoped_refptr<net::URLRequestContextGetter>& 166 const scoped_refptr<net::URLRequestContextGetter>&
170 url_request_context_getter) { 167 url_request_context_getter) {
171 return std::unique_ptr<DomainReliabilityUploader>( 168 return std::unique_ptr<DomainReliabilityUploader>(
172 new DomainReliabilityUploaderImpl(time, url_request_context_getter)); 169 new DomainReliabilityUploaderImpl(time, url_request_context_getter));
173 } 170 }
174 171
175 // static 172 // static
176 int DomainReliabilityUploader::GetURLRequestUploadDepth( 173 int DomainReliabilityUploader::GetURLRequestUploadDepth(
177 const net::URLRequest& request) { 174 const net::URLRequest& request) {
178 UploadUserData* data = static_cast<UploadUserData*>( 175 UploadUserData* data = static_cast<UploadUserData*>(
179 request.GetUserData(UploadUserData::kUserDataKey)); 176 request.GetUserData(UploadUserData::kUserDataKey));
180 if (!data) 177 if (!data)
181 return 0; 178 return 0;
182 return data->depth(); 179 return data->depth();
183 } 180 }
184 181
185 } // namespace domain_reliability 182 } // namespace domain_reliability
OLDNEW
« components/domain_reliability/monitor.cc ('K') | « components/domain_reliability/monitor_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698