OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/android/data_usage/external_data_use_reporter.h" | 5 #include "chrome/browser/android/data_usage/external_data_use_reporter.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/metrics/histogram_base.h" | 10 #include "base/metrics/histogram_base.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "chrome/browser/android/data_usage/data_use_tab_model.h" | |
14 #include "chrome/browser/android/data_usage/external_data_use_observer_bridge.h" | |
15 #include "components/data_usage/core/data_use.h" | 13 #include "components/data_usage/core/data_use.h" |
16 #include "components/variations/variations_associated_data.h" | 14 #include "components/variations/variations_associated_data.h" |
17 | 15 |
18 namespace chrome { | 16 namespace chrome { |
19 | 17 |
20 namespace android { | 18 namespace android { |
21 | 19 |
22 namespace { | 20 namespace { |
23 | 21 |
24 // Default duration after which a pending data use report is considered timed | 22 // Default duration after which a pending data use report is considered timed |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 return kDefaultDataUseReportMinBytes; | 91 return kDefaultDataUseReportMinBytes; |
94 } | 92 } |
95 | 93 |
96 } // namespace | 94 } // namespace |
97 | 95 |
98 // static | 96 // static |
99 const size_t ExternalDataUseReporter::kMaxBufferSize = 100; | 97 const size_t ExternalDataUseReporter::kMaxBufferSize = 100; |
100 | 98 |
101 ExternalDataUseReporter::ExternalDataUseReporter( | 99 ExternalDataUseReporter::ExternalDataUseReporter( |
102 const char* field_trial, | 100 const char* field_trial, |
103 DataUseTabModel* data_use_tab_model, | 101 const GetTrackingInfoCallback& get_tracking_info_callback, |
104 ExternalDataUseObserverBridge* external_data_use_observer_bridge) | 102 const ReportDataUseCallback& report_data_use_callback) |
105 : external_data_use_observer_bridge_(external_data_use_observer_bridge), | 103 : get_tracking_info_callback_(get_tracking_info_callback), |
106 data_use_tab_model_(data_use_tab_model), | 104 report_data_use_callback_(report_data_use_callback), |
107 last_data_report_submitted_ticks_(base::TimeTicks()), | 105 last_data_report_submitted_ticks_(base::TimeTicks()), |
108 pending_report_bytes_(0), | 106 pending_report_bytes_(0), |
109 previous_report_time_(base::Time::Now()), | 107 previous_report_time_(base::Time::Now()), |
110 total_bytes_buffered_(0), | 108 total_bytes_buffered_(0), |
111 data_use_report_min_bytes_(GetMinBytes(field_trial)), | 109 data_use_report_min_bytes_(GetMinBytes(field_trial)), |
112 data_report_submit_timeout_(base::TimeDelta::FromMilliseconds( | 110 data_report_submit_timeout_(base::TimeDelta::FromMilliseconds( |
113 GetDataReportSubmitTimeoutMsec(field_trial))) { | 111 GetDataReportSubmitTimeoutMsec(field_trial))) { |
| 112 DCHECK(get_tracking_info_callback_); |
| 113 DCHECK(report_data_use_callback_); |
114 DCHECK(last_data_report_submitted_ticks_.is_null()); | 114 DCHECK(last_data_report_submitted_ticks_.is_null()); |
115 // Detach from current thread since rest of ExternalDataUseReporter lives on | 115 // Detach from current thread since rest of ExternalDataUseReporter lives on |
116 // the UI thread and the current thread may not be UI thread.. | 116 // the UI thread and the current thread may not be UI thread.. |
117 thread_checker_.DetachFromThread(); | 117 thread_checker_.DetachFromThread(); |
118 } | 118 } |
119 | 119 |
120 void ExternalDataUseReporter::InitOnUIThread() { | 120 void ExternalDataUseReporter::InitOnUIThread() { |
121 DCHECK(thread_checker_.CalledOnValidThread()); | 121 DCHECK(thread_checker_.CalledOnValidThread()); |
122 #if defined(OS_ANDROID) | 122 #if defined(OS_ANDROID) |
123 app_state_listener_.reset(new base::android::ApplicationStatusListener( | 123 app_state_listener_.reset(new base::android::ApplicationStatusListener( |
124 base::Bind(&ExternalDataUseReporter::OnApplicationStateChange, | 124 base::Bind(&ExternalDataUseReporter::OnApplicationStateChange, |
125 base::Unretained(this)))); | 125 base::Unretained(this)))); |
126 #endif | 126 #endif |
127 } | 127 } |
128 | 128 |
129 ExternalDataUseReporter::~ExternalDataUseReporter() { | 129 ExternalDataUseReporter::~ExternalDataUseReporter() { |
130 DCHECK(thread_checker_.CalledOnValidThread()); | 130 DCHECK(thread_checker_.CalledOnValidThread()); |
131 } | 131 } |
132 | 132 |
133 void ExternalDataUseReporter::OnDataUse(const data_usage::DataUse& data_use) { | 133 void ExternalDataUseReporter::OnDataUse(const data_usage::DataUse& data_use) { |
134 DCHECK(thread_checker_.CalledOnValidThread()); | 134 DCHECK(thread_checker_.CalledOnValidThread()); |
135 | 135 |
136 const base::Time now_time = base::Time::Now(); | 136 const base::Time now_time = base::Time::Now(); |
137 DataUseTabModel::TrackingInfo tracking_info; | 137 DataUseTabModel::TrackingInfo tracking_info; |
138 | 138 |
139 if (!data_use_tab_model_->GetTrackingInfoForTabAtTime( | 139 if (!get_tracking_info_callback_.Run(data_use.tab_id, data_use.request_start, |
140 data_use.tab_id, data_use.request_start, &tracking_info)) { | 140 &tracking_info)) { |
141 return; | 141 return; |
142 } | 142 } |
143 | 143 |
144 BufferDataUseReport(data_use, tracking_info.label, tracking_info.tag, | 144 BufferDataUseReport(data_use, tracking_info.label, tracking_info.tag, |
145 previous_report_time_, now_time); | 145 previous_report_time_, now_time); |
146 SubmitBufferedDataUseReport(false); | 146 SubmitBufferedDataUseReport(false); |
147 previous_report_time_ = now_time; | 147 previous_report_time_ = now_time; |
148 } | 148 } |
149 | 149 |
150 void ExternalDataUseReporter::OnReportDataUseDone(bool success) { | 150 void ExternalDataUseReporter::OnReportDataUseDone(bool success) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 | 266 |
267 DCHECK_EQ(0, pending_report_bytes_); | 267 DCHECK_EQ(0, pending_report_bytes_); |
268 DCHECK(last_data_report_submitted_ticks_.is_null()); | 268 DCHECK(last_data_report_submitted_ticks_.is_null()); |
269 pending_report_bytes_ = report.bytes_downloaded + report.bytes_uploaded; | 269 pending_report_bytes_ = report.bytes_downloaded + report.bytes_uploaded; |
270 last_data_report_submitted_ticks_ = ticks_now; | 270 last_data_report_submitted_ticks_ = ticks_now; |
271 | 271 |
272 // Remove the entry from the map. | 272 // Remove the entry from the map. |
273 buffered_data_reports_.erase(it); | 273 buffered_data_reports_.erase(it); |
274 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); | 274 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); |
275 | 275 |
276 external_data_use_observer_bridge_->ReportDataUse( | 276 report_data_use_callback_.Run(key.label, key.tag, key.connection_type, |
277 key.label, key.tag, key.connection_type, key.mcc_mnc, report.start_time, | 277 key.mcc_mnc, report.start_time, report.end_time, |
278 report.end_time, report.bytes_downloaded, report.bytes_uploaded); | 278 report.bytes_downloaded, report.bytes_uploaded); |
279 } | 279 } |
280 | 280 |
281 ExternalDataUseReporter::DataUseReportKey::DataUseReportKey( | 281 ExternalDataUseReporter::DataUseReportKey::DataUseReportKey( |
282 const std::string& label, | 282 const std::string& label, |
283 const std::string& tag, | 283 const std::string& tag, |
284 net::NetworkChangeNotifier::ConnectionType connection_type, | 284 net::NetworkChangeNotifier::ConnectionType connection_type, |
285 const std::string& mcc_mnc) | 285 const std::string& mcc_mnc) |
286 : label(label), | 286 : label(label), |
287 tag(tag), | 287 tag(tag), |
288 connection_type(connection_type), | 288 connection_type(connection_type), |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 hash = hash * 23 + hash_function(k.label); | 321 hash = hash * 23 + hash_function(k.label); |
322 hash = hash * 31 + hash_function(k.tag); | 322 hash = hash * 31 + hash_function(k.tag); |
323 hash = hash * 43 + k.connection_type; | 323 hash = hash * 43 + k.connection_type; |
324 hash = hash * 83 + hash_function(k.mcc_mnc); | 324 hash = hash * 83 + hash_function(k.mcc_mnc); |
325 return hash; | 325 return hash; |
326 } | 326 } |
327 | 327 |
328 } // namespace android | 328 } // namespace android |
329 | 329 |
330 } // namespace chrome | 330 } // namespace chrome |
OLD | NEW |