summaryrefslogtreecommitdiff
path: root/chromium/components/cast_channel/logger.h
blob: f4d91e8bda57aa13742d40c226299479292a6a69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CAST_CHANNEL_LOGGER_H_
#define COMPONENTS_CAST_CHANNEL_LOGGER_H_

#include <stddef.h>

#include <map>
#include <memory>
#include <string>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/threading/thread_checker.h"
#include "components/cast_channel/cast_channel_enum.h"

namespace cast_channel {

struct AuthResult;

// Holds the most recent error encountered by a CastSocket.
struct LastError {
 public:
  LastError();
  ~LastError();

  // The most recent event that occurred at the time of the error.
  ChannelEvent channel_event;

  // The most recent ChallengeReplyError logged for the socket.
  // NOTE(mfoltz): AuthResult::ErrorType is zero-indexed and ChallengeReplyError
  // is one-indexed, so we can't use AuthResult::ErrorType here.
  ChallengeReplyError challenge_reply_error;

  // The most recent net_return_value logged for the socket.
  int net_return_value;
};

// Called with events that occur on a Cast Channel and remembers any that
// warrant reporting to the caller in LastError.
class Logger : public base::RefCountedThreadSafe<Logger> {
 public:
  Logger();

  // For events that involves socket / crypto operations that returns a value.
  void LogSocketEventWithRv(int channel_id, ChannelEvent channel_event, int rv);

  // For AUTH_CHALLENGE_REPLY event.
  void LogSocketChallengeReplyEvent(int channel_id,
                                    const AuthResult& auth_result);

  // Returns the last errors logged for |channel_id|.
  LastError GetLastError(int channel_id) const;

  // Removes a LastError entry for |channel_id| if one exists.
  void ClearLastError(int channel_id);

 private:
  friend class base::RefCountedThreadSafe<Logger>;
  ~Logger();

  // Propagate any error values in |rv| or |challenge_reply_error| to the
  // LastError for |channel_id|.
  void MaybeSetLastError(int channel_id,
                         ChannelEvent channel_event,
                         int rv,
                         ChallengeReplyError challenge_reply_error);

  // Maps channel_id to the LastError for the channel.
  std::map<int, LastError> last_errors_;

  THREAD_CHECKER(thread_checker_);

  DISALLOW_COPY_AND_ASSIGN(Logger);
};
}  // namespace cast_channel

#endif  // COMPONENTS_CAST_CHANNEL_LOGGER_H_