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 #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 by | |
|
Alexei Svitkine (slow)
2015/08/31 17:26:31
What's the units?
amohammadkhan
2015/09/01 04:52:48
Done.
| |
| 25 // 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. | |
| 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 = | |
|
blundell
2015/08/31 09:15:40
IIUC, the only usage of //content in this code is
amohammadkhan
2015/09/01 04:52:48
Thanks for heads up. I see your point. But current
| |
| 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) { | |
| 73 is_user_traffic = true; | |
| 74 } | |
| 75 | |
| 76 // These number won't be the number of bytes handed to the kernel because | |
| 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(); | |
|
mmenke
2015/08/31 17:41:29
This is -1 for chunked uploads, which a lot of int
amohammadkhan
2015/09/01 04:52:48
After reading your comment, I wanted to try to und
mmenke
2015/09/01 19:23:37
sclittle is working on adding methods to expose a
| |
| 85 net::HttpRequestHeaders request_headers; | |
| 86 if (request->GetFullRequestHeaders(&request_headers)) | |
|
mmenke
2015/08/31 17:41:29
This method doesn't really work - it's not fully h
amohammadkhan
2015/09/01 04:52:48
Do you have any suggestion for a better substitute
| |
| 87 request_header_bytes = request_headers.ToString().length(); | |
|
mmenke
2015/08/31 17:41:28
Even if the method worked, this is only true for H
mmenke
2015/08/31 17:42:06
"For HTTP, and not SPDY (Or QUIC)", rather
amohammadkhan
2015/09/01 04:52:48
I see your points. For now I am trying to get a ro
amohammadkhan
2015/09/01 04:52:48
I agree.
| |
| 88 total_upload_bytes = request_body_bytes + request_header_bytes; | |
| 89 int64_t total_received_bytes = request->GetTotalReceivedBytes(); | |
| 90 | |
| 91 ReportDataUsageURLRequest(is_user_traffic ? USER_TRAFFIC : SERVICE_TRAFFIC, | |
| 92 UPLOAD, total_upload_bytes); | |
| 93 ReportDataUsageURLRequest(is_user_traffic ? USER_TRAFFIC : SERVICE_TRAFFIC, | |
| 94 DOWNLOAD, total_received_bytes); | |
|
mmenke
2015/08/31 17:41:29
Having "service traffic" an independent dimension
amohammadkhan
2015/09/01 04:52:48
Good idea. I made a bucket for all untagged servic
| |
| 95 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | |
| 96 request->GetUserData(DataUseUserData::kUserDataKey)); | |
| 97 | |
| 98 if (attached_service_data) { | |
| 99 ReportDataUsage(attached_service_data->service_type(), UPLOAD, | |
| 100 total_upload_bytes); | |
| 101 ReportDataUsage(attached_service_data->service_type(), DOWNLOAD, | |
| 102 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 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() { | |
| 114 #if defined(OS_ANDROID) | |
| 115 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | |
| 116 return BACKGROUND; | |
| 117 #endif | |
| 118 // If the OS is not Android, all the requests are considered Foreground. | |
| 119 return FOREGROUND; | |
| 120 } | |
| 121 | |
| 122 DataUseMeasurement::ConnectionType DataUseMeasurement::CurrentConnectionType() { | |
|
mmenke
2015/08/31 17:41:29
I don't think having this as a method, or mapping
amohammadkhan
2015/09/01 04:52:48
Done.
| |
| 123 if (net::NetworkChangeNotifier::IsConnectionCellular( | |
| 124 net::NetworkChangeNotifier::GetConnectionType())) { | |
| 125 return CELLULAR; | |
| 126 } | |
| 127 return NOT_CELLULAR; | |
| 128 } | |
| 129 | |
| 130 std::string DataUseMeasurement::GetDimensionForHistogramName( | |
|
mmenke
2015/08/31 17:41:29
Dimension -> Suffix? This results a string,not di
amohammadkhan
2015/09/01 04:52:48
Done.
| |
| 131 TrafficDirection dir) { | |
| 132 AppState app_state = CurrentAppState(); | |
| 133 ConnectionType conn_type = CurrentConnectionType(); | |
| 134 return base::StringPrintf( | |
| 135 ".%s.%s.%s", dir == UPLOAD ? "Upload" : "Download", | |
|
mmenke
2015/08/31 17:41:29
This isn't categorizing requests as uploads or dow
amohammadkhan
2015/09/01 04:52:48
Done.
| |
| 136 app_state == BACKGROUND ? "Background" : "Foreground", | |
| 137 conn_type == NOT_CELLULAR ? "NotCellular" : "Cellular"); | |
| 138 } | |
| 139 | |
| 140 void DataUseMeasurement::ReportDataUsage( | |
| 141 data_use_measurement::DataUseUserData::ServiceType service, | |
| 142 TrafficDirection dir, | |
| 143 int64_t message_size) { | |
| 144 std::string service_histogram_name = | |
| 145 base::StringPrintf("DataUse.Service.%s", GetServiceName(service)); | |
| 146 std::string combined_histogram_name = | |
| 147 "DataUse.Services" + GetDimensionForHistogramName(dir); | |
|
mmenke
2015/08/31 17:41:29
Can't these two lines be combined, like in ReportD
amohammadkhan
2015/09/01 04:52:48
Our ideal was to have all dimensions for all servi
| |
| 148 RecordUMAHistogramCount(service_histogram_name, message_size); | |
| 149 if (message_size > 0) { | |
| 150 RecordSparseHistogramWithValue(combined_histogram_name, service, | |
| 151 message_size); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 void DataUseMeasurement::ReportDataUsageURLRequest( | |
| 156 ChromeTrafficType service_type, | |
| 157 TrafficDirection dir, | |
| 158 int64_t message_size) { | |
| 159 std::string histogram_name = base::StringPrintf( | |
| 160 "DataUse.%s%s", service_type == USER_TRAFFIC ? "User" : "NotUser", | |
| 161 GetDimensionForHistogramName(dir).c_str()); | |
| 162 RecordUMAHistogramCount(histogram_name, message_size); | |
| 163 } | |
| 164 | |
| 165 } // namespace data_use_measurement | |
| OLD | NEW |