Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / logger.h
blobe6462bb74b3c2b8b4b381deba625b040d0dc2858
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 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_
8 #include <deque>
9 #include <map>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/threading/thread_checker.h"
16 #include "extensions/browser/api/cast_channel/logger_util.h"
17 #include "extensions/common/api/cast_channel/logging.pb.h"
18 #include "net/base/ip_endpoint.h"
20 namespace base {
21 class TickClock;
24 namespace extensions {
25 namespace core_api {
26 namespace cast_channel {
28 struct AuthResult;
29 class CastSocket;
31 static const int kMaxSocketsToLog = 50;
32 static const int kMaxEventsPerSocket = 2000;
34 // Logs information of each channel and sockets and exports the log as
35 // a blob. Logger is done on the IO thread.
36 class Logger : public base::RefCounted<Logger> {
37 public:
38 // |clock|: Clock used for generating timestamps for the events. Owned by
39 // this class.
40 // |unix_epoch_time_ticks|: The TimeTicks that corresponds to Unix epoch.
41 Logger(scoped_ptr<base::TickClock> clock,
42 base::TimeTicks unix_epoch_time_ticks);
44 // For newly created sockets. Will create an event and log a
45 // CAST_SOCKET_CREATED event.
46 void LogNewSocketEvent(const CastSocket& cast_socket);
48 void LogSocketEvent(int channel_id, proto::EventType event_type);
49 void LogSocketEventWithDetails(int channel_id,
50 proto::EventType event_type,
51 const std::string& details);
53 // For events that involves socket / crypto operations that returns a value.
54 void LogSocketEventWithRv(int channel_id,
55 proto::EventType event_type,
56 int rv);
58 // For *_STATE_CHANGED events.
59 void LogSocketReadyState(int channel_id, proto::ReadyState new_state);
60 void LogSocketConnectState(int channel_id, proto::ConnectionState new_state);
61 void LogSocketReadState(int channel_id, proto::ReadState new_state);
62 void LogSocketWriteState(int channel_id, proto::WriteState new_state);
63 void LogSocketErrorState(int channel_id, proto::ErrorState new_state);
65 // For AUTH_CHALLENGE_REPLY event.
66 void LogSocketChallengeReplyEvent(int channel_id,
67 const AuthResult& auth_result);
69 void LogSocketEventForMessage(int channel_id,
70 proto::EventType event_type,
71 const std::string& message_namespace,
72 const std::string& details);
74 // Assembles logs collected so far and return it as a serialized Log proto,
75 // compressed in gzip format.
76 // If serialization or compression failed, returns nullptr.
77 // |length|: If successful, assigned with size of compressed content.
78 scoped_ptr<char[]> GetLogs(size_t* length) const;
80 // Clears the internal map.
81 void Reset();
83 // Returns the last errors logged for |channel_id|. If the the logs for
84 // |channel_id| are evicted before this is called, returns a LastErrors with
85 // no errors. This may happen if errors are logged and retrieved in different
86 // tasks.
87 LastErrors GetLastErrors(int channel_id) const;
89 private:
90 friend class base::RefCounted<Logger>;
91 ~Logger();
93 struct AggregatedSocketEventLog {
94 public:
95 AggregatedSocketEventLog();
96 ~AggregatedSocketEventLog();
98 // Partially constructed AggregatedSocketEvent proto populated by Logger.
99 // Contains top level info such as channel ID, IP end point and channel
100 // auth type.
101 proto::AggregatedSocketEvent aggregated_socket_event;
102 // Events to be assigned to the AggregatedSocketEvent proto. Contains the
103 // most recent |kMaxEventsPerSocket| entries. The oldest events are
104 // evicted as new events are logged.
105 std::deque<proto::SocketEvent> socket_events;
107 // The most recent errors logged for the socket.
108 LastErrors last_errors;
111 typedef std::map<int, linked_ptr<AggregatedSocketEventLog> >
112 AggregatedSocketEventLogMap;
114 // Returns a SocketEvent proto with common fields (EventType, timestamp)
115 // populated.
116 proto::SocketEvent CreateEvent(proto::EventType event_type);
118 // Records |event| associated with |channel_id|.
119 // If the internal map is already logging maximum number of sockets and this
120 // is a new socket, the socket with the smallest channel id will be discarded.
121 // Returns a reference to the AggregatedSocketEvent proto created/modified.
122 proto::AggregatedSocketEvent& LogSocketEvent(
123 int channel_id,
124 const proto::SocketEvent& socket_event);
126 scoped_ptr<base::TickClock> clock_;
127 AggregatedSocketEventLogMap aggregated_socket_events_;
128 base::TimeTicks unix_epoch_time_ticks_;
130 // Log proto holding global statistics.
131 proto::Log log_;
133 base::ThreadChecker thread_checker_;
135 DISALLOW_COPY_AND_ASSIGN(Logger);
137 } // namespace cast_channel
138 } // namespace core_api
139 } // namespace extensions
141 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_