OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 IPC_IPC_MESSAGE_PIPE_READER_H_ | |
6 #define IPC_IPC_MESSAGE_PIPE_READER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <vector> | |
12 | |
13 #include "base/atomicops.h" | |
14 #include "base/compiler_specific.h" | |
15 #include "base/macros.h" | |
16 #include "base/threading/thread_checker.h" | |
17 #include "ipc/ipc_message.h" | |
18 #include "ipc/mojo/ipc.mojom.h" | |
19 #include "mojo/public/cpp/bindings/associated_binding.h" | |
20 #include "mojo/public/cpp/system/core.h" | |
21 #include "mojo/public/cpp/system/message_pipe.h" | |
22 | |
23 namespace IPC { | |
24 namespace internal { | |
25 | |
26 class AsyncHandleWaiter; | |
27 | |
28 // A helper class to handle bytestream directly over mojo::MessagePipe | |
29 // in template-method pattern. MessagePipeReader manages the lifetime | |
30 // of given MessagePipe and participates the event loop, and | |
31 // read the stream and call the client when it is ready. | |
32 // | |
33 // Each client has to: | |
34 // | |
35 // * Provide a subclass implemenation of a specific use of a MessagePipe | |
36 // and implement callbacks. | |
37 // * Create the subclass instance with a MessagePipeHandle. | |
38 // The constructor automatically start listening on the pipe. | |
39 // | |
40 // All functions must be called on the IO thread, except for Send(), which can | |
41 // be called on any thread. All |Delegate| functions will be called on the IO | |
42 // thread. | |
43 // | |
44 class MessagePipeReader : public mojom::Channel { | |
45 public: | |
46 class Delegate { | |
47 public: | |
48 virtual void OnMessageReceived(const Message& message) = 0; | |
49 virtual void OnPipeError() = 0; | |
50 }; | |
51 | |
52 // Delay the object deletion using the current message loop. | |
53 // This is intended to used by MessagePipeReader owners. | |
54 class DelayedDeleter { | |
55 public: | |
56 typedef std::default_delete<MessagePipeReader> DefaultType; | |
57 | |
58 static void DeleteNow(MessagePipeReader* ptr) { delete ptr; } | |
59 | |
60 DelayedDeleter() {} | |
61 explicit DelayedDeleter(const DefaultType&) {} | |
62 DelayedDeleter& operator=(const DefaultType&) { return *this; } | |
63 | |
64 void operator()(MessagePipeReader* ptr) const; | |
65 }; | |
66 | |
67 // Builds a reader that reads messages from |receive_handle| and lets | |
68 // |delegate| know. | |
69 // | |
70 // |pipe| is the message pipe handle corresponding to the channel's master | |
71 // interface. This is the message pipe underlying both |sender| and | |
72 // |receiver|. | |
73 // | |
74 // Both |sender| and |receiver| must be non-null. | |
75 // | |
76 // Note that MessagePipeReader doesn't delete |delegate|. | |
77 MessagePipeReader(mojo::MessagePipeHandle pipe, | |
78 mojom::ChannelAssociatedPtr sender, | |
79 mojo::AssociatedInterfaceRequest<mojom::Channel> receiver, | |
80 base::ProcessId peer_pid, | |
81 Delegate* delegate); | |
82 ~MessagePipeReader() override; | |
83 | |
84 // Close and destroy the MessagePipe. | |
85 void Close(); | |
86 | |
87 // Return true if the MessagePipe is alive. | |
88 bool IsValid() { return sender_; } | |
89 | |
90 // Sends an IPC::Message to the other end of the pipe. Safe to call from any | |
91 // thread. | |
92 bool Send(std::unique_ptr<Message> message); | |
93 | |
94 base::ProcessId GetPeerPid() const { return peer_pid_; } | |
95 | |
96 protected: | |
97 void OnPipeClosed(); | |
98 void OnPipeError(MojoResult error); | |
99 | |
100 private: | |
101 // mojom::Channel: | |
102 void Receive(mojo::Array<uint8_t> data, | |
103 mojo::Array<mojom::SerializedHandlePtr> handles) override; | |
104 | |
105 // |delegate_| is null once the message pipe is closed. | |
106 Delegate* delegate_; | |
107 base::ProcessId peer_pid_; | |
108 mojom::ChannelAssociatedPtr sender_; | |
109 mojo::AssociatedBinding<mojom::Channel> binding_; | |
110 | |
111 // Raw message pipe handle and interface ID we use to send legacy IPC messages | |
112 // over the associated pipe. | |
113 const uint32_t sender_interface_id_; | |
114 const mojo::MessagePipeHandle sender_pipe_; | |
115 | |
116 base::ThreadChecker thread_checker_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader); | |
119 }; | |
120 | |
121 } // namespace internal | |
122 } // namespace IPC | |
123 | |
124 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_ | |
OLD | NEW |