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

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

Issue 2462983003: Move data use measurement to DataUseNetworkDelegate (Closed)
Patch Set: Rebased and fixed nits Created 4 years, 1 month 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 #ifndef COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_
6 #define COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_
7
8 #include <stdint.h>
9
10 #include <memory>
11 #include <string>
12
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "base/time/time.h"
16 #include "build/build_config.h"
17 #include "components/data_use_measurement/core/data_use_user_data.h"
18 #include "components/metrics/data_use_tracker.h"
19
20 #if defined(OS_ANDROID)
21 #include "base/android/application_status_listener.h"
22 #endif
23
24 class GURL;
25
26 namespace net {
27 class URLRequest;
28 }
29
30 namespace data_use_measurement {
31
32 // Records the data use of user traffic and various services in UMA histograms.
33 // The UMA is broken down by network technology used (Wi-Fi vs cellular). On
34 // Android, the UMA is further broken down by whether the application was in the
35 // background or foreground during the request.
36 // TODO(amohammadkhan): Complete the layered architecture.
37 // http://crbug.com/527460
38 class DataUseMeasurement {
39 public:
40 explicit DataUseMeasurement(
41 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder);
42 ~DataUseMeasurement();
43
44 // Called before a request is sent.
45 void OnBeforeURLRequest(net::URLRequest* request);
46
47 // Called right after a redirect response code was received for |request|.
48 void OnBeforeRedirect(const net::URLRequest& request,
49 const GURL& new_location);
50
51 // Called when data is received or sent on the network, respectively.
52 void OnNetworkBytesReceived(const net::URLRequest& request,
53 int64_t bytes_received);
54 void OnNetworkBytesSent(const net::URLRequest& request, int64_t bytes_sent);
55
56 // Indicates that |request| has been completed or failed.
57 void OnCompleted(const net::URLRequest& request, bool started);
58
59 // Returns true if the URLRequest |request| is initiated by user traffic.
60 static bool IsUserInitiatedRequest(const net::URLRequest& request);
61
62 #if defined(OS_ANDROID)
63 // This function should just be used for testing purposes. A change in
64 // application state can be simulated by calling this function.
65 void OnApplicationStateChangeForTesting(
66 base::android::ApplicationState application_state);
67 #endif
68
69 private:
70 friend class DataUseMeasurementTest;
71 FRIEND_TEST_ALL_PREFIXES(DataUseMeasurementTest,
72 TimeOfBackgroundDownstreamBytes);
73
74 // Specifies that data is received or sent, respectively.
75 enum TrafficDirection { DOWNSTREAM, UPSTREAM };
76
77 // Returns the current application state (Foreground or Background). It always
78 // returns Foreground if Chrome is not running on Android.
79 DataUseUserData::AppState CurrentAppState() const;
80
81 // Makes the full name of the histogram. It is made from |prefix| and suffix
82 // which is made based on network and application status. suffix is a string
83 // representing whether the data use was on the send ("Upstream") or receive
84 // ("Downstream") path, whether the app was in the "Foreground" or
85 // "Background", and whether a "Cellular" or "WiFi" network was use. For
86 // example, "Prefix.Upstream.Foreground.Cellular" is a possible output.
87 // |app_state| indicates the app state which can be foreground, background, or
88 // unknown.
89 std::string GetHistogramName(const char* prefix,
90 TrafficDirection dir,
91 DataUseUserData::AppState app_state,
92 bool is_connection_cellular) const;
93
94 #if defined(OS_ANDROID)
95 // Called whenever the application transitions from foreground to background
96 // and vice versa.
97 void OnApplicationStateChange(
98 base::android::ApplicationState application_state);
99
100 // Records the count of bytes received and sent by Chrome on the network as
101 // reported by the operating system.
102 void MaybeRecordNetworkBytesOS();
103 #endif
104
105 // Records the data use of the |request|, thus |request| must be non-null.
106 // |dir| is the direction (which is upstream or downstream) and |bytes| is the
107 // number of bytes in the direction.
108 void ReportDataUseUMA(const net::URLRequest& request,
109 TrafficDirection dir,
110 int64_t bytes);
111
112 // Updates the data use of the |request|, thus |request| must be non-null.
113 void UpdateDataUsePrefs(const net::URLRequest& request) const;
114
115 // A helper function used to record data use of services. It gets the size of
116 // exchanged message, its direction (which is upstream or downstream) and
117 // reports to two histogram groups. DataUse.MessageSize.ServiceName and
118 // DataUse.Services.{Dimensions}. In the second one, services are buckets.
119 // |app_state| indicates the app state which can be foreground, background, or
120 // unknown.
121 void ReportDataUsageServices(
122 data_use_measurement::DataUseUserData::ServiceName service,
123 TrafficDirection dir,
124 DataUseUserData::AppState app_state,
125 bool is_connection_cellular,
126 int64_t message_size) const;
127
128 // Callback for updating data use prefs.
129 // TODO(rajendrant): If a similar mechanism would need be used for components
130 // other than metrics, then the better approach would be to refactor this
131 // class to support registering arbitrary observers. crbug.com/601185
132 metrics::UpdateUsagePrefCallbackType metrics_data_use_forwarder_;
133
134 #if defined(OS_ANDROID)
135 // Application listener store the last known state of the application in this
136 // field.
137 base::android::ApplicationState app_state_;
138
139 // ApplicationStatusListener used to monitor whether the application is in the
140 // foreground or in the background. It is owned by DataUseMeasurement.
141 std::unique_ptr<base::android::ApplicationStatusListener> app_listener_;
142
143 // Number of bytes received and sent by Chromium as reported by the operating
144 // system when it was last queried for traffic statistics. Set to 0 if the
145 // operating system was never queried.
146 int64_t rx_bytes_os_;
147 int64_t tx_bytes_os_;
148
149 // Number of bytes received and sent by Chromium as reported by the network
150 // delegate since the operating system was last queried for traffic
151 // statistics.
152 int64_t bytes_transferred_since_last_traffic_stats_query_;
153
154 // The time at which Chromium app state changed to background. Can be null if
155 // app is not in background.
156 base::TimeTicks last_app_background_time_;
157
158 // True if app is in background and first network read has not yet happened.
159 bool no_reads_since_background_;
160 #endif
161
162 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurement);
163 };
164
165 } // namespace data_use_measurement
166
167 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698