OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/cast_channel/logger.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <string> | |
10 #include <utility> | |
11 | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "components/cast_channel/cast_auth_util.h" | |
15 #include "components/cast_channel/cast_socket.h" | |
16 #include "net/base/net_errors.h" | |
17 | |
18 namespace cast_channel { | |
19 | |
20 using net::IPEndPoint; | |
21 using proto::EventType; | |
22 using proto::Log; | |
23 using proto::SocketEvent; | |
24 | |
25 namespace { | |
26 | |
27 proto::ChallengeReplyErrorType ChallegeReplyErrorToProto( | |
28 AuthResult::ErrorType error_type) { | |
29 switch (error_type) { | |
30 case AuthResult::ERROR_NONE: | |
31 return proto::CHALLENGE_REPLY_ERROR_NONE; | |
32 case AuthResult::ERROR_PEER_CERT_EMPTY: | |
33 return proto::CHALLENGE_REPLY_ERROR_PEER_CERT_EMPTY; | |
34 case AuthResult::ERROR_WRONG_PAYLOAD_TYPE: | |
35 return proto::CHALLENGE_REPLY_ERROR_WRONG_PAYLOAD_TYPE; | |
36 case AuthResult::ERROR_NO_PAYLOAD: | |
37 return proto::CHALLENGE_REPLY_ERROR_NO_PAYLOAD; | |
38 case AuthResult::ERROR_PAYLOAD_PARSING_FAILED: | |
39 return proto::CHALLENGE_REPLY_ERROR_PAYLOAD_PARSING_FAILED; | |
40 case AuthResult::ERROR_MESSAGE_ERROR: | |
41 return proto::CHALLENGE_REPLY_ERROR_MESSAGE_ERROR; | |
42 case AuthResult::ERROR_NO_RESPONSE: | |
43 return proto::CHALLENGE_REPLY_ERROR_NO_RESPONSE; | |
44 case AuthResult::ERROR_FINGERPRINT_NOT_FOUND: | |
45 return proto::CHALLENGE_REPLY_ERROR_FINGERPRINT_NOT_FOUND; | |
46 case AuthResult::ERROR_CERT_PARSING_FAILED: | |
47 return proto::CHALLENGE_REPLY_ERROR_CERT_PARSING_FAILED; | |
48 case AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA: | |
49 return proto::CHALLENGE_REPLY_ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA; | |
50 case AuthResult::ERROR_CANNOT_EXTRACT_PUBLIC_KEY: | |
51 return proto::CHALLENGE_REPLY_ERROR_CANNOT_EXTRACT_PUBLIC_KEY; | |
52 case AuthResult::ERROR_SIGNED_BLOBS_MISMATCH: | |
53 return proto::CHALLENGE_REPLY_ERROR_SIGNED_BLOBS_MISMATCH; | |
54 case AuthResult::ERROR_TLS_CERT_VALIDITY_PERIOD_TOO_LONG: | |
55 return proto::CHALLENGE_REPLY_ERROR_TLS_CERT_VALIDITY_PERIOD_TOO_LONG; | |
56 case AuthResult::ERROR_TLS_CERT_VALID_START_DATE_IN_FUTURE: | |
57 return proto::CHALLENGE_REPLY_ERROR_TLS_CERT_VALID_START_DATE_IN_FUTURE; | |
58 case AuthResult::ERROR_TLS_CERT_EXPIRED: | |
59 return proto::CHALLENGE_REPLY_ERROR_TLS_CERT_EXPIRED; | |
60 case AuthResult::ERROR_CRL_INVALID: | |
61 return proto::CHALLENGE_REPLY_ERROR_CRL_INVALID; | |
62 case AuthResult::ERROR_CERT_REVOKED: | |
63 return proto::CHALLENGE_REPLY_ERROR_CERT_REVOKED; | |
64 default: | |
65 NOTREACHED(); | |
66 return proto::CHALLENGE_REPLY_ERROR_NONE; | |
67 } | |
68 } | |
69 | |
70 // Propagate any error fields set in |event| to |last_errors|. If any error | |
71 // field in |event| is set, then also set |last_errors->event_type|. | |
72 void MaybeSetLastErrors(const SocketEvent& event, LastErrors* last_errors) { | |
73 if (event.has_net_return_value() && | |
74 event.net_return_value() < net::ERR_IO_PENDING) { | |
75 last_errors->net_return_value = event.net_return_value(); | |
76 last_errors->event_type = event.type(); | |
77 } | |
78 if (event.has_challenge_reply_error_type()) { | |
79 last_errors->challenge_reply_error_type = | |
80 event.challenge_reply_error_type(); | |
81 last_errors->event_type = event.type(); | |
82 } | |
83 } | |
84 | |
85 } // namespace | |
86 | |
87 LastErrors::LastErrors() | |
88 : event_type(proto::EVENT_TYPE_UNKNOWN), | |
89 challenge_reply_error_type(proto::CHALLENGE_REPLY_ERROR_NONE), | |
90 net_return_value(net::OK) {} | |
91 | |
92 LastErrors::~LastErrors() {} | |
93 | |
94 Logger::Logger() { | |
95 // Logger may not be necessarily be created on the IO thread, but logging | |
96 // happens exclusively there. | |
97 DETACH_FROM_THREAD(thread_checker_); | |
98 } | |
99 | |
100 Logger::~Logger() {} | |
101 | |
102 void Logger::LogSocketEventWithRv(int channel_id, | |
103 EventType event_type, | |
104 int rv) { | |
105 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
106 | |
107 SocketEvent event = CreateEvent(event_type); | |
108 event.set_net_return_value(rv); | |
109 | |
110 LogSocketEvent(channel_id, event); | |
111 } | |
112 | |
113 void Logger::LogSocketChallengeReplyEvent(int channel_id, | |
114 const AuthResult& auth_result) { | |
115 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
116 | |
117 SocketEvent event = CreateEvent(proto::AUTH_CHALLENGE_REPLY); | |
118 event.set_challenge_reply_error_type( | |
119 ChallegeReplyErrorToProto(auth_result.error_type)); | |
120 | |
121 LogSocketEvent(channel_id, event); | |
122 } | |
123 | |
124 LastErrors Logger::GetLastErrors(int channel_id) const { | |
125 LastErrorsMap::const_iterator it = last_errors_.find(channel_id); | |
126 if (it != last_errors_.end()) { | |
127 return it->second; | |
128 } else { | |
129 return LastErrors(); | |
130 } | |
131 } | |
132 | |
133 void Logger::ClearLastErrors(int channel_id) { | |
134 last_errors_.erase(channel_id); | |
135 } | |
136 | |
137 SocketEvent Logger::CreateEvent(EventType event_type) { | |
138 SocketEvent event; | |
139 event.set_type(event_type); | |
140 return event; | |
141 } | |
142 | |
143 void Logger::LogSocketEvent(int channel_id, const SocketEvent& socket_event) { | |
144 LastErrorsMap::iterator it = last_errors_.find(channel_id); | |
145 if (it == last_errors_.end()) | |
146 last_errors_[channel_id] = LastErrors(); | |
147 | |
148 MaybeSetLastErrors(socket_event, &last_errors_[channel_id]); | |
149 } | |
150 | |
151 } // namespace cast_channel | |
OLD | NEW |