Chromium Code Reviews| 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 "third_party/zlib/zlib.h" | |
| 10 | 11 |
| 11 namespace extensions { | 12 namespace extensions { |
| 12 namespace core_api { | 13 namespace core_api { |
| 13 namespace cast_channel { | 14 namespace cast_channel { |
| 14 | 15 |
| 15 using net::IPEndPoint; | 16 using net::IPEndPoint; |
| 16 using proto::AggregatedSocketEvent; | 17 using proto::AggregatedSocketEvent; |
| 17 using proto::EventType; | 18 using proto::EventType; |
| 18 using proto::Log; | 19 using proto::Log; |
| 19 using proto::SocketEvent; | 20 using proto::SocketEvent; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 48 case AuthResult::ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY: | 49 case AuthResult::ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY: |
| 49 return proto::CHALLENGE_REPLY_ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY; | 50 return proto::CHALLENGE_REPLY_ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY; |
| 50 case AuthResult::ERROR_NSS_SIGNED_BLOBS_MISMATCH: | 51 case AuthResult::ERROR_NSS_SIGNED_BLOBS_MISMATCH: |
| 51 return proto::CHALLENGE_REPLY_ERROR_NSS_SIGNED_BLOBS_MISMATCH; | 52 return proto::CHALLENGE_REPLY_ERROR_NSS_SIGNED_BLOBS_MISMATCH; |
| 52 default: | 53 default: |
| 53 NOTREACHED(); | 54 NOTREACHED(); |
| 54 return proto::CHALLENGE_REPLY_ERROR_NONE; | 55 return proto::CHALLENGE_REPLY_ERROR_NONE; |
| 55 } | 56 } |
| 56 } | 57 } |
| 57 | 58 |
| 59 bool Compress(const std::string& input, std::string* output) { | |
| 60 output->resize(input.size()); | |
| 61 | |
| 62 z_stream stream = {0}; | |
| 63 int result = deflateInit2(&stream, | |
| 64 Z_DEFAULT_COMPRESSION, | |
| 65 Z_DEFLATED, | |
| 66 // 16 is added to produce a gzip header + trailer. | |
| 67 MAX_WBITS + 16, | |
| 68 8, // memLevel = 8 is default. | |
| 69 Z_DEFAULT_STRATEGY); | |
| 70 DCHECK_EQ(Z_OK, result); | |
|
mark a. foltz
2014/08/11 23:01:44
Can this never actually fail?
imcheng
2014/08/12 07:42:10
Normally this shouldn't fail, unless there is not
| |
| 71 | |
| 72 stream.next_in = reinterpret_cast<uint8*>(const_cast<char*>(&input[0])); | |
|
mark a. foltz
2014/08/11 23:01:44
Add a COMPILE_ASSERT that sizeof(uint8) == sizeof(
mark a. foltz
2014/08/11 23:01:44
input.data()
imcheng
2014/08/12 07:42:09
Done.
imcheng
2014/08/12 07:42:09
Done.
| |
| 73 stream.avail_in = input.size(); | |
| 74 stream.next_out = reinterpret_cast<uint8*>(&(*output)[0]); | |
|
mark a. foltz
2014/08/11 23:01:44
output->data()
imcheng
2014/08/12 07:42:10
Done.
| |
| 75 stream.avail_out = output->size(); | |
| 76 | |
| 77 // Do a one-shot compression. This will return Z_STREAM_END only if |output| | |
| 78 // is large enough to hold all compressed data. Otherwise, Z_OK will be | |
| 79 // returned (this should happen very rarely and for small inputs) and | |
| 80 // |output| will have to be resized. | |
| 81 result = deflate(&stream, Z_FINISH); | |
| 82 | |
| 83 if (result == Z_OK) { | |
|
mark a. foltz
2014/08/11 23:01:44
while (result == Z_OK) {
...
}
imcheng
2014/08/12 07:42:09
Removed since we are doing one-shot compression.
| |
| 84 do { | |
| 85 int old_size = output->size(); | |
| 86 output->resize(old_size + input.size()); | |
|
mark a. foltz
2014/08/11 23:01:43
net::GrowableIOBuffer would be a better abstractio
imcheng
2014/08/12 07:42:10
Removed since we are doing one-shot compression.
| |
| 87 stream.next_out = reinterpret_cast<uint8*>(&(*output)[old_size]); | |
|
mark a. foltz
2014/08/11 23:01:44
Is this code cribbed from somewhere else? I would
imcheng
2014/08/12 07:42:10
There is an utility function close to this in zlib
| |
| 88 stream.avail_out = output->size() - old_size; | |
| 89 result = deflate(&stream, Z_FINISH); | |
| 90 } while (result == Z_OK); | |
| 91 } | |
| 92 | |
| 93 bool success = (result == Z_STREAM_END); | |
| 94 | |
| 95 if (!success) | |
| 96 VLOG(2) << "deflate() failed. Result: " << result; | |
|
mark a. foltz
2014/08/11 23:01:44
return early here?
imcheng
2014/08/12 07:42:10
deflateEnd() needs to be called even if deflate()
| |
| 97 | |
| 98 result = deflateEnd(&stream); | |
| 99 DCHECK(result == Z_OK || result == Z_DATA_ERROR); | |
| 100 | |
| 101 if (success) | |
|
mark a. foltz
2014/08/11 23:01:44
Do we need to check result here?
imcheng
2014/08/12 07:42:09
No. deflateEnd() returns Z_OK if deflate() was suc
| |
| 102 output->resize(output->size() - stream.avail_out); | |
| 103 | |
| 104 return success; | |
| 105 } | |
| 106 | |
| 58 } // namespace | 107 } // namespace |
| 59 | 108 |
| 60 Logger::AggregatedSocketEventLog::AggregatedSocketEventLog() { | 109 Logger::AggregatedSocketEventLog::AggregatedSocketEventLog() { |
| 61 } | 110 } |
| 62 Logger::AggregatedSocketEventLog::~AggregatedSocketEventLog() { | 111 Logger::AggregatedSocketEventLog::~AggregatedSocketEventLog() { |
| 63 } | 112 } |
| 64 | 113 |
| 65 Logger::Logger(scoped_ptr<base::TickClock> clock, | 114 Logger::Logger(scoped_ptr<base::TickClock> clock, |
| 66 base::TimeTicks unix_epoch_time_ticks) | 115 base::TimeTicks unix_epoch_time_ticks) |
| 67 : clock_(clock.Pass()), | 116 : clock_(clock.Pass()), |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 ChannegeReplyErrorToProto(auth_result.error_type)); | 243 ChannegeReplyErrorToProto(auth_result.error_type)); |
| 195 if (auth_result.nss_error_code != 0) | 244 if (auth_result.nss_error_code != 0) |
| 196 event.set_return_value(auth_result.nss_error_code); | 245 event.set_return_value(auth_result.nss_error_code); |
| 197 | 246 |
| 198 LogSocketEvent(channel_id, event); | 247 LogSocketEvent(channel_id, event); |
| 199 } | 248 } |
| 200 | 249 |
| 201 SocketEvent Logger::CreateEvent(EventType event_type) { | 250 SocketEvent Logger::CreateEvent(EventType event_type) { |
| 202 SocketEvent event; | 251 SocketEvent event; |
| 203 event.set_type(event_type); | 252 event.set_type(event_type); |
| 204 event.set_timestamp_micros(clock_->NowTicks().ToInternalValue() + | 253 event.set_timestamp_micros(clock_->NowTicks().ToInternalValue() - |
| 205 unix_epoch_time_ticks_.ToInternalValue()); | 254 unix_epoch_time_ticks_.ToInternalValue()); |
| 206 return event; | 255 return event; |
| 207 } | 256 } |
| 208 | 257 |
| 209 void Logger::LogSocketEvent(int channel_id, const SocketEvent& socket_event) { | 258 void Logger::LogSocketEvent(int channel_id, const SocketEvent& socket_event) { |
| 210 AggregatedSocketEventLogMap::iterator it = | 259 AggregatedSocketEventLogMap::iterator it = |
| 211 aggregated_socket_events_.find(channel_id); | 260 aggregated_socket_events_.find(channel_id); |
| 212 if (it == aggregated_socket_events_.end()) { | 261 if (it == aggregated_socket_events_.end()) { |
| 213 if (aggregated_socket_events_.size() >= kMaxSocketsToLog) { | 262 if (aggregated_socket_events_.size() >= kMaxSocketsToLog) { |
| 214 AggregatedSocketEventLogMap::iterator erase_it = | 263 AggregatedSocketEventLogMap::iterator erase_it = |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 for (std::deque<SocketEvent>::const_iterator socket_event_it = | 305 for (std::deque<SocketEvent>::const_iterator socket_event_it = |
| 257 socket_events.begin(); | 306 socket_events.begin(); |
| 258 socket_event_it != socket_events.end(); | 307 socket_event_it != socket_events.end(); |
| 259 ++socket_event_it) { | 308 ++socket_event_it) { |
| 260 SocketEvent* socket_event = | 309 SocketEvent* socket_event = |
| 261 new_aggregated_socket_event->add_socket_event(); | 310 new_aggregated_socket_event->add_socket_event(); |
| 262 socket_event->CopyFrom(*socket_event_it); | 311 socket_event->CopyFrom(*socket_event_it); |
| 263 } | 312 } |
| 264 } | 313 } |
| 265 | 314 |
| 266 return log.SerializeToString(output); | 315 std::string serialized; |
| 316 if (!log.SerializeToString(&serialized)) { | |
| 317 VLOG(2) << "Failed to serialized proto to string."; | |
| 318 return false; | |
| 319 } | |
| 320 | |
| 321 if (!Compress(serialized, output)) { | |
| 322 VLOG(2) << "Failed to compress serialized contents."; | |
| 323 return false; | |
| 324 } | |
| 325 | |
| 326 return true; | |
| 267 } | 327 } |
| 268 | 328 |
| 269 void Logger::Reset() { | 329 void Logger::Reset() { |
| 270 aggregated_socket_events_.clear(); | 330 aggregated_socket_events_.clear(); |
| 271 num_evicted_aggregated_socket_events_ = 0; | 331 num_evicted_aggregated_socket_events_ = 0; |
| 272 num_evicted_socket_events_ = 0; | 332 num_evicted_socket_events_ = 0; |
| 273 } | 333 } |
| 274 | 334 |
| 275 } // namespace cast_channel | 335 } // namespace cast_channel |
| 276 } // namespace api | 336 } // namespace api |
| 277 } // namespace extensions | 337 } // namespace extensions |
| OLD | NEW |