| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_ |
| 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_ | 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <deque> | 10 #include <deque> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 18 #include "extensions/browser/api/cast_channel/logger_util.h" | |
| 19 #include "extensions/common/api/cast_channel/logging.pb.h" | 18 #include "extensions/common/api/cast_channel/logging.pb.h" |
| 20 #include "net/base/ip_endpoint.h" | |
| 21 | 19 |
| 22 namespace base { | |
| 23 class Clock; | |
| 24 } | |
| 25 | 20 |
| 26 namespace extensions { | 21 namespace extensions { |
| 27 namespace api { | 22 namespace api { |
| 28 namespace cast_channel { | 23 namespace cast_channel { |
| 29 | 24 |
| 30 struct AuthResult; | 25 struct AuthResult; |
| 31 class CastSocket; | |
| 32 | 26 |
| 33 static const int kMaxSocketsToLog = 50; | 27 // Holds the most recent errors encountered by a CastSocket. |
| 34 static const int kMaxEventsPerSocket = 2000; | 28 struct LastErrors { |
| 29 public: |
| 30 LastErrors(); |
| 31 ~LastErrors(); |
| 35 | 32 |
| 36 // Logs information of each channel and sockets and exports the log as | 33 // The most recent event that occurred at the time of the error. |
| 37 // a blob. Logger is done on the IO thread. | 34 proto::EventType event_type; |
| 35 |
| 36 // The most recent ChallengeReplyErrorType logged for the socket. |
| 37 proto::ChallengeReplyErrorType challenge_reply_error_type; |
| 38 |
| 39 // The most recent net_return_value logged for the socket. |
| 40 int net_return_value; |
| 41 }; |
| 42 |
| 43 // Called with events that occur on a Cast Channel and remembers any that |
| 44 // warrant reporting to the caller in LastErrors. |
| 38 class Logger : public base::RefCountedThreadSafe<Logger> { | 45 class Logger : public base::RefCountedThreadSafe<Logger> { |
| 39 public: | 46 public: |
| 40 // |clock|: Clock used for generating timestamps for the events. Owned by | 47 Logger(); |
| 41 // this class. | |
| 42 // |unix_epoch_time|: The Time that corresponds to the Unix epoch. | |
| 43 // Can be set to other values (e.g. zero) for testing purposes. | |
| 44 // | |
| 45 // See crbug.com/518951 for information on why base::Clock | |
| 46 // is used instead of base::TickClock. | |
| 47 Logger(std::unique_ptr<base::Clock> clock, base::Time unix_epoch_time); | |
| 48 | |
| 49 // For newly created sockets. Will create an event and log a | |
| 50 // CAST_SOCKET_CREATED event. | |
| 51 void LogNewSocketEvent(const CastSocket& cast_socket); | |
| 52 | |
| 53 void LogSocketEvent(int channel_id, proto::EventType event_type); | |
| 54 void LogSocketEventWithDetails(int channel_id, | |
| 55 proto::EventType event_type, | |
| 56 const std::string& details); | |
| 57 | 48 |
| 58 // For events that involves socket / crypto operations that returns a value. | 49 // For events that involves socket / crypto operations that returns a value. |
| 59 void LogSocketEventWithRv(int channel_id, | 50 void LogSocketEventWithRv(int channel_id, |
| 60 proto::EventType event_type, | 51 proto::EventType event_type, |
| 61 int rv); | 52 int rv); |
| 62 | 53 |
| 63 // For *_STATE_CHANGED events. | |
| 64 void LogSocketReadyState(int channel_id, proto::ReadyState new_state); | |
| 65 void LogSocketConnectState(int channel_id, proto::ConnectionState new_state); | |
| 66 void LogSocketReadState(int channel_id, proto::ReadState new_state); | |
| 67 void LogSocketWriteState(int channel_id, proto::WriteState new_state); | |
| 68 void LogSocketErrorState(int channel_id, proto::ErrorState new_state); | |
| 69 | |
| 70 // For AUTH_CHALLENGE_REPLY event. | 54 // For AUTH_CHALLENGE_REPLY event. |
| 71 void LogSocketChallengeReplyEvent(int channel_id, | 55 void LogSocketChallengeReplyEvent(int channel_id, |
| 72 const AuthResult& auth_result); | 56 const AuthResult& auth_result); |
| 73 | 57 |
| 74 void LogSocketEventForMessage(int channel_id, | 58 // Returns the last errors logged for |channel_id|. |
| 75 proto::EventType event_type, | 59 LastErrors GetLastErrors(int channel_id) const; |
| 76 const std::string& message_namespace, | |
| 77 const std::string& details); | |
| 78 | 60 |
| 79 // Assembles logs collected so far and return it as a serialized Log proto, | 61 // Removes a LastErrors entry for |channel_id| if one exists. |
| 80 // compressed in gzip format. | 62 void ClearLastErrors(int channel_id); |
| 81 // If serialization or compression failed, returns nullptr. | |
| 82 // |length|: If successful, assigned with size of compressed content. | |
| 83 std::unique_ptr<char[]> GetLogs(size_t* length) const; | |
| 84 | |
| 85 // Clears the internal map. | |
| 86 void Reset(); | |
| 87 | |
| 88 // Returns the last errors logged for |channel_id|. If the the logs for | |
| 89 // |channel_id| are evicted before this is called, returns a LastErrors with | |
| 90 // no errors. This may happen if errors are logged and retrieved in different | |
| 91 // tasks. | |
| 92 LastErrors GetLastErrors(int channel_id) const; | |
| 93 | 63 |
| 94 private: | 64 private: |
| 95 friend class base::RefCountedThreadSafe<Logger>; | 65 friend class base::RefCountedThreadSafe<Logger>; |
| 96 ~Logger(); | 66 ~Logger(); |
| 97 | 67 |
| 98 struct AggregatedSocketEventLog { | 68 using LastErrorsMap = std::map<int, LastErrors>; |
| 99 public: | |
| 100 AggregatedSocketEventLog(); | |
| 101 ~AggregatedSocketEventLog(); | |
| 102 | |
| 103 // Partially constructed AggregatedSocketEvent proto populated by Logger. | |
| 104 // Contains top level info such as channel ID, IP end point and channel | |
| 105 // auth type. | |
| 106 proto::AggregatedSocketEvent aggregated_socket_event; | |
| 107 // Events to be assigned to the AggregatedSocketEvent proto. Contains the | |
| 108 // most recent |kMaxEventsPerSocket| entries. The oldest events are | |
| 109 // evicted as new events are logged. | |
| 110 std::deque<proto::SocketEvent> socket_events; | |
| 111 | |
| 112 // The most recent errors logged for the socket. | |
| 113 LastErrors last_errors; | |
| 114 }; | |
| 115 | |
| 116 using AggregatedSocketEventLogMap = | |
| 117 std::map<int, std::unique_ptr<AggregatedSocketEventLog>>; | |
| 118 | 69 |
| 119 // Returns a SocketEvent proto with common fields (EventType, timestamp) | 70 // Returns a SocketEvent proto with common fields (EventType, timestamp) |
| 120 // populated. | 71 // populated. |
| 121 proto::SocketEvent CreateEvent(proto::EventType event_type); | 72 proto::SocketEvent CreateEvent(proto::EventType event_type); |
| 122 | 73 |
| 123 // Records |event| associated with |channel_id|. | 74 // Uses |event| associated with |channel_id| to update its LastErrors. |
| 124 // If the internal map is already logging maximum number of sockets and this | 75 void LogSocketEvent(int channel_id, const proto::SocketEvent& socket_event); |
| 125 // is a new socket, the socket with the smallest channel id will be discarded. | |
| 126 // Returns a reference to the AggregatedSocketEvent proto created/modified. | |
| 127 proto::AggregatedSocketEvent& LogSocketEvent( | |
| 128 int channel_id, | |
| 129 const proto::SocketEvent& socket_event); | |
| 130 | 76 |
| 131 std::unique_ptr<base::Clock> clock_; | 77 LastErrorsMap last_errors_; |
| 132 AggregatedSocketEventLogMap aggregated_socket_events_; | |
| 133 base::Time unix_epoch_time_; | |
| 134 | |
| 135 // Log proto holding global statistics. | |
| 136 proto::Log log_; | |
| 137 | 78 |
| 138 base::ThreadChecker thread_checker_; | 79 base::ThreadChecker thread_checker_; |
| 139 | 80 |
| 140 DISALLOW_COPY_AND_ASSIGN(Logger); | 81 DISALLOW_COPY_AND_ASSIGN(Logger); |
| 141 }; | 82 }; |
| 142 } // namespace cast_channel | 83 } // namespace cast_channel |
| 143 } // namespace api | 84 } // namespace api |
| 144 } // namespace extensions | 85 } // namespace extensions |
| 145 | 86 |
| 146 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_ | 87 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_LOGGER_H_ |
| OLD | NEW |