OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/base/net_errors.h" | 5 #include "net/base/net_errors.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/strings/stringize_macros.h" | 9 #include "base/strings/stringize_macros.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 // Get all valid error codes into an array as positive numbers, for use in the | 13 // Get all valid error codes into an array as positive numbers, for use in the |
14 // |GetAllErrorCodesForUma| function below. | 14 // |GetAllErrorCodesForUma| function below. |
15 #define NET_ERROR(label, value) -(value), | 15 #define NET_ERROR(label, value) -(value), |
16 const int kAllErrorCodes[] = { | 16 const int kAllErrorCodes[] = { |
17 #include "net/base/net_error_list.h" | 17 #include "net/base/net_error_list.h" |
18 }; | 18 }; |
19 #undef NET_ERROR | 19 #undef NET_ERROR |
20 | 20 |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 namespace net { | 23 namespace net { |
24 | 24 |
25 const char kErrorDomain[] = "net"; | 25 const char kErrorDomain[] = "net"; |
26 | 26 |
27 const char* ErrorToString(int error) { | 27 std::string ErrorToString(int error) { |
| 28 return "net::" + ErrorToShortString(error); |
| 29 } |
| 30 |
| 31 std::string ErrorToShortString(int error) { |
28 if (error == 0) | 32 if (error == 0) |
29 return "net::OK"; | 33 return "OK"; |
30 | 34 |
| 35 const char* error_string; |
31 switch (error) { | 36 switch (error) { |
32 #define NET_ERROR(label, value) \ | 37 #define NET_ERROR(label, value) \ |
33 case ERR_ ## label: \ | 38 case ERR_ ## label: \ |
34 return "net::" STRINGIZE_NO_EXPANSION(ERR_ ## label); | 39 error_string = # label; \ |
| 40 break; |
35 #include "net/base/net_error_list.h" | 41 #include "net/base/net_error_list.h" |
36 #undef NET_ERROR | 42 #undef NET_ERROR |
37 default: | 43 default: |
38 return "net::<unknown>"; | 44 NOTREACHED(); |
| 45 error_string = "<unknown>"; |
39 } | 46 } |
| 47 return std::string("ERR_") + error_string; |
| 48 } |
| 49 |
| 50 bool IsCertificateError(int error) { |
| 51 // Certificate errors are negative integers from net::ERR_CERT_BEGIN |
| 52 // (inclusive) to net::ERR_CERT_END (exclusive) in *decreasing* order. |
| 53 // ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN is currently an exception to this |
| 54 // rule. |
| 55 return (error <= ERR_CERT_BEGIN && error > ERR_CERT_END) || |
| 56 (error == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN); |
40 } | 57 } |
41 | 58 |
42 std::vector<int> GetAllErrorCodesForUma() { | 59 std::vector<int> GetAllErrorCodesForUma() { |
43 return base::CustomHistogram::ArrayToCustomRanges( | 60 return base::CustomHistogram::ArrayToCustomRanges( |
44 kAllErrorCodes, arraysize(kAllErrorCodes)); | 61 kAllErrorCodes, arraysize(kAllErrorCodes)); |
45 } | 62 } |
46 | 63 |
47 Error FileErrorToNetError(base::File::Error file_error) { | 64 Error FileErrorToNetError(base::File::Error file_error) { |
48 switch (file_error) { | 65 switch (file_error) { |
49 case base::File::FILE_OK: | 66 case base::File::FILE_OK: |
50 return net::OK; | 67 return net::OK; |
51 case base::File::FILE_ERROR_ACCESS_DENIED: | 68 case base::File::FILE_ERROR_ACCESS_DENIED: |
52 return net::ERR_ACCESS_DENIED; | 69 return net::ERR_ACCESS_DENIED; |
53 case base::File::FILE_ERROR_INVALID_URL: | 70 case base::File::FILE_ERROR_INVALID_URL: |
54 return net::ERR_INVALID_URL; | 71 return net::ERR_INVALID_URL; |
55 case base::File::FILE_ERROR_NOT_FOUND: | 72 case base::File::FILE_ERROR_NOT_FOUND: |
56 return net::ERR_FILE_NOT_FOUND; | 73 return net::ERR_FILE_NOT_FOUND; |
57 default: | 74 default: |
58 return net::ERR_FAILED; | 75 return net::ERR_FAILED; |
59 } | 76 } |
60 } | 77 } |
61 | 78 |
62 } // namespace net | 79 } // namespace net |
OLD | NEW |