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

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: Addressing reviewers' comments. 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>
Alexei Svitkine (slow) 2015/09/01 20:04:04 Nit: <> to "" Add an empty line after.
amohammadkhan 2015/09/01 23:02:43 Done. I think git cl format changes it to this way
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 in Bytes.
Alexei Svitkine (slow) 2015/09/01 20:04:04 Nit: No need to capitalize bytes. Same below.
amohammadkhan 2015/09/01 23:02:43 Done.
24 1000000, // Maximum sample size in Bytes. Should cover most of the
25 // requests by 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 }
Alexei Svitkine (slow) 2015/09/01 20:04:04 Nit: Add an empty line after this.
amohammadkhan 2015/09/01 23:02:43 Done.
62 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) {
63 bool is_user_traffic = false;
64
65 const content::ResourceRequestInfo* info =
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();
85 net::HttpRequestHeaders request_headers;
86 if (request->GetFullRequestHeaders(&request_headers))
87 request_header_bytes = request_headers.ToString().length();
88 total_upload_bytes = request_body_bytes + request_header_bytes;
89 int64_t total_received_bytes = request->GetTotalReceivedBytes();
90 ReportDataUsageGeneral(
91 is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE,
92 UPSTREAM, total_upload_bytes);
93 ReportDataUsageGeneral(
94 is_user_traffic ? REQUEST_INITIATOR_USER : REQUEST_INITIATOR_SERVICE,
95 DOWNSTREAM, total_received_bytes);
96 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>(
97 request->GetUserData(DataUseUserData::kUserDataKey));
98
99 if (!is_user_traffic) {
100 data_use_measurement::DataUseUserData::ServiceType service_type =
101 attached_service_data
102 ? attached_service_data->service_type()
103 : data_use_measurement::DataUseUserData::NOT_TAGGED;
104 ReportDataUsageServices(service_type, UPSTREAM, total_upload_bytes);
105 ReportDataUsageServices(service_type, DOWNSTREAM, total_received_bytes);
106 }
107 }
108
109 #if defined(OS_ANDROID)
110 void DataUseMeasurement::OnApplicationStateChange(
111 base::android::ApplicationState application_state) {
112 app_state_ = application_state;
113 }
114 #endif
115
116 #if defined(OS_ANDROID)
117 void DataUseMeasurement::OnApplicationStateChangeForTesting(
118 base::android::ApplicationState application_state) {
119 app_state_ = application_state;
120 }
121 #endif
122
123 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() {
124 #if defined(OS_ANDROID)
125 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES)
126 return BACKGROUND;
127 #endif
128 // If the OS is not Android, all the requests are considered Foreground.
129 return FOREGROUND;
130 }
131
132 std::string DataUseMeasurement::GetSuffixForHistogramName(
133 TrafficDirection dir) {
134 AppState app_state = CurrentAppState();
135 bool is_conn_cellular = net::NetworkChangeNotifier::IsConnectionCellular(
136 net::NetworkChangeNotifier::GetConnectionType());
137 return base::StringPrintf(
138 ".%s.%s.%s", dir == UPSTREAM ? "Upstream" : "Downstream",
139 app_state == BACKGROUND ? "Background" : "Foreground",
140 is_conn_cellular ? "Cellular" : "NotCellular");
141 }
142
143 void DataUseMeasurement::ReportDataUsageServices(
144 data_use_measurement::DataUseUserData::ServiceType service,
145 TrafficDirection dir,
146 int64_t message_size) {
147 std::string service_histogram_name =
148 base::StringPrintf("DataUse.MessageSize.%s", GetServiceName(service));
149 std::string combined_histogram_name =
150 "DataUse.Services" + GetSuffixForHistogramName(dir);
Alexei Svitkine (slow) 2015/09/01 20:04:04 How about DataUse.MessageSize.AllServices.*?
amohammadkhan 2015/09/01 23:02:43 Done.
151 RecordUMAHistogramCount(service_histogram_name, message_size);
152 if (message_size > 0) {
153 RecordSparseHistogramWithValue(combined_histogram_name, service,
154 message_size);
155 }
156 }
157
158 void DataUseMeasurement::ReportDataUsageGeneral(RequestInitiator service_type,
159 TrafficDirection dir,
160 int64_t message_size) {
161 std::string histogram_name = base::StringPrintf(
162 "DataUse.%s%s",
163 service_type == REQUEST_INITIATOR_USER ? "User" : "NotUser",
164 GetSuffixForHistogramName(dir).c_str());
165 RecordUMAHistogramCount(histogram_name, message_size);
166 }
167
168 } // namespace data_use_measurement
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698