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 "chrome/browser/android/data_usage/external_data_use_observer.h" | 5 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/android/context_utils.h" | 9 #include "base/android/context_utils.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
14 #include "base/metrics/histogram_macros.h" | |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "chrome/browser/android/data_usage/data_use_tab_model.h" | 16 #include "chrome/browser/android/data_usage/data_use_tab_model.h" |
16 #include "components/data_usage/core/data_use.h" | 17 #include "components/data_usage/core/data_use.h" |
17 #include "components/variations/variations_associated_data.h" | 18 #include "components/variations/variations_associated_data.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 #include "jni/ExternalDataUseObserver_jni.h" | 20 #include "jni/ExternalDataUseObserver_jni.h" |
20 #include "third_party/re2/re2/re2.h" | 21 #include "third_party/re2/re2/re2.h" |
21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
22 | 23 |
23 using base::android::ConvertUTF8ToJavaString; | 24 using base::android::ConvertUTF8ToJavaString; |
24 using base::android::ToJavaArrayOfStrings; | 25 using base::android::ToJavaArrayOfStrings; |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
29 // Result of data usage report submission. | |
30 enum DataUsageReportSubmissionResult { | |
31 // Submission of data use report to the external observer was successful. | |
32 DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL = 0, | |
33 // Submission of data use report to the external observer returned error. | |
34 DATAUSAGE_REPORT_SUBMISSION_FAILED = 1, | |
35 // Submission of data use report to the external observer timed out. | |
36 DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT = 2, | |
37 // Data use report was lost before an attempt was made to submit it. | |
38 DATAUSAGE_REPORT_SUBMISSION_LOST = 3, | |
39 DATAUSAGE_REPORT_SUBMISSION_BOUNDARY = 4 | |
40 }; | |
41 | |
42 // Record the result of data use report submission. |bytes| is the sum of send | |
43 // and received bytes in the report. | |
44 void RecordDataUsageReportSubmission(DataUsageReportSubmissionResult result, | |
45 int64_t bytes) { | |
46 UMA_HISTOGRAM_ENUMERATION("DataUsage.ReportSubmissionResult", result, | |
47 DATAUSAGE_REPORT_SUBMISSION_BOUNDARY); | |
48 switch (result) { | |
49 case DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL: | |
50 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Successful.Bytes", | |
51 bytes); | |
sclittle
2015/12/02 19:07:54
Histogram samples are casted to int32_t when they'
tbansal1
2015/12/03 03:19:45
Good point. Done.
| |
52 break; | |
53 case DATAUSAGE_REPORT_SUBMISSION_FAILED: | |
54 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Failed.Bytes", bytes); | |
55 break; | |
56 case DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT: | |
57 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.TimedOut.Bytes", bytes); | |
58 break; | |
59 case DATAUSAGE_REPORT_SUBMISSION_LOST: | |
60 UMA_HISTOGRAM_COUNTS("DataUsage.ReportSubmission.Lost.Bytes", bytes); | |
61 break; | |
62 default: | |
63 NOTIMPLEMENTED(); | |
64 break; | |
65 } | |
66 } | |
67 | |
28 // Default duration after which matching rules are periodically fetched. May be | 68 // Default duration after which matching rules are periodically fetched. May be |
29 // overridden by the field trial. | 69 // overridden by the field trial. |
30 const int kDefaultFetchMatchingRulesDurationSeconds = 60 * 15; // 15 minutes. | 70 const int kDefaultFetchMatchingRulesDurationSeconds = 60 * 15; // 15 minutes. |
31 | 71 |
72 // Default duration after which a pending data use report is considered timed | |
73 // out. May be overridden by the field trial. | |
74 const int kDefaultDataUseReportSubmitTimeoutSeconds = 60 * 2; // 2 minutes. | |
sclittle
2015/12/02 19:07:54
nit: Since you're controlling this with a field tr
tbansal1
2015/12/03 03:19:45
Done.
| |
75 | |
32 // Default value of the minimum number of bytes that should be buffered before | 76 // Default value of the minimum number of bytes that should be buffered before |
33 // a data use report is submitted. May be overridden by the field trial. | 77 // a data use report is submitted. May be overridden by the field trial. |
34 const int64_t kDefaultDataUseReportMinBytes = 100 * 1024; // 100 KB. | 78 const int64_t kDefaultDataUseReportMinBytes = 100 * 1024; // 100 KB. |
35 | 79 |
36 // Populates various parameters from the values specified in the field trial. | 80 // Populates various parameters from the values specified in the field trial. |
37 int32_t GetFetchMatchingRulesDurationSeconds() { | 81 int32_t GetFetchMatchingRulesDurationSeconds() { |
38 int32_t duration_seconds = -1; | 82 int32_t duration_seconds = -1; |
39 std::string variation_value = variations::GetVariationParamValue( | 83 const std::string variation_value = variations::GetVariationParamValue( |
40 chrome::android::ExternalDataUseObserver:: | 84 chrome::android::ExternalDataUseObserver:: |
41 kExternalDataUseObserverFieldTrial, | 85 kExternalDataUseObserverFieldTrial, |
42 "fetch_matching_rules_duration_seconds"); | 86 "fetch_matching_rules_duration_seconds"); |
43 if (!variation_value.empty() && | 87 if (!variation_value.empty() && |
44 base::StringToInt(variation_value, &duration_seconds)) { | 88 base::StringToInt(variation_value, &duration_seconds)) { |
45 DCHECK_LE(0, duration_seconds); | 89 DCHECK_LE(0, duration_seconds); |
46 return duration_seconds; | 90 return duration_seconds; |
47 } | 91 } |
48 return kDefaultFetchMatchingRulesDurationSeconds; | 92 return kDefaultFetchMatchingRulesDurationSeconds; |
49 } | 93 } |
50 | 94 |
51 // Populates various parameters from the values specified in the field trial. | 95 // Populates various parameters from the values specified in the field trial. |
96 int32_t GetDataReportSubmitTimeoutSeconds() { | |
97 int32_t duration_seconds = -1; | |
98 const std::string variation_value = variations::GetVariationParamValue( | |
99 chrome::android::ExternalDataUseObserver:: | |
100 kExternalDataUseObserverFieldTrial, | |
101 "data_report_submit_timeout_seconds"); | |
102 if (!variation_value.empty() && | |
103 base::StringToInt(variation_value, &duration_seconds)) { | |
104 DCHECK_LE(0, duration_seconds); | |
105 return duration_seconds; | |
106 } | |
107 return kDefaultDataUseReportSubmitTimeoutSeconds; | |
108 } | |
109 | |
110 // Populates various parameters from the values specified in the field trial. | |
52 int64_t GetMinBytes() { | 111 int64_t GetMinBytes() { |
53 int64_t min_bytes = -1; | 112 int64_t min_bytes = -1; |
54 std::string variation_value = variations::GetVariationParamValue( | 113 const std::string variation_value = variations::GetVariationParamValue( |
55 chrome::android::ExternalDataUseObserver:: | 114 chrome::android::ExternalDataUseObserver:: |
56 kExternalDataUseObserverFieldTrial, | 115 kExternalDataUseObserverFieldTrial, |
57 "data_use_report_min_bytes"); | 116 "data_use_report_min_bytes"); |
58 if (!variation_value.empty() && | 117 if (!variation_value.empty() && |
59 base::StringToInt64(variation_value, &min_bytes)) { | 118 base::StringToInt64(variation_value, &min_bytes)) { |
60 DCHECK_LE(0, min_bytes); | 119 DCHECK_LE(0, min_bytes); |
61 return min_bytes; | 120 return min_bytes; |
62 } | 121 } |
63 return kDefaultDataUseReportMinBytes; | 122 return kDefaultDataUseReportMinBytes; |
64 } | 123 } |
(...skipping 10 matching lines...) Expand all Loading... | |
75 | 134 |
76 // static | 135 // static |
77 const size_t ExternalDataUseObserver::kMaxBufferSize = 100; | 136 const size_t ExternalDataUseObserver::kMaxBufferSize = 100; |
78 | 137 |
79 ExternalDataUseObserver::ExternalDataUseObserver( | 138 ExternalDataUseObserver::ExternalDataUseObserver( |
80 data_usage::DataUseAggregator* data_use_aggregator, | 139 data_usage::DataUseAggregator* data_use_aggregator, |
81 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 140 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
82 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) | 141 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) |
83 : data_use_aggregator_(data_use_aggregator), | 142 : data_use_aggregator_(data_use_aggregator), |
84 matching_rules_fetch_pending_(false), | 143 matching_rules_fetch_pending_(false), |
85 submit_data_report_pending_(false), | 144 last_data_report_submitted_(base::TimeTicks()), |
145 pending_report_bytes_(0), | |
86 registered_as_observer_(false), | 146 registered_as_observer_(false), |
87 io_task_runner_(io_task_runner), | 147 io_task_runner_(io_task_runner), |
88 ui_task_runner_(ui_task_runner), | 148 ui_task_runner_(ui_task_runner), |
89 previous_report_time_(base::Time::Now()), | 149 previous_report_time_(base::Time::Now()), |
90 last_matching_rules_fetch_time_(base::TimeTicks::Now()), | 150 last_matching_rules_fetch_time_(base::TimeTicks::Now()), |
91 total_bytes_buffered_(0), | 151 total_bytes_buffered_(0), |
92 fetch_matching_rules_duration_( | 152 fetch_matching_rules_duration_( |
93 base::TimeDelta::FromSeconds(GetFetchMatchingRulesDurationSeconds())), | 153 base::TimeDelta::FromSeconds(GetFetchMatchingRulesDurationSeconds())), |
94 data_use_report_min_bytes_(GetMinBytes()), | 154 data_use_report_min_bytes_(GetMinBytes()), |
155 data_report_submit_timeout_( | |
156 base::TimeDelta::FromSeconds(GetDataReportSubmitTimeoutSeconds())), | |
95 io_weak_factory_(this), | 157 io_weak_factory_(this), |
96 ui_weak_factory_(this) { | 158 ui_weak_factory_(this) { |
97 DCHECK(data_use_aggregator_); | 159 DCHECK(data_use_aggregator_); |
98 DCHECK(io_task_runner_); | 160 DCHECK(io_task_runner_); |
99 DCHECK(ui_task_runner_); | 161 DCHECK(ui_task_runner_); |
162 DCHECK(last_data_report_submitted_.is_null()); | |
100 | 163 |
101 ui_task_runner_->PostTask( | 164 ui_task_runner_->PostTask( |
102 FROM_HERE, | 165 FROM_HERE, |
103 base::Bind(&ExternalDataUseObserver::CreateJavaObjectOnUIThread, | 166 base::Bind(&ExternalDataUseObserver::CreateJavaObjectOnUIThread, |
104 GetUIWeakPtr())); | 167 GetUIWeakPtr())); |
105 | 168 |
106 ui_task_runner_->PostTask( | 169 ui_task_runner_->PostTask( |
107 FROM_HERE, | 170 FROM_HERE, |
108 base::Bind(&ExternalDataUseObserver::FetchMatchingRulesOnUIThread, | 171 base::Bind(&ExternalDataUseObserver::FetchMatchingRulesOnUIThread, |
109 GetUIWeakPtr())); | 172 GetUIWeakPtr())); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 base::WeakPtr<ExternalDataUseObserver> ExternalDataUseObserver::GetIOWeakPtr() { | 260 base::WeakPtr<ExternalDataUseObserver> ExternalDataUseObserver::GetIOWeakPtr() { |
198 return io_weak_factory_.GetWeakPtr(); | 261 return io_weak_factory_.GetWeakPtr(); |
199 } | 262 } |
200 | 263 |
201 base::WeakPtr<ExternalDataUseObserver> ExternalDataUseObserver::GetUIWeakPtr() { | 264 base::WeakPtr<ExternalDataUseObserver> ExternalDataUseObserver::GetUIWeakPtr() { |
202 return ui_weak_factory_.GetWeakPtr(); | 265 return ui_weak_factory_.GetWeakPtr(); |
203 } | 266 } |
204 | 267 |
205 void ExternalDataUseObserver::OnReportDataUseDoneOnIOThread(bool success) { | 268 void ExternalDataUseObserver::OnReportDataUseDoneOnIOThread(bool success) { |
206 DCHECK(thread_checker_.CalledOnValidThread()); | 269 DCHECK(thread_checker_.CalledOnValidThread()); |
207 DCHECK(submit_data_report_pending_); | 270 DCHECK(!last_data_report_submitted_.is_null()); |
208 | 271 |
209 // TODO(tbansal): If not successful, record UMA. | 272 if (success) { |
273 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, | |
274 pending_report_bytes_); | |
275 } else { | |
276 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_FAILED, | |
277 pending_report_bytes_); | |
278 } | |
210 | 279 |
211 submit_data_report_pending_ = false; | 280 last_data_report_submitted_ = base::TimeTicks(); |
281 pending_report_bytes_ = 0; | |
212 | 282 |
213 SubmitBufferedDataUseReport(); | 283 SubmitBufferedDataUseReport(); |
214 } | 284 } |
215 | 285 |
216 void ExternalDataUseObserver::OnDataUse(const data_usage::DataUse& data_use) { | 286 void ExternalDataUseObserver::OnDataUse(const data_usage::DataUse& data_use) { |
217 DCHECK(thread_checker_.CalledOnValidThread()); | 287 DCHECK(thread_checker_.CalledOnValidThread()); |
218 const base::TimeTicks now_ticks = base::TimeTicks::Now(); | 288 const base::TimeTicks now_ticks = base::TimeTicks::Now(); |
219 const base::Time now_time = base::Time::Now(); | 289 const base::Time now_time = base::Time::Now(); |
220 | 290 |
221 // If the time when the matching rules were last fetched is more than | 291 // If the time when the matching rules were last fetched is more than |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 | 331 |
262 DataUseReport report = | 332 DataUseReport report = |
263 DataUseReport(start_time, end_time, data_use.rx_bytes, data_use.tx_bytes); | 333 DataUseReport(start_time, end_time, data_use.rx_bytes, data_use.tx_bytes); |
264 | 334 |
265 // Check if the |data_use_report_key| is already in the buffered reports. | 335 // Check if the |data_use_report_key| is already in the buffered reports. |
266 DataUseReports::iterator it = | 336 DataUseReports::iterator it = |
267 buffered_data_reports_.find(data_use_report_key); | 337 buffered_data_reports_.find(data_use_report_key); |
268 if (it == buffered_data_reports_.end()) { | 338 if (it == buffered_data_reports_.end()) { |
269 // Limit the buffer size. | 339 // Limit the buffer size. |
270 if (buffered_data_reports_.size() == kMaxBufferSize) { | 340 if (buffered_data_reports_.size() == kMaxBufferSize) { |
271 // TODO(tbansal): Add UMA to track impact of lost reports. | 341 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_LOST, |
342 data_use.rx_bytes + data_use.tx_bytes); | |
272 return; | 343 return; |
273 } | 344 } |
274 buffered_data_reports_.insert(std::make_pair(data_use_report_key, report)); | 345 buffered_data_reports_.insert(std::make_pair(data_use_report_key, report)); |
275 } else { | 346 } else { |
276 DataUseReport existing_report = DataUseReport(it->second); | 347 DataUseReport existing_report = DataUseReport(it->second); |
277 DataUseReport merged_report = DataUseReport( | 348 DataUseReport merged_report = DataUseReport( |
278 std::min(existing_report.start_time, report.start_time), | 349 std::min(existing_report.start_time, report.start_time), |
279 std::max(existing_report.end_time, report.end_time), | 350 std::max(existing_report.end_time, report.end_time), |
280 existing_report.bytes_downloaded + report.bytes_downloaded, | 351 existing_report.bytes_downloaded + report.bytes_downloaded, |
281 existing_report.bytes_uploaded + report.bytes_uploaded); | 352 existing_report.bytes_uploaded + report.bytes_uploaded); |
282 buffered_data_reports_.erase(it); | 353 buffered_data_reports_.erase(it); |
283 buffered_data_reports_.insert( | 354 buffered_data_reports_.insert( |
284 std::make_pair(data_use_report_key, merged_report)); | 355 std::make_pair(data_use_report_key, merged_report)); |
285 } | 356 } |
286 total_bytes_buffered_ += (data_use.rx_bytes + data_use.tx_bytes); | 357 total_bytes_buffered_ += (data_use.rx_bytes + data_use.tx_bytes); |
287 | 358 |
359 DCHECK_LT(0U, buffered_data_reports_.size()); | |
288 DCHECK_LE(buffered_data_reports_.size(), kMaxBufferSize); | 360 DCHECK_LE(buffered_data_reports_.size(), kMaxBufferSize); |
289 } | 361 } |
290 | 362 |
291 void ExternalDataUseObserver::SubmitBufferedDataUseReport() { | 363 void ExternalDataUseObserver::SubmitBufferedDataUseReport() { |
292 DCHECK(thread_checker_.CalledOnValidThread()); | 364 DCHECK(thread_checker_.CalledOnValidThread()); |
293 | 365 |
294 if (submit_data_report_pending_ || buffered_data_reports_.empty()) | 366 const base::TimeTicks ticks_now = base::TimeTicks::Now(); |
367 | |
368 // Return if a data use report has been pending for less than | |
369 // |data_report_submit_timeout_| duration. | |
370 if (!last_data_report_submitted_.is_null() && | |
371 ticks_now - last_data_report_submitted_ < data_report_submit_timeout_) { | |
372 return; | |
373 } | |
374 | |
375 if (buffered_data_reports_.empty()) | |
295 return; | 376 return; |
296 | 377 |
297 if (total_bytes_buffered_ < data_use_report_min_bytes_) | 378 if (total_bytes_buffered_ < data_use_report_min_bytes_) |
298 return; | 379 return; |
299 | 380 |
381 if (!last_data_report_submitted_.is_null()) { | |
382 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, | |
383 pending_report_bytes_); | |
384 pending_report_bytes_ = 0; | |
385 last_data_report_submitted_ = base::TimeTicks(); | |
386 } | |
387 | |
300 // Send one data use report. | 388 // Send one data use report. |
301 DataUseReports::iterator it = buffered_data_reports_.begin(); | 389 DataUseReports::iterator it = buffered_data_reports_.begin(); |
302 DataUseReportKey key = it->first; | 390 DataUseReportKey key = it->first; |
303 DataUseReport report = it->second; | 391 DataUseReport report = it->second; |
304 | 392 |
393 DCHECK_EQ(0, pending_report_bytes_); | |
394 DCHECK(last_data_report_submitted_.is_null()); | |
395 pending_report_bytes_ = report.bytes_downloaded + report.bytes_uploaded; | |
396 last_data_report_submitted_ = ticks_now; | |
397 | |
305 // Remove the entry from the map. | 398 // Remove the entry from the map. |
306 buffered_data_reports_.erase(it); | 399 buffered_data_reports_.erase(it); |
307 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); | 400 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); |
308 | 401 |
309 submit_data_report_pending_ = true; | |
310 | |
311 ui_task_runner_->PostTask( | 402 ui_task_runner_->PostTask( |
312 FROM_HERE, base::Bind(&ExternalDataUseObserver::ReportDataUseOnUIThread, | 403 FROM_HERE, base::Bind(&ExternalDataUseObserver::ReportDataUseOnUIThread, |
313 GetIOWeakPtr(), key, report)); | 404 GetIOWeakPtr(), key, report)); |
314 } | 405 } |
315 | 406 |
316 void ExternalDataUseObserver::ReportDataUseOnUIThread( | 407 void ExternalDataUseObserver::ReportDataUseOnUIThread( |
317 const DataUseReportKey& key, | 408 const DataUseReportKey& key, |
318 const DataUseReport& report) const { | 409 const DataUseReport& report) const { |
319 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 410 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
320 JNIEnv* env = base::android::AttachCurrentThread(); | 411 JNIEnv* env = base::android::AttachCurrentThread(); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
478 return label_; | 569 return label_; |
479 } | 570 } |
480 | 571 |
481 bool RegisterExternalDataUseObserver(JNIEnv* env) { | 572 bool RegisterExternalDataUseObserver(JNIEnv* env) { |
482 return RegisterNativesImpl(env); | 573 return RegisterNativesImpl(env); |
483 } | 574 } |
484 | 575 |
485 } // namespace android | 576 } // namespace android |
486 | 577 |
487 } // namespace chrome | 578 } // namespace chrome |
OLD | NEW |