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/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "extensions/browser/api/cast_channel/logger_util.h"
18 #include "extensions/common/api/cast_channel/logging.pb.h"
19 #include "net/base/ip_endpoint.h"
25 namespace extensions
{
27 namespace cast_channel
{
32 static const int kMaxSocketsToLog
= 50;
33 static const int kMaxEventsPerSocket
= 2000;
35 // Logs information of each channel and sockets and exports the log as
36 // a blob. Logger is done on the IO thread.
37 class Logger
: public base::RefCounted
<Logger
> {
39 // |clock|: Clock used for generating timestamps for the events. Owned by
41 // |unix_epoch_time_ticks|: The TimeTicks that corresponds to Unix epoch.
42 Logger(scoped_ptr
<base::TickClock
> clock
,
43 base::TimeTicks unix_epoch_time_ticks
);
45 // For newly created sockets. Will create an event and log a
46 // CAST_SOCKET_CREATED event.
47 void LogNewSocketEvent(const CastSocket
& cast_socket
);
49 void LogSocketEvent(int channel_id
, proto::EventType event_type
);
50 void LogSocketEventWithDetails(int channel_id
,
51 proto::EventType event_type
,
52 const std::string
& details
);
54 // For events that involves socket / crypto operations that returns a value.
55 void LogSocketEventWithRv(int channel_id
,
56 proto::EventType event_type
,
59 // For *_STATE_CHANGED events.
60 void LogSocketReadyState(int channel_id
, proto::ReadyState new_state
);
61 void LogSocketConnectState(int channel_id
, proto::ConnectionState new_state
);
62 void LogSocketReadState(int channel_id
, proto::ReadState new_state
);
63 void LogSocketWriteState(int channel_id
, proto::WriteState new_state
);
64 void LogSocketErrorState(int channel_id
, proto::ErrorState new_state
);
66 // For AUTH_CHALLENGE_REPLY event.
67 void LogSocketChallengeReplyEvent(int channel_id
,
68 const AuthResult
& auth_result
);
70 void LogSocketEventForMessage(int channel_id
,
71 proto::EventType event_type
,
72 const std::string
& message_namespace
,
73 const std::string
& details
);
75 // Assembles logs collected so far and return it as a serialized Log proto,
76 // compressed in gzip format.
77 // If serialization or compression failed, returns nullptr.
78 // |length|: If successful, assigned with size of compressed content.
79 scoped_ptr
<char[]> GetLogs(size_t* length
) const;
81 // Clears the internal map.
84 // Returns the last errors logged for |channel_id|. If the the logs for
85 // |channel_id| are evicted before this is called, returns a LastErrors with
86 // no errors. This may happen if errors are logged and retrieved in different
88 LastErrors
GetLastErrors(int channel_id
) const;
91 friend class base::RefCounted
<Logger
>;
94 struct AggregatedSocketEventLog
{
96 AggregatedSocketEventLog();
97 ~AggregatedSocketEventLog();
99 // Partially constructed AggregatedSocketEvent proto populated by Logger.
100 // Contains top level info such as channel ID, IP end point and channel
102 proto::AggregatedSocketEvent aggregated_socket_event
;
103 // Events to be assigned to the AggregatedSocketEvent proto. Contains the
104 // most recent |kMaxEventsPerSocket| entries. The oldest events are
105 // evicted as new events are logged.
106 std::deque
<proto::SocketEvent
> socket_events
;
108 // The most recent errors logged for the socket.
109 LastErrors last_errors
;
112 typedef std::map
<int, linked_ptr
<AggregatedSocketEventLog
> >
113 AggregatedSocketEventLogMap
;
115 // Returns a SocketEvent proto with common fields (EventType, timestamp)
117 proto::SocketEvent
CreateEvent(proto::EventType event_type
);
119 // Records |event| associated with |channel_id|.
120 // If the internal map is already logging maximum number of sockets and this
121 // is a new socket, the socket with the smallest channel id will be discarded.
122 // Returns a reference to the AggregatedSocketEvent proto created/modified.
123 proto::AggregatedSocketEvent
& LogSocketEvent(
125 const proto::SocketEvent
& socket_event
);
127 scoped_ptr
<base::TickClock
> clock_
;
128 AggregatedSocketEventLogMap aggregated_socket_events_
;
129 base::TimeTicks unix_epoch_time_ticks_
;
131 // Log proto holding global statistics.
134 base::ThreadChecker thread_checker_
;
136 DISALLOW_COPY_AND_ASSIGN(Logger
);
138 } // namespace cast_channel
140 } // namespace extensions
142 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_