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

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

Issue 1421983002: Include tab IDs when reporting data use accounting. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@data_use_scoped_vector
Patch Set: Created 5 years, 2 months 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 #include "components/data_usage/core/data_use_aggregator.h" 5 #include "components/data_usage/core/data_use_aggregator.h"
6 6
7 #include <sstream>
tbansal1 2015/10/26 22:24:30 why is sstream required?
sclittle 2015/10/26 23:56:01 <sstream> and <string> are just included right now
8 #include <string>
9
7 #include "base/bind.h" 10 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
10 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
11 #include "base/stl_util.h" 13 #include "base/stl_util.h"
12 #include "components/data_usage/core/data_use.h" 14 #include "components/data_usage/core/data_use.h"
13 #include "net/base/network_change_notifier.h" 15 #include "net/base/network_change_notifier.h"
14 #include "net/url_request/url_request.h" 16 #include "net/url_request/url_request.h"
15 17
16 namespace data_usage { 18 namespace data_usage {
17 19
20 namespace {
21
22 // Observer for debug logging.
23 // TODO in this CL: Remove this before landing.
bengr 2015/10/27 00:01:47 Please do remove it.
sclittle 2015/10/28 22:30:45 Done.
24 class LoggingObserver : public DataUseAggregator::Observer {
25 public:
26 LoggingObserver(DataUseAggregator* aggregator) : aggregator_(aggregator) {
27 DCHECK(aggregator_);
28 aggregator_->AddObserver(this);
29 }
30
31 ~LoggingObserver() override { aggregator_->RemoveObserver(this); }
32
33 void OnDataUse(
34 const std::vector<const DataUse*>& data_use_sequence) override {
35 std::ostringstream os;
36 for (const DataUse* data_use : data_use_sequence) {
37 os << "\n["
38 << "url=" << data_use->url << " rt=" << data_use->request_time
39 << " fp=" << data_use->first_party_for_cookies
40 << " tab_id=" << data_use->tab_id
41 << " ctype=" << data_use->connection_type
42 << " tx=" << data_use->tx_bytes << " rx=" << data_use->rx_bytes << "]";
43 }
44 LOG(WARNING) << "OnDataUse:" << os.str();
45 }
46
47 private:
48 DataUseAggregator* aggregator_;
49 };
50
51 } // namespace
52
18 DataUseAggregator::DataUseAggregator() 53 DataUseAggregator::DataUseAggregator()
19 : off_the_record_tx_bytes_since_last_flush_(0), 54 : off_the_record_tx_bytes_since_last_flush_(0),
20 off_the_record_rx_bytes_since_last_flush_(0), 55 off_the_record_rx_bytes_since_last_flush_(0),
21 is_flush_pending_(false), 56 is_flush_pending_(false),
22 weak_ptr_factory_(this) {} 57 weak_ptr_factory_(this) {}
23 58
24 DataUseAggregator::~DataUseAggregator() {} 59 DataUseAggregator::~DataUseAggregator() {}
25 60
26 void DataUseAggregator::AddObserver(Observer* observer) { 61 void DataUseAggregator::AddObserver(Observer* observer) {
27 DCHECK(thread_checker_.CalledOnValidThread()); 62 DCHECK(thread_checker_.CalledOnValidThread());
28 observer_list_.AddObserver(observer); 63 observer_list_.AddObserver(observer);
29 } 64 }
30 65
31 void DataUseAggregator::RemoveObserver(Observer* observer) { 66 void DataUseAggregator::RemoveObserver(Observer* observer) {
32 DCHECK(thread_checker_.CalledOnValidThread()); 67 DCHECK(thread_checker_.CalledOnValidThread());
33 observer_list_.RemoveObserver(observer); 68 observer_list_.RemoveObserver(observer);
34 } 69 }
35 70
36 void DataUseAggregator::ReportDataUse(const net::URLRequest& request, 71 void DataUseAggregator::ReportDataUse(int64_t tx_bytes,
37 int32_t tab_id, 72 int64_t rx_bytes,
38 int64_t tx_bytes, 73 const GURL& url,
39 int64_t rx_bytes) { 74 const base::Time& request_time,
75 const GURL& first_party_for_cookies,
76 int32_t tab_id) {
40 DCHECK(thread_checker_.CalledOnValidThread()); 77 DCHECK(thread_checker_.CalledOnValidThread());
41 78
42 scoped_ptr<DataUse> data_use(new DataUse( 79 scoped_ptr<DataUse> data_use(new DataUse(
43 request.url(), request.request_time(), request.first_party_for_cookies(), 80 url, request_time, first_party_for_cookies, tab_id,
44 tab_id, net::NetworkChangeNotifier::GetConnectionType(), tx_bytes, 81 net::NetworkChangeNotifier::GetConnectionType(), tx_bytes, rx_bytes));
45 rx_bytes));
46 82
47 // As an optimization, attempt to combine the newly reported data use with the 83 // As an optimization, attempt to combine the newly reported data use with the
48 // most recent buffered data use, if the annotations on the data use are the 84 // most recent buffered data use, if the annotations on the data use are the
49 // same. 85 // same.
50 if (!buffered_data_use_.empty() && 86 if (!buffered_data_use_.empty() &&
51 buffered_data_use_.back()->CanCombineWith(*data_use)) { 87 buffered_data_use_.back()->CanCombineWith(*data_use)) {
52 buffered_data_use_.back()->tx_bytes += tx_bytes; 88 buffered_data_use_.back()->tx_bytes += data_use->tx_bytes;
53 buffered_data_use_.back()->rx_bytes += rx_bytes; 89 buffered_data_use_.back()->rx_bytes += data_use->rx_bytes;
54 } else { 90 } else {
55 buffered_data_use_.push_back(data_use.Pass()); 91 buffered_data_use_.push_back(data_use.Pass());
56 } 92 }
57 93
58 if (!is_flush_pending_) { 94 if (!is_flush_pending_) {
59 // Post a flush operation to happen in the future, so that the 95 // Post a flush operation to happen in the future, so that the
60 // DataUseAggregator has a chance to batch together some data use before 96 // DataUseAggregator has a chance to batch together some data use before
61 // notifying observers. 97 // notifying observers.
62 base::MessageLoop::current()->task_runner()->PostTask( 98 base::MessageLoop::current()->task_runner()->PostTask(
63 FROM_HERE, 99 FROM_HERE,
64 base::Bind(&DataUseAggregator::FlushBufferedDataUse, GetWeakPtr())); 100 base::Bind(&DataUseAggregator::FlushBufferedDataUse, GetWeakPtr()));
65 is_flush_pending_ = true; 101 is_flush_pending_ = true;
66 } 102 }
67 } 103 }
68 104
69 void DataUseAggregator::ReportOffTheRecordDataUse(int64_t tx_bytes, 105 void DataUseAggregator::ReportOffTheRecordDataUse(int64_t tx_bytes,
70 int64_t rx_bytes) { 106 int64_t rx_bytes) {
71 DCHECK(thread_checker_.CalledOnValidThread()); 107 DCHECK(thread_checker_.CalledOnValidThread());
72 off_the_record_tx_bytes_since_last_flush_ += tx_bytes; 108 off_the_record_tx_bytes_since_last_flush_ += tx_bytes;
73 off_the_record_rx_bytes_since_last_flush_ += rx_bytes; 109 off_the_record_rx_bytes_since_last_flush_ += rx_bytes;
74 } 110 }
75 111
76 base::WeakPtr<DataUseAggregator> DataUseAggregator::GetWeakPtr() { 112 base::WeakPtr<DataUseAggregator> DataUseAggregator::GetWeakPtr() {
77 return weak_ptr_factory_.GetWeakPtr(); 113 return weak_ptr_factory_.GetWeakPtr();
78 } 114 }
79 115
80 void DataUseAggregator::FlushBufferedDataUse() { 116 void DataUseAggregator::FlushBufferedDataUse() {
81 DCHECK(thread_checker_.CalledOnValidThread()); 117 DCHECK(thread_checker_.CalledOnValidThread());
82 118
119 // TODO in this CL: Remove this debug logging observer.
bengr 2015/10/27 00:01:47 And this too.
sclittle 2015/10/28 22:30:45 Done.
120 LoggingObserver logging_observer(this);
121
83 // TODO(sclittle): Amortize data use on supported platforms before notifying 122 // TODO(sclittle): Amortize data use on supported platforms before notifying
84 // observers. 123 // observers.
85 124
86 // Pass Observers a sequence of const DataUse pointers instead of using the 125 // Pass Observers a sequence of const DataUse pointers instead of using the
87 // buffer directly in order to prevent Observers from modifying the DataUse 126 // buffer directly in order to prevent Observers from modifying the DataUse
88 // objects. 127 // objects.
89 std::vector<const DataUse*> const_sequence(buffered_data_use_.begin(), 128 std::vector<const DataUse*> const_sequence(buffered_data_use_.begin(),
90 buffered_data_use_.end()); 129 buffered_data_use_.end());
91 DCHECK(!ContainsValue(const_sequence, nullptr)); 130 DCHECK(!ContainsValue(const_sequence, nullptr));
92 FOR_EACH_OBSERVER(Observer, observer_list_, OnDataUse(const_sequence)); 131 FOR_EACH_OBSERVER(Observer, observer_list_, OnDataUse(const_sequence));
93 132
94 buffered_data_use_.clear(); 133 buffered_data_use_.clear();
95 off_the_record_tx_bytes_since_last_flush_ = 0; 134 off_the_record_tx_bytes_since_last_flush_ = 0;
96 off_the_record_rx_bytes_since_last_flush_ = 0; 135 off_the_record_rx_bytes_since_last_flush_ = 0;
97 is_flush_pending_ = false; 136 is_flush_pending_ = false;
98 } 137 }
99 138
100 } // namespace data_usage 139 } // namespace data_usage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698