Chromium Code Reviews| Index: extensions/browser/api/cast_channel/logger.cc |
| diff --git a/extensions/browser/api/cast_channel/logger.cc b/extensions/browser/api/cast_channel/logger.cc |
| index c1e8c9bc3949ea9debfbd991d4482e34dc5a0210..9d31aa6aeeb2608132ee87f2ef00b2c1102df044 100644 |
| --- a/extensions/browser/api/cast_channel/logger.cc |
| +++ b/extensions/browser/api/cast_channel/logger.cc |
| @@ -7,6 +7,7 @@ |
| #include "base/strings/string_util.h" |
| #include "base/time/tick_clock.h" |
| #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
| +#include "third_party/zlib/zlib.h" |
| namespace extensions { |
| namespace core_api { |
| @@ -55,6 +56,54 @@ proto::ChallengeReplyErrorType ChannegeReplyErrorToProto( |
| } |
| } |
| +bool Compress(const std::string& input, std::string* output) { |
| + output->resize(input.size()); |
| + |
| + z_stream stream = {0}; |
| + int result = deflateInit2(&stream, |
| + Z_DEFAULT_COMPRESSION, |
| + Z_DEFLATED, |
| + // 16 is added to produce a gzip header + trailer. |
| + MAX_WBITS + 16, |
| + 8, // memLevel = 8 is default. |
| + Z_DEFAULT_STRATEGY); |
| + 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
|
| + |
| + 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.
|
| + stream.avail_in = input.size(); |
| + 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.
|
| + stream.avail_out = output->size(); |
| + |
| + // Do a one-shot compression. This will return Z_STREAM_END only if |output| |
| + // is large enough to hold all compressed data. Otherwise, Z_OK will be |
| + // returned (this should happen very rarely and for small inputs) and |
| + // |output| will have to be resized. |
| + result = deflate(&stream, Z_FINISH); |
| + |
| + 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.
|
| + do { |
| + int old_size = output->size(); |
| + 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.
|
| + 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
|
| + stream.avail_out = output->size() - old_size; |
| + result = deflate(&stream, Z_FINISH); |
| + } while (result == Z_OK); |
| + } |
| + |
| + bool success = (result == Z_STREAM_END); |
| + |
| + if (!success) |
| + 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()
|
| + |
| + result = deflateEnd(&stream); |
| + DCHECK(result == Z_OK || result == Z_DATA_ERROR); |
| + |
| + 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
|
| + output->resize(output->size() - stream.avail_out); |
| + |
| + return success; |
| +} |
| + |
| } // namespace |
| Logger::AggregatedSocketEventLog::AggregatedSocketEventLog() { |
| @@ -201,7 +250,7 @@ void Logger::LogSocketChallengeReplyEvent(int channel_id, |
| SocketEvent Logger::CreateEvent(EventType event_type) { |
| SocketEvent event; |
| event.set_type(event_type); |
| - event.set_timestamp_micros(clock_->NowTicks().ToInternalValue() + |
| + event.set_timestamp_micros(clock_->NowTicks().ToInternalValue() - |
| unix_epoch_time_ticks_.ToInternalValue()); |
| return event; |
| } |
| @@ -263,7 +312,18 @@ bool Logger::LogToString(std::string* output) const { |
| } |
| } |
| - return log.SerializeToString(output); |
| + std::string serialized; |
| + if (!log.SerializeToString(&serialized)) { |
| + VLOG(2) << "Failed to serialized proto to string."; |
| + return false; |
| + } |
| + |
| + if (!Compress(serialized, output)) { |
| + VLOG(2) << "Failed to compress serialized contents."; |
| + return false; |
| + } |
| + |
| + return true; |
| } |
| void Logger::Reset() { |