[M73 Dev][EFL] Disable VizDisplayCompositor for EFL port
[platform/framework/web/chromium-efl.git] / components / cast_channel / keep_alive_delegate.h
1 // Copyright 2015 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
5 #ifndef COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_
6 #define COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_
7
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "base/timer/timer.h"
12 #include "components/cast_channel/cast_message_util.h"
13 #include "components/cast_channel/cast_transport.h"
14 #include "components/cast_channel/proto/cast_channel.pb.h"
15
16 namespace cast_channel {
17
18 class CastSocket;
19 class Logger;
20
21 // Decorator delegate which provides keep-alive functionality.
22 // Keep-alive messages are handled by this object; all other messages and
23 // errors are passed to |inner_delegate_|.
24 class KeepAliveDelegate : public CastTransport::Delegate {
25  public:
26   // |socket|: The socket to be kept alive.
27   // |logger|: The logging object which collects protocol events and error
28   //           details.
29   // |inner_delegate|: The delegate which processes all non-keep-alive
30   //                   messages. This object assumes ownership of
31   //                   |inner_delegate|.
32   // |ping_interval|: The amount of idle time to wait before sending a PING to
33   //                  the remote end.
34   // |liveness_timeout|: The amount of idle time to wait before terminating the
35   //                     connection.
36   KeepAliveDelegate(CastSocket* socket,
37                     scoped_refptr<Logger> logger,
38                     std::unique_ptr<CastTransport::Delegate> inner_delegate,
39                     base::TimeDelta ping_interval,
40                     base::TimeDelta liveness_timeout);
41
42   ~KeepAliveDelegate() override;
43
44   void SetTimersForTest(
45       std::unique_ptr<base::RetainingOneShotTimer> injected_ping_timer,
46       std::unique_ptr<base::RetainingOneShotTimer> injected_liveness_timer);
47
48   // CastTransport::Delegate implementation.
49   void Start() override;
50   void OnError(ChannelError error_state) override;
51   void OnMessage(const CastMessage& message) override;
52
53  private:
54   // Restarts the ping/liveness timeout timers. Called when a message
55   // is received from the remote end.
56   void ResetTimers();
57
58   // Sends a formatted PING or PONG message to the remote side.
59   void SendKeepAliveMessage(const CastMessage& message,
60                             CastMessageType message_type);
61
62   // Callback for SendKeepAliveMessage.
63   void SendKeepAliveMessageComplete(CastMessageType message_type, int rv);
64
65   // Called when the liveness timer expires, indicating that the remote
66   // end has not responded within the |liveness_timeout_| interval.
67   void LivenessTimeout();
68
69   // Stops the ping and liveness timers if they are started.
70   // To be called after an error.
71   void Stop();
72
73   // Indicates that Start() was called.
74   bool started_;
75
76   // Socket that is managed by the keep-alive object.
77   CastSocket* socket_;
78
79   // Logging object.
80   scoped_refptr<Logger> logger_;
81
82   // Delegate object which receives all non-keep alive messages.
83   std::unique_ptr<CastTransport::Delegate> inner_delegate_;
84
85   // Amount of idle time to wait before disconnecting.
86   base::TimeDelta liveness_timeout_;
87
88   // Amount of idle time to wait before pinging the receiver.
89   base::TimeDelta ping_interval_;
90
91   // Fired when |ping_interval_| is exceeded or when triggered by test code.
92   std::unique_ptr<base::RetainingOneShotTimer> ping_timer_;
93
94   // Fired when |liveness_timer_| is exceeded.
95   std::unique_ptr<base::RetainingOneShotTimer> liveness_timer_;
96
97   // The PING message to send over the wire.
98   const CastMessage ping_message_;
99
100   // The PONG message to send over the wire.
101   const CastMessage pong_message_;
102
103   THREAD_CHECKER(thread_checker_);
104
105   base::WeakPtrFactory<KeepAliveDelegate> weak_factory_;
106
107   DISALLOW_COPY_AND_ASSIGN(KeepAliveDelegate);
108 };
109
110 }  // namespace cast_channel
111
112 #endif  // COMPONENTS_CAST_CHANNEL_KEEP_ALIVE_DELEGATE_H_