Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Side by Side Diff: components/data_usage/core/data_use_aggregator.h

Issue 1390993005: Amortize data usage using TrafficStats on Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@data_use_buffering
Patch Set: Fixed nit Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h" 16 #include "base/observer_list.h"
18 #include "base/threading/thread_checker.h" 17 #include "base/threading/thread_checker.h"
19 #include "net/base/network_change_notifier.h" 18 #include "net/base/network_change_notifier.h"
20 19
21 namespace net { 20 namespace net {
22 class URLRequest; 21 class URLRequest;
23 } 22 }
24 23
25 namespace data_usage { 24 namespace data_usage {
26 25
26 class DataUseAmortizer;
27 class DataUseAnnotator; 27 class DataUseAnnotator;
28 struct DataUse; 28 struct DataUse;
29 29
30 // Class that collects and aggregates network usage, reporting the usage to 30 // Class that collects and aggregates network usage, reporting the usage to
31 // observers. Should only be used on the IO thread. 31 // observers. Should only be used on the IO thread.
32 class DataUseAggregator 32 class DataUseAggregator
33 : public net::NetworkChangeNotifier::ConnectionTypeObserver { 33 : public net::NetworkChangeNotifier::ConnectionTypeObserver {
34 public: 34 public:
35 class Observer { 35 class Observer {
36 public: 36 public:
37 virtual ~Observer() {} 37 virtual ~Observer() {}
38 // 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
39 // non-NULL. 39 // non-NULL.
40 virtual void OnDataUse( 40 virtual void OnDataUse(
41 const std::vector<const DataUse*>& data_use_sequence) = 0; 41 const std::vector<const DataUse*>& data_use_sequence) = 0;
42 }; 42 };
43 43
44 // Constructs a new DataUseAggregator with the given |annotator|. A NULL 44 // Constructs a new DataUseAggregator with the given |annotator| and
45 // annotator will be treated as a no-op annotator. 45 // |amortizer|. A NULL |annotator| will be treated as a no-op annotator, and a
46 explicit DataUseAggregator(scoped_ptr<DataUseAnnotator> annotator); 46 // NULL |amortizer| will be treated as a no-op amortizer.
47 DataUseAggregator(scoped_ptr<DataUseAnnotator> annotator,
48 scoped_ptr<DataUseAmortizer> amortizer);
49
47 ~DataUseAggregator() override; 50 ~DataUseAggregator() override;
48 51
49 void AddObserver(Observer* observer); 52 void AddObserver(Observer* observer);
50 void RemoveObserver(Observer* observer); 53 void RemoveObserver(Observer* observer);
51 54
52 // Virtual for testing. 55 // Virtual for testing.
53 virtual void ReportDataUse(net::URLRequest* request, 56 virtual void ReportDataUse(net::URLRequest* request,
54 int64_t tx_bytes, 57 int64_t tx_bytes,
55 int64_t rx_bytes); 58 int64_t rx_bytes);
56 59
57 // Account for off-the-record data use. This usage is only kept track of here 60 // Account for off-the-record data use. This usage is only kept track of here
58 // so that it can be taken out of any amortized data usage calculations, and a 61 // so that it can be taken out of any amortized data usage calculations, and a
59 // per-request breakdown of off-the-record data usage will never leave the 62 // per-request breakdown of off-the-record data usage will never leave the
60 // DataUseAggregator. 63 // DataUseAggregator.
61 // Virtual for testing. 64 // Virtual for testing.
62 virtual void ReportOffTheRecordDataUse(int64_t tx_bytes, int64_t rx_bytes); 65 virtual void ReportOffTheRecordDataUse(int64_t tx_bytes, int64_t rx_bytes);
63 66
64 base::WeakPtr<DataUseAggregator> GetWeakPtr(); 67 base::WeakPtr<DataUseAggregator> GetWeakPtr();
65 68
66 protected: 69 protected:
67 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. 70 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation.
68 // Protected for testing. 71 // Protected for testing.
69 void OnConnectionTypeChanged( 72 void OnConnectionTypeChanged(
70 net::NetworkChangeNotifier::ConnectionType type) override; 73 net::NetworkChangeNotifier::ConnectionType type) override;
71 74
72 // Protected for testing. 75 // Protected for testing.
73 void SetMccMncForTests(const std::string& mcc_mnc); 76 void SetMccMncForTests(const std::string& mcc_mnc);
74 77
75 private: 78 private:
76 // Appends |data_use| to the buffer of unreported data use and prepares to 79 // Passes |data_use| to |amortizer_| if it exists, or calls
77 // notify observers. 80 // OnAmortizationComplete directly if |amortizer_| doesn't exist.
78 void AppendDataUse(scoped_ptr<DataUse> data_use); 81 void PassDataUseToAmortizer(scoped_ptr<DataUse> data_use);
79 82
80 // Flush any buffered data use and notify observers. 83 // Notifies observers with the data use from |amortized_data_use|.
81 void FlushBufferedDataUse(); 84 void OnAmortizationComplete(scoped_ptr<DataUse> amortized_data_use);
82 85
83 base::ThreadChecker thread_checker_; 86 base::ThreadChecker thread_checker_;
84 scoped_ptr<DataUseAnnotator> annotator_; 87 scoped_ptr<DataUseAnnotator> annotator_;
88 scoped_ptr<DataUseAmortizer> amortizer_;
85 base::ObserverList<Observer> observer_list_; 89 base::ObserverList<Observer> observer_list_;
86 90
87 // Buffer of unreported data use.
88 ScopedVector<DataUse> buffered_data_use_;
89
90 // Current connection type as notified by NetworkChangeNotifier. 91 // Current connection type as notified by NetworkChangeNotifier.
91 net::NetworkChangeNotifier::ConnectionType connection_type_; 92 net::NetworkChangeNotifier::ConnectionType connection_type_;
92 93
93 // MCC+MNC (mobile country code + mobile network code) of the current SIM 94 // MCC+MNC (mobile country code + mobile network code) of the current SIM
94 // provider. Set to empty string if SIM is not present. |mcc_mnc_| is set 95 // provider. Set to empty string if SIM is not present. |mcc_mnc_| is set
95 // even if the current active network is not a cellular network. 96 // even if the current active network is not a cellular network.
96 std::string mcc_mnc_; 97 std::string mcc_mnc_;
97 98
98 // The total amount of off-the-record data usage that has happened since the
99 // last time the buffer was flushed.
100 int64_t off_the_record_tx_bytes_since_last_flush_;
101 int64_t off_the_record_rx_bytes_since_last_flush_;
102
103 // Indicates if a FlushBufferedDataUse() callback has been posted to run later
104 // on the IO thread.
105 bool is_flush_pending_;
106
107 base::WeakPtrFactory<DataUseAggregator> weak_ptr_factory_; 99 base::WeakPtrFactory<DataUseAggregator> weak_ptr_factory_;
108 100
109 DISALLOW_COPY_AND_ASSIGN(DataUseAggregator); 101 DISALLOW_COPY_AND_ASSIGN(DataUseAggregator);
110 }; 102 };
111 103
112 } // namespace data_usage 104 } // namespace data_usage
113 105
114 #endif // COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_ 106 #endif // COMPONENTS_DATA_USAGE_CORE_DATA_USE_AGGREGATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698