blob: 52e3e5d45056c2d77e885bd021caf9ae66a7902e [file] [log] [blame]
[email protected]d7c6cc22012-02-05 05:19:271// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]2e8b52c2011-11-22 00:07:132// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_
6#define REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_
7
dcheng0765c492016-04-06 22:41:538#include <memory>
[email protected]2e8b52c2011-11-22 00:07:139#include <string>
10
[email protected]2e8b52c2011-11-22 00:07:1311#include "base/callback.h"
avi5a080f012015-12-22 23:15:4312#include "base/macros.h"
sergeyu203832d2015-01-10 21:44:0913#include "remoting/protocol/errors.h"
sergeyud059c462016-07-20 19:34:1014#include "remoting/protocol/message_pipe.h"
[email protected]2e8b52c2011-11-22 00:07:1315
[email protected]2e8b52c2011-11-22 00:07:1316namespace remoting {
sergeyuf1005f62016-02-03 21:11:3017
18class CompoundBuffer;
19
[email protected]2e8b52c2011-11-22 00:07:1320namespace protocol {
21
sergeyu786463e2016-02-04 18:46:1022class MessageChannelFactory;
[email protected]2e8b52c2011-11-22 00:07:1323
24// Base class for channel message dispatchers. It's responsible for
25// creating the named channel. Derived dispatchers then dispatch
26// incoming messages on this channel as well as send outgoing
27// messages.
sergeyud059c462016-07-20 19:34:1028class ChannelDispatcherBase : public MessagePipe::EventHandler {
[email protected]2e8b52c2011-11-22 00:07:1329 public:
sergeyu203832d2015-01-10 21:44:0930 class EventHandler {
31 public:
32 EventHandler() {}
33 virtual ~EventHandler() {}
34
sergeyud059c462016-07-20 19:34:1035 // Called after the channel is initialized.
sergeyu203832d2015-01-10 21:44:0936 virtual void OnChannelInitialized(
37 ChannelDispatcherBase* channel_dispatcher) = 0;
sergeyud059c462016-07-20 19:34:1038
39 // Called after the channel is closed.
40 virtual void OnChannelClosed(ChannelDispatcherBase* channel_dispatcher) = 0;
sergeyu203832d2015-01-10 21:44:0941 };
42
sergeyud059c462016-07-20 19:34:1043 ~ChannelDispatcherBase() override;
[email protected]2e8b52c2011-11-22 00:07:1344
sergeyud059c462016-07-20 19:34:1045 // Creates and connects the channel using |channel_factory|.
sergeyu786463e2016-02-04 18:46:1046 void Init(MessageChannelFactory* channel_factory,
47 EventHandler* event_handler);
sergeyu203832d2015-01-10 21:44:0948
sergeyud059c462016-07-20 19:34:1049 // Initializes the channel using |message_pipe| that's already connected.
50 void Init(std::unique_ptr<MessagePipe> message_pipe,
51 EventHandler* event_handler);
52
sergeyu203832d2015-01-10 21:44:0953 const std::string& channel_name() { return channel_name_; }
[email protected]2e8b52c2011-11-22 00:07:1354
55 // Returns true if the channel is currently connected.
sergeyucc40af912016-07-22 03:49:1956 bool is_connected() { return is_connected_; }
[email protected]2e8b52c2011-11-22 00:07:1357
58 protected:
sergeyu360f5bae2016-07-22 18:54:4559 explicit ChannelDispatcherBase(const std::string& channel_name);
[email protected]2e8b52c2011-11-22 00:07:1360
sergeyuf1005f62016-02-03 21:11:3061 MessagePipe* message_pipe() { return message_pipe_.get(); }
sergeyud8af2ca2016-01-30 03:04:3662
63 // Child classes must override this method to handle incoming messages.
dcheng0765c492016-04-06 22:41:5364 virtual void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message) = 0;
[email protected]2e8b52c2011-11-22 00:07:1365
66 private:
dcheng0765c492016-04-06 22:41:5367 void OnChannelReady(std::unique_ptr<MessagePipe> message_pipe);
sergeyud059c462016-07-20 19:34:1068
69 // MessagePipe::EventHandler interface.
sergeyucc40af912016-07-22 03:49:1970 void OnMessagePipeOpen() override;
sergeyud059c462016-07-20 19:34:1071 void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override;
72 void OnMessagePipeClosed() override;
[email protected]2e8b52c2011-11-22 00:07:1373
74 std::string channel_name_;
sergeyu786463e2016-02-04 18:46:1075 MessageChannelFactory* channel_factory_ = nullptr;
76 EventHandler* event_handler_ = nullptr;
sergeyucc40af912016-07-22 03:49:1977 bool is_connected_ = false;
[email protected]2e8b52c2011-11-22 00:07:1378
dcheng0765c492016-04-06 22:41:5379 std::unique_ptr<MessagePipe> message_pipe_;
sergeyu203832d2015-01-10 21:44:0980
[email protected]2e8b52c2011-11-22 00:07:1381 DISALLOW_COPY_AND_ASSIGN(ChannelDispatcherBase);
82};
83
84} // namespace protocol
85} // namespace remoting
86
87#endif // REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_