logger.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.
  4. #ifndef COMPONENTS_CAST_CHANNEL_LOGGER_H_
  5. #define COMPONENTS_CAST_CHANNEL_LOGGER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include "base/memory/ref_counted.h"
  10. #include "base/threading/thread_checker.h"
  11. #include "components/cast_channel/cast_channel_enum.h"
  12. namespace cast_channel {
  13. struct AuthResult;
  14. // Holds the most recent error encountered by a CastSocket.
  15. struct LastError {
  16. public:
  17. LastError();
  18. ~LastError();
  19. // The most recent event that occurred at the time of the error.
  20. ChannelEvent channel_event;
  21. // The most recent ChallengeReplyError logged for the socket.
  22. // NOTE(mfoltz): AuthResult::ErrorType is zero-indexed and ChallengeReplyError
  23. // is one-indexed, so we can't use AuthResult::ErrorType here.
  24. ChallengeReplyError challenge_reply_error;
  25. // The most recent net_return_value logged for the socket.
  26. int net_return_value;
  27. };
  28. // Called with events that occur on a Cast Channel and remembers any that
  29. // warrant reporting to the caller in LastError.
  30. class Logger : public base::RefCountedThreadSafe<Logger> {
  31. public:
  32. Logger();
  33. Logger(const Logger&) = delete;
  34. Logger& operator=(const Logger&) = delete;
  35. // For events that involves socket / crypto operations that returns a value.
  36. void LogSocketEventWithRv(int channel_id, ChannelEvent channel_event, int rv);
  37. // For AUTH_CHALLENGE_REPLY event.
  38. void LogSocketChallengeReplyEvent(int channel_id,
  39. const AuthResult& auth_result);
  40. // Returns the last errors logged for |channel_id|.
  41. LastError GetLastError(int channel_id) const;
  42. // Removes a LastError entry for |channel_id| if one exists.
  43. void ClearLastError(int channel_id);
  44. private:
  45. friend class base::RefCountedThreadSafe<Logger>;
  46. ~Logger();
  47. // Propagate any error values in |rv| or |challenge_reply_error| to the
  48. // LastError for |channel_id|.
  49. void MaybeSetLastError(int channel_id,
  50. ChannelEvent channel_event,
  51. int rv,
  52. ChallengeReplyError challenge_reply_error);
  53. // Maps channel_id to the LastError for the channel.
  54. std::map<int, LastError> last_errors_;
  55. THREAD_CHECKER(thread_checker_);
  56. };
  57. } // namespace cast_channel
  58. #endif // COMPONENTS_CAST_CHANNEL_LOGGER_H_