OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_MESSAGE_PIPE_READER_H_ | 5 #ifndef IPC_IPC_MESSAGE_PIPE_READER_H_ |
6 #define IPC_IPC_MESSAGE_PIPE_READER_H_ | 6 #define IPC_IPC_MESSAGE_PIPE_READER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/atomicops.h" | 13 #include "base/atomicops.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
17 #include "ipc/ipc.mojom.h" | 17 #include "ipc/ipc.mojom.h" |
18 #include "ipc/ipc_export.h" | |
19 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
20 #include "mojo/public/cpp/bindings/associated_binding.h" | 19 #include "mojo/public/cpp/bindings/associated_binding.h" |
21 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | |
22 #include "mojo/public/cpp/system/core.h" | 20 #include "mojo/public/cpp/system/core.h" |
23 #include "mojo/public/cpp/system/message_pipe.h" | 21 #include "mojo/public/cpp/system/message_pipe.h" |
24 | 22 |
25 namespace IPC { | 23 namespace IPC { |
26 namespace internal { | 24 namespace internal { |
27 | 25 |
28 class AsyncHandleWaiter; | 26 class AsyncHandleWaiter; |
29 | 27 |
30 // A helper class to handle bytestream directly over mojo::MessagePipe | 28 // A helper class to handle bytestream directly over mojo::MessagePipe |
31 // in template-method pattern. MessagePipeReader manages the lifetime | 29 // in template-method pattern. MessagePipeReader manages the lifetime |
32 // of given MessagePipe and participates the event loop, and | 30 // of given MessagePipe and participates the event loop, and |
33 // read the stream and call the client when it is ready. | 31 // read the stream and call the client when it is ready. |
34 // | 32 // |
35 // Each client has to: | 33 // Each client has to: |
36 // | 34 // |
37 // * Provide a subclass implemenation of a specific use of a MessagePipe | 35 // * Provide a subclass implemenation of a specific use of a MessagePipe |
38 // and implement callbacks. | 36 // and implement callbacks. |
39 // * Create the subclass instance with a MessagePipeHandle. | 37 // * Create the subclass instance with a MessagePipeHandle. |
40 // The constructor automatically start listening on the pipe. | 38 // The constructor automatically start listening on the pipe. |
41 // | 39 // |
42 // All functions must be called on the IO thread, except for Send(), which can | 40 // All functions must be called on the IO thread, except for Send(), which can |
43 // be called on any thread. All |Delegate| functions will be called on the IO | 41 // be called on any thread. All |Delegate| functions will be called on the IO |
44 // thread. | 42 // thread. |
45 // | 43 // |
46 class IPC_EXPORT MessagePipeReader : public NON_EXPORTED_BASE(mojom::Channel) { | 44 class MessagePipeReader : public mojom::Channel { |
47 public: | 45 public: |
48 class Delegate { | 46 class Delegate { |
49 public: | 47 public: |
50 virtual void OnMessageReceived(const Message& message) = 0; | 48 virtual void OnMessageReceived(const Message& message) = 0; |
51 virtual void OnPipeError() = 0; | 49 virtual void OnPipeError() = 0; |
52 virtual void OnAssociatedInterfaceRequest( | |
53 const std::string& name, | |
54 mojo::ScopedInterfaceEndpointHandle handle) = 0; | |
55 }; | 50 }; |
56 | 51 |
57 // Delay the object deletion using the current message loop. | 52 // Delay the object deletion using the current message loop. |
58 // This is intended to used by MessagePipeReader owners. | 53 // This is intended to used by MessagePipeReader owners. |
59 class DelayedDeleter { | 54 class DelayedDeleter { |
60 public: | 55 public: |
61 typedef std::default_delete<MessagePipeReader> DefaultType; | 56 typedef std::default_delete<MessagePipeReader> DefaultType; |
62 | 57 |
63 static void DeleteNow(MessagePipeReader* ptr) { delete ptr; } | 58 static void DeleteNow(MessagePipeReader* ptr) { delete ptr; } |
64 | 59 |
(...skipping 24 matching lines...) Expand all Loading... |
89 // Close and destroy the MessagePipe. | 84 // Close and destroy the MessagePipe. |
90 void Close(); | 85 void Close(); |
91 | 86 |
92 // Return true if the MessagePipe is alive. | 87 // Return true if the MessagePipe is alive. |
93 bool IsValid() { return sender_; } | 88 bool IsValid() { return sender_; } |
94 | 89 |
95 // Sends an IPC::Message to the other end of the pipe. Safe to call from any | 90 // Sends an IPC::Message to the other end of the pipe. Safe to call from any |
96 // thread. | 91 // thread. |
97 bool Send(std::unique_ptr<Message> message); | 92 bool Send(std::unique_ptr<Message> message); |
98 | 93 |
99 // Requests an associated interface from the other end of the pipe. | |
100 void GetRemoteInterface(const std::string& name, | |
101 mojo::ScopedInterfaceEndpointHandle handle); | |
102 | |
103 base::ProcessId GetPeerPid() const { return peer_pid_; } | 94 base::ProcessId GetPeerPid() const { return peer_pid_; } |
104 | 95 |
105 protected: | 96 protected: |
106 void OnPipeClosed(); | 97 void OnPipeClosed(); |
107 void OnPipeError(MojoResult error); | 98 void OnPipeError(MojoResult error); |
108 | 99 |
109 private: | 100 private: |
110 // mojom::Channel: | 101 // mojom::Channel: |
111 void Receive(mojo::Array<uint8_t> data, | 102 void Receive(mojo::Array<uint8_t> data, |
112 mojo::Array<mojom::SerializedHandlePtr> handles) override; | 103 mojo::Array<mojom::SerializedHandlePtr> handles) override; |
113 void GetAssociatedInterface( | |
114 const mojo::String& name, | |
115 mojom::GenericInterfaceAssociatedRequest request) override; | |
116 | 104 |
117 // |delegate_| is null once the message pipe is closed. | 105 // |delegate_| is null once the message pipe is closed. |
118 Delegate* delegate_; | 106 Delegate* delegate_; |
119 base::ProcessId peer_pid_; | 107 base::ProcessId peer_pid_; |
120 mojom::ChannelAssociatedPtr sender_; | 108 mojom::ChannelAssociatedPtr sender_; |
121 mojo::AssociatedBinding<mojom::Channel> binding_; | 109 mojo::AssociatedBinding<mojom::Channel> binding_; |
122 | 110 |
123 // Raw message pipe handle and interface ID we use to send legacy IPC messages | 111 // Raw message pipe handle and interface ID we use to send legacy IPC messages |
124 // over the associated pipe. | 112 // over the associated pipe. |
125 const uint32_t sender_interface_id_; | 113 const uint32_t sender_interface_id_; |
126 const mojo::MessagePipeHandle sender_pipe_; | 114 const mojo::MessagePipeHandle sender_pipe_; |
127 | 115 |
128 base::ThreadChecker thread_checker_; | 116 base::ThreadChecker thread_checker_; |
129 | 117 |
130 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader); | 118 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader); |
131 }; | 119 }; |
132 | 120 |
133 } // namespace internal | 121 } // namespace internal |
134 } // namespace IPC | 122 } // namespace IPC |
135 | 123 |
136 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_ | 124 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_ |
OLD | NEW |