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_NAMED_MESSAGE_PIPE_HANDLER_H_ | |
6 #define REMOTING_PROTOCOL_NAMED_MESSAGE_PIPE_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 MessagePipe. This class manages the | |
29 // lifetime itself: it deletes itself once the MessagePipe is closed or the | |
30 // derived instance actively calls Close() function. | |
31 class NamedMessagePipeHandler : public MessagePipe::EventHandler { | |
32 protected: | |
33 // The callers should create instances of derived classes instead of this | |
34 // class. So hide the constructor. | |
35 NamedMessagePipeHandler(const std::string& name, | |
36 std::unique_ptr<MessagePipe> pipe); | |
37 | |
38 ~NamedMessagePipeHandler() override; | |
39 | |
40 // Closes the channel and eventually destructs this instance. No operations | |
41 // should be performed after executing this function. | |
42 void Close(); | |
43 | |
44 const std::string& pipe_name() const { return name_; } | |
45 | |
46 // Whether |pipe_| has been connected. | |
47 bool connected() const { return is_connected_; } | |
48 | |
49 // Sends the message through the pipe. This function should only be called | |
50 // once connected() returns true. | |
51 void Send(google::protobuf::MessageLite* message, const base::Closure& done); | |
52 | |
53 // Derived classes can override these functions to receive data from the | |
54 // connection or observe the connection state. These functions will not be | |
55 // called unless |pipe_| has been opened. | |
56 virtual void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message); | |
57 virtual void OnConnected(); | |
58 virtual void OnDisconnecting(); | |
59 | |
60 private: | |
61 friend class base::DeleteHelper<NamedMessagePipeHandler>; | |
62 | |
63 // MessagePipe::EventHandler implementation. | |
64 void OnMessagePipeOpen() override; | |
65 void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override; | |
66 void OnMessagePipeClosed() override; | |
67 | |
68 const std::string name_; | |
69 std::unique_ptr<MessagePipe> pipe_; | |
70 base::ThreadChecker thread_checker_; | |
71 bool is_connected_ = false; | |
72 }; | |
73 | |
74 } // namespace protocol | |
75 } // namespace remoting | |
76 | |
77 #endif // REMOTING_PROTOCOL_NAMED_MESSAGE_PIPE_HANDLER_H_ | |
OLD | NEW |