summaryrefslogtreecommitdiffstats
path: root/toolkit/components/reputationservice/ApplicationReputationTelemetryUtils.cpp
blob: 8487e866c59c7456069a8403b3582ed7e35cc5a9 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ApplicationReputationTelemetryUtils.h"
#include "mozilla/Assertions.h"
#include "chrome/common/safe_browsing/csd.pb.h"

using ServerLabel = mozilla::Telemetry::LABELS_APPLICATION_REPUTATION_SERVER_2;
using ServerVerdictLabel =
    mozilla::Telemetry::LABELS_APPLICATION_REPUTATION_SERVER_VERDICT_2;

struct NSErrorTelemetryResult {
  nsresult mValue;
  ServerLabel mLabel;
};

static const NSErrorTelemetryResult sResult[] = {
    {
        NS_ERROR_ALREADY_CONNECTED,
        ServerLabel::ErrAlreadyConnected,
    },
    {
        NS_ERROR_NOT_CONNECTED,
        ServerLabel::ErrNotConnected,
    },
    {
        NS_ERROR_CONNECTION_REFUSED,
        ServerLabel::ErrConnectionRefused,
    },
    {
        NS_ERROR_NET_TIMEOUT,
        ServerLabel::ErrNetTimeout,
    },
    {
        NS_ERROR_OFFLINE,
        ServerLabel::ErrOffline,
    },
    {
        NS_ERROR_PORT_ACCESS_NOT_ALLOWED,
        ServerLabel::ErrPortAccess,
    },
    {
        NS_ERROR_NET_RESET,
        ServerLabel::ErrNetReset,
    },
    {
        NS_ERROR_NET_INTERRUPT,
        ServerLabel::ErrNetInterrupt,
    },
    {
        NS_ERROR_PROXY_CONNECTION_REFUSED,
        ServerLabel::ErrProxyConnection,
    },
    {
        NS_ERROR_NET_PARTIAL_TRANSFER,
        ServerLabel::ErrNetPartial,
    },
    {
        NS_ERROR_NET_INADEQUATE_SECURITY,
        ServerLabel::ErrNetInadequate,
    },
    {
        NS_ERROR_UNKNOWN_HOST,
        ServerLabel::ErrUnknownHost,
    },
    {
        NS_ERROR_DNS_LOOKUP_QUEUE_FULL,
        ServerLabel::ErrDNSLookupQueue,
    },
    {
        NS_ERROR_UNKNOWN_PROXY_HOST,
        ServerLabel::ErrUnknownProxyHost,
    },
};

mozilla::Telemetry::LABELS_APPLICATION_REPUTATION_SERVER_2 NSErrorToLabel(
    nsresult aRv) {
  MOZ_ASSERT(aRv != NS_OK);

  for (const auto& p : sResult) {
    if (p.mValue == aRv) {
      return p.mLabel;
    }
  }
  return ServerLabel::ErrOthers;
}

mozilla::Telemetry::LABELS_APPLICATION_REPUTATION_SERVER_2 HTTPStatusToLabel(
    uint32_t aStatus) {
  MOZ_ASSERT(aStatus != 200);

  switch (aStatus) {
    case 100:
    case 101:
      // Unexpected 1xx return code
      return ServerLabel::HTTP1xx;
    case 201:
    case 202:
    case 203:
    case 205:
    case 206:
      // Unexpected 2xx return code
      return ServerLabel::HTTP2xx;
    case 204:
      // No Content
      return ServerLabel::HTTP204;
    case 300:
    case 301:
    case 302:
    case 303:
    case 304:
    case 305:
    case 307:
    case 308:
      // Unexpected 3xx return code
      return ServerLabel::HTTP3xx;
    case 400:
      // Bad Request - The HTTP request was not correctly formed.
      // The client did not provide all required CGI parameters.
      return ServerLabel::HTTP400;
    case 401:
    case 402:
    case 405:
    case 406:
    case 407:
    case 409:
    case 410:
    case 411:
    case 412:
    case 414:
    case 415:
    case 416:
    case 417:
    case 421:
    case 426:
    case 428:
    case 429:
    case 431:
    case 451:
      // Unexpected 4xx return code
      return ServerLabel::HTTP4xx;
    case 403:
      // Forbidden - The client id is invalid.
      return ServerLabel::HTTP403;
    case 404:
      // Not Found
      return ServerLabel::HTTP404;
    case 408:
      // Request Timeout
      return ServerLabel::HTTP408;
    case 413:
      // Request Entity Too Large
      return ServerLabel::HTTP413;
    case 500:
    case 501:
    case 510:
      // Unexpected 5xx return code
      return ServerLabel::HTTP5xx;
    case 502:
    case 504:
    case 511:
      // Local network errors, we'll ignore these.
      return ServerLabel::HTTP502_504_511;
    case 503:
      // Service Unavailable - The server cannot handle the request.
      // Clients MUST follow the backoff behavior specified in the
      // Request Frequency section.
      return ServerLabel::HTTP503;
    case 505:
      // HTTP Version Not Supported - The server CANNOT handle the requested
      // protocol major version.
      return ServerLabel::HTTP505;
    default:
      return ServerLabel::HTTPOthers;
  }
}

mozilla::Telemetry::LABELS_APPLICATION_REPUTATION_SERVER_VERDICT_2
VerdictToLabel(uint32_t aVerdict) {
  switch (aVerdict) {
    case safe_browsing::ClientDownloadResponse::DANGEROUS:
      return ServerVerdictLabel::Dangerous;
    case safe_browsing::ClientDownloadResponse::DANGEROUS_HOST:
      return ServerVerdictLabel::DangerousHost;
    case safe_browsing::ClientDownloadResponse::POTENTIALLY_UNWANTED:
      return ServerVerdictLabel::PotentiallyUnwanted;
    case safe_browsing::ClientDownloadResponse::UNCOMMON:
      return ServerVerdictLabel::Uncommon;
    case safe_browsing::ClientDownloadResponse::UNKNOWN:
      return ServerVerdictLabel::Unknown;
    default:
      return ServerVerdictLabel::Safe;
  }
}