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

Side by Side Diff: net/url_request/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: 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.
bengr 2015/08/07 18:00:01 This and the other data_use_measurement.cc should
amohammadkhan 2015/08/11 22:04:36 Done.
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/hash.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/sparse_histogram.h"
10 #include "net/base/network_change_notifier.h"
11 #include "net/url_request/data_use_measurement.h"
12 #include "net/url_request/url_fetcher.h"
13
14 #if defined(OS_ANDROID)
15 #include "base/android/application_status_listener.h"
16 #endif
17
18 #define UMA_HISTOGRAM_SPARSE_SLOWLY_WITH_VALUE(name, sample, value) \
19 do { \
20 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( \
21 name, base::HistogramBase::kUmaTargetedHistogramFlag); \
22 histogram->AddCount(sample, value); \
23 } while (0)
24
25 // Copied from chromecast/base/metrics/cast_histograms.h
bengr 2015/08/07 18:00:01 More copying? Add a TODO to unify with cast. Are t
amohammadkhan 2015/08/11 22:04:36 Done.
26 #define STATIC_HISTOGRAM_POINTER_BLOCK_NO_CACHE( \
27 constant_histogram_name, histogram_add_method_invocation, \
28 histogram_factory_get_invocation) \
29 do { \
30 base::HistogramBase* histogram_pointer = histogram_factory_get_invocation; \
31 if (DCHECK_IS_ON()) \
32 histogram_pointer->CheckName(constant_histogram_name); \
33 histogram_pointer->histogram_add_method_invocation; \
34 } while (0)
35
36 // Copied from chromecast/base/metrics/cast_histograms.h
37 #define UMA_HISTOGRAM_CUSTOM_COUNTS_NO_CACHE(name, sample, min, max, \
38 bucket_count) \
39 STATIC_HISTOGRAM_POINTER_BLOCK_NO_CACHE( \
40 name, Add(sample), base::Histogram::FactoryGet( \
41 name, min, max, bucket_count, \
42 base::HistogramBase::kUmaTargetedHistogramFlag))
43
44 // NO_CACHE version of UMA_HISTOGRAM_CUSTOM_COUNT is used because the names of
45 // histograms may change when measurement is done for different services
46 // the maximum size of the request is about 5MB which may not be enough for
47 // large contents.
48 #define TRAFFIC_MEASUREMENT_UMA_HISTOGRAM(name, sample) \
49 UMA_HISTOGRAM_CUSTOM_COUNTS_NO_CACHE(name, sample, 1, 5000000, 50)
50
51 namespace {
52
53 // Enumerates the direction of data. Specifies the measured data was sent or
bengr 2015/08/07 18:00:01 Remove the first sentence.
amohammadkhan 2015/08/11 22:04:36 Done.
54 // received data
55 enum TrafficDirection { DOWNLOAD, UPLOAD };
56
57 // Enumerates application state. For non android versions it is always
bengr 2015/08/07 18:00:01 Android.
amohammadkhan 2015/08/11 22:04:36 Done.
58 // foreground
bengr 2015/08/07 18:00:01 Add a period.
amohammadkhan 2015/08/11 22:04:36 Done.
59 enum AppState { BACKGROUND, FOREGROUND };
60
61 enum ConnectionType { CELLULAR, WIFI };
62
63 #if defined(OS_ANDROID)
64 // It seems it is not updated at the beginning, so if we start in background
65 // somehow, it might be buggy until an event happens.
bengr 2015/08/07 18:00:01 Be clearer about this.
amohammadkhan 2015/08/11 22:04:36 Done.
66 base::android::ApplicationState app_state =
67 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES;
68 base::android::ApplicationStatusListener* app_listener;
69 #endif
70
71 // A flag to signal we have just started and we do not have a listener so we
72 // need to make one.
73 bool listener_made = false;
74
75 #if defined(OS_ANDROID)
76 void OnApplicationStateChange(
77 base::android::ApplicationState application_state) {
78 app_state = application_state;
79 }
80 #endif
81
82 AppState CurrentAppState() {
83 #if defined(OS_ANDROID)
84 if (app_state == base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES)
85 return FOREGROUND;
86 else
87 return BACKGROUND;
88 #endif
89 // If the OS is not Android, all the requests are considered foreground so for
90 // fair comparison between foreground and background data use, -A histograms
91 // should be chosen in UMA.
92 return FOREGROUND;
93 }
94
95 ConnectionType CurrentConnectionType() {
96 if (net::NetworkChangeNotifier::IsConnectionCellular(
97 net::NetworkChangeNotifier::GetConnectionType())) {
98 return CELLULAR;
99 }
100 // All not cellular connections are considered Wifi.
101 return WIFI;
102 }
103
104 void ReportDataUsage(const std::string& service_name,
105 TrafficDirection dir,
106 int message_size) {
107 AppState current_app_state = CurrentAppState();
108 ConnectionType connType = CurrentConnectionType();
109 std::string first_suffix = "";
110 std::string second_suffix = "";
111 std::string third_suffix = "";
112 if (dir == UPLOAD) {
113 first_suffix = ".Download";
114 } else {
115 first_suffix = ".Upload";
116 }
117 if (current_app_state == BACKGROUND) {
118 second_suffix = ".Background";
119 } else {
120 second_suffix = ".Foreground";
121 }
122 if (connType == WIFI) {
123 third_suffix = ".Wifi";
124 } else {
125 third_suffix = ".Cell";
126 }
127 std::string serviceHistogramName = "DataUse.Service." + service_name;
128 std::string combinedHistogramName =
129 "DataUse.Services" + first_suffix + second_suffix + third_suffix;
130 UMA_HISTOGRAM_SPARSE_SLOWLY_WITH_VALUE(
131 combinedHistogramName, base::Hash(service_name), message_size);
132 TRAFFIC_MEASUREMENT_UMA_HISTOGRAM(serviceHistogramName, message_size);
133 }
134
135 } // namespace
136
137 void DataUseReport(const std::string& service_name,
138 const net::URLFetcher* fetcher) {
139 #if defined(OS_ANDROID)
140 if (!listener_made) {
141 listener_made = true;
142 app_listener = new base::android::ApplicationStatusListener(
143 base::Bind(&OnApplicationStateChange));
144 }
145 #endif
146 int total_upload_bytes = fetcher->GetBytesSentSize();
147 int total_received_bytes = fetcher->GetBytesReceivedSize();
148 ReportDataUsage(service_name, UPLOAD, total_upload_bytes);
149 ReportDataUsage(service_name, DOWNLOAD, total_received_bytes);
150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698