[email protected] | d7c6cc2 | 2012-02-05 05:19:27 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 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 REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_ | ||||
6 | #define REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_ | ||||
7 | |||||
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 9 | #include <string> |
10 | |||||
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 11 | #include "base/callback.h" |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 12 | #include "base/macros.h" |
sergeyu | 203832d | 2015-01-10 21:44:09 | [diff] [blame] | 13 | #include "remoting/protocol/errors.h" |
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 14 | #include "remoting/protocol/message_pipe.h" |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 15 | |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 16 | namespace remoting { |
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 17 | |
18 | class CompoundBuffer; | ||||
19 | |||||
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 20 | namespace protocol { |
21 | |||||
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 22 | class MessageChannelFactory; |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 23 | |
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. | ||||
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 28 | class ChannelDispatcherBase : public MessagePipe::EventHandler { |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 29 | public: |
sergeyu | 203832d | 2015-01-10 21:44:09 | [diff] [blame] | 30 | class EventHandler { |
31 | public: | ||||
32 | EventHandler() {} | ||||
33 | virtual ~EventHandler() {} | ||||
34 | |||||
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 35 | // Called after the channel is initialized. |
sergeyu | 203832d | 2015-01-10 21:44:09 | [diff] [blame] | 36 | virtual void OnChannelInitialized( |
37 | ChannelDispatcherBase* channel_dispatcher) = 0; | ||||
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 38 | |
39 | // Called after the channel is closed. | ||||
40 | virtual void OnChannelClosed(ChannelDispatcherBase* channel_dispatcher) = 0; | ||||
sergeyu | 203832d | 2015-01-10 21:44:09 | [diff] [blame] | 41 | }; |
42 | |||||
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 43 | ~ChannelDispatcherBase() override; |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 44 | |
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 45 | // Creates and connects the channel using |channel_factory|. |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 46 | void Init(MessageChannelFactory* channel_factory, |
47 | EventHandler* event_handler); | ||||
sergeyu | 203832d | 2015-01-10 21:44:09 | [diff] [blame] | 48 | |
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 49 | // Initializes the channel using |message_pipe| that's already connected. |
50 | void Init(std::unique_ptr<MessagePipe> message_pipe, | ||||
51 | EventHandler* event_handler); | ||||
52 | |||||
sergeyu | 203832d | 2015-01-10 21:44:09 | [diff] [blame] | 53 | const std::string& channel_name() { return channel_name_; } |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 54 | |
55 | // Returns true if the channel is currently connected. | ||||
sergeyu | cc40af91 | 2016-07-22 03:49:19 | [diff] [blame] | 56 | bool is_connected() { return is_connected_; } |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 57 | |
58 | protected: | ||||
sergeyu | 360f5bae | 2016-07-22 18:54:45 | [diff] [blame] | 59 | explicit ChannelDispatcherBase(const std::string& channel_name); |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 60 | |
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 61 | MessagePipe* message_pipe() { return message_pipe_.get(); } |
sergeyu | d8af2ca | 2016-01-30 03:04:36 | [diff] [blame] | 62 | |
63 | // Child classes must override this method to handle incoming messages. | ||||
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 64 | virtual void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message) = 0; |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 65 | |
66 | private: | ||||
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 67 | void OnChannelReady(std::unique_ptr<MessagePipe> message_pipe); |
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 68 | |
69 | // MessagePipe::EventHandler interface. | ||||
sergeyu | cc40af91 | 2016-07-22 03:49:19 | [diff] [blame] | 70 | void OnMessagePipeOpen() override; |
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 71 | void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override; |
72 | void OnMessagePipeClosed() override; | ||||
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 73 | |
74 | std::string channel_name_; | ||||
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 75 | MessageChannelFactory* channel_factory_ = nullptr; |
76 | EventHandler* event_handler_ = nullptr; | ||||
sergeyu | cc40af91 | 2016-07-22 03:49:19 | [diff] [blame] | 77 | bool is_connected_ = false; |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 78 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 79 | std::unique_ptr<MessagePipe> message_pipe_; |
sergeyu | 203832d | 2015-01-10 21:44:09 | [diff] [blame] | 80 | |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 81 | DISALLOW_COPY_AND_ASSIGN(ChannelDispatcherBase); |
82 | }; | ||||
83 | |||||
84 | } // namespace protocol | ||||
85 | } // namespace remoting | ||||
86 | |||||
87 | #endif // REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_ |