Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Unified Diff: extensions/browser/api/cast_channel/logger.cc

Issue 456213002: Cast channel: Add cast.channel.getLogs extension API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 3d08c0ab4e86c51a5c92fb60a914be2acd2ff61e..a665052ac841c74aa53b56c83063699f5799d080 100644
--- a/extensions/browser/api/cast_channel/logger.cc
+++ b/extensions/browser/api/cast_channel/logger.cc
@@ -9,6 +9,7 @@
#include "extensions/browser/api/cast_channel/cast_auth_util.h"
#include "extensions/browser/api/cast_channel/logger_util.h"
#include "net/base/net_errors.h"
+#include "third_party/zlib/zlib.h"
namespace extensions {
namespace core_api {
@@ -57,6 +58,46 @@ proto::ChallengeReplyErrorType ChallegeReplyErrorToProto(
}
}
+scoped_ptr<char[]> Compress(const std::string& input, size_t* length) {
+ *length = 0;
+ 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);
+
+ size_t out_size = deflateBound(&stream, input.size());
+ scoped_ptr<char[]> out(new char[out_size]);
+
+ COMPILE_ASSERT(sizeof(uint8) == sizeof(char), uint8_char_different_sizes);
+
+ stream.next_in = reinterpret_cast<uint8*>(const_cast<char*>(input.data()));
+ stream.avail_in = input.size();
+ stream.next_out = reinterpret_cast<uint8*>(out.get());
+ stream.avail_out = out_size;
+
+ // Do a one-shot compression. This will return Z_STREAM_END only if |output|
+ // is large enough to hold all compressed data.
+ result = deflate(&stream, Z_FINISH);
+
+ bool success = (result == Z_STREAM_END);
+
+ if (!success)
+ VLOG(2) << "deflate() failed. Result: " << result;
+
+ result = deflateEnd(&stream);
+ DCHECK(result == Z_OK || result == Z_DATA_ERROR);
+
+ if (success)
+ *length = out_size - stream.avail_out;
+
+ return out.Pass();
+}
+
} // namespace
Logger::AggregatedSocketEventLog::AggregatedSocketEventLog() {
@@ -204,7 +245,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;
}
@@ -250,8 +291,8 @@ void Logger::LogSocketEvent(int channel_id, const SocketEvent& socket_event) {
it->second->last_errors.nss_error_code = socket_event.nss_error_code();
}
-bool Logger::LogToString(std::string* output) const {
- output->clear();
+scoped_ptr<char[]> Logger::GetLogs(size_t* length) const {
+ *length = 0;
Log log;
log.set_num_evicted_aggregated_socket_events(
@@ -277,7 +318,13 @@ 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 scoped_ptr<char[]>();
+ }
+
+ return Compress(serialized, length);
}
void Logger::Reset() {
« no previous file with comments | « extensions/browser/api/cast_channel/logger.h ('k') | extensions/browser/api/cast_channel/logger_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698