| 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" |
| 11 #include "base/logging.h" |
| 11 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/supports_user_data.h" | 13 #include "base/supports_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 30 matching lines...) Expand all Loading... |
| 51 | 52 |
| 52 class DomainReliabilityUploaderImpl | 53 class DomainReliabilityUploaderImpl |
| 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), |
| 63 shutdown_(false) {} |
| 62 | 64 |
| 63 ~DomainReliabilityUploaderImpl() override {} | 65 ~DomainReliabilityUploaderImpl() override { |
| 66 DCHECK(shutdown_); |
| 67 } |
| 64 | 68 |
| 65 // DomainReliabilityUploader implementation: | 69 // DomainReliabilityUploader implementation: |
| 66 void UploadReport( | 70 void UploadReport( |
| 67 const std::string& report_json, | 71 const std::string& report_json, |
| 68 int max_upload_depth, | 72 int max_upload_depth, |
| 69 const GURL& upload_url, | 73 const GURL& upload_url, |
| 70 const DomainReliabilityUploader::UploadCallback& callback) override { | 74 const DomainReliabilityUploader::UploadCallback& callback) override { |
| 71 VLOG(1) << "Uploading report to " << upload_url; | 75 VLOG(1) << "Uploading report to " << upload_url; |
| 72 VLOG(2) << "Report JSON: " << report_json; | 76 VLOG(2) << "Report JSON: " << report_json; |
| 73 | 77 |
| 74 if (discard_uploads_) { | 78 if (discard_uploads_ || shutdown_) { |
| 75 VLOG(1) << "Discarding report instead of uploading."; | 79 VLOG(1) << "Discarding report instead of uploading."; |
| 76 UploadResult result; | 80 UploadResult result; |
| 77 result.status = UploadResult::SUCCESS; | 81 result.status = UploadResult::SUCCESS; |
| 78 callback.Run(result); | 82 callback.Run(result); |
| 79 return; | 83 return; |
| 80 } | 84 } |
| 81 | 85 |
| 82 std::unique_ptr<net::URLFetcher> owned_fetcher = | 86 std::unique_ptr<net::URLFetcher> owned_fetcher = |
| 83 net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this); | 87 net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this); |
| 84 net::URLFetcher* fetcher = owned_fetcher.get(); | 88 net::URLFetcher* fetcher = owned_fetcher.get(); |
| 85 fetcher->SetRequestContext(url_request_context_getter_.get()); | 89 fetcher->SetRequestContext(url_request_context_getter_.get()); |
| 86 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 90 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 87 net::LOAD_DO_NOT_SAVE_COOKIES); | 91 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 88 fetcher->SetUploadData(kJsonMimeType, report_json); | 92 fetcher->SetUploadData(kJsonMimeType, report_json); |
| 89 fetcher->SetAutomaticallyRetryOn5xx(false); | 93 fetcher->SetAutomaticallyRetryOn5xx(false); |
| 90 fetcher->SetURLRequestUserData( | 94 fetcher->SetURLRequestUserData( |
| 91 UploadUserData::kUserDataKey, | 95 UploadUserData::kUserDataKey, |
| 92 UploadUserData::CreateCreateDataCallback(max_upload_depth + 1)); | 96 UploadUserData::CreateCreateDataCallback(max_upload_depth + 1)); |
| 93 fetcher->Start(); | 97 fetcher->Start(); |
| 94 | 98 |
| 95 upload_callbacks_[fetcher] = {std::move(owned_fetcher), callback}; | 99 uploads_[fetcher] = {std::move(owned_fetcher), callback}; |
| 96 | 100 |
| 97 base::TimeTicks now = base::TimeTicks::Now(); | 101 base::TimeTicks now = base::TimeTicks::Now(); |
| 98 if (!last_upload_start_time_.is_null()) { | 102 if (!last_upload_start_time_.is_null()) { |
| 99 UMA_HISTOGRAM_LONG_TIMES("DomainReliability.UploadIntervalGlobal", | 103 UMA_HISTOGRAM_LONG_TIMES("DomainReliability.UploadIntervalGlobal", |
| 100 now - last_upload_start_time_); | 104 now - last_upload_start_time_); |
| 101 } | 105 } |
| 102 last_upload_start_time_ = now; | 106 last_upload_start_time_ = now; |
| 103 } | 107 } |
| 104 | 108 |
| 105 void set_discard_uploads(bool discard_uploads) override { | 109 void set_discard_uploads(bool discard_uploads) override { |
| 106 discard_uploads_ = discard_uploads; | 110 discard_uploads_ = discard_uploads; |
| 107 VLOG(1) << "Setting discard_uploads to " << discard_uploads; | 111 VLOG(1) << "Setting discard_uploads to " << discard_uploads; |
| 108 } | 112 } |
| 109 | 113 |
| 114 void Shutdown() override { |
| 115 DCHECK(!shutdown_); |
| 116 shutdown_ = true; |
| 117 uploads_.clear(); |
| 118 } |
| 119 |
| 110 // net::URLFetcherDelegate implementation: | 120 // net::URLFetcherDelegate implementation: |
| 111 void OnURLFetchComplete(const net::URLFetcher* fetcher) override { | 121 void OnURLFetchComplete(const net::URLFetcher* fetcher) override { |
| 112 DCHECK(fetcher); | 122 DCHECK(fetcher); |
| 113 | 123 |
| 114 auto callback_it = upload_callbacks_.find(fetcher); | 124 auto callback_it = uploads_.find(fetcher); |
| 115 DCHECK(callback_it != upload_callbacks_.end()); | 125 DCHECK(callback_it != uploads_.end()); |
| 116 | 126 |
| 117 int net_error = GetNetErrorFromURLRequestStatus(fetcher->GetStatus()); | 127 int net_error = GetNetErrorFromURLRequestStatus(fetcher->GetStatus()); |
| 118 int http_response_code = fetcher->GetResponseCode(); | 128 int http_response_code = fetcher->GetResponseCode(); |
| 119 base::TimeDelta retry_after; | 129 base::TimeDelta retry_after; |
| 120 { | 130 { |
| 121 std::string retry_after_string; | 131 std::string retry_after_string; |
| 122 if (fetcher->GetResponseHeaders() && | 132 if (fetcher->GetResponseHeaders() && |
| 123 fetcher->GetResponseHeaders()->EnumerateHeader(nullptr, | 133 fetcher->GetResponseHeaders()->EnumerateHeader(nullptr, |
| 124 "Retry-After", | 134 "Retry-After", |
| 125 &retry_after_string)) { | 135 &retry_after_string)) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 138 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadNetError", | 148 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadNetError", |
| 139 -net_error); | 149 -net_error); |
| 140 | 150 |
| 141 UploadResult result; | 151 UploadResult result; |
| 142 GetUploadResultFromResponseDetails(net_error, | 152 GetUploadResultFromResponseDetails(net_error, |
| 143 http_response_code, | 153 http_response_code, |
| 144 retry_after, | 154 retry_after, |
| 145 &result); | 155 &result); |
| 146 callback_it->second.second.Run(result); | 156 callback_it->second.second.Run(result); |
| 147 | 157 |
| 148 upload_callbacks_.erase(callback_it); | 158 uploads_.erase(callback_it); |
| 149 } | 159 } |
| 150 | 160 |
| 151 private: | 161 private: |
| 152 using DomainReliabilityUploader::UploadCallback; | 162 using DomainReliabilityUploader::UploadCallback; |
| 153 | 163 |
| 154 MockableTime* time_; | 164 MockableTime* time_; |
| 155 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 165 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 156 std::map<const net::URLFetcher*, | 166 std::map<const net::URLFetcher*, |
| 157 std::pair<std::unique_ptr<net::URLFetcher>, UploadCallback>> | 167 std::pair<std::unique_ptr<net::URLFetcher>, UploadCallback>> |
| 158 upload_callbacks_; | 168 uploads_; |
| 159 bool discard_uploads_; | 169 bool discard_uploads_; |
| 160 base::TimeTicks last_upload_start_time_; | 170 base::TimeTicks last_upload_start_time_; |
| 171 bool shutdown_; |
| 161 }; | 172 }; |
| 162 | 173 |
| 163 } // namespace | 174 } // namespace |
| 164 | 175 |
| 165 DomainReliabilityUploader::DomainReliabilityUploader() {} | 176 DomainReliabilityUploader::DomainReliabilityUploader() {} |
| 166 DomainReliabilityUploader::~DomainReliabilityUploader() {} | 177 DomainReliabilityUploader::~DomainReliabilityUploader() {} |
| 167 | 178 |
| 168 // static | 179 // static |
| 169 std::unique_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create( | 180 std::unique_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create( |
| 170 MockableTime* time, | 181 MockableTime* time, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 183 // static | 194 // static |
| 184 int DomainReliabilityUploader::GetURLRequestUploadDepth( | 195 int DomainReliabilityUploader::GetURLRequestUploadDepth( |
| 185 const net::URLRequest& request) { | 196 const net::URLRequest& request) { |
| 186 UploadUserData* data = static_cast<UploadUserData*>( | 197 UploadUserData* data = static_cast<UploadUserData*>( |
| 187 request.GetUserData(UploadUserData::kUserDataKey)); | 198 request.GetUserData(UploadUserData::kUserDataKey)); |
| 188 if (!data) | 199 if (!data) |
| 189 return 0; | 200 return 0; |
| 190 return data->depth(); | 201 return data->depth(); |
| 191 } | 202 } |
| 192 | 203 |
| 204 void DomainReliabilityUploader::Shutdown() {} |
| 205 |
| 193 } // namespace domain_reliability | 206 } // namespace domain_reliability |
| OLD | NEW |