Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / logger.h
blob4dc73e4df1fd4dde583aaa5e6a2f8becba31a7fa
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/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"
21 namespace base {
22 class Clock;
25 namespace extensions {
26 namespace api {
27 namespace cast_channel {
29 struct AuthResult;
30 class CastSocket;
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> {
38 public:
39 // |clock|: Clock used for generating timestamps for the events. Owned by
40 // this class.
41 // |unix_epoch_time|: The Time that corresponds to the Unix epoch.
42 // Can be set to other values (e.g. zero) for testing purposes.
44 // See crbug.com/518951 for information on why base::Clock
45 // is used instead of base::TickClock.
46 Logger(scoped_ptr<base::Clock> clock, base::Time unix_epoch_time);
48 // For newly created sockets. Will create an event and log a
49 // CAST_SOCKET_CREATED event.
50 void LogNewSocketEvent(const CastSocket& cast_socket);
52 void LogSocketEvent(int channel_id, proto::EventType event_type);
53 void LogSocketEventWithDetails(int channel_id,
54 proto::EventType event_type,
55 const std::string& details);
57 // For events that involves socket / crypto operations that returns a value.
58 void LogSocketEventWithRv(int channel_id,
59 proto::EventType event_type,
60 int rv);
62 // For *_STATE_CHANGED events.
63 void LogSocketReadyState(int channel_id, proto::ReadyState new_state);
64 void LogSocketConnectState(int channel_id, proto::ConnectionState new_state);
65 void LogSocketReadState(int channel_id, proto::ReadState new_state);
66 void LogSocketWriteState(int channel_id, proto::WriteState new_state);
67 void LogSocketErrorState(int channel_id, proto::ErrorState new_state);
69 // For AUTH_CHALLENGE_REPLY event.
70 void LogSocketChallengeReplyEvent(int channel_id,
71 const AuthResult& auth_result);
73 void LogSocketEventForMessage(int channel_id,
74 proto::EventType event_type,
75 const std::string& message_namespace,
76 const std::string& details);
78 // Assembles logs collected so far and return it as a serialized Log proto,
79 // compressed in gzip format.
80 // If serialization or compression failed, returns nullptr.
81 // |length|: If successful, assigned with size of compressed content.
82 scoped_ptr<char[]> GetLogs(size_t* length) const;
84 // Clears the internal map.
85 void Reset();
87 // Returns the last errors logged for |channel_id|. If the the logs for
88 // |channel_id| are evicted before this is called, returns a LastErrors with
89 // no errors. This may happen if errors are logged and retrieved in different
90 // tasks.
91 LastErrors GetLastErrors(int channel_id) const;
93 private:
94 friend class base::RefCounted<Logger>;
95 ~Logger();
97 struct AggregatedSocketEventLog {
98 public:
99 AggregatedSocketEventLog();
100 ~AggregatedSocketEventLog();
102 // Partially constructed AggregatedSocketEvent proto populated by Logger.
103 // Contains top level info such as channel ID, IP end point and channel
104 // auth type.
105 proto::AggregatedSocketEvent aggregated_socket_event;
106 // Events to be assigned to the AggregatedSocketEvent proto. Contains the
107 // most recent |kMaxEventsPerSocket| entries. The oldest events are
108 // evicted as new events are logged.
109 std::deque<proto::SocketEvent> socket_events;
111 // The most recent errors logged for the socket.
112 LastErrors last_errors;
115 typedef std::map<int, linked_ptr<AggregatedSocketEventLog> >
116 AggregatedSocketEventLogMap;
118 // Returns a SocketEvent proto with common fields (EventType, timestamp)
119 // populated.
120 proto::SocketEvent CreateEvent(proto::EventType event_type);
122 // Records |event| associated with |channel_id|.
123 // If the internal map is already logging maximum number of sockets and this
124 // is a new socket, the socket with the smallest channel id will be discarded.
125 // Returns a reference to the AggregatedSocketEvent proto created/modified.
126 proto::AggregatedSocketEvent& LogSocketEvent(
127 int channel_id,
128 const proto::SocketEvent& socket_event);
130 scoped_ptr<base::Clock> clock_;
131 AggregatedSocketEventLogMap aggregated_socket_events_;
132 base::Time unix_epoch_time_;
134 // Log proto holding global statistics.
135 proto::Log log_;
137 base::ThreadChecker thread_checker_;
139 DISALLOW_COPY_AND_ASSIGN(Logger);
141 } // namespace cast_channel
142 } // namespace api
143 } // namespace extensions
145 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_