Index: components/data_use_measurement/content/data_use_measurement.cc |
diff --git a/components/data_use_measurement/content/data_use_measurement.cc b/components/data_use_measurement/content/data_use_measurement.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..90d53d6222d744bd54427570ccfbbf2accf0997d |
--- /dev/null |
+++ b/components/data_use_measurement/content/data_use_measurement.cc |
@@ -0,0 +1,167 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/data_use_measurement/content/data_use_measurement.h" |
+ |
+#include "base/metrics/histogram.h" |
+#include "base/metrics/sparse_histogram.h" |
+#include "base/strings/stringprintf.h" |
+#include "content/public/browser/resource_request_info.h" |
+#include "net/base/network_change_notifier.h" |
+#include "net/base/upload_data_stream.h" |
+#include "net/http/http_response_headers.h" |
+#include "net/url_request/url_request.h" |
+ |
+namespace data_use_measurement { |
+ |
+namespace { |
+ |
+// Records the occurrence of |sample| in |name| histogram. Conventional UMA |
+// histograms are not used because the |name| is not static. |
+void RecordUMAHistogramCount(const std::string& name, int64_t sample) { |
mmenke
2015/09/04 20:51:45
#include <stdint.h>
#include <string>
amohammadkhan
2015/09/05 00:58:02
They are included in data_use_measurement.h. do I
|
+ base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( |
+ name, |
+ 1, // Minimum sample size in bytes. |
+ 1000000, // Maximum sample size in bytes. Should cover most of the |
+ // requests by services. |
+ 50, // Bucket count. |
+ base::HistogramBase::kUmaTargetedHistogramFlag); |
+ histogram_pointer->Add(sample); |
+} |
+ |
+// This function increases the value of |sample| bucket in |name| sparse |
+// histogram by |value|. Conventional UMA histograms are not used because the |
+// |name| is not static. |
mmenke
2015/09/04 20:51:45
nit: the |name| -> |name|
amohammadkhan
2015/09/05 00:58:02
Done.
|
+void RecordSparseHistogramWithValue(const std::string& name, |
mmenke
2015/09/04 20:51:46
This name is confusing - it sounds like it's recor
amohammadkhan
2015/09/05 00:58:02
Done.
|
+ int64_t sample, |
+ int64_t value) { |
+ base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( |
+ name, base::HistogramBase::kUmaTargetedHistogramFlag); |
+ histogram->AddCount(sample, value); |
+} |
+ |
+} // namespace |
+ |
+DataUseMeasurement::DataUseMeasurement() |
+#if defined(OS_ANDROID) |
+ : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), |
+ app_listener_(new base::android::ApplicationStatusListener( |
+ base::Bind(&DataUseMeasurement::OnApplicationStateChange, |
+ base::Unretained(this)))) |
+#endif |
+{ |
+} |
+ |
+DataUseMeasurement::~DataUseMeasurement() { |
+#if defined(OS_ANDROID) |
+ // Prevent notification between the destruction of the DataUseMeasurement |
+ // instance and the destruction of |app_listener_|. |
+ app_listener_.reset(); |
+#endif |
mmenke
2015/09/04 20:51:46
This explicit reset is not needed - it's a scoped_
amohammadkhan
2015/09/05 00:58:02
Done.
|
+} |
+ |
+void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) { |
+ const content::ResourceRequestInfo* info = |
+ content::ResourceRequestInfo::ForRequest(request); |
+ // Having |info| is the sign of a request for a web content from user. For now |
+ // we could add a condition to check ProcessType in info is |
+ // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming |
+ // PlzNavigate architecture. So just existence of |info| is verified, and the |
+ // current check should be compatible with upcoming changes in PlzNavigate. |
+ bool is_user_traffic = info != nullptr; |
+ |
+ // Counts rely on URLRequest::GetTotalReceivedBytes(), which does not include |
+ // the send path, network layer overhead, TLS overhead, and DNS. |
+ // TODO(amohammadkhan): Make these measured bytes more in line with number of |
+ // bytes in lower levels. |
+ int64_t request_body_bytes = 0; |
+ int64_t request_header_bytes = 0; |
+ int64_t total_upload_bytes = 0; |
+ if (request->has_upload()) |
+ request_body_bytes = request->get_upload()->size(); |
+ net::HttpRequestHeaders request_headers; |
+ if (request->GetFullRequestHeaders(&request_headers)) |
+ request_header_bytes = request_headers.ToString().length(); |
+ total_upload_bytes = request_body_bytes + request_header_bytes; |
+ int64_t total_received_bytes = request->GetTotalReceivedBytes(); |
+ |
+ ReportDataUsageGeneral( |
+ is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE, |
+ UPSTREAM, total_upload_bytes); |
+ ReportDataUsageGeneral( |
+ is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE, |
+ DOWNSTREAM, total_received_bytes); |
+ DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( |
+ request->GetUserData(DataUseUserData::kUserDataKey)); |
+ |
+ if (!is_user_traffic) { |
+ data_use_measurement::DataUseUserData::ServiceType service_type = |
+ attached_service_data |
+ ? attached_service_data->service_type() |
+ : data_use_measurement::DataUseUserData::NOT_TAGGED; |
+ ReportDataUsageServices(service_type, UPSTREAM, total_upload_bytes); |
+ ReportDataUsageServices(service_type, DOWNSTREAM, total_received_bytes); |
+ } |
+} |
+ |
+#if defined(OS_ANDROID) |
+void DataUseMeasurement::OnApplicationStateChange( |
+ base::android::ApplicationState application_state) { |
+ app_state_ = application_state; |
+} |
+#endif |
+ |
+#if defined(OS_ANDROID) |
+void DataUseMeasurement::OnApplicationStateChangeForTesting( |
+ base::android::ApplicationState application_state) { |
+ app_state_ = application_state; |
+} |
+#endif |
+ |
+DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() { |
+#if defined(OS_ANDROID) |
+ if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
+ return BACKGROUND; |
+#endif |
+ // If the OS is not Android, all the requests are considered Foreground. |
+ return FOREGROUND; |
+} |
+ |
+const std::string DataUseMeasurement::GetSuffixForHistogramName( |
+ TrafficDirection dir) { |
+ AppState app_state = CurrentAppState(); |
+ bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular( |
+ net::NetworkChangeNotifier::GetConnectionType()); |
+ return base::StringPrintf( |
+ ".%s.%s.%s", dir == UPSTREAM ? "Upstream" : "Downstream", |
+ app_state == BACKGROUND ? "Background" : "Foreground", |
+ is_conn_cellular ? "Cellular" : "NotCellular"); |
+} |
+ |
+void DataUseMeasurement::ReportDataUsageServices( |
+ data_use_measurement::DataUseUserData::ServiceType service, |
+ TrafficDirection dir, |
+ int64_t message_size) { |
+ RecordUMAHistogramCount( |
+ "DataUse.MessageSize." + |
+ std::string(DataUseUserData::GetServiceName(service)), |
+ message_size); |
+ if (message_size > 0) { |
+ RecordSparseHistogramWithValue( |
+ "DataUse.MessageSize.AllServices" + GetSuffixForHistogramName(dir), |
+ service, message_size); |
+ } |
+} |
+ |
+void DataUseMeasurement::ReportDataUsageGeneral(RequestInitiator service_type, |
+ TrafficDirection dir, |
+ int64_t message_size) { |
+ std::string histogram_name = base::StringPrintf( |
+ "DataUse.TrafficSize.%s%s", |
+ service_type == REQUEST_INITIATOR_USER ? "User" : "System", |
+ GetSuffixForHistogramName(dir).c_str()); |
+ RecordUMAHistogramCount(histogram_name, message_size); |
+} |
+ |
+} // namespace data_use_measurement |