blob: 3e70a7d0e5df015ff291339fb60a1bd8ccc99467 [file] [log] [blame]
// Copyright 2019 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 REMOTING_SIGNALING_FTL_MESSAGING_CLIENT_H_
#define REMOTING_SIGNALING_FTL_MESSAGING_CLIENT_H_
#include <memory>
#include <string>
#include "base/callback_forward.h"
#include "base/callback_list.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "remoting/signaling/ftl_services.grpc.pb.h"
namespace remoting {
class FtlGrpcContext;
class MessageReceptionChannel;
class ScopedGrpcServerStream;
// A class for sending and receiving messages via the FTL API.
class FtlMessagingClient final {
public:
using MessageCallback =
base::RepeatingCallback<void(const std::string& sender_id,
const std::string& message)>;
using MessageCallbackSubscription =
base::CallbackList<void(const std::string&,
const std::string&)>::Subscription;
using DoneCallback = base::OnceCallback<void(const grpc::Status& status)>;
explicit FtlMessagingClient(FtlGrpcContext* context);
~FtlMessagingClient();
// Registers a callback which is run for each new message received.
// Simply delete the returned subscription object to unregister. The
// subscription object must be deleted before |this| is deleted.
std::unique_ptr<MessageCallbackSubscription> RegisterMessageCallback(
const MessageCallback& callback);
// Retrieves messages from the user's inbox over slow path and calls the
// registered MessageCallback on every received message.
// |on_done| is called once the messages have been received and acked on the
// server's inbox.
void PullMessages(DoneCallback on_done);
// Opens a stream to continuously receive new messages from the server and
// calls the registered MessageCallback once a new message is received.
// |on_done| is called once the stream is successfully opened or failed to
// open due to an error.
void StartReceivingMessages(DoneCallback on_done);
// Stops the stream for continuously receiving new messages.
void StopReceivingMessages();
void SetMessageReceptionChannelForTesting(
std::unique_ptr<MessageReceptionChannel> channel);
private:
using Messaging =
google::internal::communications::instantmessaging::v1::Messaging;
void OnPullMessagesResponse(DoneCallback on_done,
const grpc::Status& status,
const ftl::PullMessagesResponse& response);
void AckMessages(const ftl::AckMessagesRequest& request,
DoneCallback on_done);
void OnAckMessagesResponse(DoneCallback on_done,
const grpc::Status& status,
const ftl::AckMessagesResponse& response);
void OpenReceiveMessagesStream(
base::OnceCallback<void(std::unique_ptr<ScopedGrpcServerStream>)>
on_stream_started,
const base::RepeatingCallback<void(const ftl::ReceiveMessagesResponse&)>&
on_incoming_msg,
base::OnceCallback<void(const grpc::Status&)> on_channel_closed);
void RunMessageCallbacks(const ftl::InboxMessage& message);
void OnMessageReceived(const ftl::InboxMessage& message);
FtlGrpcContext* context_;
std::unique_ptr<Messaging::Stub> messaging_stub_;
std::unique_ptr<MessageReceptionChannel> reception_channel_;
base::CallbackList<void(const std::string&, const std::string&)>
callback_list_;
base::WeakPtrFactory<FtlMessagingClient> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(FtlMessagingClient);
};
} // namespace remoting
#endif // REMOTING_SIGNALING_FTL_MESSAGING_CLIENT_H_