Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(917)

Side by Side Diff: components/data_use_measurement/content/data_use_measurement.cc

Issue 1279543002: Support needed to measure user and service traffic in Chrome. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@NewHistogram
Patch Set: Improving the naming of source_sets in gn files. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "components/data_use_measurement/content/data_use_measurement.h"
6
7 #include "base/metrics/histogram.h"
8 #include "base/metrics/sparse_histogram.h"
9 #include "base/strings/stringprintf.h"
10 #include "content/public/browser/resource_request_info.h"
11 #include "net/base/network_change_notifier.h"
12 #include "net/base/upload_data_stream.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/url_request/url_request.h"
15
16 namespace data_use_measurement {
17
18 namespace {
19
20 // Records the occurrence of |sample| in |name| histogram. Conventional UMA
21 // histograms are not used because the |name| is not static.
22 void RecordUMAHistogramCount(const std::string& name, int64_t sample) {
mmenke 2015/09/04 20:51:45 #include <stdint.h> #include <string>
amohammadkhan 2015/09/05 00:58:02 They are included in data_use_measurement.h. do I
23 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet(
24 name,
25 1, // Minimum sample size in bytes.
26 1000000, // Maximum sample size in bytes. Should cover most of the
27 // requests by services.
28 50, // Bucket count.
29 base::HistogramBase::kUmaTargetedHistogramFlag);
30 histogram_pointer->Add(sample);
31 }
32
33 // This function increases the value of |sample| bucket in |name| sparse
34 // histogram by |value|. Conventional UMA histograms are not used because the
35 // |name| is not static.
mmenke 2015/09/04 20:51:45 nit: the |name| -> |name|
amohammadkhan 2015/09/05 00:58:02 Done.
36 void RecordSparseHistogramWithValue(const std::string& name,
mmenke 2015/09/04 20:51:46 This name is confusing - it sounds like it's recor
amohammadkhan 2015/09/05 00:58:02 Done.
37 int64_t sample,
38 int64_t value) {
39 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet(
40 name, base::HistogramBase::kUmaTargetedHistogramFlag);
41 histogram->AddCount(sample, value);
42 }
43
44 } // namespace
45
46 DataUseMeasurement::DataUseMeasurement()
47 #if defined(OS_ANDROID)
48 : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES),
49 app_listener_(new base::android::ApplicationStatusListener(
50 base::Bind(&DataUseMeasurement::OnApplicationStateChange,
51 base::Unretained(this))))
52 #endif
53 {
54 }
55
56 DataUseMeasurement::~DataUseMeasurement() {
57 #if defined(OS_ANDROID)
58 // Prevent notification between the destruction of the DataUseMeasurement
59 // instance and the destruction of |app_listener_|.
60 app_listener_.reset();
61 #endif
mmenke 2015/09/04 20:51:46 This explicit reset is not needed - it's a scoped_
amohammadkhan 2015/09/05 00:58:02 Done.
62 }
63
64 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) {
65 const content::ResourceRequestInfo* info =
66 content::ResourceRequestInfo::ForRequest(request);
67 // Having |info| is the sign of a request for a web content from user. For now
68 // we could add a condition to check ProcessType in info is
69 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming
70 // PlzNavigate architecture. So just existence of |info| is verified, and the
71 // current check should be compatible with upcoming changes in PlzNavigate.
72 bool is_user_traffic = info != nullptr;
73
74 // Counts rely on URLRequest::GetTotalReceivedBytes(), which does not include
75 // the send path, network layer overhead, TLS overhead, and DNS.
76 // TODO(amohammadkhan): Make these measured bytes more in line with number of
77 // bytes in lower levels.
78 int64_t request_body_bytes = 0;
79 int64_t request_header_bytes = 0;
80 int64_t total_upload_bytes = 0;
81 if (request->has_upload())
82 request_body_bytes = request->get_upload()->size();
83 net::HttpRequestHeaders request_headers;
84 if (request->GetFullRequestHeaders(&request_headers))
85 request_header_bytes = request_headers.ToString().length();
86 total_upload_bytes = request_body_bytes + request_header_bytes;
87 int64_t total_received_bytes = request->GetTotalReceivedBytes();
88
89 ReportDataUsageGeneral(
90 is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE,
91 UPSTREAM, total_upload_bytes);
92 ReportDataUsageGeneral(
93 is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE,
94 DOWNSTREAM, total_received_bytes);
95 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>(
96 request->GetUserData(DataUseUserData::kUserDataKey));
97
98 if (!is_user_traffic) {
99 data_use_measurement::DataUseUserData::ServiceType service_type =
100 attached_service_data
101 ? attached_service_data->service_type()
102 : data_use_measurement::DataUseUserData::NOT_TAGGED;
103 ReportDataUsageServices(service_type, UPSTREAM, total_upload_bytes);
104 ReportDataUsageServices(service_type, DOWNSTREAM, total_received_bytes);
105 }
106 }
107
108 #if defined(OS_ANDROID)
109 void DataUseMeasurement::OnApplicationStateChange(
110 base::android::ApplicationState application_state) {
111 app_state_ = application_state;
112 }
113 #endif
114
115 #if defined(OS_ANDROID)
116 void DataUseMeasurement::OnApplicationStateChangeForTesting(
117 base::android::ApplicationState application_state) {
118 app_state_ = application_state;
119 }
120 #endif
121
122 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() {
123 #if defined(OS_ANDROID)
124 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES)
125 return BACKGROUND;
126 #endif
127 // If the OS is not Android, all the requests are considered Foreground.
128 return FOREGROUND;
129 }
130
131 const std::string DataUseMeasurement::GetSuffixForHistogramName(
132 TrafficDirection dir) {
133 AppState app_state = CurrentAppState();
134 bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular(
135 net::NetworkChangeNotifier::GetConnectionType());
136 return base::StringPrintf(
137 ".%s.%s.%s", dir == UPSTREAM ? "Upstream" : "Downstream",
138 app_state == BACKGROUND ? "Background" : "Foreground",
139 is_conn_cellular ? "Cellular" : "NotCellular");
140 }
141
142 void DataUseMeasurement::ReportDataUsageServices(
143 data_use_measurement::DataUseUserData::ServiceType service,
144 TrafficDirection dir,
145 int64_t message_size) {
146 RecordUMAHistogramCount(
147 "DataUse.MessageSize." +
148 std::string(DataUseUserData::GetServiceName(service)),
149 message_size);
150 if (message_size > 0) {
151 RecordSparseHistogramWithValue(
152 "DataUse.MessageSize.AllServices" + GetSuffixForHistogramName(dir),
153 service, message_size);
154 }
155 }
156
157 void DataUseMeasurement::ReportDataUsageGeneral(RequestInitiator service_type,
158 TrafficDirection dir,
159 int64_t message_size) {
160 std::string histogram_name = base::StringPrintf(
161 "DataUse.TrafficSize.%s%s",
162 service_type == REQUEST_INITIATOR_USER ? "User" : "System",
163 GetSuffixForHistogramName(dir).c_str());
164 RecordUMAHistogramCount(histogram_name, message_size);
165 }
166
167 } // namespace data_use_measurement
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698