1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/download/public/common/download_ukm_helper.h"
6
7 #include "services/metrics/public/cpp/ukm_builders.h"
8
9 namespace download {
10
11 namespace {
12 // Return the "size" of the bucket based on the allowed percent_error.
CalcBucketIncrement()13 double CalcBucketIncrement() {
14 double percent_error = 10;
15 return log10(1 + percent_error / 100);
16 }
17 } // namespace
18
CalcExponentialBucket(int value)19 int DownloadUkmHelper::CalcExponentialBucket(int value) {
20 return static_cast<int>(floor(log10(value + 1) / CalcBucketIncrement()));
21 }
22
CalcNearestKB(int num_bytes)23 int DownloadUkmHelper::CalcNearestKB(int num_bytes) {
24 return num_bytes / 1024;
25 }
26
RecordDownloadStarted(int download_id,ukm::SourceId source_id,DownloadContent file_type,DownloadSource download_source,DownloadConnectionSecurity state,bool is_same_host_download)27 void DownloadUkmHelper::RecordDownloadStarted(int download_id,
28 ukm::SourceId source_id,
29 DownloadContent file_type,
30 DownloadSource download_source,
31 DownloadConnectionSecurity state,
32 bool is_same_host_download) {
33 ukm::builders::Download_Started(source_id)
34 .SetDownloadId(download_id)
35 .SetFileType(static_cast<int>(file_type))
36 .SetDownloadSource(static_cast<int>(download_source))
37 .SetDownloadConnectionSecurity(static_cast<int>(state))
38 .SetIsSameHostDownload(is_same_host_download)
39 .Record(ukm::UkmRecorder::Get());
40 }
41
RecordDownloadInterrupted(int download_id,base::Optional<int> change_in_file_size,DownloadInterruptReason reason,int resulting_file_size,const base::TimeDelta & time_since_start,int64_t bytes_wasted)42 void DownloadUkmHelper::RecordDownloadInterrupted(
43 int download_id,
44 base::Optional<int> change_in_file_size,
45 DownloadInterruptReason reason,
46 int resulting_file_size,
47 const base::TimeDelta& time_since_start,
48 int64_t bytes_wasted) {
49 ukm::SourceId source_id = ukm::UkmRecorder::GetNewSourceID();
50 ukm::builders::Download_Interrupted builder(source_id);
51 builder.SetDownloadId(download_id)
52 .SetReason(static_cast<int>(reason))
53 .SetResultingFileSize(
54 DownloadUkmHelper::CalcExponentialBucket(resulting_file_size))
55 .SetTimeSinceStart(time_since_start.InMilliseconds())
56 .SetBytesWasted(DownloadUkmHelper::CalcNearestKB(bytes_wasted));
57 if (change_in_file_size.has_value()) {
58 builder.SetChangeInFileSize(
59 DownloadUkmHelper::CalcExponentialBucket(change_in_file_size.value()));
60 }
61 builder.Record(ukm::UkmRecorder::Get());
62 }
63
RecordDownloadResumed(int download_id,ResumeMode mode,const base::TimeDelta & time_since_start)64 void DownloadUkmHelper::RecordDownloadResumed(
65 int download_id,
66 ResumeMode mode,
67 const base::TimeDelta& time_since_start) {
68 ukm::SourceId source_id = ukm::UkmRecorder::GetNewSourceID();
69 ukm::builders::Download_Resumed(source_id)
70 .SetDownloadId(download_id)
71 .SetMode(static_cast<int>(mode))
72 .SetTimeSinceStart(time_since_start.InMilliseconds())
73 .Record(ukm::UkmRecorder::Get());
74 }
75
RecordDownloadCompleted(int download_id,int resulting_file_size,const base::TimeDelta & time_since_start,int64_t bytes_wasted)76 void DownloadUkmHelper::RecordDownloadCompleted(
77 int download_id,
78 int resulting_file_size,
79 const base::TimeDelta& time_since_start,
80 int64_t bytes_wasted) {
81 ukm::SourceId source_id = ukm::UkmRecorder::GetNewSourceID();
82 ukm::builders::Download_Completed(source_id)
83 .SetDownloadId(download_id)
84 .SetResultingFileSize(
85 DownloadUkmHelper::CalcExponentialBucket(resulting_file_size))
86 .SetTimeSinceStart(time_since_start.InMilliseconds())
87 .SetBytesWasted(DownloadUkmHelper::CalcNearestKB(bytes_wasted))
88 .Record(ukm::UkmRecorder::Get());
89 }
90
91 } // namespace download
92