| Index: components/data_usage/core/data_use_aggregator.cc
|
| diff --git a/components/data_usage/core/data_use_aggregator.cc b/components/data_usage/core/data_use_aggregator.cc
|
| index 792510b56b31b0a245f41f236e137db250d8a347..091a9008c5175897b814245bd6fa86bf20bdd6d8 100644
|
| --- a/components/data_usage/core/data_use_aggregator.cc
|
| +++ b/components/data_usage/core/data_use_aggregator.cc
|
| @@ -5,11 +5,13 @@
|
| #include "components/data_usage/core/data_use_aggregator.h"
|
|
|
| #include "base/bind.h"
|
| +#include "base/callback.h"
|
| #include "base/memory/scoped_ptr.h"
|
| #include "base/message_loop/message_loop.h"
|
| #include "base/single_thread_task_runner.h"
|
| #include "base/stl_util.h"
|
| #include "components/data_usage/core/data_use.h"
|
| +#include "components/data_usage/core/data_use_annotator.h"
|
| #include "net/base/load_timing_info.h"
|
| #include "net/base/network_change_notifier.h"
|
| #include "net/url_request/url_request.h"
|
| @@ -20,8 +22,9 @@
|
|
|
| namespace data_usage {
|
|
|
| -DataUseAggregator::DataUseAggregator()
|
| - : connection_type_(net::NetworkChangeNotifier::GetConnectionType()),
|
| +DataUseAggregator::DataUseAggregator(scoped_ptr<DataUseAnnotator> annotator)
|
| + : annotator_(annotator.Pass()),
|
| + connection_type_(net::NetworkChangeNotifier::GetConnectionType()),
|
| off_the_record_tx_bytes_since_last_flush_(0),
|
| off_the_record_rx_bytes_since_last_flush_(0),
|
| is_flush_pending_(false),
|
| @@ -46,39 +49,27 @@ void DataUseAggregator::RemoveObserver(Observer* observer) {
|
| observer_list_.RemoveObserver(observer);
|
| }
|
|
|
| -void DataUseAggregator::ReportDataUse(const net::URLRequest& request,
|
| - int32_t tab_id,
|
| +void DataUseAggregator::ReportDataUse(net::URLRequest* request,
|
| int64_t tx_bytes,
|
| int64_t rx_bytes) {
|
| DCHECK(thread_checker_.CalledOnValidThread());
|
| +
|
| net::LoadTimingInfo load_timing_info;
|
| - request.GetLoadTimingInfo(&load_timing_info);
|
| + request->GetLoadTimingInfo(&load_timing_info);
|
|
|
| scoped_ptr<DataUse> data_use(
|
| - new DataUse(request.url(), load_timing_info.request_start,
|
| - request.first_party_for_cookies(), tab_id, connection_type_,
|
| - mcc_mnc_, tx_bytes, rx_bytes));
|
| + new DataUse(request->url(), load_timing_info.request_start,
|
| + request->first_party_for_cookies(), -1 /* tab_id */,
|
| + connection_type_, mcc_mnc_, tx_bytes, rx_bytes));
|
|
|
| - // As an optimization, attempt to combine the newly reported data use with the
|
| - // most recent buffered data use, if the annotations on the data use are the
|
| - // same.
|
| - if (!buffered_data_use_.empty() &&
|
| - buffered_data_use_.back()->CanCombineWith(*data_use)) {
|
| - buffered_data_use_.back()->tx_bytes += tx_bytes;
|
| - buffered_data_use_.back()->rx_bytes += rx_bytes;
|
| - } else {
|
| - buffered_data_use_.push_back(data_use.Pass());
|
| + if (!annotator_) {
|
| + AppendDataUse(data_use.Pass());
|
| + return;
|
| }
|
|
|
| - if (!is_flush_pending_) {
|
| - // Post a flush operation to happen in the future, so that the
|
| - // DataUseAggregator has a chance to batch together some data use before
|
| - // notifying observers.
|
| - base::MessageLoop::current()->task_runner()->PostTask(
|
| - FROM_HERE,
|
| - base::Bind(&DataUseAggregator::FlushBufferedDataUse, GetWeakPtr()));
|
| - is_flush_pending_ = true;
|
| - }
|
| + annotator_->Annotate(
|
| + request, data_use.Pass(),
|
| + base::Bind(&DataUseAggregator::AppendDataUse, GetWeakPtr()));
|
| }
|
|
|
| void DataUseAggregator::ReportOffTheRecordDataUse(int64_t tx_bytes,
|
| @@ -89,6 +80,7 @@ void DataUseAggregator::ReportOffTheRecordDataUse(int64_t tx_bytes,
|
| }
|
|
|
| base::WeakPtr<DataUseAggregator> DataUseAggregator::GetWeakPtr() {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| return weak_ptr_factory_.GetWeakPtr();
|
| }
|
|
|
| @@ -107,6 +99,32 @@ void DataUseAggregator::SetMccMncForTests(const std::string& mcc_mnc) {
|
| mcc_mnc_ = mcc_mnc;
|
| }
|
|
|
| +void DataUseAggregator::AppendDataUse(scoped_ptr<DataUse> data_use) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(data_use);
|
| +
|
| + // As an optimization, attempt to combine the newly reported data use with the
|
| + // most recent buffered data use, if the annotations on the data use are the
|
| + // same.
|
| + if (!buffered_data_use_.empty() &&
|
| + buffered_data_use_.back()->CanCombineWith(*data_use)) {
|
| + buffered_data_use_.back()->tx_bytes += data_use->tx_bytes;
|
| + buffered_data_use_.back()->rx_bytes += data_use->rx_bytes;
|
| + } else {
|
| + buffered_data_use_.push_back(data_use.Pass());
|
| + }
|
| +
|
| + if (!is_flush_pending_) {
|
| + // Post a flush operation to happen in the future, so that the
|
| + // DataUseAggregator has a chance to batch together some data use before
|
| + // notifying observers.
|
| + base::MessageLoop::current()->task_runner()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&DataUseAggregator::FlushBufferedDataUse, GetWeakPtr()));
|
| + is_flush_pending_ = true;
|
| + }
|
| +}
|
| +
|
| void DataUseAggregator::FlushBufferedDataUse() {
|
| DCHECK(thread_checker_.CalledOnValidThread());
|
|
|
|
|