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 #ifndef COMPONENTS_DATA_USAGE_ANDROID_TRAFFIC_STATS_AMORTIZER_H_ | |
6 #define COMPONENTS_DATA_USAGE_ANDROID_TRAFFIC_STATS_AMORTIZER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/memory/linked_ptr.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/threading/thread_checker.h" | |
18 #include "base/time/time.h" | |
19 #include "components/data_usage/core/data_use_amortizer.h" | |
20 | |
21 namespace base { | |
22 class TickClock; | |
23 class Timer; | |
24 } | |
25 | |
26 namespace data_usage { | |
27 | |
28 struct DataUse; | |
29 | |
30 namespace android { | |
31 | |
32 // Class that uses Android TrafficStats to amortize any unincluded overhead | |
33 // (e.g. network layer, TLS, DNS) into the data usage reported by the network | |
34 // stack. Should only be used on the IO thread. Since TrafficStats measurements | |
35 // are global for the entire application, a TrafficStatsAmortizer should be | |
36 // notified of every byte possible, or else it might mistakenly classify the | |
37 // corresponding additional TrafficStats bytes for those as overhead. | |
38 // TrafficStats have been available in Android since API level 8 (Android 2.2). | |
bengr
2015/11/11 22:52:53
nit: "The TrafficStats API has..."
sclittle
2015/11/11 22:56:25
Done.
| |
39 class TrafficStatsAmortizer : public DataUseAmortizer { | |
40 public: | |
41 TrafficStatsAmortizer(); | |
42 ~TrafficStatsAmortizer() override; | |
43 | |
44 // Amortizes any unincluded network bytes overhead for |data_use| into | |
45 // |data_use|, and passes the updated |data_use| to |callback| once | |
46 // amortization is complete. | |
47 void AmortizeDataUse(scoped_ptr<DataUse> data_use, | |
48 const AmortizationCompleteCallback& callback) override; | |
49 | |
50 // Notifies the amortizer that some extra bytes have been transferred that | |
51 // aren't associated with any DataUse objects (e.g. off-the-record traffic), | |
52 // so that the TrafficStatsAmortizer can avoid mistakenly counting these bytes | |
53 // as overhead. | |
54 void OnExtraBytes(int64_t extra_tx_bytes, int64_t extra_rx_bytes) override; | |
55 | |
56 base::WeakPtr<TrafficStatsAmortizer> GetWeakPtr(); | |
57 | |
58 protected: | |
59 // Constructor for testing purposes, allowing for tests to take full control | |
60 // over the timing of the TrafficStatsAmortizer and the byte counts returned | |
61 // from TrafficStats. |traffic_stats_query_timer| must not be a repeating | |
62 // timer. | |
63 TrafficStatsAmortizer(scoped_ptr<base::TickClock> tick_clock, | |
64 scoped_ptr<base::Timer> traffic_stats_query_timer, | |
65 const base::TimeDelta& traffic_stats_query_delay, | |
66 const base::TimeDelta& max_amortization_delay, | |
67 size_t max_data_use_buffer_size); | |
68 | |
69 // Queries the total transmitted and received bytes for the application from | |
70 // TrafficStats. Stores the byte counts in |tx_bytes| and |rx_bytes| | |
71 // respectively and returns true if both values are available from | |
72 // TrafficStats, otherwise returns false. |tx_bytes| and |rx_bytes| must not | |
73 // be NULL. | |
74 // Virtual for testing. | |
75 virtual bool QueryTrafficStats(int64_t* tx_bytes, int64_t* rx_bytes) const; | |
76 | |
77 private: | |
78 // Adds |tx_bytes| and |rx_bytes| as data usage that should not be counted as | |
79 // overhead (i.e. bytes from DataUse objects and extra bytes reported to this | |
80 // TrafficStatsAmortizer), and schedules amortization to happen later. | |
81 void AddPreAmortizationBytes(int64_t tx_bytes, int64_t rx_bytes); | |
82 | |
83 // Amortizes any additional overhead from TrafficStats byte counts into the | |
84 // |buffered_data_use_|, then passes the post-amortization DataUse objects to | |
85 // their respective callbacks, flushing |buffered_data_use_|. Overhead is | |
86 // calculated as the difference between the TrafficStats byte counts and the | |
87 // pre-amortization byte counts. | |
88 void AmortizeNow(); | |
89 | |
90 base::ThreadChecker thread_checker_; | |
91 | |
92 // TickClock for determining the current time tick. | |
93 scoped_ptr<base::TickClock> tick_clock_; | |
94 | |
95 // One-shot timer used to wait a short time after receiving DataUse before | |
96 // querying TrafficStats, to give TrafficStats time to update and give the | |
97 // network stack time to finish reporting multiple DataUse objects that happen | |
98 // in rapid succession. This must not be a repeating timer. | |
99 // |traffic_stats_query_timer_| is owned as a scoped_ptr so that fake timers | |
100 // can be passed in for tests. | |
101 scoped_ptr<base::Timer> traffic_stats_query_timer_; | |
102 | |
103 // The delay between data usage being reported to the amortizer before | |
104 // querying TrafficStats. Used with |traffic_stats_query_timer_|. | |
105 const base::TimeDelta traffic_stats_query_delay_; | |
106 | |
107 // The maximum amount of time that the TrafficStatsAmortizer is allowed to | |
108 // spend waiting to perform amortization. Used with | |
109 // |traffic_stats_query_timer_|. | |
110 const base::TimeDelta max_amortization_delay_; | |
111 | |
112 // The maximum allowed size of the |buffered_data_use_| buffer, to prevent the | |
113 // buffer from hogging memory. | |
114 const size_t max_data_use_buffer_size_; | |
115 | |
116 // Indicates whether or not the TrafficStatsAmortizer currently has | |
117 // pre-amortization bytes waiting for amortization to be performed. | |
118 bool is_amortization_in_progress_; | |
119 | |
120 // The time when the first pre-amortization bytes for the current amortization | |
121 // run were given to this TrafficStatsAmortizer. | |
122 base::TimeTicks current_amortization_run_start_time_; | |
123 | |
124 // Buffer of pre-amortization data use that has accumulated since the last | |
125 // time amortization was performed, paired with the callbacks for each DataUse | |
126 // object. Only the |buffered_data_use_| may hold linked_ptrs to the DataUse | |
127 // objects, so that these linked_ptrs can be released later. | |
128 std::vector<std::pair<linked_ptr<DataUse>, AmortizationCompleteCallback>> | |
129 buffered_data_use_; | |
130 | |
131 // Indicates if TrafficStats byte counts were available during the last time | |
132 // amortization was performed. | |
133 bool are_last_amortization_traffic_stats_available_; | |
134 | |
135 // The total transmitted bytes according to TrafficStats during the last time | |
136 // amortization was performed, if they were available. | |
137 int64_t last_amortization_traffic_stats_tx_bytes_; | |
138 | |
139 // The total received bytes according to TrafficStats during the last time | |
140 // amortization was performed, if they were available. | |
141 int64_t last_amortization_traffic_stats_rx_bytes_; | |
142 | |
143 // Total pre-amortization transmitted bytes since the last time amortization | |
144 // was performed, including bytes from |buffered_data_use_| and any extra | |
145 // bytes that were added. | |
146 int64_t pre_amortization_tx_bytes_; | |
147 | |
148 // Total pre-amortization received bytes since the last time amortization was | |
149 // performed, including bytes from |buffered_data_use_| and any extra bytes | |
150 // that were added. | |
151 int64_t pre_amortization_rx_bytes_; | |
152 | |
153 base::WeakPtrFactory<TrafficStatsAmortizer> weak_ptr_factory_; | |
154 | |
155 DISALLOW_COPY_AND_ASSIGN(TrafficStatsAmortizer); | |
156 }; | |
157 | |
158 } // namespace android | |
159 | |
160 } // namespace data_usage | |
161 | |
162 #endif // COMPONENTS_DATA_USAGE_ANDROID_TRAFFIC_STATS_AMORTIZER_H_ | |
OLD | NEW |