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