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/metrics/net/net_metrics_log_uploader.h"
7 #include "base/metrics/histogram.h"
8 #include "net/base/load_flags.h"
9 #include "net/base/network_change_notifier.h"
10 #include "net/url_request/url_fetcher.h"
15 // Records the network connection type if upload was successful.
16 void RecordConnectionType(int response_code
) {
17 if (response_code
== 200) {
18 UMA_HISTOGRAM_ENUMERATION("UMA.LogUpload.ConnetionType",
19 net::NetworkChangeNotifier::GetConnectionType(),
20 net::NetworkChangeNotifier::CONNECTION_LAST
);
28 NetMetricsLogUploader::NetMetricsLogUploader(
29 net::URLRequestContextGetter
* request_context_getter
,
30 const std::string
& server_url
,
31 const std::string
& mime_type
,
32 const base::Callback
<void(int)>& on_upload_complete
)
33 : MetricsLogUploader(server_url
, mime_type
, on_upload_complete
),
34 request_context_getter_(request_context_getter
) {
37 NetMetricsLogUploader::~NetMetricsLogUploader() {
40 bool NetMetricsLogUploader::UploadLog(const std::string
& compressed_log_data
,
41 const std::string
& log_hash
) {
43 net::URLFetcher::Create(GURL(server_url_
), net::URLFetcher::POST
, this);
44 current_fetch_
->SetRequestContext(request_context_getter_
);
45 current_fetch_
->SetUploadData(mime_type_
, compressed_log_data
);
47 // Tell the server that we're uploading gzipped protobufs.
48 current_fetch_
->SetExtraRequestHeaders("content-encoding: gzip");
50 DCHECK(!log_hash
.empty());
51 current_fetch_
->AddExtraRequestHeader("X-Chrome-UMA-Log-SHA1: " + log_hash
);
53 // We already drop cookies server-side, but we might as well strip them out
54 // client-side as well.
55 current_fetch_
->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES
|
56 net::LOAD_DO_NOT_SEND_COOKIES
);
57 current_fetch_
->Start();
61 void NetMetricsLogUploader::OnURLFetchComplete(const net::URLFetcher
* source
) {
62 // We're not allowed to re-use the existing |URLFetcher|s, so free them here.
63 // Note however that |source| is aliased to the fetcher, so we should be
64 // careful not to delete it too early.
65 DCHECK_EQ(current_fetch_
.get(), source
);
67 int response_code
= source
->GetResponseCode();
68 if (response_code
== net::URLFetcher::RESPONSE_CODE_INVALID
)
70 current_fetch_
.reset();
71 RecordConnectionType(response_code
);
72 on_upload_complete_
.Run(response_code
);
75 } // namespace metrics