OLD | NEW |
---|---|
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> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 | 52 |
53 class DomainReliabilityUploaderImpl | 53 class DomainReliabilityUploaderImpl |
54 : public DomainReliabilityUploader, net::URLFetcherDelegate { | 54 : public DomainReliabilityUploader, net::URLFetcherDelegate { |
55 public: | 55 public: |
56 DomainReliabilityUploaderImpl( | 56 DomainReliabilityUploaderImpl( |
57 MockableTime* time, | 57 MockableTime* time, |
58 const scoped_refptr< | 58 const scoped_refptr< |
59 net::URLRequestContextGetter>& url_request_context_getter) | 59 net::URLRequestContextGetter>& url_request_context_getter) |
60 : time_(time), | 60 : time_(time), |
61 url_request_context_getter_(url_request_context_getter), | 61 url_request_context_getter_(url_request_context_getter), |
62 discard_uploads_(true) {} | 62 discard_uploads_(true), |
63 shutdown_(false) {} | |
63 | 64 |
64 ~DomainReliabilityUploaderImpl() override {} | 65 ~DomainReliabilityUploaderImpl() override { |
66 DCHECK(shutdown_); | |
67 } | |
65 | 68 |
66 // DomainReliabilityUploader implementation: | 69 // DomainReliabilityUploader implementation: |
67 void UploadReport( | 70 void UploadReport( |
68 const std::string& report_json, | 71 const std::string& report_json, |
69 int max_upload_depth, | 72 int max_upload_depth, |
70 const GURL& upload_url, | 73 const GURL& upload_url, |
71 const DomainReliabilityUploader::UploadCallback& callback) override { | 74 const DomainReliabilityUploader::UploadCallback& callback) override { |
72 VLOG(1) << "Uploading report to " << upload_url; | 75 VLOG(1) << "Uploading report to " << upload_url; |
73 VLOG(2) << "Report JSON: " << report_json; | 76 VLOG(2) << "Report JSON: " << report_json; |
74 | 77 |
75 if (discard_uploads_) { | 78 if (discard_uploads_ || shutdown_) { |
mmenke
2016/09/27 16:56:15
Test coverage for this?
| |
76 VLOG(1) << "Discarding report instead of uploading."; | 79 VLOG(1) << "Discarding report instead of uploading."; |
77 UploadResult result; | 80 UploadResult result; |
78 result.status = UploadResult::SUCCESS; | 81 result.status = UploadResult::SUCCESS; |
79 callback.Run(result); | 82 callback.Run(result); |
80 return; | 83 return; |
81 } | 84 } |
82 | 85 |
83 std::unique_ptr<net::URLFetcher> owned_fetcher = | 86 std::unique_ptr<net::URLFetcher> owned_fetcher = |
84 net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this); | 87 net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this); |
85 net::URLFetcher* fetcher = owned_fetcher.get(); | 88 net::URLFetcher* fetcher = owned_fetcher.get(); |
(...skipping 10 matching lines...) Expand all Loading... | |
96 fetcher->Start(); | 99 fetcher->Start(); |
97 | 100 |
98 upload_callbacks_[fetcher] = {std::move(owned_fetcher), callback}; | 101 upload_callbacks_[fetcher] = {std::move(owned_fetcher), callback}; |
99 } | 102 } |
100 | 103 |
101 void set_discard_uploads(bool discard_uploads) override { | 104 void set_discard_uploads(bool discard_uploads) override { |
102 discard_uploads_ = discard_uploads; | 105 discard_uploads_ = discard_uploads; |
103 VLOG(1) << "Setting discard_uploads to " << discard_uploads; | 106 VLOG(1) << "Setting discard_uploads to " << discard_uploads; |
104 } | 107 } |
105 | 108 |
109 void Shutdown() override { | |
110 DCHECK(!shutdown_); | |
111 shutdown_ = true; | |
112 upload_callbacks_.clear(); | |
113 } | |
114 | |
106 // net::URLFetcherDelegate implementation: | 115 // net::URLFetcherDelegate implementation: |
107 void OnURLFetchComplete(const net::URLFetcher* fetcher) override { | 116 void OnURLFetchComplete(const net::URLFetcher* fetcher) override { |
108 DCHECK(fetcher); | 117 DCHECK(fetcher); |
109 | 118 |
110 auto callback_it = upload_callbacks_.find(fetcher); | 119 auto callback_it = upload_callbacks_.find(fetcher); |
111 DCHECK(callback_it != upload_callbacks_.end()); | 120 DCHECK(callback_it != upload_callbacks_.end()); |
112 | 121 |
113 int net_error = GetNetErrorFromURLRequestStatus(fetcher->GetStatus()); | 122 int net_error = GetNetErrorFromURLRequestStatus(fetcher->GetStatus()); |
114 int http_response_code = fetcher->GetResponseCode(); | 123 int http_response_code = fetcher->GetResponseCode(); |
115 base::TimeDelta retry_after; | 124 base::TimeDelta retry_after; |
(...skipping 30 matching lines...) Expand all Loading... | |
146 | 155 |
147 private: | 156 private: |
148 using DomainReliabilityUploader::UploadCallback; | 157 using DomainReliabilityUploader::UploadCallback; |
149 | 158 |
150 MockableTime* time_; | 159 MockableTime* time_; |
151 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 160 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
152 std::map<const net::URLFetcher*, | 161 std::map<const net::URLFetcher*, |
153 std::pair<std::unique_ptr<net::URLFetcher>, UploadCallback>> | 162 std::pair<std::unique_ptr<net::URLFetcher>, UploadCallback>> |
154 upload_callbacks_; | 163 upload_callbacks_; |
155 bool discard_uploads_; | 164 bool discard_uploads_; |
165 bool shutdown_; | |
156 }; | 166 }; |
157 | 167 |
158 } // namespace | 168 } // namespace |
159 | 169 |
160 DomainReliabilityUploader::DomainReliabilityUploader() {} | 170 DomainReliabilityUploader::DomainReliabilityUploader() {} |
161 DomainReliabilityUploader::~DomainReliabilityUploader() {} | 171 DomainReliabilityUploader::~DomainReliabilityUploader() {} |
162 | 172 |
163 // static | 173 // static |
164 std::unique_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create( | 174 std::unique_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create( |
165 MockableTime* time, | 175 MockableTime* time, |
166 const scoped_refptr<net::URLRequestContextGetter>& | 176 const scoped_refptr<net::URLRequestContextGetter>& |
167 url_request_context_getter) { | 177 url_request_context_getter) { |
168 return std::unique_ptr<DomainReliabilityUploader>( | 178 return std::unique_ptr<DomainReliabilityUploader>( |
169 new DomainReliabilityUploaderImpl(time, url_request_context_getter)); | 179 new DomainReliabilityUploaderImpl(time, url_request_context_getter)); |
170 } | 180 } |
171 | 181 |
172 // static | 182 // static |
173 int DomainReliabilityUploader::GetURLRequestUploadDepth( | 183 int DomainReliabilityUploader::GetURLRequestUploadDepth( |
174 const net::URLRequest& request) { | 184 const net::URLRequest& request) { |
175 UploadUserData* data = static_cast<UploadUserData*>( | 185 UploadUserData* data = static_cast<UploadUserData*>( |
176 request.GetUserData(UploadUserData::kUserDataKey)); | 186 request.GetUserData(UploadUserData::kUserDataKey)); |
177 if (!data) | 187 if (!data) |
178 return 0; | 188 return 0; |
179 return data->depth(); | 189 return data->depth(); |
180 } | 190 } |
181 | 191 |
192 void DomainReliabilityUploader::Shutdown() {} | |
193 | |
182 } // namespace domain_reliability | 194 } // namespace domain_reliability |
OLD | NEW |