OLD | NEW |
---|---|
(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 <string> | |
6 | |
7 #include "base/metrics/histogram.h" | |
8 #include "chrome/browser/net/data_use_measurement.h" | |
9 #include "content/public/browser/resource_request_info.h" | |
10 #include "content/public/common/process_type.h" | |
11 #include "net/base/network_change_notifier.h" | |
12 #include "net/base/upload_data_stream.h" | |
13 #include "net/url_request/url_request.h" | |
14 | |
15 #if defined(OS_ANDROID) | |
16 #include "base/android/application_status_listener.h" | |
17 #endif | |
18 | |
19 // Copied from chromecast/base/metrics/cast_histograms.h | |
bengr
2015/08/07 18:00:01
Why are you copying code? Add a TODO to unify thes
amohammadkhan
2015/08/11 22:04:34
I didn't like copying myself too, but I didn't wan
| |
20 #define STATIC_HISTOGRAM_POINTER_BLOCK_NO_CACHE( \ | |
21 constant_histogram_name, histogram_add_method_invocation, \ | |
22 histogram_factory_get_invocation) \ | |
23 do { \ | |
24 base::HistogramBase* histogram_pointer = histogram_factory_get_invocation; \ | |
25 if (DCHECK_IS_ON()) \ | |
26 histogram_pointer->CheckName(constant_histogram_name); \ | |
27 histogram_pointer->histogram_add_method_invocation; \ | |
28 } while (0) | |
29 | |
30 // Copied from chromecast/base/metrics/cast_histograms.h | |
31 #define UMA_HISTOGRAM_CUSTOM_COUNTS_NO_CACHE(name, sample, min, max, \ | |
32 bucket_count) \ | |
33 STATIC_HISTOGRAM_POINTER_BLOCK_NO_CACHE(name, Add(sample), \ | |
34 base::Histogram::FactoryGet(name, min, max, bucket_count, \ | |
35 base::HistogramBase::kUmaTargetedHistogramFlag)) | |
36 | |
37 // NO_CACHE version of UMA_HISTOGRAM_CUSTOM_COUNT is used because the names of | |
bengr
2015/08/07 18:00:00
I can't parse this comment.
amohammadkhan
2015/08/11 22:04:35
Sorry. I tried to rephrase it. Please let me know
| |
38 // histograms may change when measurement is done for different services | |
39 // the maximum size of the request is about 5MB which may not be enough for | |
40 // large contents. | |
41 #define TRAFFIC_MEASUREMENT_UMA_HISTOGRAM(name, sample) \ | |
42 UMA_HISTOGRAM_CUSTOM_COUNTS_NO_CACHE(name, sample, 1, 5000000, 50) | |
43 | |
44 namespace { | |
45 | |
46 // Enumerate the direction of data. Specifies the measured data was sent or | |
bengr
2015/08/07 18:00:01
Rewrite comment:
// Specifies that data is receiv
amohammadkhan
2015/08/11 22:04:35
Done.
| |
47 // received data | |
48 enum TrafficDirection { | |
49 DOWNLOAD, | |
50 UPLOAD | |
51 }; | |
52 | |
53 enum AppState { | |
bengr
2015/08/07 18:00:00
// The state of the application. Only available on
amohammadkhan
2015/08/11 22:04:35
Done.
| |
54 BACKGROUND, | |
55 FOREGROUND | |
56 }; | |
57 | |
58 enum ConnectionType { | |
bengr
2015/08/07 18:00:01
The type of network connection that was used.
amohammadkhan
2015/08/11 22:04:35
Done.
| |
59 CELLULAR, | |
60 WIFI | |
bengr
2015/08/07 18:00:00
Should this be tri-state: CELLULAR, WIFI, OTHER?
amohammadkhan
2015/08/11 22:04:35
Basically I am checking isCellular, so I changed i
| |
61 }; | |
62 | |
63 enum ChromeServiceType { | |
bengr
2015/08/07 18:00:01
The reason a request was sent.
amohammadkhan
2015/08/11 22:04:35
Done.
| |
64 USER_TRAFFIC, | |
65 NOT_USER_TRAFFIC | |
bengr
2015/08/07 18:00:00
Maybe all this SERVICE_TRAFFIC.
amohammadkhan
2015/08/11 22:04:34
Done.
| |
66 }; | |
67 | |
68 #if defined(OS_ANDROID) | |
69 // It seems it is not updated at the beginning, so if we start in background | |
70 // somehow, it might be buggy until an event happens. | |
bengr
2015/08/07 18:00:00
Don't try to land buggy code. We should know for s
amohammadkhan
2015/08/11 22:04:35
I think I was over thinking. The Chrome should alw
| |
71 // These are for just android, do I need to add if here? | |
bengr
2015/08/07 18:00:01
Strange comment. Is it needed?
amohammadkhan
2015/08/11 22:04:35
Done.
| |
72 base::android::ApplicationState app_state = | |
73 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES; | |
74 base::android::ApplicationStatusListener* app_listener; | |
75 #endif | |
76 | |
77 bool listener_made = false; | |
bengr
2015/08/07 18:00:00
Don't use file scope variables. If you have state
amohammadkhan
2015/08/11 22:04:35
Done.
| |
78 | |
79 #if defined(OS_ANDROID) | |
80 void OnApplicationStateChange( | |
81 base::android::ApplicationState application_state) { | |
82 app_state = application_state; | |
83 } | |
84 #endif | |
85 | |
86 std::string MakeHistogramName(ChromeServiceType service_type) { | |
bengr
2015/08/07 18:00:00
Delete this function and see my comment on line 12
amohammadkhan
2015/08/11 22:04:35
Done.
| |
87 std::string histogram_prefix = "DataUse."; | |
88 switch (service_type) { | |
89 case USER_TRAFFIC: | |
90 return histogram_prefix + "UserData"; | |
91 case NOT_USER_TRAFFIC: | |
92 return histogram_prefix + "NotUserData"; | |
93 } | |
94 assert(false); | |
95 return histogram_prefix; | |
96 } | |
97 | |
98 AppState CurrentAppState() { | |
bengr
2015/08/07 18:00:01
AppState CurrentAppState() {
#if defined(OS_ANDROI
amohammadkhan
2015/08/11 22:04:35
I'm gonna use the similar approach to your propose
| |
99 #if defined(OS_ANDROID) | |
100 if (app_state == base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | |
bengr
2015/08/07 18:00:00
Is this really the right way to check?
amohammadkhan
2015/08/11 22:04:35
I think so. At least it is working well in my test
| |
101 return FOREGROUND; | |
102 else | |
103 return BACKGROUND; | |
104 #endif | |
105 // If the OS is not Android, all the requests are considered foreground so for | |
106 // fair comparison between foreground and background data use, -A histograms | |
107 // should be chosen in UMA. | |
108 return FOREGROUND; | |
109 } | |
110 | |
111 ConnectionType CurrentConnectionType() { | |
112 if (net::NetworkChangeNotifier::IsConnectionCellular( | |
113 net::NetworkChangeNotifier::GetConnectionType())) { | |
114 return CELLULAR; | |
115 } | |
116 return WIFI; | |
117 } | |
118 | |
119 void ReportDataUsage(ChromeServiceType service_type, | |
120 TrafficDirection dir, int message_size) { | |
121 AppState appState = CurrentAppState(); | |
122 ConnectionType connType = CurrentConnectionType(); | |
123 std::string first_suffix = ""; | |
124 std::string second_suffix = ""; | |
125 std::string third_suffix = ""; | |
126 if (dir == UPLOAD) { | |
bengr
2015/08/07 18:00:01
Remove curly braces. Also, I think you have the di
amohammadkhan
2015/08/11 22:04:35
Done.
| |
127 first_suffix = ".Download"; | |
128 } else { | |
129 first_suffix = ".Upload"; | |
130 } | |
131 if (appState == BACKGROUND) { | |
132 second_suffix = ".Background"; | |
133 } else { | |
134 second_suffix = ".Foreground"; | |
135 } | |
136 if (connType == WIFI) { | |
137 third_suffix = ".Wifi"; | |
138 } else { | |
139 third_suffix = ".Cell"; | |
140 } | |
141 std::string histogram_name = MakeHistogramName(service_type); | |
142 std::string final_histogram_name = histogram_name + first_suffix + | |
143 second_suffix + third_suffix; | |
144 TRAFFIC_MEASUREMENT_UMA_HISTOGRAM(final_histogram_name, message_size); | |
145 } | |
146 | |
147 } // namespace | |
148 | |
149 void ReportDataUsage(const net::URLRequest* request) { | |
150 #if defined(OS_ANDROID) | |
151 if (!listener_made) { | |
bengr
2015/08/07 18:00:01
What does listener_made mean?
amohammadkhan
2015/08/11 22:04:35
Removed in new version.
| |
152 listener_made = true; | |
153 app_listener = new base::android::ApplicationStatusListener( | |
154 base::Bind(&OnApplicationStateChange)); | |
155 } | |
156 #endif | |
157 bool renderer_flag = false; | |
158 const content::ResourceRequestInfo* info = | |
159 content::ResourceRequestInfo::ForRequest(request); | |
160 if (!info) { | |
bengr
2015/08/07 18:00:01
Do:
// All data from a renderer process is assume
amohammadkhan
2015/08/11 22:04:35
Done.
| |
161 // Requests without info are categorized as NOT_USER_TRAFFIC data | |
162 } else if (info->GetProcessType() == content::PROCESS_TYPE_RENDERER) { | |
163 // We assume all data from renderer process are USER_DATA | |
164 renderer_flag = true; | |
165 } | |
166 int request_body_bytes = 0; | |
167 int request_header_bytes = 0; | |
bengr
2015/08/07 18:00:01
Add a comment explaining that these won't be the n
amohammadkhan
2015/08/11 22:04:35
Done.
| |
168 int total_upload_bytes = 0; | |
169 if (request->has_upload()){ | |
170 request_body_bytes = request->get_upload()->size(); | |
171 } | |
172 net::HttpRequestHeaders request_headers; | |
173 if (request->GetFullRequestHeaders(&request_headers)) { | |
bengr
2015/08/07 18:00:01
Remove curly braces.
amohammadkhan
2015/08/11 22:04:35
Done.
| |
174 request_header_bytes = request_headers.ToString().length(); | |
175 } | |
176 total_upload_bytes = request_body_bytes + request_header_bytes; | |
bengr
2015/08/07 18:00:00
Remove extra space
amohammadkhan
2015/08/11 22:04:35
Done.
| |
177 int total_received_bytes = request->GetTotalReceivedBytes(); | |
178 if (renderer_flag) { | |
bengr
2015/08/07 18:00:00
Instead of having two cases, do:
ReportDataUsage(r
amohammadkhan
2015/08/11 22:04:35
Done.
| |
179 ReportDataUsage(USER_TRAFFIC, UPLOAD, total_upload_bytes); | |
180 ReportDataUsage(USER_TRAFFIC, DOWNLOAD, total_received_bytes); | |
181 } else { | |
182 ReportDataUsage(NOT_USER_TRAFFIC, UPLOAD, total_upload_bytes); | |
183 ReportDataUsage(NOT_USER_TRAFFIC, DOWNLOAD, total_received_bytes); | |
184 } | |
185 } | |
OLD | NEW |