This source file includes following definitions.
- UploadReport
- OnURLFetchComplete
- Create
#include "components/domain_reliability/uploader.h"
#include "base/bind.h"
#include "base/stl_util.h"
#include "net/base/load_flags.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context_getter.h"
namespace domain_reliability {
namespace {
class DomainReliabilityUploaderImpl
: public DomainReliabilityUploader, net::URLFetcherDelegate {
public:
DomainReliabilityUploaderImpl(
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter)
: url_request_context_getter_(url_request_context_getter) {}
virtual ~DomainReliabilityUploaderImpl() {
STLDeleteContainerPairFirstPointers(
upload_callbacks_.begin(), upload_callbacks_.end());
}
virtual void UploadReport(
const std::string& report_json,
const GURL& upload_url,
const DomainReliabilityUploader::UploadCallback& callback) OVERRIDE {
VLOG(1) << "Uploading report to " << upload_url;
VLOG(2) << "Report JSON: " << report_json;
net::URLFetcher* fetcher =
net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this);
fetcher->SetRequestContext(url_request_context_getter_);
fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SAVE_COOKIES);
fetcher->SetUploadData("text/json", report_json);
fetcher->SetAutomaticallyRetryOn5xx(false);
fetcher->Start();
upload_callbacks_[fetcher] = callback;
}
virtual void OnURLFetchComplete(
const net::URLFetcher* fetcher) OVERRIDE {
DCHECK(fetcher);
UploadCallbackMap::iterator callback_it = upload_callbacks_.find(fetcher);
DCHECK(callback_it != upload_callbacks_.end());
VLOG(1) << "Upload finished with " << fetcher->GetResponseCode();
bool success = fetcher->GetResponseCode() == 200;
callback_it->second.Run(success);
delete callback_it->first;
upload_callbacks_.erase(callback_it);
}
private:
using DomainReliabilityUploader::UploadCallback;
typedef std::map<const net::URLFetcher*, UploadCallback> UploadCallbackMap;
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
UploadCallbackMap upload_callbacks_;
};
}
DomainReliabilityUploader::DomainReliabilityUploader() {}
DomainReliabilityUploader::~DomainReliabilityUploader() {}
scoped_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create(
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) {
return scoped_ptr<DomainReliabilityUploader>(
new DomainReliabilityUploaderImpl(url_request_context_getter));
}
}