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/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "components/data_usage/core/data_use.h" | 12 #include "components/data_usage/core/data_use.h" |
13 #include "net/base/load_timing_info.h" | 13 #include "net/base/load_timing_info.h" |
14 #include "net/base/network_change_notifier.h" | 14 #include "net/base/network_change_notifier.h" |
15 #include "net/url_request/url_request.h" | |
16 | 15 |
17 namespace data_usage { | 16 namespace data_usage { |
18 | 17 |
19 DataUseAggregator::DataUseAggregator() | 18 DataUseAggregator::DataUseAggregator() |
20 : off_the_record_tx_bytes_since_last_flush_(0), | 19 : off_the_record_tx_bytes_since_last_flush_(0), |
21 off_the_record_rx_bytes_since_last_flush_(0), | 20 off_the_record_rx_bytes_since_last_flush_(0), |
22 is_flush_pending_(false), | 21 is_flush_pending_(false), |
23 weak_ptr_factory_(this) {} | 22 weak_ptr_factory_(this) {} |
24 | 23 |
25 DataUseAggregator::~DataUseAggregator() {} | 24 DataUseAggregator::~DataUseAggregator() {} |
26 | 25 |
27 void DataUseAggregator::AddObserver(Observer* observer) { | 26 void DataUseAggregator::AddObserver(Observer* observer) { |
28 DCHECK(thread_checker_.CalledOnValidThread()); | 27 DCHECK(thread_checker_.CalledOnValidThread()); |
29 observer_list_.AddObserver(observer); | 28 observer_list_.AddObserver(observer); |
30 } | 29 } |
31 | 30 |
32 void DataUseAggregator::RemoveObserver(Observer* observer) { | 31 void DataUseAggregator::RemoveObserver(Observer* observer) { |
33 DCHECK(thread_checker_.CalledOnValidThread()); | 32 DCHECK(thread_checker_.CalledOnValidThread()); |
34 observer_list_.RemoveObserver(observer); | 33 observer_list_.RemoveObserver(observer); |
35 } | 34 } |
36 | 35 |
37 void DataUseAggregator::ReportDataUse(const net::URLRequest& request, | 36 void DataUseAggregator::ReportDataUse(int64_t tx_bytes, |
38 int32_t tab_id, | 37 int64_t rx_bytes, |
39 int64_t tx_bytes, | 38 const GURL& url, |
40 int64_t rx_bytes) { | 39 const base::TimeTicks& request_start, |
| 40 const GURL& first_party_for_cookies, |
| 41 int32_t tab_id) { |
41 DCHECK(thread_checker_.CalledOnValidThread()); | 42 DCHECK(thread_checker_.CalledOnValidThread()); |
42 net::LoadTimingInfo load_timing_info; | |
43 request.GetLoadTimingInfo(&load_timing_info); | |
44 | 43 |
45 scoped_ptr<DataUse> data_use(new DataUse( | 44 scoped_ptr<DataUse> data_use(new DataUse( |
46 request.url(), load_timing_info.request_start, | 45 url, request_start, first_party_for_cookies, tab_id, |
47 request.first_party_for_cookies(), tab_id, | |
48 net::NetworkChangeNotifier::GetConnectionType(), tx_bytes, rx_bytes)); | 46 net::NetworkChangeNotifier::GetConnectionType(), tx_bytes, rx_bytes)); |
49 | 47 |
50 // As an optimization, attempt to combine the newly reported data use with the | 48 // As an optimization, attempt to combine the newly reported data use with the |
51 // most recent buffered data use, if the annotations on the data use are the | 49 // most recent buffered data use, if the annotations on the data use are the |
52 // same. | 50 // same. |
53 if (!buffered_data_use_.empty() && | 51 if (!buffered_data_use_.empty() && |
54 buffered_data_use_.back()->CanCombineWith(*data_use)) { | 52 buffered_data_use_.back()->CanCombineWith(*data_use)) { |
55 buffered_data_use_.back()->tx_bytes += tx_bytes; | 53 buffered_data_use_.back()->tx_bytes += data_use->tx_bytes; |
56 buffered_data_use_.back()->rx_bytes += rx_bytes; | 54 buffered_data_use_.back()->rx_bytes += data_use->rx_bytes; |
57 } else { | 55 } else { |
58 buffered_data_use_.push_back(data_use.Pass()); | 56 buffered_data_use_.push_back(data_use.Pass()); |
59 } | 57 } |
60 | 58 |
61 if (!is_flush_pending_) { | 59 if (!is_flush_pending_) { |
62 // Post a flush operation to happen in the future, so that the | 60 // Post a flush operation to happen in the future, so that the |
63 // DataUseAggregator has a chance to batch together some data use before | 61 // DataUseAggregator has a chance to batch together some data use before |
64 // notifying observers. | 62 // notifying observers. |
65 base::MessageLoop::current()->task_runner()->PostTask( | 63 base::MessageLoop::current()->task_runner()->PostTask( |
66 FROM_HERE, | 64 FROM_HERE, |
(...skipping 27 matching lines...) Expand all Loading... |
94 DCHECK(!ContainsValue(const_sequence, nullptr)); | 92 DCHECK(!ContainsValue(const_sequence, nullptr)); |
95 FOR_EACH_OBSERVER(Observer, observer_list_, OnDataUse(const_sequence)); | 93 FOR_EACH_OBSERVER(Observer, observer_list_, OnDataUse(const_sequence)); |
96 | 94 |
97 buffered_data_use_.clear(); | 95 buffered_data_use_.clear(); |
98 off_the_record_tx_bytes_since_last_flush_ = 0; | 96 off_the_record_tx_bytes_since_last_flush_ = 0; |
99 off_the_record_rx_bytes_since_last_flush_ = 0; | 97 off_the_record_rx_bytes_since_last_flush_ = 0; |
100 is_flush_pending_ = false; | 98 is_flush_pending_ = false; |
101 } | 99 } |
102 | 100 |
103 } // namespace data_usage | 101 } // namespace data_usage |
OLD | NEW |