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