sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 1 | // Copyright 2016 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 REMOTING_PROTOCOL_MESSAGE_PIPE_H_ |
| 6 | #define REMOTING_PROTOCOL_MESSAGE_PIPE_H_ |
| 7 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 10 | #include "base/callback_forward.h" |
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 11 | |
| 12 | namespace google { |
| 13 | namespace protobuf { |
| 14 | class MessageLite; |
| 15 | } // namespace protobuf |
| 16 | } // namespace google |
| 17 | |
| 18 | namespace remoting { |
| 19 | |
| 20 | class CompoundBuffer; |
| 21 | |
| 22 | namespace protocol { |
| 23 | |
| 24 | // Represents a bi-directional pipe that allows to send and receive messages. |
| 25 | class MessagePipe { |
| 26 | public: |
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 27 | class EventHandler { |
| 28 | public: |
sergeyu | cc40af91 | 2016-07-22 03:49:19 | [diff] [blame] | 29 | // Called when the channel is open. |
| 30 | virtual void OnMessagePipeOpen() = 0; |
| 31 | |
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 32 | // Called when a message is received. |
| 33 | virtual void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) = 0; |
| 34 | |
| 35 | // Called when the channel is closed. |
| 36 | virtual void OnMessagePipeClosed() = 0; |
| 37 | |
| 38 | protected: |
| 39 | virtual ~EventHandler() {} |
| 40 | }; |
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 41 | |
| 42 | virtual ~MessagePipe() {} |
| 43 | |
sergeyu | cc40af91 | 2016-07-22 03:49:19 | [diff] [blame] | 44 | // Starts the channel. Must be called immediately after MessagePipe is |
| 45 | // created. |event_handler| will be notified when state of the pipe changes or |
| 46 | // when a message is received. |
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 47 | virtual void Start(EventHandler* event_handler) = 0; |
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 48 | |
| 49 | // Sends a message. |done| is called when the message has been sent to the |
| 50 | // client, but it doesn't mean that the client has received it. |done| is |
| 51 | // never called if the message is never sent (e.g. if the pipe is destroyed |
| 52 | // before the message is sent). |
Zijie He | 55f07c0 | 2017-08-31 17:25:49 | [diff] [blame] | 53 | // |message| is guaranteed to be used synchronously. It won't be referred |
| 54 | // after this function returns. |
| 55 | // TODO(zijiehe): the |message| should be const-ref. |
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 56 | virtual void Send(google::protobuf::MessageLite* message, |
| 57 | const base::Closure& done) = 0; |
| 58 | }; |
| 59 | |
| 60 | } // namespace protocol |
| 61 | } // namespace remoting |
| 62 | |
| 63 | #endif // REMOTING_PROTOCOL_MESSAGE_PIPE_H_ |