OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/data_use_measurement/content/data_use_measurement.h" | 5 #include "components/data_use_measurement/content/data_use_measurement.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/metrics/sparse_histogram.h" | 8 #include "base/metrics/sparse_histogram.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 void IncreaseSparseHistogramByValue(const std::string& name, | 37 void IncreaseSparseHistogramByValue(const std::string& name, |
38 int64_t sample, | 38 int64_t sample, |
39 int64_t value) { | 39 int64_t value) { |
40 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( | 40 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( |
41 name, base::HistogramBase::kUmaTargetedHistogramFlag); | 41 name, base::HistogramBase::kUmaTargetedHistogramFlag); |
42 histogram->AddCount(sample, value); | 42 histogram->AddCount(sample, value); |
43 } | 43 } |
44 | 44 |
45 } // namespace | 45 } // namespace |
46 | 46 |
47 DataUseMeasurement::DataUseMeasurement() | 47 DataUseMeasurement::DataUseMeasurement( |
| 48 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder) |
| 49 : metrics_data_use_forwarder_(metrics_data_use_forwarder) |
48 #if defined(OS_ANDROID) | 50 #if defined(OS_ANDROID) |
49 : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), | 51 , |
| 52 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), |
50 app_listener_(new base::android::ApplicationStatusListener( | 53 app_listener_(new base::android::ApplicationStatusListener( |
51 base::Bind(&DataUseMeasurement::OnApplicationStateChange, | 54 base::Bind(&DataUseMeasurement::OnApplicationStateChange, |
52 base::Unretained(this)))) | 55 base::Unretained(this)))) |
53 #endif | 56 #endif |
54 { | 57 { |
55 } | 58 } |
56 | 59 |
57 DataUseMeasurement::~DataUseMeasurement(){}; | 60 DataUseMeasurement::~DataUseMeasurement(){}; |
58 | 61 |
59 void DataUseMeasurement::ReportDataUseUMA( | 62 void DataUseMeasurement::ReportDataUseUMA( |
60 const net::URLRequest* request) const { | 63 const net::URLRequest* request) const { |
61 const content::ResourceRequestInfo* info = | 64 const content::ResourceRequestInfo* info = |
62 content::ResourceRequestInfo::ForRequest(request); | 65 content::ResourceRequestInfo::ForRequest(request); |
63 // Having |info| is the sign of a request for a web content from user. For now | 66 // Having |info| is the sign of a request for a web content from user. For now |
64 // we could add a condition to check ProcessType in info is | 67 // we could add a condition to check ProcessType in info is |
65 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming | 68 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming |
66 // PlzNavigate architecture. So just existence of |info| is verified, and the | 69 // PlzNavigate architecture. So just existence of |info| is verified, and the |
67 // current check should be compatible with upcoming changes in PlzNavigate. | 70 // current check should be compatible with upcoming changes in PlzNavigate. |
68 bool is_user_traffic = info != nullptr; | 71 bool is_user_traffic = info != nullptr; |
69 | 72 |
70 // Counts rely on URLRequest::GetTotalReceivedBytes() and | 73 // Counts rely on URLRequest::GetTotalReceivedBytes() and |
71 // URLRequest::GetTotalSentBytes(), which does not include the send path, | 74 // URLRequest::GetTotalSentBytes(), which does not include the send path, |
72 // network layer overhead, TLS overhead, and DNS. | 75 // network layer overhead, TLS overhead, and DNS. |
73 // TODO(amohammadkhan): Make these measured bytes more in line with number of | 76 // TODO(amohammadkhan): Make these measured bytes more in line with number of |
74 // bytes in lower levels. | 77 // bytes in lower levels. |
75 int64_t total_upload_bytes = request->GetTotalSentBytes(); | 78 int64_t total_upload_bytes = request->GetTotalSentBytes(); |
76 int64_t total_received_bytes = request->GetTotalReceivedBytes(); | 79 int64_t total_received_bytes = request->GetTotalReceivedBytes(); |
77 | 80 |
| 81 bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular( |
| 82 net::NetworkChangeNotifier::GetConnectionType()); |
78 RecordUMAHistogramCount( | 83 RecordUMAHistogramCount( |
79 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | 84 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" |
80 : "DataUse.TrafficSize.System", | 85 : "DataUse.TrafficSize.System", |
81 UPSTREAM), | 86 UPSTREAM, is_conn_cellular), |
82 total_upload_bytes); | 87 total_upload_bytes); |
83 RecordUMAHistogramCount( | 88 RecordUMAHistogramCount( |
84 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | 89 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" |
85 : "DataUse.TrafficSize.System", | 90 : "DataUse.TrafficSize.System", |
86 DOWNSTREAM), | 91 DOWNSTREAM, is_conn_cellular), |
87 total_received_bytes); | 92 total_received_bytes); |
88 | 93 |
89 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | 94 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( |
90 request->GetUserData(DataUseUserData::kUserDataKey)); | 95 request->GetUserData(DataUseUserData::kUserDataKey)); |
| 96 DataUseUserData::ServiceName service_name = |
| 97 attached_service_data ? attached_service_data->service_name() |
| 98 : DataUseUserData::NOT_TAGGED; |
| 99 if (!is_user_traffic) { |
| 100 ReportDataUsageServices(service_name, UPSTREAM, is_conn_cellular, |
| 101 total_upload_bytes); |
| 102 ReportDataUsageServices(service_name, DOWNSTREAM, is_conn_cellular, |
| 103 total_received_bytes); |
| 104 } |
91 | 105 |
92 if (!is_user_traffic) { | 106 // Update data use prefs for cellular connections. |
93 DataUseUserData::ServiceName service_name = | 107 if (!metrics_data_use_forwarder_.is_null()) { |
94 attached_service_data ? attached_service_data->service_name() | 108 metrics_data_use_forwarder_.Run( |
95 : DataUseUserData::NOT_TAGGED; | 109 attached_service_data->GetServiceNameAsString(service_name), |
96 ReportDataUsageServices(service_name, UPSTREAM, total_upload_bytes); | 110 total_upload_bytes + total_received_bytes, is_conn_cellular); |
97 ReportDataUsageServices(service_name, DOWNSTREAM, total_received_bytes); | |
98 } | 111 } |
99 } | 112 } |
100 | 113 |
101 #if defined(OS_ANDROID) | 114 #if defined(OS_ANDROID) |
102 void DataUseMeasurement::OnApplicationStateChangeForTesting( | 115 void DataUseMeasurement::OnApplicationStateChangeForTesting( |
103 base::android::ApplicationState application_state) { | 116 base::android::ApplicationState application_state) { |
104 app_state_ = application_state; | 117 app_state_ = application_state; |
105 } | 118 } |
106 #endif | 119 #endif |
107 | 120 |
108 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { | 121 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { |
109 #if defined(OS_ANDROID) | 122 #if defined(OS_ANDROID) |
110 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | 123 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
111 return BACKGROUND; | 124 return BACKGROUND; |
112 #endif | 125 #endif |
113 // If the OS is not Android, all the requests are considered Foreground. | 126 // If the OS is not Android, all the requests are considered Foreground. |
114 return FOREGROUND; | 127 return FOREGROUND; |
115 } | 128 } |
116 | 129 |
117 std::string DataUseMeasurement::GetHistogramName(const char* prefix, | 130 std::string DataUseMeasurement::GetHistogramName(const char* prefix, |
118 TrafficDirection dir) const { | 131 TrafficDirection dir, |
| 132 bool is_conn_cellular) const { |
119 AppState app_state = CurrentAppState(); | 133 AppState app_state = CurrentAppState(); |
120 bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular( | |
121 net::NetworkChangeNotifier::GetConnectionType()); | |
122 return base::StringPrintf( | 134 return base::StringPrintf( |
123 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", | 135 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", |
124 app_state == BACKGROUND ? "Background" : "Foreground", | 136 app_state == BACKGROUND ? "Background" : "Foreground", |
125 is_conn_cellular ? "Cellular" : "NotCellular"); | 137 is_conn_cellular ? "Cellular" : "NotCellular"); |
126 } | 138 } |
127 | 139 |
128 #if defined(OS_ANDROID) | 140 #if defined(OS_ANDROID) |
129 void DataUseMeasurement::OnApplicationStateChange( | 141 void DataUseMeasurement::OnApplicationStateChange( |
130 base::android::ApplicationState application_state) { | 142 base::android::ApplicationState application_state) { |
131 app_state_ = application_state; | 143 app_state_ = application_state; |
132 } | 144 } |
133 #endif | 145 #endif |
134 | 146 |
135 void DataUseMeasurement::ReportDataUsageServices( | 147 void DataUseMeasurement::ReportDataUsageServices( |
136 DataUseUserData::ServiceName service, | 148 DataUseUserData::ServiceName service, |
137 TrafficDirection dir, | 149 TrafficDirection dir, |
| 150 bool is_conn_cellular, |
138 int64_t message_size) const { | 151 int64_t message_size) const { |
139 RecordUMAHistogramCount( | 152 RecordUMAHistogramCount( |
140 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), | 153 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), |
141 message_size); | 154 message_size); |
142 if (message_size > 0) { | 155 if (message_size > 0) { |
143 IncreaseSparseHistogramByValue( | 156 IncreaseSparseHistogramByValue( |
144 GetHistogramName("DataUse.MessageSize.AllServices", dir), service, | 157 GetHistogramName("DataUse.MessageSize.AllServices", dir, |
145 message_size); | 158 is_conn_cellular), |
| 159 service, message_size); |
146 } | 160 } |
147 } | 161 } |
148 | 162 |
149 } // namespace data_use_measurement | 163 } // namespace data_use_measurement |
OLD | NEW |