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_
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"
24 namespace extensions
{
26 namespace cast_channel
{
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
> {
38 // |clock|: Clock used for generating timestamps for the events. Owned by
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
,
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.
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
87 LastErrors
GetLastErrors(int channel_id
) const;
90 friend class base::RefCounted
<Logger
>;
93 struct AggregatedSocketEventLog
{
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
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)
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(
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.
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_