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