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
3 // found in the LICENSE file.
5 #include "components/domain_reliability/uploader.h"
8 #include "base/stl_util.h"
9 #include "net/base/load_flags.h"
10 #include "net/url_request/url_fetcher.h"
11 #include "net/url_request/url_fetcher_delegate.h"
12 #include "net/url_request/url_request_context_getter.h"
14 namespace domain_reliability
{
18 class DomainReliabilityUploaderImpl
19 : public DomainReliabilityUploader
, net::URLFetcherDelegate
{
21 DomainReliabilityUploaderImpl(
22 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
)
23 : url_request_context_getter_(url_request_context_getter
) {}
25 virtual ~DomainReliabilityUploaderImpl() {
26 // Delete any in-flight URLFetchers.
27 STLDeleteContainerPairFirstPointers(
28 upload_callbacks_
.begin(), upload_callbacks_
.end());
31 // DomainReliabilityUploader implementation:
32 virtual void UploadReport(
33 const std::string
& report_json
,
34 const GURL
& upload_url
,
35 const DomainReliabilityUploader::UploadCallback
& callback
) OVERRIDE
{
36 VLOG(1) << "Uploading report to " << upload_url
;
37 VLOG(2) << "Report JSON: " << report_json
;
39 net::URLFetcher
* fetcher
=
40 net::URLFetcher::Create(0, upload_url
, net::URLFetcher::POST
, this);
41 fetcher
->SetRequestContext(url_request_context_getter_
);
42 fetcher
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
43 net::LOAD_DO_NOT_SAVE_COOKIES
);
44 fetcher
->SetUploadData("text/json", report_json
);
45 fetcher
->SetAutomaticallyRetryOn5xx(false);
48 upload_callbacks_
[fetcher
] = callback
;
51 // net::URLFetcherDelegate implementation:
52 virtual void OnURLFetchComplete(
53 const net::URLFetcher
* fetcher
) OVERRIDE
{
56 UploadCallbackMap::iterator callback_it
= upload_callbacks_
.find(fetcher
);
57 DCHECK(callback_it
!= upload_callbacks_
.end());
59 VLOG(1) << "Upload finished with " << fetcher
->GetResponseCode();
61 bool success
= fetcher
->GetResponseCode() == 200;
62 callback_it
->second
.Run(success
);
64 delete callback_it
->first
;
65 upload_callbacks_
.erase(callback_it
);
69 using DomainReliabilityUploader::UploadCallback
;
70 typedef std::map
<const net::URLFetcher
*, UploadCallback
> UploadCallbackMap
;
72 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
73 UploadCallbackMap upload_callbacks_
;
78 DomainReliabilityUploader::DomainReliabilityUploader() {}
80 DomainReliabilityUploader::~DomainReliabilityUploader() {}
83 scoped_ptr
<DomainReliabilityUploader
> DomainReliabilityUploader::Create(
84 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
) {
85 return scoped_ptr
<DomainReliabilityUploader
>(
86 new DomainReliabilityUploaderImpl(url_request_context_getter
));
89 } // namespace domain_reliability