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 #include "components/data_usage/core/data_use_aggregator.h" | |
6 | |
7 namespace data_usage { | |
8 | |
9 DataUseDelta::DataUseDelta() | |
10 : tab_id(-1), | |
11 connection_type(net::NetworkChangeNotifier::CONNECTION_UNKNOWN), | |
12 tx_bytes(0), | |
13 rx_bytes(0) {} | |
14 | |
15 DataUseAggregator::Observer::~Observer() {} | |
16 | |
17 // static | |
18 scoped_ptr<DataUseAggregator> DataUseAggregator::Create() { | |
19 scoped_ptr<DataUseAggregator> data_use_aggregator(new DataUseAggregator()); | |
20 return data_use_aggregator.Pass(); | |
21 } | |
22 | |
23 DataUseAggregator::~DataUseAggregator() { | |
24 // TODO(sclittle): Most observers are probably already destroyed by the time | |
25 // this destructor runs. Find some way to ensure that no data usage gets lost. | |
26 if (!buffered_data_use_deltas_.empty()) { | |
27 NotifyDataUse(buffered_data_use_deltas_); | |
28 buffered_data_use_deltas_.clear(); | |
29 } | |
30 } | |
31 | |
32 void DataUseAggregator::RegisterObserver(Observer* observer) { | |
33 auto insertion_result = observers_.insert(observer); | |
34 DCHECK(insertion_result.second) << "Duplicate observer registered."; | |
35 } | |
36 | |
37 void DataUseAggregator::UnregisterObserver(Observer* observer) { | |
38 auto observer_it = observers_.find(observer); | |
tbansal1
2015/10/01 20:30:43
Using ObserverList would be simpler and is probab
sclittle
2015/10/03 03:27:52
Done.
| |
39 DCHECK(observer_it != observers_.end()) | |
tbansal1
2015/10/01 20:30:43
This DCHECK might be compiled out. If iterator is
sclittle
2015/10/03 03:27:52
Removed this, since the ObserverList already handl
| |
40 << "Nonexistent observer unregistered."; | |
41 observers_.erase(observer_it); | |
42 } | |
43 | |
44 void DataUseAggregator::ReportDataUse(const GURL& url, | |
45 const GURL& first_party_for_cookies, | |
46 TabId tab_id, | |
47 int64_t tx_bytes, | |
48 int64_t rx_bytes) { | |
49 DataUseDelta data_use_delta; | |
50 data_use_delta.url = url; | |
bengr
2015/10/02 19:32:22
DataUseDelta could have a constructor for all of t
sclittle
2015/10/03 03:27:52
Done.
| |
51 data_use_delta.first_party_for_cookies = first_party_for_cookies; | |
52 data_use_delta.tab_id = tab_id; | |
53 data_use_delta.connection_type = | |
54 net::NetworkChangeNotifier::GetConnectionType(); | |
55 data_use_delta.tx_bytes = tx_bytes; | |
56 data_use_delta.rx_bytes = rx_bytes; | |
57 | |
58 // TODO(sclittle): Once actual buffering/aggregation is being done, consider | |
59 // combining deltas into previous deltas with the same keys. | |
60 buffered_data_use_deltas_.push_back(data_use_delta); | |
bengr
2015/10/02 19:32:22
Is there any limit to how much this class will buf
sclittle
2015/10/03 03:27:52
Right now, it doesn't buffer at all. We can impose
| |
61 | |
62 // TODO(sclittle): Amortize data use on supported platforms. | |
63 NotifyDataUse(buffered_data_use_deltas_); | |
64 buffered_data_use_deltas_.clear(); | |
65 } | |
66 | |
67 void DataUseAggregator::ReportOffTheRecordDataUse(int64_t tx_bytes, | |
68 int64_t rx_bytes) { | |
69 // TODO(sclittle): Once data usage amortization is implemented, keep track of | |
70 // the off-the-record bytes so that they can be taken out of the amortized | |
71 // data usage calculations if applicable. | |
72 } | |
73 | |
74 base::WeakPtr<DataUseAggregator> DataUseAggregator::GetWeakPtr() { | |
75 return weak_ptr_factory_.GetWeakPtr(); | |
76 } | |
77 | |
78 DataUseAggregator::DataUseAggregator() : weak_ptr_factory_(this) {} | |
79 | |
80 void DataUseAggregator::NotifyDataUse( | |
81 const std::vector<DataUseDelta>& data_use_deltas) { | |
82 for (Observer* observer : observers_) { | |
83 observer->OnDataUse(data_use_deltas); | |
84 } | |
85 } | |
86 | |
87 } // namespace data_usage | |
OLD | NEW |