OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_DATA_CHANNEL_HANDLER_H_ |
| 6 #define REMOTING_PROTOCOL_DATA_CHANNEL_HANDLER_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/sequenced_task_runner_helpers.h" |
| 13 #include "base/threading/thread_checker.h" |
| 14 #include "remoting/protocol/message_pipe.h" |
| 15 |
| 16 namespace google { |
| 17 namespace protobuf { |
| 18 class MessageLite; |
| 19 } // namespace protobuf |
| 20 } // namespace google |
| 21 |
| 22 namespace remoting { |
| 23 |
| 24 class CompoundBuffer; |
| 25 |
| 26 namespace protocol { |
| 27 |
| 28 // A base class to handle data from a named data channel. This class manages the |
| 29 // lifetime itself: it deletes itself once the data channel is closed or the |
| 30 // derived instance actively calls Close() function. |
| 31 class DataChannelHandler : public MessagePipe::EventHandler { |
| 32 protected: |
| 33 // Creates a DataChannelHandler to handle data from |pipe|. |
| 34 // This class is not virtual, but it is an no-op instance. So hide the |
| 35 // constructor. |
| 36 DataChannelHandler(const std::string& name, |
| 37 std::unique_ptr<MessagePipe> pipe); |
| 38 |
| 39 ~DataChannelHandler() override; |
| 40 |
| 41 // Closes the channel and eventually destructs this instance. No operations |
| 42 // should be performed after executing this function. |
| 43 void Close(); |
| 44 |
| 45 const std::string& pipe_name() const { return name_; } |
| 46 |
| 47 // Returns true if |this| object has been scheduled to be deleted. This |
| 48 // function always indicates the lifetime of |this| even the |pipe_| has never |
| 49 // been opened. |
| 50 bool finalized() const { return pipe_ == nullptr; } |
| 51 |
| 52 // Whether |pipe_| has been connected. |
| 53 bool connected() const { return is_connected_; } |
| 54 |
| 55 // Returns false if the message is not sent, usually it happens before the |
| 56 // |pipe_| is opened. |
| 57 bool Send(google::protobuf::MessageLite* message, const base::Closure& done); |
| 58 |
| 59 // Derived classes can override these functions to receive data from the |
| 60 // connection or observe the connection state. These functions will not be |
| 61 // called unless |pipe_| has been opened. |
| 62 |
| 63 virtual void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message); |
| 64 virtual void OnConnected(); |
| 65 virtual void OnDisconnecting(); |
| 66 |
| 67 private: |
| 68 friend class base::DeleteHelper<DataChannelHandler>; |
| 69 |
| 70 // MessagePipe::EventHandler implementation. |
| 71 void OnMessagePipeOpen() override; |
| 72 void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override; |
| 73 void OnMessagePipeClosed() override; |
| 74 |
| 75 const std::string name_; |
| 76 std::unique_ptr<MessagePipe> pipe_; |
| 77 base::ThreadChecker thread_checker_; |
| 78 bool is_connected_ = false; |
| 79 }; |
| 80 |
| 81 } // namespace protocol |
| 82 } // namespace remoting |
| 83 |
| 84 #endif // REMOTING_PROTOCOL_DATA_CHANNEL_HANDLER_H_ |
OLD | NEW |