#ifndef REMOTING_SIGNALING_MESSAGE_CHANNEL_H_
#define REMOTING_SIGNALING_MESSAGE_CHANNEL_H_
#include <list>
#include <memory>
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "net/base/backoff_entry.h"
namespace remoting {
class HttpStatus;
class MessageChannelStrategy;
class ScopedProtobufHttpRequest;
class SignalingTracker;
class MessageChannel final {
public:
using DoneCallback = base::OnceCallback<void(const HttpStatus& status)>;
MessageChannel(std::unique_ptr<MessageChannelStrategy> strategy,
SignalingTracker* signaling_tracker);
MessageChannel(const MessageChannel&) = delete;
MessageChannel& operator=(const MessageChannel&) = delete;
~MessageChannel();
void StartReceivingMessages(base::OnceClosure on_ready,
DoneCallback on_closed);
void StopReceivingMessages();
bool IsReceivingMessages() const;
const net::BackoffEntry& GetReconnectRetryBackoffEntryForTesting() const;
private:
enum class State {
STOPPED,
STARTING,
STARTED,
};
void OnReceiveMessagesStreamReady();
void OnReceiveMessagesStreamClosed(const HttpStatus& status);
void OnChannelActive();
void RunStreamReadyCallbacks();
void RunStreamClosedCallbacks(const HttpStatus& status);
void RetryStartReceivingMessagesWithBackoff();
void RetryStartReceivingMessages();
void StartReceivingMessagesInternal();
void StopReceivingMessagesInternal();
void BeginStreamTimers();
void OnInactivityTimeout();
std::unique_ptr<MessageChannelStrategy> strategy_;
std::unique_ptr<ScopedProtobufHttpRequest> receive_messages_stream_;
std::list<base::OnceClosure> stream_ready_callbacks_;
std::list<DoneCallback> stream_closed_callbacks_;
State state_ = State::STOPPED;
net::BackoffEntry reconnect_retry_backoff_;
base::OneShotTimer reconnect_retry_timer_;
std::unique_ptr<base::DelayTimer> stream_inactivity_timer_;
raw_ptr<SignalingTracker> signaling_tracker_;
base::WeakPtrFactory<MessageChannel> weak_factory_{this};
};
}
#endif