OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/cast_channel/logger.h" | 5 #include "extensions/browser/api/cast_channel/logger.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/time/tick_clock.h" | 8 #include "base/time/tick_clock.h" |
9 #include "extensions/browser/api/cast_channel/cast_auth_util.h" | 9 #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
10 #include "extensions/browser/api/cast_channel/logger_util.h" | 10 #include "extensions/browser/api/cast_channel/logger_util.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "third_party/zlib/zlib.h" |
12 | 13 |
13 namespace extensions { | 14 namespace extensions { |
14 namespace core_api { | 15 namespace core_api { |
15 namespace cast_channel { | 16 namespace cast_channel { |
16 | 17 |
17 using net::IPEndPoint; | 18 using net::IPEndPoint; |
18 using proto::AggregatedSocketEvent; | 19 using proto::AggregatedSocketEvent; |
19 using proto::EventType; | 20 using proto::EventType; |
20 using proto::Log; | 21 using proto::Log; |
21 using proto::SocketEvent; | 22 using proto::SocketEvent; |
(...skipping 28 matching lines...) Expand all Loading... |
50 case AuthResult::ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY: | 51 case AuthResult::ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY: |
51 return proto::CHALLENGE_REPLY_ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY; | 52 return proto::CHALLENGE_REPLY_ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY; |
52 case AuthResult::ERROR_NSS_SIGNED_BLOBS_MISMATCH: | 53 case AuthResult::ERROR_NSS_SIGNED_BLOBS_MISMATCH: |
53 return proto::CHALLENGE_REPLY_ERROR_NSS_SIGNED_BLOBS_MISMATCH; | 54 return proto::CHALLENGE_REPLY_ERROR_NSS_SIGNED_BLOBS_MISMATCH; |
54 default: | 55 default: |
55 NOTREACHED(); | 56 NOTREACHED(); |
56 return proto::CHALLENGE_REPLY_ERROR_NONE; | 57 return proto::CHALLENGE_REPLY_ERROR_NONE; |
57 } | 58 } |
58 } | 59 } |
59 | 60 |
| 61 scoped_ptr<char[]> Compress(const std::string& input, size_t* length) { |
| 62 *length = 0; |
| 63 z_stream stream = {0}; |
| 64 int result = deflateInit2(&stream, |
| 65 Z_DEFAULT_COMPRESSION, |
| 66 Z_DEFLATED, |
| 67 // 16 is added to produce a gzip header + trailer. |
| 68 MAX_WBITS + 16, |
| 69 8, // memLevel = 8 is default. |
| 70 Z_DEFAULT_STRATEGY); |
| 71 DCHECK_EQ(Z_OK, result); |
| 72 |
| 73 size_t out_size = deflateBound(&stream, input.size()); |
| 74 scoped_ptr<char[]> out(new char[out_size]); |
| 75 |
| 76 COMPILE_ASSERT(sizeof(uint8) == sizeof(char), uint8_char_different_sizes); |
| 77 |
| 78 stream.next_in = reinterpret_cast<uint8*>(const_cast<char*>(input.data())); |
| 79 stream.avail_in = input.size(); |
| 80 stream.next_out = reinterpret_cast<uint8*>(out.get()); |
| 81 stream.avail_out = out_size; |
| 82 |
| 83 // Do a one-shot compression. This will return Z_STREAM_END only if |output| |
| 84 // is large enough to hold all compressed data. |
| 85 result = deflate(&stream, Z_FINISH); |
| 86 |
| 87 bool success = (result == Z_STREAM_END); |
| 88 |
| 89 if (!success) |
| 90 VLOG(2) << "deflate() failed. Result: " << result; |
| 91 |
| 92 result = deflateEnd(&stream); |
| 93 DCHECK(result == Z_OK || result == Z_DATA_ERROR); |
| 94 |
| 95 if (success) |
| 96 *length = out_size - stream.avail_out; |
| 97 |
| 98 return out.Pass(); |
| 99 } |
| 100 |
60 } // namespace | 101 } // namespace |
61 | 102 |
62 Logger::AggregatedSocketEventLog::AggregatedSocketEventLog() { | 103 Logger::AggregatedSocketEventLog::AggregatedSocketEventLog() { |
63 } | 104 } |
64 | 105 |
65 Logger::AggregatedSocketEventLog::~AggregatedSocketEventLog() { | 106 Logger::AggregatedSocketEventLog::~AggregatedSocketEventLog() { |
66 } | 107 } |
67 | 108 |
68 Logger::Logger(scoped_ptr<base::TickClock> clock, | 109 Logger::Logger(scoped_ptr<base::TickClock> clock, |
69 base::TimeTicks unix_epoch_time_ticks) | 110 base::TimeTicks unix_epoch_time_ticks) |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 ChallegeReplyErrorToProto(auth_result.error_type)); | 238 ChallegeReplyErrorToProto(auth_result.error_type)); |
198 if (auth_result.nss_error_code != 0) | 239 if (auth_result.nss_error_code != 0) |
199 event.set_nss_error_code(auth_result.nss_error_code); | 240 event.set_nss_error_code(auth_result.nss_error_code); |
200 | 241 |
201 LogSocketEvent(channel_id, event); | 242 LogSocketEvent(channel_id, event); |
202 } | 243 } |
203 | 244 |
204 SocketEvent Logger::CreateEvent(EventType event_type) { | 245 SocketEvent Logger::CreateEvent(EventType event_type) { |
205 SocketEvent event; | 246 SocketEvent event; |
206 event.set_type(event_type); | 247 event.set_type(event_type); |
207 event.set_timestamp_micros(clock_->NowTicks().ToInternalValue() + | 248 event.set_timestamp_micros(clock_->NowTicks().ToInternalValue() - |
208 unix_epoch_time_ticks_.ToInternalValue()); | 249 unix_epoch_time_ticks_.ToInternalValue()); |
209 return event; | 250 return event; |
210 } | 251 } |
211 | 252 |
212 void Logger::LogSocketEvent(int channel_id, const SocketEvent& socket_event) { | 253 void Logger::LogSocketEvent(int channel_id, const SocketEvent& socket_event) { |
213 AggregatedSocketEventLogMap::iterator it = | 254 AggregatedSocketEventLogMap::iterator it = |
214 aggregated_socket_events_.find(channel_id); | 255 aggregated_socket_events_.find(channel_id); |
215 if (it == aggregated_socket_events_.end()) { | 256 if (it == aggregated_socket_events_.end()) { |
216 if (aggregated_socket_events_.size() >= kMaxSocketsToLog) { | 257 if (aggregated_socket_events_.size() >= kMaxSocketsToLog) { |
217 AggregatedSocketEventLogMap::iterator erase_it = | 258 AggregatedSocketEventLogMap::iterator erase_it = |
(...skipping 25 matching lines...) Expand all Loading... |
243 it->second->last_errors.net_return_value = socket_event.net_return_value(); | 284 it->second->last_errors.net_return_value = socket_event.net_return_value(); |
244 } | 285 } |
245 if (socket_event.has_challenge_reply_error_type()) { | 286 if (socket_event.has_challenge_reply_error_type()) { |
246 it->second->last_errors.challenge_reply_error_type = | 287 it->second->last_errors.challenge_reply_error_type = |
247 socket_event.challenge_reply_error_type(); | 288 socket_event.challenge_reply_error_type(); |
248 } | 289 } |
249 if (socket_event.has_nss_error_code()) | 290 if (socket_event.has_nss_error_code()) |
250 it->second->last_errors.nss_error_code = socket_event.nss_error_code(); | 291 it->second->last_errors.nss_error_code = socket_event.nss_error_code(); |
251 } | 292 } |
252 | 293 |
253 bool Logger::LogToString(std::string* output) const { | 294 scoped_ptr<char[]> Logger::GetLogs(size_t* length) const { |
254 output->clear(); | 295 *length = 0; |
255 | 296 |
256 Log log; | 297 Log log; |
257 log.set_num_evicted_aggregated_socket_events( | 298 log.set_num_evicted_aggregated_socket_events( |
258 num_evicted_aggregated_socket_events_); | 299 num_evicted_aggregated_socket_events_); |
259 log.set_num_evicted_socket_events(num_evicted_socket_events_); | 300 log.set_num_evicted_socket_events(num_evicted_socket_events_); |
260 | 301 |
261 for (AggregatedSocketEventLogMap::const_iterator it = | 302 for (AggregatedSocketEventLogMap::const_iterator it = |
262 aggregated_socket_events_.begin(); | 303 aggregated_socket_events_.begin(); |
263 it != aggregated_socket_events_.end(); | 304 it != aggregated_socket_events_.end(); |
264 ++it) { | 305 ++it) { |
265 AggregatedSocketEvent* new_aggregated_socket_event = | 306 AggregatedSocketEvent* new_aggregated_socket_event = |
266 log.add_aggregated_socket_event(); | 307 log.add_aggregated_socket_event(); |
267 new_aggregated_socket_event->CopyFrom(it->second->aggregated_socket_event); | 308 new_aggregated_socket_event->CopyFrom(it->second->aggregated_socket_event); |
268 | 309 |
269 const std::deque<SocketEvent>& socket_events = it->second->socket_events; | 310 const std::deque<SocketEvent>& socket_events = it->second->socket_events; |
270 for (std::deque<SocketEvent>::const_iterator socket_event_it = | 311 for (std::deque<SocketEvent>::const_iterator socket_event_it = |
271 socket_events.begin(); | 312 socket_events.begin(); |
272 socket_event_it != socket_events.end(); | 313 socket_event_it != socket_events.end(); |
273 ++socket_event_it) { | 314 ++socket_event_it) { |
274 SocketEvent* socket_event = | 315 SocketEvent* socket_event = |
275 new_aggregated_socket_event->add_socket_event(); | 316 new_aggregated_socket_event->add_socket_event(); |
276 socket_event->CopyFrom(*socket_event_it); | 317 socket_event->CopyFrom(*socket_event_it); |
277 } | 318 } |
278 } | 319 } |
279 | 320 |
280 return log.SerializeToString(output); | 321 std::string serialized; |
| 322 if (!log.SerializeToString(&serialized)) { |
| 323 VLOG(2) << "Failed to serialized proto to string."; |
| 324 return scoped_ptr<char[]>(); |
| 325 } |
| 326 |
| 327 return Compress(serialized, length); |
281 } | 328 } |
282 | 329 |
283 void Logger::Reset() { | 330 void Logger::Reset() { |
284 aggregated_socket_events_.clear(); | 331 aggregated_socket_events_.clear(); |
285 num_evicted_aggregated_socket_events_ = 0; | 332 num_evicted_aggregated_socket_events_ = 0; |
286 num_evicted_socket_events_ = 0; | 333 num_evicted_socket_events_ = 0; |
287 } | 334 } |
288 | 335 |
289 LastErrors Logger::GetLastErrors(int channel_id) const { | 336 LastErrors Logger::GetLastErrors(int channel_id) const { |
290 AggregatedSocketEventLogMap::const_iterator it = | 337 AggregatedSocketEventLogMap::const_iterator it = |
291 aggregated_socket_events_.find(channel_id); | 338 aggregated_socket_events_.find(channel_id); |
292 if (it != aggregated_socket_events_.end()) { | 339 if (it != aggregated_socket_events_.end()) { |
293 return it->second->last_errors; | 340 return it->second->last_errors; |
294 } else { | 341 } else { |
295 return LastErrors(); | 342 return LastErrors(); |
296 } | 343 } |
297 } | 344 } |
298 | 345 |
299 } // namespace cast_channel | 346 } // namespace cast_channel |
300 } // namespace api | 347 } // namespace api |
301 } // namespace extensions | 348 } // namespace extensions |
OLD | NEW |