1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/domain_reliability/uploader.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/scoped_vector.h"
#include "base/metrics/sparse_histogram.h"
#include "base/stl_util.h"
#include "base/supports_user_data.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 {
const char* kJsonMimeType = "application/json; charset=utf-8";
class UploadUserData : public base::SupportsUserData::Data {
public:
static net::URLFetcher::CreateDataCallback CreateCreateDataCallback() {
return base::Bind(&UploadUserData::CreateUploadUserData);
}
static const void* kUserDataKey;
private:
static base::SupportsUserData::Data* CreateUploadUserData() {
return new UploadUserData();
}
};
const void* UploadUserData::kUserDataKey =
static_cast<const void*>(&UploadUserData::kUserDataKey);
class DomainReliabilityUploaderImpl
: public DomainReliabilityUploader, net::URLFetcherDelegate {
public:
DomainReliabilityUploaderImpl(const scoped_refptr<
net::URLRequestContextGetter>& url_request_context_getter)
: url_request_context_getter_(url_request_context_getter),
discard_uploads_(true) {}
virtual ~DomainReliabilityUploaderImpl() {
// Delete any in-flight URLFetchers.
STLDeleteContainerPairFirstPointers(
upload_callbacks_.begin(), upload_callbacks_.end());
}
// DomainReliabilityUploader implementation:
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;
if (discard_uploads_) {
VLOG(1) << "Discarding report instead of uploading.";
callback.Run(true);
return;
}
net::URLFetcher* fetcher =
net::URLFetcher::Create(0, upload_url, net::URLFetcher::POST, this);
fetcher->SetRequestContext(url_request_context_getter_.get());
fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SAVE_COOKIES);
fetcher->SetUploadData(kJsonMimeType, report_json);
fetcher->SetAutomaticallyRetryOn5xx(false);
fetcher->SetURLRequestUserData(
UploadUserData::kUserDataKey,
UploadUserData::CreateCreateDataCallback());
fetcher->Start();
upload_callbacks_[fetcher] = callback;
}
virtual void set_discard_uploads(bool discard_uploads) override {
discard_uploads_ = discard_uploads;
VLOG(1) << "Setting discard_uploads to " << discard_uploads;
}
// net::URLFetcherDelegate implementation:
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();
UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadResponseCode",
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_;
bool discard_uploads_;
};
} // namespace
DomainReliabilityUploader::DomainReliabilityUploader() {}
DomainReliabilityUploader::~DomainReliabilityUploader() {}
// static
scoped_ptr<DomainReliabilityUploader> DomainReliabilityUploader::Create(
const scoped_refptr<net::URLRequestContextGetter>&
url_request_context_getter) {
return scoped_ptr<DomainReliabilityUploader>(
new DomainReliabilityUploaderImpl(url_request_context_getter));
}
// static
bool DomainReliabilityUploader::URLRequestIsUpload(
const net::URLRequest& request) {
return request.GetUserData(UploadUserData::kUserDataKey) != NULL;
}
} // namespace domain_reliability
|