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 if (!is_user_traffic) { |
| 96 data_use_measurement::DataUseUserData::ServiceType service_type = |
| 97 attached_service_data |
| 98 ? attached_service_data->service_type() |
| 99 : data_use_measurement::DataUseUserData::NOT_TAGGED; |
| 100 ReportDataUsageServices(service_type, UPSTREAM, total_upload_bytes); |
| 101 ReportDataUsageServices(service_type, DOWNSTREAM, total_received_bytes); |
| 102 } |
| 103 } |
| 104 |
| 105 #if defined(OS_ANDROID) |
| 106 void DataUseMeasurement::OnApplicationStateChange( |
| 107 base::android::ApplicationState application_state) { |
| 108 app_state_ = application_state; |
| 109 } |
| 110 #endif |
| 111 |
| 112 #if defined(OS_ANDROID) |
| 113 void DataUseMeasurement::OnApplicationStateChangeForTesting( |
| 114 base::android::ApplicationState application_state) { |
| 115 app_state_ = application_state; |
| 116 } |
| 117 #endif |
| 118 |
| 119 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() { |
| 120 #if defined(OS_ANDROID) |
| 121 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
| 122 return BACKGROUND; |
| 123 #endif |
| 124 // If the OS is not Android, all the requests are considered Foreground. |
| 125 return FOREGROUND; |
| 126 } |
| 127 |
| 128 const std::string DataUseMeasurement::GetSuffixForHistogramName( |
| 129 TrafficDirection dir) { |
| 130 AppState app_state = CurrentAppState(); |
| 131 bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular( |
| 132 net::NetworkChangeNotifier::GetConnectionType()); |
| 133 return base::StringPrintf( |
| 134 ".%s.%s.%s", dir == UPSTREAM ? "Upstream" : "Downstream", |
| 135 app_state == BACKGROUND ? "Background" : "Foreground", |
| 136 is_conn_cellular ? "Cellular" : "NotCellular"); |
| 137 } |
| 138 |
| 139 void DataUseMeasurement::ReportDataUsageServices( |
| 140 data_use_measurement::DataUseUserData::ServiceType service, |
| 141 TrafficDirection dir, |
| 142 int64_t message_size) { |
| 143 RecordUMAHistogramCount( |
| 144 "DataUse.MessageSize." + |
| 145 std::string(DataUseUserData::GetServiceName(service)), |
| 146 message_size); |
| 147 if (message_size > 0) { |
| 148 RecordSparseHistogramWithValue( |
| 149 "DataUse.MessageSize.AllServices" + GetSuffixForHistogramName(dir), |
| 150 service, message_size); |
| 151 } |
| 152 } |
| 153 |
| 154 void DataUseMeasurement::ReportDataUsageGeneral(RequestInitiator service_type, |
| 155 TrafficDirection dir, |
| 156 int64_t message_size) { |
| 157 std::string histogram_name = base::StringPrintf( |
| 158 "DataUse.TrafficSize.%s%s", |
| 159 service_type == REQUEST_INITIATOR_USER ? "User" : "System", |
| 160 GetSuffixForHistogramName(dir).c_str()); |
| 161 RecordUMAHistogramCount(histogram_name, message_size); |
| 162 } |
| 163 |
| 164 } // namespace data_use_measurement |
OLD | NEW |