OLD | NEW |
---|---|
1 // Copyright 2015 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_observer.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/containers/hash_tables.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
12 #include "base/metrics/histogram_base.h" | 10 #include "base/metrics/histogram_base.h" |
13 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
14 #include "base/single_thread_task_runner.h" | |
15 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
16 #include "base/task_runner_util.h" | 13 #include "chrome/browser/android/data_usage/data_use_tab_model.h" |
17 #include "chrome/browser/android/data_usage/external_data_use_observer_bridge.h" | 14 #include "chrome/browser/android/data_usage/external_data_use_observer_bridge.h" |
18 #include "components/data_usage/core/data_use.h" | 15 #include "components/data_usage/core/data_use.h" |
19 #include "components/variations/variations_associated_data.h" | 16 #include "components/variations/variations_associated_data.h" |
20 #include "content/public/browser/browser_thread.h" | |
21 | 17 |
22 namespace chrome { | 18 namespace chrome { |
23 | 19 |
24 namespace android { | 20 namespace android { |
25 | 21 |
26 namespace { | 22 namespace { |
27 | 23 |
24 // Default duration after which a pending data use report is considered timed | |
25 // out. May be overridden by the field trial. | |
26 const int kDefaultDataUseReportSubmitTimeoutMsec = 60 * 2 * 1000; // 2 minutes. | |
27 | |
28 // Default value of the minimum number of bytes that should be buffered before | |
29 // a data use report is submitted. May be overridden by the field trial. | |
30 const int64_t kDefaultDataUseReportMinBytes = 100 * 1024; // 100 KB. | |
31 | |
28 // Record the result of data use report submission. |bytes| is the sum of send | 32 // Record the result of data use report submission. |bytes| is the sum of send |
29 // and received bytes in the report. | 33 // and received bytes in the report. |
30 void RecordDataUsageReportSubmission( | 34 void RecordDataUsageReportSubmission( |
31 ExternalDataUseObserver::DataUsageReportSubmissionResult result, | 35 ExternalDataUseReporter::DataUsageReportSubmissionResult result, |
32 int64_t bytes) { | 36 int64_t bytes) { |
33 DCHECK_LE(0, bytes); | 37 DCHECK_LE(0, bytes); |
34 UMA_HISTOGRAM_ENUMERATION( | 38 UMA_HISTOGRAM_ENUMERATION( |
35 "DataUsage.ReportSubmissionResult", result, | 39 "DataUsage.ReportSubmissionResult", result, |
36 ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_MAX); | 40 ExternalDataUseReporter::DATAUSAGE_REPORT_SUBMISSION_MAX); |
37 // Cap to the maximum sample value. | 41 // Cap to the maximum sample value. |
38 const int32_t bytes_capped = bytes <= base::HistogramBase::kSampleType_MAX - 1 | 42 const int32_t bytes_capped = bytes <= base::HistogramBase::kSampleType_MAX - 1 |
39 ? bytes | 43 ? bytes |
40 : base::HistogramBase::kSampleType_MAX - 1; | 44 : base::HistogramBase::kSampleType_MAX - 1; |
41 switch (result) { | 45 switch (result) { |
42 case ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL: | 46 case ExternalDataUseReporter::DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL: |
43 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Bytes.Successful", | 47 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Bytes.Successful", |
44 bytes_capped); | 48 bytes_capped); |
45 break; | 49 break; |
46 case ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_FAILED: | 50 case ExternalDataUseReporter::DATAUSAGE_REPORT_SUBMISSION_FAILED: |
47 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Bytes.Failed", | 51 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Bytes.Failed", |
48 bytes_capped); | 52 bytes_capped); |
49 break; | 53 break; |
50 case ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT: | 54 case ExternalDataUseReporter::DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT: |
51 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Bytes.TimedOut", | 55 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Bytes.TimedOut", |
52 bytes_capped); | 56 bytes_capped); |
53 break; | 57 break; |
54 case ExternalDataUseObserver::DATAUSAGE_REPORT_SUBMISSION_LOST: | 58 case ExternalDataUseReporter::DATAUSAGE_REPORT_SUBMISSION_LOST: |
55 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Bytes.Lost", | 59 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Bytes.Lost", |
56 bytes_capped); | 60 bytes_capped); |
57 break; | 61 break; |
58 default: | 62 default: |
59 NOTIMPLEMENTED(); | 63 NOTIMPLEMENTED(); |
60 break; | 64 break; |
61 } | 65 } |
62 } | 66 } |
63 | 67 |
64 // Default duration after which matching rules are periodically fetched. May be | 68 // Populates various parameters from the values specified in the field trial |
65 // overridden by the field trial. | 69 // |field_trial|. |
66 const int kDefaultFetchMatchingRulesDurationSeconds = 60 * 15; // 15 minutes. | 70 int32_t GetDataReportSubmitTimeoutMsec(const char* field_trial) { |
67 | |
68 // Default duration after which a pending data use report is considered timed | |
69 // out. May be overridden by the field trial. | |
70 const int kDefaultDataUseReportSubmitTimeoutMsec = 60 * 2 * 1000; // 2 minutes. | |
71 | |
72 // Default value of the minimum number of bytes that should be buffered before | |
73 // a data use report is submitted. May be overridden by the field trial. | |
74 const int64_t kDefaultDataUseReportMinBytes = 100 * 1024; // 100 KB. | |
75 | |
76 // Populates various parameters from the values specified in the field trial. | |
77 int32_t GetFetchMatchingRulesDurationSeconds() { | |
78 int32_t duration_seconds = -1; | 71 int32_t duration_seconds = -1; |
79 const std::string variation_value = variations::GetVariationParamValue( | 72 const std::string variation_value = variations::GetVariationParamValue( |
80 chrome::android::ExternalDataUseObserver:: | 73 field_trial, "data_report_submit_timeout_msec"); |
81 kExternalDataUseObserverFieldTrial, | |
82 "fetch_matching_rules_duration_seconds"); | |
83 if (!variation_value.empty() && | 74 if (!variation_value.empty() && |
84 base::StringToInt(variation_value, &duration_seconds)) { | 75 base::StringToInt(variation_value, &duration_seconds)) { |
85 DCHECK_LE(0, duration_seconds); | 76 DCHECK_LE(0, duration_seconds); |
86 return duration_seconds; | |
87 } | |
88 return kDefaultFetchMatchingRulesDurationSeconds; | |
89 } | |
90 | |
91 // Populates various parameters from the values specified in the field trial. | |
92 int32_t GetDataReportSubmitTimeoutMsec() { | |
93 int32_t duration_seconds = -1; | |
94 const std::string variation_value = variations::GetVariationParamValue( | |
95 chrome::android::ExternalDataUseObserver:: | |
96 kExternalDataUseObserverFieldTrial, | |
97 "data_report_submit_timeout_msec"); | |
98 if (!variation_value.empty() && | |
99 base::StringToInt(variation_value, &duration_seconds)) { | |
100 DCHECK_LE(0, duration_seconds); | |
101 return duration_seconds; | 77 return duration_seconds; |
102 } | 78 } |
103 return kDefaultDataUseReportSubmitTimeoutMsec; | 79 return kDefaultDataUseReportSubmitTimeoutMsec; |
104 } | 80 } |
105 | 81 |
106 // Populates various parameters from the values specified in the field trial. | 82 // Populates various parameters from the values specified in the field trial |
107 int64_t GetMinBytes() { | 83 // |field_trial|. |
84 int64_t GetMinBytes(const char* field_trial) { | |
108 int64_t min_bytes = -1; | 85 int64_t min_bytes = -1; |
109 const std::string variation_value = variations::GetVariationParamValue( | 86 const std::string variation_value = variations::GetVariationParamValue( |
110 chrome::android::ExternalDataUseObserver:: | 87 field_trial, "data_use_report_min_bytes"); |
111 kExternalDataUseObserverFieldTrial, | |
112 "data_use_report_min_bytes"); | |
113 if (!variation_value.empty() && | 88 if (!variation_value.empty() && |
114 base::StringToInt64(variation_value, &min_bytes)) { | 89 base::StringToInt64(variation_value, &min_bytes)) { |
115 DCHECK_LE(0, min_bytes); | 90 DCHECK_LE(0, min_bytes); |
116 return min_bytes; | 91 return min_bytes; |
117 } | 92 } |
118 return kDefaultDataUseReportMinBytes; | 93 return kDefaultDataUseReportMinBytes; |
119 } | 94 } |
120 | 95 |
121 } // namespace | 96 } // namespace |
122 | 97 |
123 // static | 98 // static |
124 const char ExternalDataUseObserver::kExternalDataUseObserverFieldTrial[] = | 99 const size_t ExternalDataUseReporter::kMaxBufferSize = 100; |
125 "ExternalDataUseObserver"; | |
126 | 100 |
127 // static | 101 ExternalDataUseReporter::ExternalDataUseReporter( |
128 const size_t ExternalDataUseObserver::kMaxBufferSize = 100; | 102 const char* field_trial, |
129 | 103 DataUseTabModel* data_use_tab_model, |
130 ExternalDataUseObserver::ExternalDataUseObserver( | 104 ExternalDataUseObserverBridge* external_data_use_observer_bridge) |
131 data_usage::DataUseAggregator* data_use_aggregator, | 105 : external_data_use_observer_bridge_(external_data_use_observer_bridge), |
132 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 106 data_use_tab_model_(data_use_tab_model), |
133 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) | |
134 : data_use_aggregator_(data_use_aggregator), | |
135 external_data_use_observer_bridge_(new ExternalDataUseObserverBridge()), | |
136 data_use_tab_model_(new DataUseTabModel()), | |
137 last_data_report_submitted_ticks_(base::TimeTicks()), | 107 last_data_report_submitted_ticks_(base::TimeTicks()), |
138 pending_report_bytes_(0), | 108 pending_report_bytes_(0), |
139 ui_task_runner_(ui_task_runner), | |
140 previous_report_time_(base::Time::Now()), | 109 previous_report_time_(base::Time::Now()), |
141 last_matching_rules_fetch_time_(base::TimeTicks::Now()), | |
142 total_bytes_buffered_(0), | 110 total_bytes_buffered_(0), |
143 fetch_matching_rules_duration_( | 111 data_use_report_min_bytes_(GetMinBytes(field_trial)), |
144 base::TimeDelta::FromSeconds(GetFetchMatchingRulesDurationSeconds())), | 112 data_report_submit_timeout_(base::TimeDelta::FromMilliseconds( |
145 data_use_report_min_bytes_(GetMinBytes()), | 113 GetDataReportSubmitTimeoutMsec(field_trial))) { |
146 data_report_submit_timeout_( | |
147 base::TimeDelta::FromMilliseconds(GetDataReportSubmitTimeoutMsec())), | |
148 #if defined(OS_ANDROID) | |
149 app_state_listener_(new base::android::ApplicationStatusListener( | |
150 base::Bind(&ExternalDataUseObserver::OnApplicationStateChange, | |
151 base::Unretained(this)))), | |
152 #endif | |
153 registered_as_data_use_observer_(false), | |
154 weak_factory_(this) { | |
155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
156 DCHECK(data_use_aggregator_); | |
157 DCHECK(io_task_runner); | |
158 DCHECK(ui_task_runner_); | |
159 DCHECK(last_data_report_submitted_ticks_.is_null()); | 114 DCHECK(last_data_report_submitted_ticks_.is_null()); |
160 | 115 // Detach from current thread since rest of ExternalDataUseReporter lives on |
161 ui_task_runner_->PostTask(FROM_HERE, | 116 // the UI thread and the current thread may not be UI thread.. |
162 base::Bind(&DataUseTabModel::InitOnUIThread, | 117 thread_checker_.DetachFromThread(); |
163 base::Unretained(data_use_tab_model_), | |
164 external_data_use_observer_bridge_)); | |
165 | |
166 // Initialize the ExternalDataUseObserverBridge object. It is okay to use | |
167 // base::Unretained here since |external_data_use_observer_bridge_| is owned | |
168 // by |this|, and is destroyed on UI thread when |this| is destroyed. | |
169 ui_task_runner_->PostTask( | |
170 FROM_HERE, | |
171 base::Bind(&ExternalDataUseObserverBridge::Init, | |
172 base::Unretained(external_data_use_observer_bridge_), | |
173 io_task_runner, GetWeakPtr(), data_use_tab_model_)); | |
174 } | 118 } |
175 | 119 |
176 ExternalDataUseObserver::~ExternalDataUseObserver() { | 120 void ExternalDataUseReporter::InitOnUIThread() { |
121 #if defined(OS_ANDROID) | |
122 app_state_listener_.reset(new base::android::ApplicationStatusListener( | |
tbansal1
2016/07/18 16:42:12
thread checker
Raj
2016/07/18 18:12:36
Done.
| |
123 base::Bind(&ExternalDataUseReporter::OnApplicationStateChange, | |
124 base::Unretained(this)))); | |
125 #endif | |
126 } | |
127 | |
128 ExternalDataUseReporter::~ExternalDataUseReporter() { | |
129 DCHECK(thread_checker_.CalledOnValidThread()); | |
130 } | |
131 | |
132 void ExternalDataUseReporter::OnDataUse(const data_usage::DataUse& data_use) { | |
177 DCHECK(thread_checker_.CalledOnValidThread()); | 133 DCHECK(thread_checker_.CalledOnValidThread()); |
178 | 134 |
179 if (registered_as_data_use_observer_) | 135 const base::Time now_time = base::Time::Now(); |
180 data_use_aggregator_->RemoveObserver(this); | 136 DataUseTabModel::TrackingInfo tracking_info; |
181 | 137 |
182 // Delete |data_use_tab_model_| on the UI thread. |data_use_tab_model_| should | 138 if (!data_use_tab_model_->GetTrackingInfoForTabAtTime( |
183 // be deleted before |external_data_use_observer_bridge_|. | 139 data_use.tab_id, data_use.request_start, &tracking_info)) { |
184 if (!ui_task_runner_->DeleteSoon(FROM_HERE, data_use_tab_model_)) { | 140 return; |
185 NOTIMPLEMENTED() << " DataUseTabModel was not deleted successfully"; | |
186 } | 141 } |
187 | 142 |
188 // Delete |external_data_use_observer_bridge_| on the UI thread. | 143 BufferDataUseReport(data_use, tracking_info.label, tracking_info.tag, |
189 if (!ui_task_runner_->DeleteSoon(FROM_HERE, | 144 previous_report_time_, now_time); |
190 external_data_use_observer_bridge_)) { | 145 SubmitBufferedDataUseReport(false); |
191 NOTIMPLEMENTED() | 146 previous_report_time_ = now_time; |
192 << " ExternalDataUseObserverBridge was not deleted successfully"; | |
193 } | |
194 } | 147 } |
195 | 148 |
196 void ExternalDataUseObserver::OnReportDataUseDone(bool success) { | 149 void ExternalDataUseReporter::OnReportDataUseDone(bool success) { |
197 DCHECK(thread_checker_.CalledOnValidThread()); | 150 DCHECK(thread_checker_.CalledOnValidThread()); |
198 DCHECK(!last_data_report_submitted_ticks_.is_null()); | 151 DCHECK(!last_data_report_submitted_ticks_.is_null()); |
199 | 152 |
200 if (success) { | 153 if (success) { |
201 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, | 154 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, |
202 pending_report_bytes_); | 155 pending_report_bytes_); |
203 } else { | 156 } else { |
204 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_FAILED, | 157 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_FAILED, |
205 pending_report_bytes_); | 158 pending_report_bytes_); |
206 } | 159 } |
207 UMA_HISTOGRAM_TIMES( | 160 UMA_HISTOGRAM_TIMES( |
208 "DataUsage.Perf.ReportSubmissionDuration", | 161 "DataUsage.Perf.ReportSubmissionDuration", |
209 base::TimeTicks::Now() - last_data_report_submitted_ticks_); | 162 base::TimeTicks::Now() - last_data_report_submitted_ticks_); |
210 | 163 |
211 last_data_report_submitted_ticks_ = base::TimeTicks(); | 164 last_data_report_submitted_ticks_ = base::TimeTicks(); |
212 pending_report_bytes_ = 0; | 165 pending_report_bytes_ = 0; |
213 | 166 |
214 SubmitBufferedDataUseReport(false); | 167 SubmitBufferedDataUseReport(false); |
215 } | 168 } |
216 | 169 |
217 #if defined(OS_ANDROID) | 170 #if defined(OS_ANDROID) |
218 void ExternalDataUseObserver::OnApplicationStateChange( | 171 void ExternalDataUseReporter::OnApplicationStateChange( |
219 base::android::ApplicationState new_state) { | 172 base::android::ApplicationState new_state) { |
220 DCHECK(thread_checker_.CalledOnValidThread()); | 173 DCHECK(thread_checker_.CalledOnValidThread()); |
174 | |
175 // TODO(rajendrant): When Chromium is backgrounded, only one data use report | |
176 // is submitted since the external API only supports one pending report | |
177 // submission. Once the external API supports submitting multiple reports | |
178 // in one call, all reports should be submitted immediately. | |
221 if (new_state == base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES) | 179 if (new_state == base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES) |
222 SubmitBufferedDataUseReport(true); | 180 SubmitBufferedDataUseReport(true); |
223 } | 181 } |
224 #endif | 182 #endif |
225 | 183 |
226 void ExternalDataUseObserver::OnDataUse(const data_usage::DataUse& data_use) { | 184 void ExternalDataUseReporter::BufferDataUseReport( |
227 DCHECK(thread_checker_.CalledOnValidThread()); | |
228 DCHECK(registered_as_data_use_observer_); | |
229 | |
230 const base::TimeTicks now_ticks = base::TimeTicks::Now(); | |
231 const base::Time now_time = base::Time::Now(); | |
232 | |
233 // If the time when the matching rules were last fetched is more than | |
234 // |fetch_matching_rules_duration_|, fetch them again. | |
235 if (now_ticks - last_matching_rules_fetch_time_ >= | |
236 fetch_matching_rules_duration_) { | |
237 FetchMatchingRules(); | |
238 } | |
239 | |
240 std::unique_ptr<DataUseTabModel::TrackingInfo> tracking_info( | |
241 new DataUseTabModel::TrackingInfo()); | |
242 | |
243 content::BrowserThread::PostTaskAndReplyWithResult( | |
244 content::BrowserThread::UI, FROM_HERE, | |
245 base::Bind(&DataUseTabModel::GetTrackingInfoForTabAtTime, | |
246 base::Unretained(data_use_tab_model_), data_use.tab_id, | |
247 data_use.request_start, tracking_info.get()), | |
248 base::Bind(&ExternalDataUseObserver::DataUseTrackingInfoRetrieved, | |
249 GetWeakPtr(), data_use, previous_report_time_, now_time, | |
250 base::Owned(tracking_info.release()))); | |
251 | |
252 previous_report_time_ = now_time; | |
253 } | |
254 | |
255 void ExternalDataUseObserver::ShouldRegisterAsDataUseObserver( | |
256 bool should_register) { | |
257 DCHECK(thread_checker_.CalledOnValidThread()); | |
258 if (registered_as_data_use_observer_ == should_register) | |
259 return; | |
260 | |
261 if (!registered_as_data_use_observer_ && should_register) | |
262 data_use_aggregator_->AddObserver(this); | |
263 | |
264 if (registered_as_data_use_observer_ && !should_register) | |
265 data_use_aggregator_->RemoveObserver(this); | |
266 | |
267 registered_as_data_use_observer_ = should_register; | |
268 } | |
269 | |
270 void ExternalDataUseObserver::FetchMatchingRules() { | |
271 DCHECK(thread_checker_.CalledOnValidThread()); | |
272 | |
273 last_matching_rules_fetch_time_ = base::TimeTicks::Now(); | |
274 | |
275 // It is okay to use base::Unretained here since | |
276 // |external_data_use_observer_bridge_| is owned by |this|, and is destroyed | |
277 // on UI thread when |this| is destroyed. | |
278 ui_task_runner_->PostTask( | |
279 FROM_HERE, | |
280 base::Bind(&ExternalDataUseObserverBridge::FetchMatchingRules, | |
281 base::Unretained(external_data_use_observer_bridge_))); | |
282 } | |
283 | |
284 void ExternalDataUseObserver::DataUseTrackingInfoRetrieved( | |
285 const data_usage::DataUse& data_use, | |
286 const base::Time& start_time, | |
287 const base::Time& end_time, | |
288 const DataUseTabModel::TrackingInfo* tracking_info, | |
289 bool tracking_info_valid) { | |
290 DCHECK(thread_checker_.CalledOnValidThread()); | |
291 | |
292 if (!tracking_info_valid) | |
293 return; | |
294 | |
295 BufferDataUseReport(data_use, tracking_info->label, tracking_info->tag, | |
296 start_time, end_time); | |
297 SubmitBufferedDataUseReport(false); | |
298 } | |
299 | |
300 void ExternalDataUseObserver::BufferDataUseReport( | |
301 const data_usage::DataUse& data_use, | 185 const data_usage::DataUse& data_use, |
302 const std::string& label, | 186 const std::string& label, |
303 const std::string& tag, | 187 const std::string& tag, |
304 const base::Time& start_time, | 188 const base::Time& start_time, |
305 const base::Time& end_time) { | 189 const base::Time& end_time) { |
306 DCHECK(thread_checker_.CalledOnValidThread()); | 190 DCHECK(thread_checker_.CalledOnValidThread()); |
307 DCHECK(!label.empty()); | 191 DCHECK(!label.empty()); |
308 DCHECK_LE(0, data_use.rx_bytes); | 192 DCHECK_LE(0, data_use.rx_bytes); |
309 DCHECK_LE(0, data_use.tx_bytes); | 193 DCHECK_LE(0, data_use.tx_bytes); |
310 // Skip if the |data_use| does not report any network traffic. | 194 // Skip if the |data_use| does not report any network traffic. |
311 if (data_use.rx_bytes == 0 && data_use.tx_bytes == 0) | 195 if (data_use.rx_bytes == 0 && data_use.tx_bytes == 0) |
312 return; | 196 return; |
313 | 197 |
314 DataUseReportKey data_use_report_key = | 198 DataUseReportKey data_use_report_key = |
315 DataUseReportKey(label, tag, data_use.connection_type, data_use.mcc_mnc); | 199 DataUseReportKey(label, tag, data_use.connection_type, data_use.mcc_mnc); |
316 | 200 |
317 DataUseReport report = | 201 DataUseReport report = |
318 DataUseReport(start_time, end_time, data_use.rx_bytes, data_use.tx_bytes); | 202 DataUseReport(start_time, end_time, data_use.rx_bytes, data_use.tx_bytes); |
319 | 203 |
320 // Check if the |data_use_report_key| is already in the buffered reports. | 204 // Check if the |data_use_report_key| is already in the buffered reports. |
321 DataUseReports::iterator it = | 205 DataUseReports::iterator it = |
322 buffered_data_reports_.find(data_use_report_key); | 206 buffered_data_reports_.find(data_use_report_key); |
323 if (it == buffered_data_reports_.end()) { | 207 if (it == buffered_data_reports_.end()) { |
324 // Limit the buffer size. | 208 // Limit the buffer size. |
325 if (buffered_data_reports_.size() == kMaxBufferSize) { | 209 if (buffered_data_reports_.size() == |
210 ExternalDataUseReporter::kMaxBufferSize) { | |
326 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_LOST, | 211 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_LOST, |
327 data_use.rx_bytes + data_use.tx_bytes); | 212 data_use.rx_bytes + data_use.tx_bytes); |
328 return; | 213 return; |
329 } | 214 } |
330 buffered_data_reports_.insert(std::make_pair(data_use_report_key, report)); | 215 buffered_data_reports_.insert(std::make_pair(data_use_report_key, report)); |
331 } else { | 216 } else { |
332 DataUseReport existing_report = DataUseReport(it->second); | 217 DataUseReport existing_report = DataUseReport(it->second); |
333 DataUseReport merged_report = DataUseReport( | 218 DataUseReport merged_report = DataUseReport( |
334 std::min(existing_report.start_time, report.start_time), | 219 std::min(existing_report.start_time, report.start_time), |
335 std::max(existing_report.end_time, report.end_time), | 220 std::max(existing_report.end_time, report.end_time), |
336 existing_report.bytes_downloaded + report.bytes_downloaded, | 221 existing_report.bytes_downloaded + report.bytes_downloaded, |
337 existing_report.bytes_uploaded + report.bytes_uploaded); | 222 existing_report.bytes_uploaded + report.bytes_uploaded); |
338 buffered_data_reports_.erase(it); | 223 buffered_data_reports_.erase(it); |
339 buffered_data_reports_.insert( | 224 buffered_data_reports_.insert( |
340 std::make_pair(data_use_report_key, merged_report)); | 225 std::make_pair(data_use_report_key, merged_report)); |
341 } | 226 } |
342 total_bytes_buffered_ += (data_use.rx_bytes + data_use.tx_bytes); | 227 total_bytes_buffered_ += (data_use.rx_bytes + data_use.tx_bytes); |
343 | 228 |
344 DCHECK_LT(0U, buffered_data_reports_.size()); | 229 DCHECK_LT(0U, buffered_data_reports_.size()); |
345 DCHECK_LE(buffered_data_reports_.size(), kMaxBufferSize); | 230 DCHECK_LE(buffered_data_reports_.size(), |
231 ExternalDataUseReporter::kMaxBufferSize); | |
346 } | 232 } |
347 | 233 |
348 void ExternalDataUseObserver::SubmitBufferedDataUseReport(bool immediate) { | 234 void ExternalDataUseReporter::SubmitBufferedDataUseReport(bool immediate) { |
349 DCHECK(thread_checker_.CalledOnValidThread()); | 235 DCHECK(thread_checker_.CalledOnValidThread()); |
350 | 236 |
351 const base::TimeTicks ticks_now = base::TimeTicks::Now(); | 237 const base::TimeTicks ticks_now = base::TimeTicks::Now(); |
352 | 238 |
353 // Return if a data use report has been pending for less than | 239 // Return if a data use report has been pending for less than |
354 // |data_report_submit_timeout_| duration. | 240 // |data_report_submit_timeout_| duration. |
355 if (!last_data_report_submitted_ticks_.is_null() && | 241 if (!last_data_report_submitted_ticks_.is_null() && |
356 ticks_now - last_data_report_submitted_ticks_ < | 242 ticks_now - last_data_report_submitted_ticks_ < |
357 data_report_submit_timeout_) { | 243 data_report_submit_timeout_) { |
358 return; | 244 return; |
(...skipping 20 matching lines...) Expand all Loading... | |
379 | 265 |
380 DCHECK_EQ(0, pending_report_bytes_); | 266 DCHECK_EQ(0, pending_report_bytes_); |
381 DCHECK(last_data_report_submitted_ticks_.is_null()); | 267 DCHECK(last_data_report_submitted_ticks_.is_null()); |
382 pending_report_bytes_ = report.bytes_downloaded + report.bytes_uploaded; | 268 pending_report_bytes_ = report.bytes_downloaded + report.bytes_uploaded; |
383 last_data_report_submitted_ticks_ = ticks_now; | 269 last_data_report_submitted_ticks_ = ticks_now; |
384 | 270 |
385 // Remove the entry from the map. | 271 // Remove the entry from the map. |
386 buffered_data_reports_.erase(it); | 272 buffered_data_reports_.erase(it); |
387 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); | 273 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); |
388 | 274 |
389 // It is okay to use base::Unretained here since | 275 external_data_use_observer_bridge_->ReportDataUse( |
390 // |external_data_use_observer_bridge_| is owned by |this|, and is destroyed | 276 key.label, key.tag, key.connection_type, key.mcc_mnc, report.start_time, |
391 // on UI thread when |this| is destroyed. | 277 report.end_time, report.bytes_downloaded, report.bytes_uploaded); |
392 ui_task_runner_->PostTask( | |
393 FROM_HERE, | |
394 base::Bind(&ExternalDataUseObserverBridge::ReportDataUse, | |
395 base::Unretained(external_data_use_observer_bridge_), | |
396 key.label, key.tag, key.connection_type, key.mcc_mnc, | |
397 report.start_time, report.end_time, report.bytes_downloaded, | |
398 report.bytes_uploaded)); | |
399 } | 278 } |
400 | 279 |
401 base::WeakPtr<ExternalDataUseObserver> ExternalDataUseObserver::GetWeakPtr() { | 280 ExternalDataUseReporter::DataUseReportKey::DataUseReportKey( |
402 DCHECK(thread_checker_.CalledOnValidThread()); | |
403 return weak_factory_.GetWeakPtr(); | |
404 } | |
405 | |
406 DataUseTabModel* ExternalDataUseObserver::GetDataUseTabModel() const { | |
407 DCHECK(thread_checker_.CalledOnValidThread()); | |
408 return data_use_tab_model_; | |
409 } | |
410 | |
411 ExternalDataUseObserver::DataUseReportKey::DataUseReportKey( | |
412 const std::string& label, | 281 const std::string& label, |
413 const std::string& tag, | 282 const std::string& tag, |
414 net::NetworkChangeNotifier::ConnectionType connection_type, | 283 net::NetworkChangeNotifier::ConnectionType connection_type, |
415 const std::string& mcc_mnc) | 284 const std::string& mcc_mnc) |
416 : label(label), | 285 : label(label), |
417 tag(tag), | 286 tag(tag), |
418 connection_type(connection_type), | 287 connection_type(connection_type), |
419 mcc_mnc(mcc_mnc) {} | 288 mcc_mnc(mcc_mnc) {} |
420 | 289 |
421 ExternalDataUseObserver::DataUseReportKey::DataUseReportKey( | 290 ExternalDataUseReporter::DataUseReportKey::DataUseReportKey( |
422 const ExternalDataUseObserver::DataUseReportKey& other) = | 291 const ExternalDataUseReporter::DataUseReportKey& other) = default; |
423 default; | |
424 | 292 |
425 bool ExternalDataUseObserver::DataUseReportKey::operator==( | 293 bool ExternalDataUseReporter::DataUseReportKey::operator==( |
426 const DataUseReportKey& other) const { | 294 const DataUseReportKey& other) const { |
427 return label == other.label && tag == other.tag && | 295 return label == other.label && tag == other.tag && |
428 connection_type == other.connection_type && mcc_mnc == other.mcc_mnc; | 296 connection_type == other.connection_type && mcc_mnc == other.mcc_mnc; |
429 } | 297 } |
430 | 298 |
431 ExternalDataUseObserver::DataUseReport::DataUseReport( | 299 ExternalDataUseReporter::DataUseReport::DataUseReport( |
432 const base::Time& start_time, | 300 const base::Time& start_time, |
433 const base::Time& end_time, | 301 const base::Time& end_time, |
434 int64_t bytes_downloaded, | 302 int64_t bytes_downloaded, |
435 int64_t bytes_uploaded) | 303 int64_t bytes_uploaded) |
436 : start_time(start_time), | 304 : start_time(start_time), |
437 end_time(end_time), | 305 end_time(end_time), |
438 bytes_downloaded(bytes_downloaded), | 306 bytes_downloaded(bytes_downloaded), |
439 bytes_uploaded(bytes_uploaded) {} | 307 bytes_uploaded(bytes_uploaded) {} |
440 | 308 |
441 size_t ExternalDataUseObserver::DataUseReportKeyHash::operator()( | 309 size_t ExternalDataUseReporter::DataUseReportKeyHash::operator()( |
442 const DataUseReportKey& k) const { | 310 const DataUseReportKey& k) const { |
443 // The hash is computed by hashing individual variables and combining them | 311 // The hash is computed by hashing individual variables and combining them |
444 // using prime numbers. Prime numbers are used for multiplication because the | 312 // using prime numbers. Prime numbers are used for multiplication because the |
445 // number of buckets used by map is always an even number. Using a prime | 313 // number of buckets used by map is always an even number. Using a prime |
446 // number ensures that for two different DataUseReportKey objects (say |j| | 314 // number ensures that for two different DataUseReportKey objects (say |j| |
447 // and |k|), if the hash value of |k.label| is equal to hash value of | 315 // and |k|), if the hash value of |k.label| is equal to hash value of |
448 // |j.mcc_mnc|, then |j| and |k| map to different buckets. Large prime | 316 // |j.mcc_mnc|, then |j| and |k| map to different buckets. Large prime |
449 // numbers are used so that hash value is spread over a larger range. | 317 // numbers are used so that hash value is spread over a larger range. |
450 std::hash<std::string> hash_function; | 318 std::hash<std::string> hash_function; |
451 size_t hash = 1; | 319 size_t hash = 1; |
452 hash = hash * 23 + hash_function(k.label); | 320 hash = hash * 23 + hash_function(k.label); |
453 hash = hash * 31 + hash_function(k.tag); | 321 hash = hash * 31 + hash_function(k.tag); |
454 hash = hash * 43 + k.connection_type; | 322 hash = hash * 43 + k.connection_type; |
455 hash = hash * 83 + hash_function(k.mcc_mnc); | 323 hash = hash * 83 + hash_function(k.mcc_mnc); |
456 return hash; | 324 return hash; |
457 } | 325 } |
458 | 326 |
459 } // namespace android | 327 } // namespace android |
460 | 328 |
461 } // namespace chrome | 329 } // namespace chrome |
OLD | NEW |