Chromium Code Reviews| 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 | |
| 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) { | |
| 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 |name| | |
| 35 // is not static. | |
| 36 void IncreaseSparseHistogramByValue(const std::string& name, | |
| 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 | |
| 58 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) { | |
| 59 const content::ResourceRequestInfo* info = | |
| 60 content::ResourceRequestInfo::ForRequest(request); | |
| 61 // Having |info| is the sign of a request for a web content from user. For now | |
| 62 // we could add a condition to check ProcessType in info is | |
| 63 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming | |
| 64 // PlzNavigate architecture. So just existence of |info| is verified, and the | |
| 65 // current check should be compatible with upcoming changes in PlzNavigate. | |
| 66 bool is_user_traffic = info != nullptr; | |
| 67 | |
| 68 // Counts rely on URLRequest::GetTotalReceivedBytes(), which does not include | |
| 69 // the send path, network layer overhead, TLS overhead, and DNS. | |
| 70 // TODO(amohammadkhan): Make these measured bytes more in line with number of | |
| 71 // bytes in lower levels. | |
| 72 int64_t request_body_bytes = 0; | |
| 73 int64_t request_header_bytes = 0; | |
| 74 int64_t total_upload_bytes = 0; | |
| 75 if (request->has_upload()) | |
| 76 request_body_bytes = request->get_upload()->size(); | |
| 77 net::HttpRequestHeaders request_headers; | |
| 78 if (request->GetFullRequestHeaders(&request_headers)) | |
| 79 request_header_bytes = request_headers.ToString().length(); | |
| 80 total_upload_bytes = request_body_bytes + request_header_bytes; | |
|
mmenke
2015/09/10 15:43:00
Per earlier comments, all this upstream stuff is w
amohammadkhan
2015/09/10 20:09:00
Done. I made this CL dependent on that CL and used
| |
| 81 int64_t total_received_bytes = request->GetTotalReceivedBytes(); | |
| 82 | |
| 83 ReportDataUsageGeneral( | |
| 84 is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE, | |
| 85 UPSTREAM, total_upload_bytes); | |
| 86 ReportDataUsageGeneral( | |
| 87 is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE, | |
| 88 DOWNSTREAM, total_received_bytes); | |
| 89 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | |
| 90 request->GetUserData(DataUseUserData::kUserDataKey)); | |
| 91 | |
| 92 if (!is_user_traffic) { | |
|
mmenke
2015/09/10 15:43:00
optional: I'd suggest just treating user traffic
amohammadkhan
2015/09/10 20:09:00
I see your point. But we are interested to have mo
| |
| 93 data_use_measurement::DataUseUserData::ServiceName service_type = | |
| 94 attached_service_data | |
| 95 ? attached_service_data->service_type() | |
|
mmenke
2015/09/10 15:43:00
The naming here is inconsistent - the enumeration
amohammadkhan
2015/09/10 20:09:00
Done.
| |
| 96 : data_use_measurement::DataUseUserData::NOT_TAGGED; | |
|
mmenke
2015/09/10 15:43:00
data_use_measurement:: namespace prefix not needed
amohammadkhan
2015/09/10 20:09:00
Done.
| |
| 97 ReportDataUsageServices(service_type, UPSTREAM, total_upload_bytes); | |
| 98 ReportDataUsageServices(service_type, DOWNSTREAM, 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 #if defined(OS_ANDROID) | |
| 110 void DataUseMeasurement::OnApplicationStateChangeForTesting( | |
|
mmenke
2015/09/10 15:43:00
Order in the cc file should match order in the hea
amohammadkhan
2015/09/10 20:09:00
Done.
| |
| 111 base::android::ApplicationState application_state) { | |
| 112 app_state_ = application_state; | |
| 113 } | |
| 114 #endif | |
| 115 | |
| 116 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() { | |
| 117 #if defined(OS_ANDROID) | |
| 118 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | |
| 119 return BACKGROUND; | |
| 120 #endif | |
| 121 // If the OS is not Android, all the requests are considered Foreground. | |
| 122 return FOREGROUND; | |
| 123 } | |
| 124 | |
| 125 const std::string DataUseMeasurement::GetSuffixForHistogramName( | |
| 126 TrafficDirection dir) { | |
| 127 AppState app_state = CurrentAppState(); | |
| 128 bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular( | |
| 129 net::NetworkChangeNotifier::GetConnectionType()); | |
| 130 return base::StringPrintf( | |
| 131 ".%s.%s.%s", dir == UPSTREAM ? "Upstream" : "Downstream", | |
| 132 app_state == BACKGROUND ? "Background" : "Foreground", | |
| 133 is_conn_cellular ? "Cellular" : "NotCellular"); | |
| 134 } | |
| 135 | |
| 136 void DataUseMeasurement::ReportDataUsageServices( | |
| 137 data_use_measurement::DataUseUserData::ServiceName service, | |
| 138 TrafficDirection dir, | |
| 139 int64_t message_size) { | |
| 140 RecordUMAHistogramCount( | |
| 141 "DataUse.MessageSize." + | |
| 142 std::string(DataUseUserData::GetServiceNameAsString(service)), | |
|
mmenke
2015/09/10 15:43:00
To reduce string operations by one, suggest passin
amohammadkhan
2015/09/10 20:09:00
I am not sure if I understood it correctly. Please
mmenke
2015/09/11 13:59:07
below have:
IncreaseSparseHistogramByValue(
mmenke
2015/09/11 14:00:03
Oops...meant to delete the above comment - ignore
| |
| 143 message_size); | |
| 144 if (message_size > 0) { | |
| 145 IncreaseSparseHistogramByValue( | |
| 146 "DataUse.MessageSize.AllServices" + GetSuffixForHistogramName(dir), | |
| 147 service, message_size); | |
|
mmenke
2015/09/11 13:59:07
Was thinking of having this here:
IncreaseSp
| |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void DataUseMeasurement::ReportDataUsageGeneral(RequestInitiator service_type, | |
|
mmenke
2015/09/10 15:43:00
Note that service_type and DataUseUserData::servic
amohammadkhan
2015/09/10 20:09:00
Done.
| |
| 152 TrafficDirection dir, | |
| 153 int64_t message_size) { | |
| 154 std::string histogram_name = base::StringPrintf( | |
| 155 "DataUse.TrafficSize.%s%s", | |
| 156 service_type == REQUEST_INITIATOR_USER ? "User" : "System", | |
| 157 GetSuffixForHistogramName(dir).c_str()); | |
| 158 RecordUMAHistogramCount(histogram_name, message_size); | |
| 159 } | |
| 160 | |
| 161 } // namespace data_use_measurement | |
| OLD | NEW |