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 #include "components/data_usage/core/data_use_aggregator.h" | 5 #include "components/data_usage/core/data_use_aggregator.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "components/data_usage/core/data_use.h" | 10 #include "components/data_usage/core/data_use.h" |
11 #include "components/data_usage/core/data_use_amortizer.h" | |
12 #include "components/data_usage/core/data_use_annotator.h" | |
13 #include "net/base/load_timing_info.h" | 11 #include "net/base/load_timing_info.h" |
14 #include "net/base/network_change_notifier.h" | 12 #include "net/base/network_change_notifier.h" |
15 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
16 | 14 |
17 #if defined(OS_ANDROID) | 15 #if defined(OS_ANDROID) |
18 #include "net/android/network_library.h" | 16 #include "net/android/network_library.h" |
19 #endif // OS_ANDROID | 17 #endif // OS_ANDROID |
20 | 18 |
21 namespace data_usage { | 19 namespace data_usage { |
22 | 20 |
23 DataUseAggregator::DataUseAggregator(scoped_ptr<DataUseAnnotator> annotator, | 21 DataUseAggregator::DataUseAggregator(scoped_ptr<DataUseAnnotator> annotator, |
24 scoped_ptr<DataUseAmortizer> amortizer) | 22 scoped_ptr<DataUseAmortizer> amortizer) |
25 : annotator_(annotator.Pass()), | 23 : annotator_(annotator.Pass()), |
26 amortizer_(amortizer.Pass()), | 24 amortizer_(amortizer.Pass()), |
27 connection_type_(net::NetworkChangeNotifier::GetConnectionType()), | 25 connection_type_(net::NetworkChangeNotifier::GetConnectionType()), |
28 weak_ptr_factory_(this) { | 26 weak_ptr_factory_(this) { |
29 #if defined(OS_ANDROID) | 27 #if defined(OS_ANDROID) |
30 mcc_mnc_ = net::android::GetTelephonySimOperator(); | 28 mcc_mnc_ = net::android::GetTelephonySimOperator(); |
31 #endif // OS_ANDROID | 29 #endif // OS_ANDROID |
32 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 30 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
33 } | 31 } |
34 | 32 |
35 DataUseAggregator::~DataUseAggregator() { | 33 DataUseAggregator::~DataUseAggregator() { |
36 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 34 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 35 |
| 36 // Reset the callbacks to remove any WeakPtr references to |this| inside them. |
| 37 annotation_callback_.Reset(); |
| 38 amortization_callback_.Reset(); |
37 } | 39 } |
38 | 40 |
39 void DataUseAggregator::AddObserver(Observer* observer) { | 41 void DataUseAggregator::AddObserver(Observer* observer) { |
40 DCHECK(thread_checker_.CalledOnValidThread()); | 42 DCHECK(thread_checker_.CalledOnValidThread()); |
41 observer_list_.AddObserver(observer); | 43 observer_list_.AddObserver(observer); |
42 } | 44 } |
43 | 45 |
44 void DataUseAggregator::RemoveObserver(Observer* observer) { | 46 void DataUseAggregator::RemoveObserver(Observer* observer) { |
45 DCHECK(thread_checker_.CalledOnValidThread()); | 47 DCHECK(thread_checker_.CalledOnValidThread()); |
46 observer_list_.RemoveObserver(observer); | 48 observer_list_.RemoveObserver(observer); |
(...skipping 10 matching lines...) Expand all Loading... |
57 scoped_ptr<DataUse> data_use( | 59 scoped_ptr<DataUse> data_use( |
58 new DataUse(request->url(), load_timing_info.request_start, | 60 new DataUse(request->url(), load_timing_info.request_start, |
59 request->first_party_for_cookies(), -1 /* tab_id */, | 61 request->first_party_for_cookies(), -1 /* tab_id */, |
60 connection_type_, mcc_mnc_, tx_bytes, rx_bytes)); | 62 connection_type_, mcc_mnc_, tx_bytes, rx_bytes)); |
61 | 63 |
62 if (!annotator_) { | 64 if (!annotator_) { |
63 PassDataUseToAmortizer(data_use.Pass()); | 65 PassDataUseToAmortizer(data_use.Pass()); |
64 return; | 66 return; |
65 } | 67 } |
66 | 68 |
67 // TODO(sclittle): Instead of binding a new callback every time, re-use the | 69 // As an optimization, re-use a lazily initialized callback object for every |
68 // same callback every time. | 70 // call into |annotator_|, so that a new callback object doesn't have to be |
69 annotator_->Annotate( | 71 // allocated and held onto every time. |
70 request, data_use.Pass(), | 72 if (annotation_callback_.is_null()) { |
71 base::Bind(&DataUseAggregator::PassDataUseToAmortizer, GetWeakPtr())); | 73 annotation_callback_ = |
| 74 base::Bind(&DataUseAggregator::PassDataUseToAmortizer, GetWeakPtr()); |
| 75 } |
| 76 annotator_->Annotate(request, data_use.Pass(), annotation_callback_); |
72 } | 77 } |
73 | 78 |
74 void DataUseAggregator::ReportOffTheRecordDataUse(int64_t tx_bytes, | 79 void DataUseAggregator::ReportOffTheRecordDataUse(int64_t tx_bytes, |
75 int64_t rx_bytes) { | 80 int64_t rx_bytes) { |
76 DCHECK(thread_checker_.CalledOnValidThread()); | 81 DCHECK(thread_checker_.CalledOnValidThread()); |
77 if (!amortizer_) | 82 if (!amortizer_) |
78 return; | 83 return; |
79 | 84 |
80 amortizer_->OnExtraBytes(tx_bytes, rx_bytes); | 85 amortizer_->OnExtraBytes(tx_bytes, rx_bytes); |
81 } | 86 } |
(...skipping 20 matching lines...) Expand all Loading... |
102 | 107 |
103 void DataUseAggregator::PassDataUseToAmortizer(scoped_ptr<DataUse> data_use) { | 108 void DataUseAggregator::PassDataUseToAmortizer(scoped_ptr<DataUse> data_use) { |
104 DCHECK(thread_checker_.CalledOnValidThread()); | 109 DCHECK(thread_checker_.CalledOnValidThread()); |
105 DCHECK(data_use); | 110 DCHECK(data_use); |
106 | 111 |
107 if (!amortizer_) { | 112 if (!amortizer_) { |
108 OnAmortizationComplete(data_use.Pass()); | 113 OnAmortizationComplete(data_use.Pass()); |
109 return; | 114 return; |
110 } | 115 } |
111 | 116 |
112 // TODO(sclittle): Instead of binding a new callback every time, re-use the | 117 // As an optimization, re-use a lazily initialized callback object for every |
113 // same callback every time. | 118 // call into |amortizer_|, so that a new callback object doesn't have to be |
114 amortizer_->AmortizeDataUse( | 119 // allocated and held onto every time. |
115 data_use.Pass(), | 120 if (amortization_callback_.is_null()) { |
116 base::Bind(&DataUseAggregator::OnAmortizationComplete, GetWeakPtr())); | 121 amortization_callback_ = |
| 122 base::Bind(&DataUseAggregator::OnAmortizationComplete, GetWeakPtr()); |
| 123 } |
| 124 amortizer_->AmortizeDataUse(data_use.Pass(), amortization_callback_); |
117 } | 125 } |
118 | 126 |
119 void DataUseAggregator::OnAmortizationComplete( | 127 void DataUseAggregator::OnAmortizationComplete( |
120 scoped_ptr<DataUse> amortized_data_use) { | 128 scoped_ptr<DataUse> amortized_data_use) { |
121 DCHECK(thread_checker_.CalledOnValidThread()); | 129 DCHECK(thread_checker_.CalledOnValidThread()); |
122 | 130 |
123 // Pass Observers a sequence of const DataUse pointers instead of using the | 131 // Pass Observers a sequence of const DataUse pointers instead of using the |
124 // buffer directly in order to prevent Observers from modifying the DataUse | 132 // buffer directly in order to prevent Observers from modifying the DataUse |
125 // objects. | 133 // objects. |
126 // TODO(sclittle): Change the observer interface to take in a const DataUse&. | 134 // TODO(sclittle): Change the observer interface to take in a const DataUse&. |
127 std::vector<const DataUse*> const_sequence(1, amortized_data_use.get()); | 135 std::vector<const DataUse*> const_sequence(1, amortized_data_use.get()); |
128 DCHECK(!ContainsValue(const_sequence, nullptr)); | 136 DCHECK(!ContainsValue(const_sequence, nullptr)); |
129 FOR_EACH_OBSERVER(Observer, observer_list_, OnDataUse(const_sequence)); | 137 FOR_EACH_OBSERVER(Observer, observer_list_, OnDataUse(const_sequence)); |
130 } | 138 } |
131 | 139 |
132 } // namespace data_usage | 140 } // namespace data_usage |
OLD | NEW |