Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: components/data_use_measurement/content/data_use_measurement.cc

Issue 1279543002: Support needed to measure user and service traffic in Chrome. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@NewHistogram
Patch Set: Minor updates in dep files. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/http/http_response_headers.h"
14 #include "net/url_request/url_request.h"
15
16 namespace data_use_measurement {
17
18 namespace {
19
20 // Records the occurrence of |sample| in |name| histogram. Conventional UMA
21 // histograms are not used because the |name| is not static.
22 void RecordUMAHistogramCount(const std::string& name, int64_t sample) {
23 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet(
24 name,
25 1, // Minimum sample size in bytes.
26 1000000, // Maximum sample size in bytes. Should cover most of the
27 // requests by services.
28 50, // Bucket count.
29 base::HistogramBase::kUmaTargetedHistogramFlag);
30 histogram_pointer->Add(sample);
31 }
32
33 // This function increases the value of |sample| bucket in |name| sparse
34 // histogram by |value|. Conventional UMA histograms are not used because |name|
35 // is not static.
36 void IncreaseSparseHistogramByValue(const std::string& name,
37 int64_t sample,
38 int64_t value) {
39 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet(
40 name, base::HistogramBase::kUmaTargetedHistogramFlag);
41 histogram->AddCount(sample, value);
42 }
43
44 } // namespace
45
46 DataUseMeasurement::DataUseMeasurement()
47 #if defined(OS_ANDROID)
48 : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES),
49 app_listener_(new base::android::ApplicationStatusListener(
50 base::Bind(&DataUseMeasurement::OnApplicationStateChange,
51 base::Unretained(this))))
52 #endif
53 {
54 }
55
56 DataUseMeasurement::~DataUseMeasurement(){};
57
58 void DataUseMeasurement::ReportDataUseUMA(
59 const net::URLRequest* request) const {
60 const content::ResourceRequestInfo* info =
61 content::ResourceRequestInfo::ForRequest(request);
62 // Having |info| is the sign of a request for a web content from user. For now
63 // we could add a condition to check ProcessType in info is
64 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming
65 // PlzNavigate architecture. So just existence of |info| is verified, and the
66 // current check should be compatible with upcoming changes in PlzNavigate.
67 bool is_user_traffic = info != nullptr;
68
69 // Counts rely on URLRequest::GetTotalReceivedBytes() and
70 // URLRequest::GetTotalSentBytes(), which does not include the send path,
71 // network layer overhead, TLS overhead, and DNS.
72 // TODO(amohammadkhan): Make these measured bytes more in line with number of
73 // bytes in lower levels.
74 int64_t total_upload_bytes = request->GetTotalSentBytes();
75 int64_t total_received_bytes = request->GetTotalReceivedBytes();
76
77 ReportDataUsageGeneral(
78 is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE,
79 UPSTREAM, total_upload_bytes);
80 ReportDataUsageGeneral(
81 is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE,
82 DOWNSTREAM, total_received_bytes);
83 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>(
84 request->GetUserData(DataUseUserData::kUserDataKey));
85
86 if (!is_user_traffic) {
87 DataUseUserData::ServiceName service_name =
88 attached_service_data ? attached_service_data->service_name()
89 : DataUseUserData::NOT_TAGGED;
90 ReportDataUsageServices(service_name, UPSTREAM, total_upload_bytes);
91 ReportDataUsageServices(service_name, DOWNSTREAM, total_received_bytes);
92 }
93 }
94
95 #if defined(OS_ANDROID)
96 void DataUseMeasurement::OnApplicationStateChangeForTesting(
97 base::android::ApplicationState application_state) {
98 app_state_ = application_state;
99 }
100 #endif
101
102 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const {
103 #if defined(OS_ANDROID)
104 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES)
105 return BACKGROUND;
106 #endif
107 // If the OS is not Android, all the requests are considered Foreground.
108 return FOREGROUND;
109 }
110
111 std::string DataUseMeasurement::GetSuffixForHistogramName(
112 TrafficDirection dir) const {
113 AppState app_state = CurrentAppState();
114 bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular(
115 net::NetworkChangeNotifier::GetConnectionType());
116 return base::StringPrintf(
117 ".%s.%s.%s", dir == UPSTREAM ? "Upstream" : "Downstream",
118 app_state == BACKGROUND ? "Background" : "Foreground",
119 is_conn_cellular ? "Cellular" : "NotCellular");
120 }
121
122 #if defined(OS_ANDROID)
123 void DataUseMeasurement::OnApplicationStateChange(
124 base::android::ApplicationState application_state) {
125 app_state_ = application_state;
126 }
127 #endif
128
129 void DataUseMeasurement::ReportDataUsageServices(
130 DataUseUserData::ServiceName service,
131 TrafficDirection dir,
132 int64_t message_size) const {
133 RecordUMAHistogramCount(
134 DataUseUserData::GetServiceNameAsString(service, "DataUse.MessageSize."),
135 message_size);
mmenke 2015/09/11 13:59:07 Sorry, put my comment at the wrong spot. See comm
amohammadkhan 2015/09/11 17:42:48 Done.
136 if (message_size > 0) {
137 IncreaseSparseHistogramByValue(
138 "DataUse.MessageSize.AllServices" + GetSuffixForHistogramName(dir),
139 service, message_size);
mmenke 2015/09/11 13:59:07 Was thinking of having this here: IncreaseSp
amohammadkhan 2015/09/11 17:42:48 Done.
140 }
141 }
142
143 void DataUseMeasurement::ReportDataUsageGeneral(
144 RequestInitiator request_initiator,
145 TrafficDirection dir,
146 int64_t message_size) const {
147 std::string histogram_name = base::StringPrintf(
148 "DataUse.TrafficSize.%s%s",
149 request_initiator == REQUEST_INITIATOR_USER ? "User" : "System",
150 GetSuffixForHistogramName(dir).c_str());
151 RecordUMAHistogramCount(histogram_name, message_size);
152 }
153
154 } // namespace data_use_measurement
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698