OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/metrics/net/net_metrics_log_uploader.h" | |
6 | |
7 #include "base/metrics/histogram.h" | |
8 #include "components/metrics/net/compression_utils.h" | |
9 #include "net/base/load_flags.h" | |
10 #include "net/url_request/url_fetcher.h" | |
11 #include "url/gurl.h" | |
12 | |
13 namespace metrics { | |
14 | |
15 NetMetricsLogUploader::NetMetricsLogUploader( | |
16 net::URLRequestContextGetter* request_context_getter, | |
17 const std::string& server_url, | |
18 const std::string& mime_type, | |
19 const base::Callback<void(int)> on_upload_complete) | |
20 : request_context_getter_(request_context_getter), | |
21 server_url_(server_url), | |
22 mime_type_(mime_type), | |
23 on_upload_complete_(on_upload_complete) { | |
24 } | |
25 | |
26 NetMetricsLogUploader::~NetMetricsLogUploader() { | |
27 } | |
28 | |
29 bool NetMetricsLogUploader::UploadLog(const std::string& log_data, | |
30 const std::string& log_hash) { | |
31 std::string compressed_log_text; | |
32 if (!GzipCompress(log_data, &compressed_log_text)) { | |
33 NOTREACHED(); | |
34 return false; | |
35 } | |
36 | |
37 UMA_HISTOGRAM_PERCENTAGE( | |
38 "UMA.ProtoCompressionRatio", | |
39 100 * compressed_log_text.size() / log_data.size()); | |
40 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
41 "UMA.ProtoGzippedKBSaved", | |
42 (log_data.size() - compressed_log_text.size()) / 1024, | |
43 1, 2000, 50); | |
44 | |
45 current_fetch_.reset( | |
46 net::URLFetcher::Create(GURL(server_url_), net::URLFetcher::POST, this)); | |
47 current_fetch_->SetRequestContext(request_context_getter_); | |
48 current_fetch_->SetUploadData(mime_type_, compressed_log_text); | |
49 | |
50 // Tell the server that we're uploading gzipped protobufs. | |
51 current_fetch_->SetExtraRequestHeaders("content-encoding: gzip"); | |
52 | |
53 DCHECK(!log_hash.empty()); | |
54 current_fetch_->AddExtraRequestHeader("X-Chrome-UMA-Log-SHA1: " + log_hash); | |
55 | |
56 // We already drop cookies server-side, but we might as well strip them out | |
57 // client-side as well. | |
58 current_fetch_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | |
59 net::LOAD_DO_NOT_SEND_COOKIES); | |
60 return true; | |
61 } | |
62 | |
63 void NetMetricsLogUploader::OnURLFetchComplete(const net::URLFetcher* source) { | |
64 // We're not allowed to re-use the existing |URLFetcher|s, so free them here. | |
65 // Note however that |source| is aliased to the fetcher, so we should be | |
66 // careful not to delete it too early. | |
67 DCHECK_EQ(current_fetch_.get(), source); | |
68 scoped_ptr<net::URLFetcher> fetch(current_fetch_.Pass()); | |
blundell
2014/05/26 07:56:56
nit: This is pretty bizarre code. Why not just do
Alexei Svitkine (slow)
2014/05/26 20:04:10
Done. I had copied the pattern from the previous c
| |
69 | |
70 int response_code = source->GetResponseCode(); | |
71 if (response_code == net::URLFetcher::RESPONSE_CODE_INVALID) | |
72 response_code = -1; | |
73 on_upload_complete_.Run(response_code); | |
74 } | |
75 | |
76 } // namespace metrics | |
OLD | NEW |