File: download_stats.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (149 lines) | stat: -rw-r--r-- 6,193 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/safe_browsing/content/browser/download/download_stats.h"

#include "base/files/file_path.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/time/time.h"
#include "components/download/public/common/download_content.h"
#include "components/download/public/common/download_danger_type.h"
#include "components/safe_browsing/content/common/file_type_policies.h"

namespace {

using safe_browsing::FileTypePolicies;

constexpr char kShownMetricSuffix[] = ".Shown";
constexpr char kBypassedMetricSuffix[] = ".Bypassed";

std::string GetDangerTypeMetricSuffix(
    download::DownloadDangerType danger_type) {
  switch (danger_type) {
    case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE:
      return ".DangerousFileType";
    case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
    case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
    case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST:
    case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_ACCOUNT_COMPROMISE:
    case download::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED:
    case download::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_OPENED_DANGEROUS:
      return ".Malicious";
    case download::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT:
      return ".Uncommon";
    case download::DOWNLOAD_DANGER_TYPE_PROMPT_FOR_SCANNING:
    case download::DOWNLOAD_DANGER_TYPE_PROMPT_FOR_LOCAL_PASSWORD_SCANNING:
    case download::DOWNLOAD_DANGER_TYPE_ASYNC_SCANNING:
    case download::DOWNLOAD_DANGER_TYPE_ASYNC_LOCAL_PASSWORD_SCANNING:
    case download::DOWNLOAD_DANGER_TYPE_BLOCKED_PASSWORD_PROTECTED:
    case download::DOWNLOAD_DANGER_TYPE_BLOCKED_TOO_LARGE:
    case download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_WARNING:
    case download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_BLOCK:
    case download::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_SAFE:
    case download::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_FAILED:
    case download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS:
    case download::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT:
    case download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED:
    case download::DOWNLOAD_DANGER_TYPE_ALLOWLISTED_BY_POLICY:
    case download::DOWNLOAD_DANGER_TYPE_BLOCKED_SCAN_FAILED:
    case download::DOWNLOAD_DANGER_TYPE_MAX:
      return ".Others";
  }
}

void RecordDangerousWarningFileType(download::DownloadDangerType danger_type,
                                    const base::FilePath& file_path,
                                    const std::string& metrics_suffix) {
  base::UmaHistogramSparse(
      "SBClientDownload.Warning.FileType" +
          GetDangerTypeMetricSuffix(danger_type) + metrics_suffix,
      FileTypePolicies::GetInstance()->UmaValueForFile(file_path));
}

void RecordDangerousWarningIsHttps(download::DownloadDangerType danger_type,
                                   bool is_https,
                                   const std::string& metrics_suffix) {
  base::UmaHistogramBoolean("SBClientDownload.Warning.DownloadIsHttps" +
                                GetDangerTypeMetricSuffix(danger_type) +
                                metrics_suffix,
                            is_https);
}

void RecordDangerousWarningHasUserGesture(
    download::DownloadDangerType danger_type,
    bool has_user_gesture,
    const std::string& metrics_suffix) {
  base::UmaHistogramBoolean("SBClientDownload.Warning.DownloadHasUserGesture" +
                                GetDangerTypeMetricSuffix(danger_type) +
                                metrics_suffix,
                            has_user_gesture);
}

}  // namespace

namespace safe_browsing {

void RecordDangerousDownloadWarningShown(
    download::DownloadDangerType danger_type,
    const base::FilePath& file_path,
    bool is_https,
    bool has_user_gesture) {
  RecordDangerousWarningFileType(danger_type, file_path, kShownMetricSuffix);
  RecordDangerousWarningIsHttps(danger_type, is_https, kShownMetricSuffix);
  RecordDangerousWarningHasUserGesture(danger_type, has_user_gesture,
                                       kShownMetricSuffix);
  base::RecordAction(
      base::UserMetricsAction("SafeBrowsing.Download.WarningShown"));
}

void RecordDangerousDownloadWarningBypassed(
    download::DownloadDangerType danger_type,
    const base::FilePath& file_path,
    bool is_https,
    bool has_user_gesture) {
  RecordDangerousWarningFileType(danger_type, file_path, kBypassedMetricSuffix);
  RecordDangerousWarningIsHttps(danger_type, is_https, kBypassedMetricSuffix);
  RecordDangerousWarningHasUserGesture(danger_type, has_user_gesture,
                                       kBypassedMetricSuffix);
  base::RecordAction(
      base::UserMetricsAction("SafeBrowsing.Download.WarningBypassed"));
}

void RecordDownloadFileTypeAttributes(
    DownloadFileType::DangerLevel danger_level,
    bool has_user_gesture,
    bool visited_referrer_before,
    std::optional<base::Time> last_bypass_time) {
  if (danger_level != DownloadFileType::ALLOW_ON_USER_GESTURE) {
    return;
  }
  base::UmaHistogramEnumeration(
      "SBClientDownload.UserGestureFileType.Attributes",
      UserGestureFileTypeAttributes::TOTAL_TYPE_CHECKED);
  if (has_user_gesture) {
    base::UmaHistogramEnumeration(
        "SBClientDownload.UserGestureFileType.Attributes",
        UserGestureFileTypeAttributes::HAS_USER_GESTURE);
  }
  if (visited_referrer_before) {
    base::UmaHistogramEnumeration(
        "SBClientDownload.UserGestureFileType.Attributes",
        UserGestureFileTypeAttributes::HAS_REFERRER_VISIT);
  }
  if (has_user_gesture && visited_referrer_before) {
    base::UmaHistogramEnumeration(
        "SBClientDownload.UserGestureFileType.Attributes",
        UserGestureFileTypeAttributes::
            HAS_BOTH_USER_GESTURE_AND_REFERRER_VISIT);
  }
  if (last_bypass_time) {
    base::UmaHistogramEnumeration(
        "SBClientDownload.UserGestureFileType.Attributes",
        UserGestureFileTypeAttributes::HAS_BYPASSED_DOWNLOAD_WARNING);
  }
}

}  // namespace safe_browsing