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