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> | |
bengr
2015/09/01 20:16:01
#include "components/.../data_use_measurement.h" a
amohammadkhan
2015/09/01 23:02:42
Done.
| |
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 in Bytes. | |
24 1000000, // Maximum sample size in Bytes. Should cover most of the | |
25 // requests by services. | |
26 50, // Bucket count. | |
27 base::HistogramBase::kUmaTargetedHistogramFlag); | |
28 histogram_pointer->Add(sample); | |
29 } | |
30 | |
31 // This function increases the value of |sample| bucket in |name| sparse | |
32 // histogram by |value|. Conventional UMA histograms are not used because the | |
33 // |name| is not static. | |
34 void RecordSparseHistogramWithValue(const std::string& name, | |
35 int64_t sample, | |
36 int64_t value) { | |
37 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( | |
38 name, base::HistogramBase::kUmaTargetedHistogramFlag); | |
39 histogram->AddCount(sample, value); | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 DataUseMeasurement::DataUseMeasurement() | |
45 #if defined(OS_ANDROID) | |
46 : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), | |
47 app_listener_(new base::android::ApplicationStatusListener( | |
48 base::Bind(&DataUseMeasurement::OnApplicationStateChange, | |
49 base::Unretained(this)))) | |
50 #endif | |
51 { | |
52 } | |
53 | |
54 DataUseMeasurement::~DataUseMeasurement() { | |
55 #if defined(OS_ANDROID) | |
56 // |app_listener_| is destroyed before DataUseMeasurement instance is | |
57 // destroyed, so a callback isn't sent between the DataUseMeasurement instance | |
58 // being destroyed and the |app_listener_| being destroyed. | |
bengr
2015/09/01 20:16:01
// Prevent notification between the destruction of
amohammadkhan
2015/09/01 23:02:42
Done.
| |
59 app_listener_.reset(); | |
60 #endif | |
61 } | |
62 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) { | |
63 bool is_user_traffic = false; | |
64 | |
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 if (info) { | |
bengr
2015/09/01 20:16:01
bool is_user_traffic = info != nullptr;
amohammadkhan
2015/09/01 23:02:43
Done.
| |
73 is_user_traffic = true; | |
74 } | |
75 | |
76 // These number won't be the number of bytes handed to the kernel because | |
bengr
2015/09/01 20:16:01
// Counts rely on URLRequest::GetTotalReceivedByte
amohammadkhan
2015/09/01 23:02:43
Done.
| |
77 // session layer framing and compression is not being accounted for. | |
78 // TODO(amohammadkhan): Make these measured bytes more in line with number of | |
79 // bytes in lower levels. | |
80 int64_t request_body_bytes = 0; | |
81 int64_t request_header_bytes = 0; | |
82 int64_t total_upload_bytes = 0; | |
83 if (request->has_upload()) | |
84 request_body_bytes = request->get_upload()->size(); | |
85 net::HttpRequestHeaders request_headers; | |
86 if (request->GetFullRequestHeaders(&request_headers)) | |
87 request_header_bytes = request_headers.ToString().length(); | |
88 total_upload_bytes = request_body_bytes + request_header_bytes; | |
89 int64_t total_received_bytes = request->GetTotalReceivedBytes(); | |
90 ReportDataUsageURLRequest(is_user_traffic ? USER_TRAFFIC : SERVICE_TRAFFIC, | |
91 UPSTREAM, total_upload_bytes); | |
92 ReportDataUsageURLRequest(is_user_traffic ? USER_TRAFFIC : SERVICE_TRAFFIC, | |
93 DOWNSTREAM, total_received_bytes); | |
94 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | |
95 request->GetUserData(DataUseUserData::kUserDataKey)); | |
96 | |
97 if (!is_user_traffic) { | |
98 data_use_measurement::DataUseUserData::ServiceType service_type = | |
99 attached_service_data | |
100 ? attached_service_data->service_type() | |
101 : data_use_measurement::DataUseUserData::NOT_TAGGED; | |
102 ReportDataUsage(service_type, UPSTREAM, total_upload_bytes); | |
103 ReportDataUsage(service_type, DOWNSTREAM, total_received_bytes); | |
104 } | |
105 } | |
106 | |
107 #if defined(OS_ANDROID) | |
108 void DataUseMeasurement::OnApplicationStateChange( | |
109 base::android::ApplicationState application_state) { | |
110 app_state_ = application_state; | |
111 } | |
112 #endif | |
113 | |
114 #if defined(OS_ANDROID) | |
115 void DataUseMeasurement::OnApplicationStateChangeForTesting( | |
116 base::android::ApplicationState application_state) { | |
117 app_state_ = application_state; | |
118 } | |
119 #endif | |
120 | |
121 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() { | |
122 #if defined(OS_ANDROID) | |
123 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | |
124 return BACKGROUND; | |
125 #endif | |
126 // If the OS is not Android, all the requests are considered Foreground. | |
bengr
2015/09/01 20:16:01
Make sure this is documented in histograms.xml.
amohammadkhan
2015/09/01 23:02:42
Done.
| |
127 return FOREGROUND; | |
128 } | |
129 | |
130 std::string DataUseMeasurement::GetSuffixForHistogramName( | |
bengr
2015/09/01 20:16:01
The method should be const.
amohammadkhan
2015/09/01 23:02:43
Done.
| |
131 TrafficDirection dir) { | |
132 AppState app_state = CurrentAppState(); | |
133 bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular( | |
134 net::NetworkChangeNotifier::GetConnectionType()); | |
135 return base::StringPrintf( | |
136 ".%s.%s.%s", dir == UPSTREAM ? "Upstream" : "Downstream", | |
137 app_state == BACKGROUND ? "Background" : "Foreground", | |
138 is_conn_cellular ? "Cellular" : "NotCellular"); | |
139 } | |
140 | |
141 void DataUseMeasurement::ReportDataUsage( | |
142 data_use_measurement::DataUseUserData::ServiceType service, | |
143 TrafficDirection dir, | |
144 int64_t message_size) { | |
145 std::string service_histogram_name = | |
146 base::StringPrintf("DataUse.MessageSize.%s", GetServiceName(service)); | |
147 std::string combined_histogram_name = | |
148 "DataUse.Services" + GetSuffixForHistogramName(dir); | |
bengr
2015/09/01 20:16:00
Why does this prefix not end with '.' and the othe
amohammadkhan
2015/09/01 23:02:42
There is no inconsistency here. The '.' is needed
| |
149 RecordUMAHistogramCount(service_histogram_name, message_size); | |
bengr
2015/09/01 20:16:01
I would get rid of service_histogram_name and repl
amohammadkhan
2015/09/01 23:02:42
Done.
| |
150 if (message_size > 0) { | |
151 RecordSparseHistogramWithValue(combined_histogram_name, service, | |
152 message_size); | |
153 } | |
154 } | |
155 | |
156 void DataUseMeasurement::ReportDataUsageURLRequest( | |
157 ChromeTrafficType service_type, | |
158 TrafficDirection dir, | |
159 int64_t message_size) { | |
160 std::string histogram_name = base::StringPrintf( | |
161 "DataUse.%s%s", service_type == USER_TRAFFIC ? "User" : "NotUser", | |
bengr
2015/09/01 20:16:01
NotUser -> Service?
amohammadkhan
2015/09/01 23:02:42
Yes service may be better. But basically we had a
| |
162 GetSuffixForHistogramName(dir).c_str()); | |
163 RecordUMAHistogramCount(histogram_name, message_size); | |
164 } | |
165 | |
166 } // namespace data_use_measurement | |
OLD | NEW |