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/content/data_use_measurement.h> | |
6 #include "base/metrics/histogram.h" | |
7 #include "base/metrics/sparse_histogram.h" | |
8 #include "base/strings/stringprintf.h" | |
9 #include "content/public/browser/resource_request_info.h" | |
10 #include "net/base/network_change_notifier.h" | |
11 #include "net/base/upload_data_stream.h" | |
12 #include "net/url_request/url_request.h" | |
13 | |
14 namespace data_use_measurement { | |
15 | |
16 namespace { | |
17 | |
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 1000000, // Maximum sample size. Should cover most of the requests. | |
sclittle
2015/08/26 23:08:30
nit: Maybe specify in this comment that this shoul
amohammadkhan
2015/08/29 01:00:59
Done.
| |
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 } // namespace | |
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(); | |
sclittle
2015/08/26 23:08:29
nit: add a comment here describing why you're dest
amohammadkhan
2015/08/29 01:00:59
Done.
| |
56 #endif | |
57 } | |
58 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) { | |
59 bool user_traffic_flag = false; | |
sclittle
2015/08/26 23:08:29
nit: rename to |is_user_traffic|
amohammadkhan
2015/08/29 01:00:59
Done.
| |
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) { | |
sclittle
2015/08/26 23:08:30
nit: rename this method to be like a verb, i.e. Ge
amohammadkhan
2015/08/29 01:00:59
Done.
| |
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 service_histogram_name = | |
140 base::StringPrintf("DataUse.Service.%s", ReturnServiceName(service)); | |
141 std::string combined_histogram_name = | |
142 "DataUse.Services" + DimensionMaker(dir); | |
143 RecordUMAHistogramCount(service_histogram_name, message_size); | |
144 if (message_size > 0) { | |
145 RecordSparseHistogramWithValue(combined_histogram_name, service, | |
146 message_size); | |
147 } | |
148 } | |
149 | |
150 void DataUseMeasurement::ReportDataUsageURLRequest( | |
151 ChromeTrafficType service_type, | |
152 TrafficDirection dir, | |
153 int64_t message_size) { | |
154 std::string histogram_name = base::StringPrintf( | |
155 "DataUse.%s%s", service_type == USER_TRAFFIC ? "User" : "NotUser", | |
156 DimensionMaker(dir).c_str()); | |
157 RecordUMAHistogramCount(histogram_name, message_size); | |
158 } | |
159 | |
160 } // data_use_measurement namespace | |
sclittle
2015/08/26 23:08:29
nit: change comment to "// namespace data_use_meas
amohammadkhan
2015/08/29 01:00:59
Done.
| |
OLD | NEW |