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( | 133 void ExternalDataUseReporter::OnDataUse( |
134 std::unique_ptr<const std::deque<const data_usage::DataUse>> | 134 std::unique_ptr<const std::deque<const data_usage::DataUse>> |
135 data_use_list) { | 135 data_use_list) { |
136 DCHECK(thread_checker_.CalledOnValidThread()); | 136 DCHECK(thread_checker_.CalledOnValidThread()); |
137 DCHECK(data_use_list); | 137 DCHECK(data_use_list); |
138 | 138 |
139 const base::Time now_time = base::Time::Now(); | 139 const base::Time now_time = base::Time::Now(); |
140 DataUseTabModel::TrackingInfo tracking_info; | 140 DataUseTabModel::TrackingInfo tracking_info; |
141 | 141 |
142 for (const auto& data_use : *data_use_list) { | 142 for (const auto& data_use : *data_use_list) { |
143 if (!data_use_tab_model_->GetTrackingInfoForTabAtTime( | 143 if (!get_tracking_info_callback_.Run( |
144 data_use.tab_id, data_use.request_start, &tracking_info)) { | 144 data_use.tab_id, data_use.request_start, &tracking_info)) { |
145 continue; | 145 continue; |
146 } | 146 } |
147 | 147 |
148 BufferDataUseReport(data_use, tracking_info.label, tracking_info.tag, | 148 BufferDataUseReport(data_use, tracking_info.label, tracking_info.tag, |
149 previous_report_time_, now_time); | 149 previous_report_time_, now_time); |
150 SubmitBufferedDataUseReport(false); | 150 SubmitBufferedDataUseReport(false); |
151 } | 151 } |
152 previous_report_time_ = now_time; | 152 previous_report_time_ = now_time; |
153 } | 153 } |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 | 271 |
272 DCHECK_EQ(0, pending_report_bytes_); | 272 DCHECK_EQ(0, pending_report_bytes_); |
273 DCHECK(last_data_report_submitted_ticks_.is_null()); | 273 DCHECK(last_data_report_submitted_ticks_.is_null()); |
274 pending_report_bytes_ = report.bytes_downloaded + report.bytes_uploaded; | 274 pending_report_bytes_ = report.bytes_downloaded + report.bytes_uploaded; |
275 last_data_report_submitted_ticks_ = ticks_now; | 275 last_data_report_submitted_ticks_ = ticks_now; |
276 | 276 |
277 // Remove the entry from the map. | 277 // Remove the entry from the map. |
278 buffered_data_reports_.erase(it); | 278 buffered_data_reports_.erase(it); |
279 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); | 279 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); |
280 | 280 |
281 external_data_use_observer_bridge_->ReportDataUse( | 281 report_data_use_callback_.Run(key.label, key.tag, key.connection_type, |
282 key.label, key.tag, key.connection_type, key.mcc_mnc, report.start_time, | 282 key.mcc_mnc, report.start_time, report.end_time, |
283 report.end_time, report.bytes_downloaded, report.bytes_uploaded); | 283 report.bytes_downloaded, report.bytes_uploaded); |
284 } | 284 } |
285 | 285 |
286 ExternalDataUseReporter::DataUseReportKey::DataUseReportKey( | 286 ExternalDataUseReporter::DataUseReportKey::DataUseReportKey( |
287 const std::string& label, | 287 const std::string& label, |
288 const std::string& tag, | 288 const std::string& tag, |
289 net::NetworkChangeNotifier::ConnectionType connection_type, | 289 net::NetworkChangeNotifier::ConnectionType connection_type, |
290 const std::string& mcc_mnc) | 290 const std::string& mcc_mnc) |
291 : label(label), | 291 : label(label), |
292 tag(tag), | 292 tag(tag), |
293 connection_type(connection_type), | 293 connection_type(connection_type), |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 hash = hash * 23 + hash_function(k.label); | 326 hash = hash * 23 + hash_function(k.label); |
327 hash = hash * 31 + hash_function(k.tag); | 327 hash = hash * 31 + hash_function(k.tag); |
328 hash = hash * 43 + k.connection_type; | 328 hash = hash * 43 + k.connection_type; |
329 hash = hash * 83 + hash_function(k.mcc_mnc); | 329 hash = hash * 83 + hash_function(k.mcc_mnc); |
330 return hash; | 330 return hash; |
331 } | 331 } |
332 | 332 |
333 } // namespace android | 333 } // namespace android |
334 | 334 |
335 } // namespace chrome | 335 } // namespace chrome |
OLD | NEW |