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