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_use_measurement/content/data_use_measurement.h" | 5 #include "components/data_use_measurement/content/data_use_measurement.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/metrics/sparse_histogram.h" | 8 #include "base/metrics/sparse_histogram.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "components/metrics/data_use_tracker.h" | |
11 #include "content/public/browser/resource_request_info.h" | 12 #include "content/public/browser/resource_request_info.h" |
12 #include "net/base/network_change_notifier.h" | 13 #include "net/base/network_change_notifier.h" |
13 #include "net/base/upload_data_stream.h" | 14 #include "net/base/upload_data_stream.h" |
14 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
15 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
16 | 17 |
17 namespace data_use_measurement { | 18 namespace data_use_measurement { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
(...skipping 21 matching lines...) Expand all Loading... | |
42 histogram->AddCount(sample, value); | 43 histogram->AddCount(sample, value); |
43 } | 44 } |
44 | 45 |
45 } // namespace | 46 } // namespace |
46 | 47 |
47 DataUseMeasurement::DataUseMeasurement() | 48 DataUseMeasurement::DataUseMeasurement() |
48 #if defined(OS_ANDROID) | 49 #if defined(OS_ANDROID) |
49 : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), | 50 : app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), |
50 app_listener_(new base::android::ApplicationStatusListener( | 51 app_listener_(new base::android::ApplicationStatusListener( |
51 base::Bind(&DataUseMeasurement::OnApplicationStateChange, | 52 base::Bind(&DataUseMeasurement::OnApplicationStateChange, |
52 base::Unretained(this)))) | 53 base::Unretained(this)))), |
54 weak_ptr_factory_(this) | |
53 #endif | 55 #endif |
54 { | 56 : weak_ptr_factory_(this) { |
55 } | 57 } |
56 | 58 |
57 DataUseMeasurement::~DataUseMeasurement(){}; | 59 DataUseMeasurement::~DataUseMeasurement(){}; |
58 | 60 |
59 void DataUseMeasurement::ReportDataUseUMA( | 61 void DataUseMeasurement::ReportDataUseUMA( |
60 const net::URLRequest* request) const { | 62 const net::URLRequest* request) const { |
61 const content::ResourceRequestInfo* info = | 63 const content::ResourceRequestInfo* info = |
62 content::ResourceRequestInfo::ForRequest(request); | 64 content::ResourceRequestInfo::ForRequest(request); |
63 // Having |info| is the sign of a request for a web content from user. For now | 65 // Having |info| is the sign of a request for a web content from user. For now |
64 // we could add a condition to check ProcessType in info is | 66 // we could add a condition to check ProcessType in info is |
(...skipping 16 matching lines...) Expand all Loading... | |
81 UPSTREAM), | 83 UPSTREAM), |
82 total_upload_bytes); | 84 total_upload_bytes); |
83 RecordUMAHistogramCount( | 85 RecordUMAHistogramCount( |
84 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | 86 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" |
85 : "DataUse.TrafficSize.System", | 87 : "DataUse.TrafficSize.System", |
86 DOWNSTREAM), | 88 DOWNSTREAM), |
87 total_received_bytes); | 89 total_received_bytes); |
88 | 90 |
89 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | 91 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( |
90 request->GetUserData(DataUseUserData::kUserDataKey)); | 92 request->GetUserData(DataUseUserData::kUserDataKey)); |
91 | 93 DataUseUserData::ServiceName service_name = |
94 attached_service_data ? attached_service_data->service_name() | |
95 : DataUseUserData::NOT_TAGGED; | |
92 if (!is_user_traffic) { | 96 if (!is_user_traffic) { |
93 DataUseUserData::ServiceName service_name = | |
94 attached_service_data ? attached_service_data->service_name() | |
95 : DataUseUserData::NOT_TAGGED; | |
96 ReportDataUsageServices(service_name, UPSTREAM, total_upload_bytes); | 97 ReportDataUsageServices(service_name, UPSTREAM, total_upload_bytes); |
97 ReportDataUsageServices(service_name, DOWNSTREAM, total_received_bytes); | 98 ReportDataUsageServices(service_name, DOWNSTREAM, total_received_bytes); |
98 } | 99 } |
100 bool is_cellular = net::NetworkChangeNotifier::IsConnectionCellular( | |
101 net::NetworkChangeNotifier::GetConnectionType()); | |
102 if (is_cellular) | |
Alexei Svitkine (slow)
2016/03/29 16:29:10
Nit: Combine this with if below.
gayane -on leave until 09-2017
2016/03/31 01:38:24
Done.
| |
103 if (!metrics_data_use_forwarder_.is_null()) { | |
104 metrics::DataUseTracker::RunOnUI( | |
105 metrics_data_use_forwarder_, | |
106 attached_service_data->GetServiceNameAsString(service_name), | |
107 total_upload_bytes + total_received_bytes); | |
108 } | |
99 } | 109 } |
100 | 110 |
101 #if defined(OS_ANDROID) | 111 #if defined(OS_ANDROID) |
102 void DataUseMeasurement::OnApplicationStateChangeForTesting( | 112 void DataUseMeasurement::OnApplicationStateChangeForTesting( |
103 base::android::ApplicationState application_state) { | 113 base::android::ApplicationState application_state) { |
104 app_state_ = application_state; | 114 app_state_ = application_state; |
105 } | 115 } |
106 #endif | 116 #endif |
107 | 117 |
108 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { | 118 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { |
(...skipping 30 matching lines...) Expand all Loading... | |
139 RecordUMAHistogramCount( | 149 RecordUMAHistogramCount( |
140 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), | 150 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), |
141 message_size); | 151 message_size); |
142 if (message_size > 0) { | 152 if (message_size > 0) { |
143 IncreaseSparseHistogramByValue( | 153 IncreaseSparseHistogramByValue( |
144 GetHistogramName("DataUse.MessageSize.AllServices", dir), service, | 154 GetHistogramName("DataUse.MessageSize.AllServices", dir), service, |
145 message_size); | 155 message_size); |
146 } | 156 } |
147 } | 157 } |
148 | 158 |
159 void DataUseMeasurement::SetMetricsDataUseForwarder( | |
160 base::Callback<void(const std::string&, int)> metrics_data_use_forwarder) { | |
161 metrics_data_use_forwarder_ = metrics_data_use_forwarder; | |
162 } | |
163 | |
164 base::WeakPtr<DataUseMeasurement> DataUseMeasurement::GetWeakPtr() { | |
165 return weak_ptr_factory_.GetWeakPtr(); | |
166 } | |
149 } // namespace data_use_measurement | 167 } // namespace data_use_measurement |
OLD | NEW |