OLD | NEW |
---|---|
(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/core/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/url_request/url_request.h" | |
14 | |
15 namespace data_use_measurement { | |
16 | |
17 namespace { | |
sclittle
2015/08/24 23:59:34
nit: add blank line after "namespace {"
amohammadkhan
2015/08/25 00:12:07
Done.
| |
18 // Records the occurrence of |sample| in |name| histogram. Conventional UMA | |
19 // histograms are not used because the |name| is not static. | |
20 void RecordUMAHistogramCount(const std::string& name, int64_t sample) { | |
21 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( | |
22 name, | |
23 1, // Minimum sample size. | |
24 5000000, // Maximum sample size. Should cover most of the requests. | |
25 50, // Bucket count. | |
26 base::HistogramBase::kUmaTargetedHistogramFlag); | |
27 histogram_pointer->Add(sample); | |
28 } | |
29 | |
30 // This function increases the value of |sample| bucket in |name| sparse | |
31 // histogram by |value|. Conventional UMA histograms are not used because the | |
32 // |name| is not static. | |
33 void RecordSparseHistogramWithValue(const std::string& name, | |
34 int64_t sample, | |
35 int64_t value) { | |
36 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( | |
37 name, base::HistogramBase::kUmaTargetedHistogramFlag); | |
38 histogram->AddCount(sample, value); | |
39 } | |
40 | |
41 } // Anonymous namespace. | |
sclittle
2015/08/24 23:59:34
nit: this line should just be "} // namespace", d
amohammadkhan
2015/08/25 00:12:07
Done.
| |
42 | |
43 DataUseMeasurement::DataUseMeasurement() | |
44 #if defined(OS_ANDROID) | |
45 : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), | |
46 app_listener_(new base::android::ApplicationStatusListener( | |
47 base::Bind(&DataUseMeasurement::OnApplicationStateChange, | |
48 base::Unretained(this)))) | |
49 #endif | |
50 { | |
51 } | |
52 | |
53 DataUseMeasurement::~DataUseMeasurement() { | |
54 #if defined(OS_ANDROID) | |
55 app_listener_.reset(); | |
56 #endif | |
57 } | |
58 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) { | |
59 bool user_traffic_flag = false; | |
60 | |
61 const content::ResourceRequestInfo* info = | |
62 content::ResourceRequestInfo::ForRequest(request); | |
63 // Having |info| is the sign of a request for a web content from user. For now | |
64 // we could add a condition to check ProcessType in info is | |
65 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming | |
66 // PlzNavigate architecture. So just existence of |info| is verified, and the | |
67 // current check should be compatible with upcoming changes in PlzNavigate. | |
68 if (info) { | |
69 user_traffic_flag = true; | |
70 } | |
71 | |
72 // These number won't be the number of bytes handed to the kernel because | |
73 // session layer framing and compression is not being accounted for. | |
74 // TODO(amohammadkhan): Make these measured bytes more in line with number of | |
75 // bytes in lower levels. | |
76 int64_t request_body_bytes = 0; | |
77 int64_t request_header_bytes = 0; | |
78 int64_t total_upload_bytes = 0; | |
79 if (request->has_upload()) | |
80 request_body_bytes = request->get_upload()->size(); | |
81 net::HttpRequestHeaders request_headers; | |
82 if (request->GetFullRequestHeaders(&request_headers)) | |
83 request_header_bytes = request_headers.ToString().length(); | |
84 total_upload_bytes = request_body_bytes + request_header_bytes; | |
85 int64_t total_received_bytes = request->GetTotalReceivedBytes(); | |
86 | |
87 ReportDataUsageURLRequest(user_traffic_flag ? USER_TRAFFIC : SERVICE_TRAFFIC, | |
88 UPLOAD, total_upload_bytes); | |
89 ReportDataUsageURLRequest(user_traffic_flag ? USER_TRAFFIC : SERVICE_TRAFFIC, | |
90 DOWNLOAD, total_received_bytes); | |
91 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | |
92 request->GetUserData(DataUseUserData::kUserDataKey)); | |
93 | |
94 if (attached_service_data) { | |
95 ReportDataUsage(attached_service_data->service_type(), UPLOAD, | |
96 total_upload_bytes); | |
97 ReportDataUsage(attached_service_data->service_type(), DOWNLOAD, | |
98 total_received_bytes); | |
99 } | |
100 } | |
101 | |
102 #if defined(OS_ANDROID) | |
103 void DataUseMeasurement::OnApplicationStateChange( | |
104 base::android::ApplicationState application_state) { | |
105 app_state_ = application_state; | |
106 } | |
107 #endif | |
108 | |
109 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() { | |
110 #if defined(OS_ANDROID) | |
111 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | |
112 return BACKGROUND; | |
113 #endif | |
114 // If the OS is not Android, all the requests are considered Foreground. | |
115 return FOREGROUND; | |
116 } | |
117 | |
118 DataUseMeasurement::ConnectionType DataUseMeasurement::CurrentConnectionType() { | |
119 if (net::NetworkChangeNotifier::IsConnectionCellular( | |
120 net::NetworkChangeNotifier::GetConnectionType())) { | |
121 return CELLULAR; | |
122 } | |
123 return NOT_CELLULAR; | |
124 } | |
125 | |
126 std::string DataUseMeasurement::DimensionMaker(TrafficDirection dir) { | |
127 AppState app_state = CurrentAppState(); | |
128 ConnectionType conn_type = CurrentConnectionType(); | |
129 return base::StringPrintf( | |
130 ".%s.%s.%s", dir == UPLOAD ? "Upload" : "Download", | |
131 app_state == BACKGROUND ? "Background" : "Foreground", | |
132 conn_type == NOT_CELLULAR ? "NotCellular" : "Cellular"); | |
133 } | |
134 | |
135 void DataUseMeasurement::ReportDataUsage( | |
136 data_use_measurement::DataUseUserData::ServiceType service, | |
137 TrafficDirection dir, | |
138 int64_t message_size) { | |
139 std::string serviceHistogramName = | |
140 base::StringPrintf("DataUse.Service.%s", ReturnServiceName(service)); | |
141 std::string combinedHistogramName = "DataUse.Services" + DimensionMaker(dir); | |
sclittle
2015/08/24 23:59:34
variable names should use underscores, not camel c
amohammadkhan
2015/08/25 00:12:07
Done.
| |
142 RecordUMAHistogramCount(serviceHistogramName, message_size); | |
143 if (message_size > 0) { | |
144 RecordSparseHistogramWithValue(combinedHistogramName, service, | |
145 message_size); | |
146 } | |
147 } | |
148 | |
149 void DataUseMeasurement::ReportDataUsageURLRequest( | |
150 ChromeTrafficType service_type, | |
151 TrafficDirection dir, | |
152 int64_t message_size) { | |
153 std::string histogram_name = base::StringPrintf( | |
154 "DataUse.%s%s", service_type == USER_TRAFFIC ? "User" : "NotUser", | |
155 DimensionMaker(dir).c_str()); | |
156 RecordUMAHistogramCount(histogram_name, message_size); | |
157 } | |
158 | |
159 } // data_use_measurement namespace | |
OLD | NEW |