Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / logger.cc
blob77b281f01154447635573b616e4bd6526571ce8b
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.
5 #include "extensions/browser/api/cast_channel/logger.h"
7 #include <string>
9 #include "base/strings/string_util.h"
10 #include "base/time/clock.h"
11 #include "extensions/browser/api/cast_channel/cast_auth_util.h"
12 #include "extensions/browser/api/cast_channel/cast_socket.h"
13 #include "extensions/browser/api/cast_channel/logger_util.h"
14 #include "net/base/net_errors.h"
15 #include "third_party/zlib/zlib.h"
17 namespace extensions {
18 namespace api {
19 namespace cast_channel {
21 using net::IPEndPoint;
22 using proto::AggregatedSocketEvent;
23 using proto::EventType;
24 using proto::Log;
25 using proto::SocketEvent;
27 namespace {
29 const char* kInternalNamespacePrefix = "com.google.cast";
31 proto::ChallengeReplyErrorType ChallegeReplyErrorToProto(
32 AuthResult::ErrorType error_type) {
33 switch (error_type) {
34 case AuthResult::ERROR_NONE:
35 return proto::CHALLENGE_REPLY_ERROR_NONE;
36 case AuthResult::ERROR_PEER_CERT_EMPTY:
37 return proto::CHALLENGE_REPLY_ERROR_PEER_CERT_EMPTY;
38 case AuthResult::ERROR_WRONG_PAYLOAD_TYPE:
39 return proto::CHALLENGE_REPLY_ERROR_WRONG_PAYLOAD_TYPE;
40 case AuthResult::ERROR_NO_PAYLOAD:
41 return proto::CHALLENGE_REPLY_ERROR_NO_PAYLOAD;
42 case AuthResult::ERROR_PAYLOAD_PARSING_FAILED:
43 return proto::CHALLENGE_REPLY_ERROR_PAYLOAD_PARSING_FAILED;
44 case AuthResult::ERROR_MESSAGE_ERROR:
45 return proto::CHALLENGE_REPLY_ERROR_MESSAGE_ERROR;
46 case AuthResult::ERROR_NO_RESPONSE:
47 return proto::CHALLENGE_REPLY_ERROR_NO_RESPONSE;
48 case AuthResult::ERROR_FINGERPRINT_NOT_FOUND:
49 return proto::CHALLENGE_REPLY_ERROR_FINGERPRINT_NOT_FOUND;
50 case AuthResult::ERROR_CERT_PARSING_FAILED:
51 return proto::CHALLENGE_REPLY_ERROR_CERT_PARSING_FAILED;
52 case AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA:
53 return proto::CHALLENGE_REPLY_ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA;
54 case AuthResult::ERROR_CANNOT_EXTRACT_PUBLIC_KEY:
55 return proto::CHALLENGE_REPLY_ERROR_CANNOT_EXTRACT_PUBLIC_KEY;
56 case AuthResult::ERROR_SIGNED_BLOBS_MISMATCH:
57 return proto::CHALLENGE_REPLY_ERROR_SIGNED_BLOBS_MISMATCH;
58 default:
59 NOTREACHED();
60 return proto::CHALLENGE_REPLY_ERROR_NONE;
64 scoped_ptr<char[]> Compress(const std::string& input, size_t* length) {
65 *length = 0;
66 z_stream stream = {0};
67 int result = deflateInit2(&stream,
68 Z_DEFAULT_COMPRESSION,
69 Z_DEFLATED,
70 // 16 is added to produce a gzip header + trailer.
71 MAX_WBITS + 16,
72 8, // memLevel = 8 is default.
73 Z_DEFAULT_STRATEGY);
74 DCHECK_EQ(Z_OK, result);
76 size_t out_size = deflateBound(&stream, input.size());
77 scoped_ptr<char[]> out(new char[out_size]);
79 static_assert(sizeof(uint8) == sizeof(char),
80 "uint8 char should be of different sizes");
82 stream.next_in = reinterpret_cast<uint8*>(const_cast<char*>(input.data()));
83 stream.avail_in = input.size();
84 stream.next_out = reinterpret_cast<uint8*>(out.get());
85 stream.avail_out = out_size;
87 // Do a one-shot compression. This will return Z_STREAM_END only if |output|
88 // is large enough to hold all compressed data.
89 result = deflate(&stream, Z_FINISH);
91 bool success = (result == Z_STREAM_END);
93 if (!success)
94 VLOG(2) << "deflate() failed. Result: " << result;
96 result = deflateEnd(&stream);
97 DCHECK(result == Z_OK || result == Z_DATA_ERROR);
99 if (success)
100 *length = out_size - stream.avail_out;
102 return out.Pass();
105 // Propagate any error fields set in |event| to |last_errors|. If any error
106 // field in |event| is set, then also set |last_errors->event_type|.
107 void MaybeSetLastErrors(const SocketEvent& event, LastErrors* last_errors) {
108 if (event.has_net_return_value() &&
109 event.net_return_value() < net::ERR_IO_PENDING) {
110 last_errors->net_return_value = event.net_return_value();
111 last_errors->event_type = event.type();
113 if (event.has_challenge_reply_error_type()) {
114 last_errors->challenge_reply_error_type =
115 event.challenge_reply_error_type();
116 last_errors->event_type = event.type();
118 if (event.has_nss_error_code()) {
119 last_errors->nss_error_code = event.nss_error_code();
120 last_errors->event_type = event.type();
124 } // namespace
126 Logger::AggregatedSocketEventLog::AggregatedSocketEventLog() {
129 Logger::AggregatedSocketEventLog::~AggregatedSocketEventLog() {
132 Logger::Logger(scoped_ptr<base::Clock> clock, base::Time unix_epoch_time)
133 : clock_(clock.Pass()), unix_epoch_time_(unix_epoch_time) {
134 DCHECK(clock_);
136 // Logger may not be necessarily be created on the IO thread, but logging
137 // happens exclusively there.
138 thread_checker_.DetachFromThread();
141 Logger::~Logger() {
144 void Logger::LogNewSocketEvent(const CastSocket& cast_socket) {
145 DCHECK(thread_checker_.CalledOnValidThread());
147 SocketEvent event = CreateEvent(proto::CAST_SOCKET_CREATED);
148 AggregatedSocketEvent& aggregated_socket_event =
149 LogSocketEvent(cast_socket.id(), event);
151 const net::IPAddressNumber& ip = cast_socket.ip_endpoint().address();
152 aggregated_socket_event.set_endpoint_id(ip.back());
153 aggregated_socket_event.set_channel_auth_type(cast_socket.channel_auth() ==
154 CHANNEL_AUTH_TYPE_SSL
155 ? proto::SSL
156 : proto::SSL_VERIFIED);
159 void Logger::LogSocketEvent(int channel_id, EventType event_type) {
160 DCHECK(thread_checker_.CalledOnValidThread());
162 LogSocketEventWithDetails(channel_id, event_type, std::string());
165 void Logger::LogSocketEventWithDetails(int channel_id,
166 EventType event_type,
167 const std::string& details) {
168 DCHECK(thread_checker_.CalledOnValidThread());
170 SocketEvent event = CreateEvent(event_type);
171 if (!details.empty())
172 event.set_details(details);
174 LogSocketEvent(channel_id, event);
177 void Logger::LogSocketEventWithRv(int channel_id,
178 EventType event_type,
179 int rv) {
180 DCHECK(thread_checker_.CalledOnValidThread());
182 SocketEvent event = CreateEvent(event_type);
183 event.set_net_return_value(rv);
185 AggregatedSocketEvent& aggregated_socket_event =
186 LogSocketEvent(channel_id, event);
188 if ((event_type == proto::SOCKET_READ || event_type == proto::SOCKET_WRITE) &&
189 rv > 0) {
190 if (event_type == proto::SOCKET_READ) {
191 aggregated_socket_event.set_bytes_read(
192 aggregated_socket_event.bytes_read() + rv);
193 } else {
194 aggregated_socket_event.set_bytes_written(
195 aggregated_socket_event.bytes_written() + rv);
200 void Logger::LogSocketReadyState(int channel_id, proto::ReadyState new_state) {
201 DCHECK(thread_checker_.CalledOnValidThread());
203 SocketEvent event = CreateEvent(proto::READY_STATE_CHANGED);
204 event.set_ready_state(new_state);
206 LogSocketEvent(channel_id, event);
209 void Logger::LogSocketConnectState(int channel_id,
210 proto::ConnectionState new_state) {
211 DCHECK(thread_checker_.CalledOnValidThread());
213 SocketEvent event = CreateEvent(proto::CONNECTION_STATE_CHANGED);
214 event.set_connection_state(new_state);
216 LogSocketEvent(channel_id, event);
219 void Logger::LogSocketReadState(int channel_id, proto::ReadState new_state) {
220 DCHECK(thread_checker_.CalledOnValidThread());
222 SocketEvent event = CreateEvent(proto::READ_STATE_CHANGED);
223 event.set_read_state(new_state);
225 LogSocketEvent(channel_id, event);
228 void Logger::LogSocketWriteState(int channel_id, proto::WriteState new_state) {
229 DCHECK(thread_checker_.CalledOnValidThread());
231 SocketEvent event = CreateEvent(proto::WRITE_STATE_CHANGED);
232 event.set_write_state(new_state);
234 LogSocketEvent(channel_id, event);
237 void Logger::LogSocketErrorState(int channel_id, proto::ErrorState new_state) {
238 DCHECK(thread_checker_.CalledOnValidThread());
240 SocketEvent event = CreateEvent(proto::ERROR_STATE_CHANGED);
241 event.set_error_state(new_state);
243 LogSocketEvent(channel_id, event);
246 void Logger::LogSocketEventForMessage(int channel_id,
247 EventType event_type,
248 const std::string& message_namespace,
249 const std::string& details) {
250 DCHECK(thread_checker_.CalledOnValidThread());
252 SocketEvent event = CreateEvent(event_type);
253 if (base::StartsWith(message_namespace, kInternalNamespacePrefix,
254 base::CompareCase::INSENSITIVE_ASCII))
255 event.set_message_namespace(message_namespace);
256 event.set_details(details);
258 LogSocketEvent(channel_id, event);
261 void Logger::LogSocketChallengeReplyEvent(int channel_id,
262 const AuthResult& auth_result) {
263 DCHECK(thread_checker_.CalledOnValidThread());
265 SocketEvent event = CreateEvent(proto::AUTH_CHALLENGE_REPLY);
266 event.set_challenge_reply_error_type(
267 ChallegeReplyErrorToProto(auth_result.error_type));
268 if (auth_result.nss_error_code != 0)
269 event.set_nss_error_code(auth_result.nss_error_code);
271 LogSocketEvent(channel_id, event);
274 SocketEvent Logger::CreateEvent(EventType event_type) {
275 SocketEvent event;
276 event.set_type(event_type);
277 event.set_timestamp_micros(
278 (clock_->Now() - unix_epoch_time_).InMicroseconds());
279 return event;
282 AggregatedSocketEvent& Logger::LogSocketEvent(int channel_id,
283 const SocketEvent& socket_event) {
284 AggregatedSocketEventLogMap::iterator it =
285 aggregated_socket_events_.find(channel_id);
286 if (it == aggregated_socket_events_.end()) {
287 if (aggregated_socket_events_.size() >= kMaxSocketsToLog) {
288 AggregatedSocketEventLogMap::iterator erase_it =
289 aggregated_socket_events_.begin();
291 log_.set_num_evicted_aggregated_socket_events(
292 log_.num_evicted_aggregated_socket_events() + 1);
293 log_.set_num_evicted_socket_events(
294 log_.num_evicted_socket_events() +
295 erase_it->second->socket_events.size());
297 aggregated_socket_events_.erase(erase_it);
300 it = aggregated_socket_events_
301 .insert(std::make_pair(
302 channel_id, make_linked_ptr(new AggregatedSocketEventLog)))
303 .first;
304 it->second->aggregated_socket_event.set_id(channel_id);
307 std::deque<proto::SocketEvent>& socket_events = it->second->socket_events;
308 if (socket_events.size() >= kMaxEventsPerSocket) {
309 socket_events.pop_front();
310 log_.set_num_evicted_socket_events(log_.num_evicted_socket_events() + 1);
312 socket_events.push_back(socket_event);
314 MaybeSetLastErrors(socket_event, &(it->second->last_errors));
316 return it->second->aggregated_socket_event;
319 scoped_ptr<char[]> Logger::GetLogs(size_t* length) const {
320 *length = 0;
322 Log log;
323 // Copy "global" values from |log_|. Don't use |log_| directly since this
324 // function is const.
325 log.CopyFrom(log_);
327 for (AggregatedSocketEventLogMap::const_iterator it =
328 aggregated_socket_events_.begin();
329 it != aggregated_socket_events_.end();
330 ++it) {
331 AggregatedSocketEvent* new_aggregated_socket_event =
332 log.add_aggregated_socket_event();
333 new_aggregated_socket_event->CopyFrom(it->second->aggregated_socket_event);
335 const std::deque<SocketEvent>& socket_events = it->second->socket_events;
336 for (std::deque<SocketEvent>::const_iterator socket_event_it =
337 socket_events.begin();
338 socket_event_it != socket_events.end();
339 ++socket_event_it) {
340 SocketEvent* socket_event =
341 new_aggregated_socket_event->add_socket_event();
342 socket_event->CopyFrom(*socket_event_it);
346 std::string serialized;
347 if (!log.SerializeToString(&serialized)) {
348 VLOG(2) << "Failed to serialized proto to string.";
349 return scoped_ptr<char[]>();
352 return Compress(serialized, length);
355 void Logger::Reset() {
356 aggregated_socket_events_.clear();
357 log_.Clear();
360 LastErrors Logger::GetLastErrors(int channel_id) const {
361 AggregatedSocketEventLogMap::const_iterator it =
362 aggregated_socket_events_.find(channel_id);
363 if (it != aggregated_socket_events_.end()) {
364 return it->second->last_errors;
365 } else {
366 return LastErrors();
370 } // namespace cast_channel
371 } // namespace api
372 } // namespace extensions