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 "chrome/browser/net/data_use_measurement.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/hash.h" | |
| 10 #include "base/metrics/histogram.h" | |
| 11 #include "base/metrics/sparse_histogram.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "chrome/browser/net/data_use_user_data.h" | |
| 14 #include "content/public/browser/resource_request_info.h" | |
| 15 #include "net/base/network_change_notifier.h" | |
| 16 #include "net/base/upload_data_stream.h" | |
| 17 #include "net/url_request/url_request.h" | |
| 18 | |
| 19 #if defined(OS_ANDROID) | |
| 20 #include "base/android/application_status_listener.h" | |
| 21 #endif | |
| 22 | |
| 23 // This macro increases the value of |sample| bucket in |name| sparse histogram | |
| 24 // by |value|. This Macro can be called with different names. | |
| 25 #define UMA_HISTOGRAM_SPARSE_SLOWLY_WITH_VALUE(name, sample, value) \ | |
| 26 do { \ | |
|
sclittle
2015/08/19 23:32:39
Why does this need to be a macro? Could this just
amohammadkhan
2015/08/21 23:06:31
Done.
| |
| 27 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( \ | |
| 28 name, base::HistogramBase::kUmaTargetedHistogramFlag); \ | |
| 29 histogram->AddCount(sample, value); \ | |
| 30 } while (0) | |
| 31 | |
| 32 // Copied from chromecast/base/metrics/cast_histograms.h | |
| 33 // TODO: This macro can be unified with the cast_histogram.h macros. | |
| 34 #define STATIC_HISTOGRAM_POINTER_BLOCK_NO_CACHE( \ | |
|
sclittle
2015/08/19 23:32:39
Same here, why a macro and not just a function?
amohammadkhan
2015/08/21 23:06:31
Done.
| |
| 35 constant_histogram_name, histogram_add_method_invocation, \ | |
| 36 histogram_factory_get_invocation) \ | |
| 37 do { \ | |
| 38 base::HistogramBase* histogram_pointer = histogram_factory_get_invocation; \ | |
| 39 if (DCHECK_IS_ON()) \ | |
| 40 histogram_pointer->CheckName(constant_histogram_name); \ | |
| 41 histogram_pointer->histogram_add_method_invocation; \ | |
| 42 } while (0) | |
| 43 | |
| 44 // Copied from chromecast/base/metrics/cast_histograms.h | |
| 45 // TODO: This macro can be unified with the cast_histogram.h macros. | |
| 46 #define UMA_HISTOGRAM_CUSTOM_COUNTS_NO_CACHE(name, sample, min, max, \ | |
| 47 bucket_count) \ | |
| 48 STATIC_HISTOGRAM_POINTER_BLOCK_NO_CACHE( \ | |
| 49 name, Add(sample), base::Histogram::FactoryGet( \ | |
| 50 name, min, max, bucket_count, \ | |
| 51 base::HistogramBase::kUmaTargetedHistogramFlag)) | |
| 52 | |
| 53 // In this macro UMA_HISTOGRAM_CUSTOM_COUNTS_NO_CACHE instead of the common | |
| 54 // UMA_HISTOGRAM_CUSTOM_COUNTS is used, because this macro is called with | |
| 55 // variable |name| and UMA_HISTOGRAM_CUSTOM_COUNTS does not support it. | |
| 56 // In this macro the maximum size of request is 5000000 (about 5MB), which may | |
| 57 // not be enough for large requests but it should cover the big majority of the | |
| 58 // requests. | |
| 59 #define TRAFFIC_MEASUREMENT_UMA_HISTOGRAM(name, sample) \ | |
| 60 UMA_HISTOGRAM_CUSTOM_COUNTS_NO_CACHE(name, sample, 1, 5000000, 50) | |
| 61 | |
| 62 #if defined(OS_ANDROID) | |
| 63 // When Chrome starts working it is in foreground. | |
| 64 base::android::ApplicationState app_state_ = | |
| 65 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES; | |
| 66 | |
| 67 void DataUseMeasurement::OnApplicationStateChange( | |
| 68 base::android::ApplicationState application_state) { | |
| 69 app_state_ = application_state; | |
| 70 } | |
| 71 #endif | |
| 72 | |
| 73 AppState DataUseMeasurement::CurrentAppState() { | |
| 74 #if defined(OS_ANDROID) | |
| 75 if (app_state_ == base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | |
| 76 return FOREGROUND; | |
| 77 else | |
| 78 return BACKGROUND; | |
| 79 #endif | |
| 80 // If the OS is not Android, all the requests are considered foreground so for | |
| 81 // fair comparison between foreground and background data use, -A histograms | |
|
sclittle
2015/08/19 23:32:39
Just say "If the OS is not Android, all the reques
amohammadkhan
2015/08/21 23:06:31
Done.
| |
| 82 // should be chosen in UMA. | |
| 83 return FOREGROUND; | |
| 84 } | |
| 85 | |
| 86 ConnectionType DataUseMeasurement::CurrentConnectionType() { | |
| 87 if (net::NetworkChangeNotifier::IsConnectionCellular( | |
| 88 net::NetworkChangeNotifier::GetConnectionType())) { | |
| 89 return CELLULAR; | |
| 90 } | |
| 91 return NOT_CELLULAR; | |
| 92 } | |
| 93 | |
| 94 std::string DataUseMeasurement::DimensionMaker(TrafficDirection dir) { | |
| 95 AppState app_state = CurrentAppState(); | |
| 96 ConnectionType conn_type = CurrentConnectionType(); | |
| 97 return base::StringPrintf( | |
| 98 ".%s.%s.%s", dir == UPLOAD ? "Upload" : "Download", | |
| 99 app_state == BACKGROUND ? "Background" : "Foreground", | |
| 100 conn_type == NOT_CELLULAR ? "NotCellular" : "Cellular"); | |
| 101 } | |
| 102 | |
| 103 void DataUseMeasurement::ReportDataUsage(const std::string& service_name, | |
| 104 TrafficDirection dir, | |
| 105 int message_size) { | |
| 106 std::string serviceHistogramName = "DataUse.Service." + service_name; | |
| 107 std::string combinedHistogramName = "DataUse.Services" + DimensionMaker(dir); | |
| 108 TRAFFIC_MEASUREMENT_UMA_HISTOGRAM(serviceHistogramName, message_size); | |
| 109 if (message_size > 0) { | |
| 110 UMA_HISTOGRAM_SPARSE_SLOWLY_WITH_VALUE( | |
| 111 combinedHistogramName, base::Hash(service_name), message_size); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void DataUseMeasurement::ReportDataUsageURLRequest( | |
| 116 ChromeTrafficType service_type, | |
| 117 TrafficDirection dir, | |
| 118 int message_size) { | |
| 119 std::string histogram_name = base::StringPrintf( | |
| 120 "DataUse.%s%s", service_type == USER_TRAFFIC ? "User" : "NotUser", | |
| 121 DimensionMaker(dir).c_str()); | |
| 122 TRAFFIC_MEASUREMENT_UMA_HISTOGRAM(histogram_name, message_size); | |
| 123 } | |
| 124 | |
| 125 DataUseMeasurement::DataUseMeasurement() { | |
| 126 #if defined(OS_ANDROID) | |
| 127 app_listener_ = new base::android::ApplicationStatusListener( | |
| 128 base::Bind(&DataUseMeasurement::OnApplicationStateChange, this)); | |
|
sclittle
2015/08/19 23:32:39
Instead of posting this as a scoped_refptr, it mig
amohammadkhan
2015/08/21 23:06:31
I checked the examples of this class again, it see
| |
| 129 #endif | |
| 130 } | |
| 131 | |
| 132 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) { | |
|
sclittle
2015/08/19 23:32:39
nit: please add blank lines where appropriate in t
amohammadkhan
2015/08/21 23:06:31
Done.
| |
| 133 bool user_traffic_flag = false; | |
| 134 const content::ResourceRequestInfo* info = | |
| 135 content::ResourceRequestInfo::ForRequest(request); | |
| 136 // Having |info| is the sign of a request for a web content from user. For now | |
| 137 // we could add a condition to check ProcessType in info is | |
| 138 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming | |
| 139 // PlzNavigate architecture. So just existence of |info| is verified, and the | |
| 140 // current check should be compatible with upcoming changes in PlzNavigate. | |
| 141 if (info) { | |
| 142 user_traffic_flag = true; | |
| 143 } | |
| 144 // These number won't be the number of bytes handed to the | |
| 145 // kernel because session layer framing and compression is not being accounted | |
| 146 // for. | |
| 147 // TODO: Make these measured bytes more accurate based on above explanation. | |
| 148 int request_body_bytes = 0; | |
| 149 int request_header_bytes = 0; | |
| 150 int total_upload_bytes = 0; | |
| 151 if (request->has_upload()) | |
| 152 request_body_bytes = request->get_upload()->size(); | |
| 153 net::HttpRequestHeaders request_headers; | |
| 154 if (request->GetFullRequestHeaders(&request_headers)) | |
| 155 request_header_bytes = request_headers.ToString().length(); | |
| 156 total_upload_bytes = request_body_bytes + request_header_bytes; | |
| 157 int total_received_bytes = request->GetTotalReceivedBytes(); | |
|
sclittle
2015/08/19 23:32:39
Always use int64_t for byte counts unless you have
amohammadkhan
2015/08/21 23:06:31
I see your point. But the problem is that UMA is u
| |
| 158 ReportDataUsageURLRequest(user_traffic_flag ? USER_TRAFFIC : SERVICE_TRAFFIC, | |
| 159 UPLOAD, total_upload_bytes); | |
| 160 ReportDataUsageURLRequest(user_traffic_flag ? USER_TRAFFIC : SERVICE_TRAFFIC, | |
| 161 DOWNLOAD, total_received_bytes); | |
| 162 DataUseUserData* sData = static_cast<DataUseUserData*>( | |
|
sclittle
2015/08/19 23:32:39
nit: use reinterpret_cast here instead, since it's
amohammadkhan
2015/08/21 23:06:31
Done.
| |
| 163 request->GetUserData(DataUseUserData::kUserDataKey)); | |
| 164 if (sData) { | |
| 165 ReportDataUsage(sData->service_name(), UPLOAD, total_upload_bytes); | |
| 166 ReportDataUsage(sData->service_name(), DOWNLOAD, total_received_bytes); | |
| 167 } | |
| 168 } | |
| OLD | NEW |