| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 const void* UploadUserData::kUserDataKey = | 38 const void* UploadUserData::kUserDataKey = |
| 39 static_cast<const void*>(&UploadUserData::kUserDataKey); | 39 static_cast<const void*>(&UploadUserData::kUserDataKey); |
| 40 | 40 |
| 41 class DomainReliabilityUploaderImpl | 41 class DomainReliabilityUploaderImpl |
| 42 : public DomainReliabilityUploader, net::URLFetcherDelegate { | 42 : public DomainReliabilityUploader, net::URLFetcherDelegate { |
| 43 public: | 43 public: |
| 44 DomainReliabilityUploaderImpl(const scoped_refptr< | 44 DomainReliabilityUploaderImpl(const scoped_refptr< |
| 45 net::URLRequestContextGetter>& url_request_context_getter) | 45 net::URLRequestContextGetter>& url_request_context_getter) |
| 46 : url_request_context_getter_(url_request_context_getter) {} | 46 : url_request_context_getter_(url_request_context_getter), |
| 47 discard_uploads_(true) {} |
| 47 | 48 |
| 48 virtual ~DomainReliabilityUploaderImpl() { | 49 virtual ~DomainReliabilityUploaderImpl() { |
| 49 // Delete any in-flight URLFetchers. | 50 // Delete any in-flight URLFetchers. |
| 50 STLDeleteContainerPairFirstPointers( | 51 STLDeleteContainerPairFirstPointers( |
| 51 upload_callbacks_.begin(), upload_callbacks_.end()); | 52 upload_callbacks_.begin(), upload_callbacks_.end()); |
| 52 } | 53 } |
| 53 | 54 |
| 54 // DomainReliabilityUploader implementation: | 55 // DomainReliabilityUploader implementation: |
| 55 virtual void UploadReport( | 56 virtual void UploadReport( |
| 56 const std::string& report_json, | 57 const std::string& report_json, |
| 57 const GURL& upload_url, | 58 const GURL& upload_url, |
| 58 const DomainReliabilityUploader::UploadCallback& callback) OVERRIDE { | 59 const DomainReliabilityUploader::UploadCallback& callback) OVERRIDE { |
| 59 VLOG(1) << "Uploading report to " << upload_url; | 60 VLOG(1) << "Uploading report to " << upload_url; |
| 60 VLOG(2) << "Report JSON: " << report_json; | 61 VLOG(2) << "Report JSON: " << report_json; |
| 61 | 62 |
| 63 if (discard_uploads_) { |
| 64 VLOG(1) << "Discarding report instead of uploading."; |
| 65 callback.Run(true); |
| 66 } |
| 67 |
| 62 net::URLFetcher* fetcher = | 68 net::URLFetcher* fetcher = |
| 63 net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this); | 69 net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this); |
| 64 fetcher->SetRequestContext(url_request_context_getter_); | 70 fetcher->SetRequestContext(url_request_context_getter_); |
| 65 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 71 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 66 net::LOAD_DO_NOT_SAVE_COOKIES); | 72 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 67 fetcher->SetUploadData(kJsonMimeType, report_json); | 73 fetcher->SetUploadData(kJsonMimeType, report_json); |
| 68 fetcher->SetAutomaticallyRetryOn5xx(false); | 74 fetcher->SetAutomaticallyRetryOn5xx(false); |
| 69 fetcher->SetURLRequestUserData( | 75 fetcher->SetURLRequestUserData( |
| 70 UploadUserData::kUserDataKey, | 76 UploadUserData::kUserDataKey, |
| 71 UploadUserData::CreateCreateDataCallback()); | 77 UploadUserData::CreateCreateDataCallback()); |
| 72 fetcher->Start(); | 78 fetcher->Start(); |
| 73 | 79 |
| 74 upload_callbacks_[fetcher] = callback; | 80 upload_callbacks_[fetcher] = callback; |
| 75 } | 81 } |
| 76 | 82 |
| 83 virtual void set_discard_uploads(bool discard_uploads) OVERRIDE { |
| 84 discard_uploads_ = discard_uploads; |
| 85 } |
| 86 |
| 77 // net::URLFetcherDelegate implementation: | 87 // net::URLFetcherDelegate implementation: |
| 78 virtual void OnURLFetchComplete( | 88 virtual void OnURLFetchComplete( |
| 79 const net::URLFetcher* fetcher) OVERRIDE { | 89 const net::URLFetcher* fetcher) OVERRIDE { |
| 80 DCHECK(fetcher); | 90 DCHECK(fetcher); |
| 81 | 91 |
| 82 UploadCallbackMap::iterator callback_it = upload_callbacks_.find(fetcher); | 92 UploadCallbackMap::iterator callback_it = upload_callbacks_.find(fetcher); |
| 83 DCHECK(callback_it != upload_callbacks_.end()); | 93 DCHECK(callback_it != upload_callbacks_.end()); |
| 84 | 94 |
| 85 VLOG(1) << "Upload finished with " << fetcher->GetResponseCode(); | 95 VLOG(1) << "Upload finished with " << fetcher->GetResponseCode(); |
| 86 | 96 |
| 87 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadResponseCode", | 97 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadResponseCode", |
| 88 fetcher->GetResponseCode()); | 98 fetcher->GetResponseCode()); |
| 89 | 99 |
| 90 bool success = fetcher->GetResponseCode() == 200; | 100 bool success = fetcher->GetResponseCode() == 200; |
| 91 callback_it->second.Run(success); | 101 callback_it->second.Run(success); |
| 92 | 102 |
| 93 delete callback_it->first; | 103 delete callback_it->first; |
| 94 upload_callbacks_.erase(callback_it); | 104 upload_callbacks_.erase(callback_it); |
| 95 } | 105 } |
| 96 | 106 |
| 97 private: | 107 private: |
| 98 using DomainReliabilityUploader::UploadCallback; | 108 using DomainReliabilityUploader::UploadCallback; |
| 99 typedef std::map<const net::URLFetcher*, UploadCallback> UploadCallbackMap; | 109 typedef std::map<const net::URLFetcher*, UploadCallback> UploadCallbackMap; |
| 100 | 110 |
| 101 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 111 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 102 UploadCallbackMap upload_callbacks_; | 112 UploadCallbackMap upload_callbacks_; |
| 113 bool discard_uploads_; |
| 103 }; | 114 }; |
| 104 | 115 |
| 105 } // namespace | 116 } // namespace |
| 106 | 117 |
| 107 DomainReliabilityUploader::DomainReliabilityUploader() {} | 118 DomainReliabilityUploader::DomainReliabilityUploader() {} |
| 108 DomainReliabilityUploader::~DomainReliabilityUploader() {} | 119 DomainReliabilityUploader::~DomainReliabilityUploader() {} |
| 109 | 120 |
| 110 // static | 121 // static |
| 111 scoped_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create( | 122 scoped_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create( |
| 112 const scoped_refptr<net::URLRequestContextGetter>& | 123 const scoped_refptr<net::URLRequestContextGetter>& |
| 113 url_request_context_getter) { | 124 url_request_context_getter) { |
| 114 return scoped_ptr<DomainReliabilityUploader>( | 125 return scoped_ptr<DomainReliabilityUploader>( |
| 115 new DomainReliabilityUploaderImpl(url_request_context_getter)); | 126 new DomainReliabilityUploaderImpl(url_request_context_getter)); |
| 116 } | 127 } |
| 117 | 128 |
| 118 // static | 129 // static |
| 119 bool DomainReliabilityUploader::URLRequestIsUpload( | 130 bool DomainReliabilityUploader::URLRequestIsUpload( |
| 120 const net::URLRequest& request) { | 131 const net::URLRequest& request) { |
| 121 return request.GetUserData(UploadUserData::kUserDataKey) != NULL; | 132 return request.GetUserData(UploadUserData::kUserDataKey) != NULL; |
| 122 } | 133 } |
| 123 | 134 |
| 124 } // namespace domain_reliability | 135 } // namespace domain_reliability |
| OLD | NEW |