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

Side by Side Diff: components/data_use_measurement/core/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: Removing few commented lines. Created 5 years, 4 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/core/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 "components/data_use_measurement/content/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 namespace data_use_measurement {
24
25 // Records the occurrence of |sample| in |name| histogram. Conventional UMA
26 // histograms are not used because the |name| is not static.
27 void UMAHistogramRecorder(const std::string& name, int64_t sample) {
sclittle 2015/08/21 23:55:34 Name this function more like a verb, e.g. RecordUM
amohammadkhan 2015/08/24 23:18:47 Done.
28 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet(
29 name,
30 1, // Minimum sample size.
31 5000000, // Maximum sample size. Should cover most of the requests.
sclittle 2015/08/21 23:55:34 Why just 5 MB? Lots of fetches are more than 5 MB.
amohammadkhan 2015/08/24 23:18:48 I agree with you. What do you think is the good nu
sclittle 2015/08/24 23:59:34 After discussing offline, it seems like the servic
amohammadkhan 2015/08/26 22:28:41 Done.
32 50, // Bucket count.
33 base::HistogramBase::kUmaTargetedHistogramFlag);
34 histogram_pointer->Add(sample); \
sclittle 2015/08/21 23:55:34 remove backslash
amohammadkhan 2015/08/24 23:18:47 It is very odd that I don't see the backslash in m
35 }
36
37 // This function increases the value of |sample| bucket in |name| sparse
38 // histogram by |value|. Conventional UMA histograms are not used because the
39 // |name| is not static.
40 void SparseHistogramWithValue(const std::string& name, int64_t sample,
sclittle 2015/08/21 23:55:34 Put these helper functions that aren't part of the
amohammadkhan 2015/08/24 23:18:47 Done.
41 int64_t value)
42 {
43 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet(
44 name, base::HistogramBase::kUmaTargetedHistogramFlag);
45 histogram->AddCount(sample, value);
46 }
47
48 DataUseMeasurement::DataUseMeasurement()
49 #if defined(OS_ANDROID)
50 :app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES),
51 app_listener_ (base::Bind(&DataUseMeasurement::OnApplicationStateChange,
52 base::Unretained(this)))
53 #endif
54 {}
55
56 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest* request) {
57 bool user_traffic_flag = false;
58
59 const content::ResourceRequestInfo* info =
60 content::ResourceRequestInfo::ForRequest(request);
61 // Having |info| is the sign of a request for a web content from user. For now
62 // we could add a condition to check ProcessType in info is
63 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming
64 // PlzNavigate architecture. So just existence of |info| is verified, and the
65 // current check should be compatible with upcoming changes in PlzNavigate.
66 if (info) {
67 user_traffic_flag = true;
68 }
69
70 // These number won't be the number of bytes handed to the kernel because
71 // session layer framing and compression is not being accounted for.
72 // TODO(amohammadkhan): Make these measured bytes more in line with number of
73 // bytes in lower levels.
74 int64_t request_body_bytes = 0;
75 int64_t request_header_bytes = 0;
76 int64_t total_upload_bytes = 0;
77 if (request->has_upload())
78 request_body_bytes = request->get_upload()->size();
79 net::HttpRequestHeaders request_headers;
80 if (request->GetFullRequestHeaders(&request_headers))
81 request_header_bytes = request_headers.ToString().length();
82 total_upload_bytes = request_body_bytes + request_header_bytes;
83 int64_t total_received_bytes = request->GetTotalReceivedBytes();
84
85 ReportDataUsageURLRequest(user_traffic_flag ? USER_TRAFFIC : SERVICE_TRAFFIC,
86 UPLOAD, total_upload_bytes);
87 ReportDataUsageURLRequest(user_traffic_flag ? USER_TRAFFIC : SERVICE_TRAFFIC,
88 DOWNLOAD, total_received_bytes);
89 DataUseUserData* sData = reinterpret_cast<DataUseUserData*>(
sclittle 2015/08/21 23:55:34 Please be more descriptive than "sData", maybe "Se
amohammadkhan 2015/08/24 23:18:47 Done.
90 request->GetUserData(DataUseUserData::kUserDataKey));
91
92 if (sData) {
93 ReportDataUsage(sData->service_name(), UPLOAD, total_upload_bytes);
94 ReportDataUsage(sData->service_name(), DOWNLOAD, total_received_bytes);
95 }
96 }
97
98 #if defined(OS_ANDROID)
99 void DataUseMeasurement::OnApplicationStateChange(
100 base::android::ApplicationState application_state) {
101 app_state_ = application_state;
102 }
103 #endif
104
105 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() {
106 #if defined(OS_ANDROID)
107 if (app_state_ == base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES)
sclittle 2015/08/21 23:55:34 Reorganize this function to be like #if defined(O
amohammadkhan 2015/08/24 23:18:47 Done.
108 return FOREGROUND;
109 else
110 return BACKGROUND;
111 #endif
112 // If the OS is not Android, all the requests are considered Foreground.
113 return FOREGROUND;
114 }
115
116 DataUseMeasurement::ConnectionType DataUseMeasurement::CurrentConnectionType() {
117 if (net::NetworkChangeNotifier::IsConnectionCellular(
118 net::NetworkChangeNotifier::GetConnectionType())) {
119 return CELLULAR;
120 }
121 return NOT_CELLULAR;
122 }
123
124 std::string DataUseMeasurement::DimensionMaker(TrafficDirection dir) {
125 AppState app_state = CurrentAppState();
126 ConnectionType conn_type = CurrentConnectionType();
127 return base::StringPrintf(
128 ".%s.%s.%s", dir == UPLOAD ? "Upload" : "Download",
129 app_state == BACKGROUND ? "Background" : "Foreground",
130 conn_type == NOT_CELLULAR ? "NotCellular" : "Cellular");
131 }
132
133 void DataUseMeasurement::ReportDataUsage(const std::string& service_name,
134 TrafficDirection dir,
135 int64_t message_size) {
136 std::string serviceHistogramName = "DataUse.Service." + service_name;
137 std::string combinedHistogramName = "DataUse.Services" + DimensionMaker(dir);
138 UMAHistogramRecorder(serviceHistogramName, message_size);
139 if (message_size > 0) {
140 SparseHistogramWithValue(combinedHistogramName, base::Hash(service_name),
sclittle 2015/08/21 23:55:34 Instead of hashing the service name, which is depe
amohammadkhan 2015/08/24 23:18:48 Done.
141 message_size);
142 }
143 }
144
145 void DataUseMeasurement::ReportDataUsageURLRequest(
146 ChromeTrafficType service_type,
147 TrafficDirection dir,
148 int64_t message_size) {
149 std::string histogram_name = base::StringPrintf(
150 "DataUse.%s%s", service_type == USER_TRAFFIC ? "User" : "NotUser",
151 DimensionMaker(dir).c_str());
152 UMAHistogramRecorder(histogram_name, message_size);
153 }
154
155 } // data_use_measurement namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698