Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / net / request_source_bandwidth_histograms.cc
bloba9a7b470da8cabbf6b9230c4a022fb818b201ff2
1 // Copyright 2015 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 "chrome/browser/net/request_source_bandwidth_histograms.h"
7 #include "base/metrics/histogram_macros.h"
8 #include "content/public/browser/resource_request_info.h"
9 #include "content/public/common/process_type.h"
10 #include "net/url_request/url_request.h"
12 namespace {
14 enum Bucket {
15 BUCKET_UNKNOWN,
16 BUCKET_RENDERER,
17 BUCKET_BROWSER,
18 BUCKET_MAX,
21 bool ShouldHistogramRequest(const net::URLRequest* request, bool started) {
22 return started &&
23 !request->was_cached() &&
24 request->url().SchemeIsHTTPOrHTTPS();
27 Bucket GetBucketForRequest(const net::URLRequest* request) {
28 const content::ResourceRequestInfo* info =
29 content::ResourceRequestInfo::ForRequest(request);
30 if (!info)
31 return BUCKET_BROWSER;
32 else if (info->GetProcessType() == content::PROCESS_TYPE_RENDERER)
33 return BUCKET_RENDERER;
34 else
35 return BUCKET_UNKNOWN;
38 // Histogram response sizes in kilobytes, from 1 KB to 4 GB.
39 #define UMA_HISTOGRAM_RESPONSE_KB(bucket, sample) \
40 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.ResponseSizeByProcess." bucket, sample, \
41 1, 4 * 1024 * 1024, 100)
43 void LogRequest(Bucket bucket, int64 bytes) {
44 int64 kilobytes = bytes / 1024;
45 switch (bucket) {
46 case BUCKET_UNKNOWN:
47 UMA_HISTOGRAM_RESPONSE_KB("Unknown", kilobytes);
48 break;
49 case BUCKET_RENDERER:
50 UMA_HISTOGRAM_RESPONSE_KB("Renderer", kilobytes);
51 break;
52 case BUCKET_BROWSER:
53 UMA_HISTOGRAM_RESPONSE_KB("Browser", kilobytes);
54 break;
55 default:
56 NOTREACHED();
60 } // namespace
62 void RecordRequestSourceBandwidth(const net::URLRequest* request,
63 bool started) {
64 if (ShouldHistogramRequest(request, started))
65 LogRequest(GetBucketForRequest(request), request->GetTotalReceivedBytes());