| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_ | 5 #ifndef COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_ |
| 6 #define COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_ | 6 #define COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 15 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 16 #include "components/data_usage/core/data_use.h" | 17 #include "components/data_usage/core/data_use.h" |
| 18 #include "components/data_usage/core/data_use_amortizer.h" |
| 17 | 19 |
| 18 namespace net { | 20 namespace net { |
| 19 class URLRequest; | 21 class URLRequest; |
| 20 } | 22 } |
| 21 | 23 |
| 22 namespace data_usage { | 24 namespace data_usage { |
| 23 | 25 |
| 24 // Class that collects and aggregates network usage, reporting the usage to | 26 // Class that collects and aggregates network usage, reporting the usage to |
| 25 // observers. Should only be used on the IO thread. | 27 // observers. Should only be used on the IO thread. |
| 26 class DataUseAggregator { | 28 class DataUseAggregator { |
| 27 public: | 29 public: |
| 28 class Observer { | 30 class Observer { |
| 29 public: | 31 public: |
| 30 virtual ~Observer() {} | 32 virtual ~Observer() {} |
| 31 // TODO(sclittle): Consider performing some pre-aggregation on the data use. | |
| 32 virtual void OnDataUse(const std::vector<DataUse>& data_use_sequence) = 0; | 33 virtual void OnDataUse(const std::vector<DataUse>& data_use_sequence) = 0; |
| 33 }; | 34 }; |
| 34 | 35 |
| 35 DataUseAggregator(); | 36 explicit DataUseAggregator(scoped_ptr<DataUseAmortizer> data_use_amortizer); |
| 36 virtual ~DataUseAggregator(); | 37 virtual ~DataUseAggregator(); |
| 37 | 38 |
| 38 void AddObserver(Observer* observer); | 39 void AddObserver(Observer* observer); |
| 39 void RemoveObserver(Observer* observer); | 40 void RemoveObserver(Observer* observer); |
| 40 | 41 |
| 41 // Virtual for testing. | 42 // Virtual for testing. |
| 42 virtual void ReportDataUse(const net::URLRequest& request, | 43 virtual void ReportDataUse(const net::URLRequest& request, |
| 43 int32_t tab_id, | 44 int32_t tab_id, |
| 44 int64_t tx_bytes, | 45 int64_t tx_bytes, |
| 45 int64_t rx_bytes); | 46 int64_t rx_bytes); |
| 46 | 47 |
| 47 // Account for off-the-record data use. This usage is only kept track of here | 48 // Account for off-the-record data use. This usage is only kept track of here |
| 48 // so that it can be taken out of any amortized data usage calculations, and a | 49 // so that it can be taken out of any amortized data usage calculations, and a |
| 49 // per-request breakdown of off-the-record data usage will never leave the | 50 // per-request breakdown of off-the-record data usage will never leave the |
| 50 // DataUseAggregator. | 51 // DataUseAggregator. |
| 51 // Virtual for testing. | 52 // Virtual for testing. |
| 52 virtual void ReportOffTheRecordDataUse(int64_t tx_bytes, int64_t rx_bytes); | 53 virtual void ReportOffTheRecordDataUse(int64_t tx_bytes, int64_t rx_bytes); |
| 53 | 54 |
| 54 base::WeakPtr<DataUseAggregator> GetWeakPtr(); | 55 base::WeakPtr<DataUseAggregator> GetWeakPtr(); |
| 55 | 56 |
| 56 private: | 57 private: |
| 57 void FlushBufferedDataUse(); | 58 void FlushBufferedDataUse(); |
| 58 | 59 |
| 59 base::ThreadChecker thread_checker_; | 60 base::ThreadChecker thread_checker_; |
| 61 |
| 62 scoped_ptr<DataUseAmortizer> data_use_amortizer_; |
| 60 base::ObserverList<Observer> observer_list_; | 63 base::ObserverList<Observer> observer_list_; |
| 61 | 64 |
| 62 // Buffer of unreported data use. | 65 // Buffer of unreported data use. |
| 63 std::vector<DataUse> buffered_data_use_; | 66 std::vector<DataUse> buffered_data_use_; |
| 64 | 67 |
| 65 // The total amount of off-the-record data usage that has happened since the | 68 // The total amount of off-the-record data usage that has happened since the |
| 66 // last time the buffer was flushed. | 69 // last time the buffer was flushed. |
| 67 int64_t off_the_record_tx_bytes_since_last_flush_; | 70 int64_t off_the_record_tx_bytes_since_last_flush_; |
| 68 int64_t off_the_record_rx_bytes_since_last_flush_; | 71 int64_t off_the_record_rx_bytes_since_last_flush_; |
| 69 | 72 |
| 70 // Indicates if a FlushBufferedDataUse() callback has been posted to run later | 73 // Indicates if a FlushBufferedDataUse() callback has been posted to run later |
| 71 // on the IO thread. | 74 // on the IO thread. |
| 72 bool is_flush_pending_; | 75 bool is_flush_pending_; |
| 73 | 76 |
| 74 base::WeakPtrFactory<DataUseAggregator> weak_ptr_factory_; | 77 base::WeakPtrFactory<DataUseAggregator> weak_ptr_factory_; |
| 75 | 78 |
| 76 DISALLOW_COPY_AND_ASSIGN(DataUseAggregator); | 79 DISALLOW_COPY_AND_ASSIGN(DataUseAggregator); |
| 77 }; | 80 }; |
| 78 | 81 |
| 79 } // namespace data_usage | 82 } // namespace data_usage |
| 80 | 83 |
| 81 #endif // COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_ | 84 #endif // COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_ |
| OLD | NEW |