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 <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | |
14 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
16 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
17 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
18 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
19 | 20 |
20 namespace net { | 21 namespace net { |
21 class URLRequest; | 22 class URLRequest; |
22 } | 23 } |
23 | 24 |
24 namespace data_usage { | 25 namespace data_usage { |
26 | |
27 class DataUseAnnotator; | |
25 struct DataUse; | 28 struct DataUse; |
26 | 29 |
27 // Class that collects and aggregates network usage, reporting the usage to | 30 // Class that collects and aggregates network usage, reporting the usage to |
28 // observers. Should only be used on the IO thread. | 31 // observers. Should only be used on the IO thread. |
29 class DataUseAggregator | 32 class DataUseAggregator |
30 : public net::NetworkChangeNotifier::ConnectionTypeObserver { | 33 : public net::NetworkChangeNotifier::ConnectionTypeObserver { |
31 public: | 34 public: |
32 class Observer { | 35 class Observer { |
33 public: | 36 public: |
34 virtual ~Observer() {} | 37 virtual ~Observer() {} |
35 // Each of the elements of |data_use_sequence| are guaranteed to be | 38 // Each of the elements of |data_use_sequence| are guaranteed to be |
36 // non-NULL. | 39 // non-NULL. |
37 virtual void OnDataUse( | 40 virtual void OnDataUse( |
38 const std::vector<const DataUse*>& data_use_sequence) = 0; | 41 const std::vector<const DataUse*>& data_use_sequence) = 0; |
39 }; | 42 }; |
40 | 43 |
41 DataUseAggregator(); | 44 // Construct a new DataUseAggregator with the given |annotator|. A NULL |
bengr
2015/11/05 00:00:38
"Constructs"
sclittle
2015/11/05 01:22:48
Done.
| |
45 // annotator will be treated as a no-op annotator. | |
46 explicit DataUseAggregator(scoped_ptr<DataUseAnnotator> annotator); | |
42 ~DataUseAggregator() override; | 47 ~DataUseAggregator() override; |
43 | 48 |
44 void AddObserver(Observer* observer); | 49 void AddObserver(Observer* observer); |
45 void RemoveObserver(Observer* observer); | 50 void RemoveObserver(Observer* observer); |
46 | 51 |
47 // Virtual for testing. | 52 // Virtual for testing. |
48 virtual void ReportDataUse(const net::URLRequest& request, | 53 virtual void ReportDataUse(net::URLRequest* request, |
49 int32_t tab_id, | |
50 int64_t tx_bytes, | 54 int64_t tx_bytes, |
51 int64_t rx_bytes); | 55 int64_t rx_bytes); |
52 | 56 |
53 // Account for off-the-record data use. This usage is only kept track of here | 57 // Account for off-the-record data use. This usage is only kept track of here |
54 // so that it can be taken out of any amortized data usage calculations, and a | 58 // so that it can be taken out of any amortized data usage calculations, and a |
55 // per-request breakdown of off-the-record data usage will never leave the | 59 // per-request breakdown of off-the-record data usage will never leave the |
56 // DataUseAggregator. | 60 // DataUseAggregator. |
57 // Virtual for testing. | 61 // Virtual for testing. |
58 virtual void ReportOffTheRecordDataUse(int64_t tx_bytes, int64_t rx_bytes); | 62 virtual void ReportOffTheRecordDataUse(int64_t tx_bytes, int64_t rx_bytes); |
59 | 63 |
60 base::WeakPtr<DataUseAggregator> GetWeakPtr(); | 64 base::WeakPtr<DataUseAggregator> GetWeakPtr(); |
61 | 65 |
62 protected: | 66 protected: |
63 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. | 67 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. |
64 // Protected for testing. | 68 // Protected for testing. |
65 void OnConnectionTypeChanged( | 69 void OnConnectionTypeChanged( |
66 net::NetworkChangeNotifier::ConnectionType type) override; | 70 net::NetworkChangeNotifier::ConnectionType type) override; |
67 | 71 |
68 // Protected for testing. | 72 // Protected for testing. |
69 void SetMccMncForTests(const std::string& mcc_mnc); | 73 void SetMccMncForTests(const std::string& mcc_mnc); |
70 | 74 |
71 private: | 75 private: |
76 // Append |data_use| to the buffer of unreported data use and prepare to | |
bengr
2015/11/05 00:00:38
"Appends", "prepares"
sclittle
2015/11/05 01:22:48
Done.
| |
77 // notify observers. | |
78 void AppendDataUse(scoped_ptr<DataUse> data_use); | |
79 | |
72 // Flush any buffered data use and notify observers. | 80 // Flush any buffered data use and notify observers. |
73 void FlushBufferedDataUse(); | 81 void FlushBufferedDataUse(); |
74 | 82 |
75 base::ThreadChecker thread_checker_; | 83 base::ThreadChecker thread_checker_; |
84 scoped_ptr<DataUseAnnotator> annotator_; | |
76 base::ObserverList<Observer> observer_list_; | 85 base::ObserverList<Observer> observer_list_; |
77 | 86 |
78 // Buffer of unreported data use. | 87 // Buffer of unreported data use. |
79 ScopedVector<DataUse> buffered_data_use_; | 88 ScopedVector<DataUse> buffered_data_use_; |
80 | 89 |
81 // Current connection type as notified by NetworkChangeNotifier. | 90 // Current connection type as notified by NetworkChangeNotifier. |
82 net::NetworkChangeNotifier::ConnectionType connection_type_; | 91 net::NetworkChangeNotifier::ConnectionType connection_type_; |
83 | 92 |
84 // MCC+MNC (mobile country code + mobile network code) of the current SIM | 93 // MCC+MNC (mobile country code + mobile network code) of the current SIM |
85 // provider. Set to empty string if SIM is not present. |mcc_mnc_| is set | 94 // provider. Set to empty string if SIM is not present. |mcc_mnc_| is set |
(...skipping 10 matching lines...) Expand all Loading... | |
96 bool is_flush_pending_; | 105 bool is_flush_pending_; |
97 | 106 |
98 base::WeakPtrFactory<DataUseAggregator> weak_ptr_factory_; | 107 base::WeakPtrFactory<DataUseAggregator> weak_ptr_factory_; |
99 | 108 |
100 DISALLOW_COPY_AND_ASSIGN(DataUseAggregator); | 109 DISALLOW_COPY_AND_ASSIGN(DataUseAggregator); |
101 }; | 110 }; |
102 | 111 |
103 } // namespace data_usage | 112 } // namespace data_usage |
104 | 113 |
105 #endif // COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_ | 114 #endif // COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_ |
OLD | NEW |