| 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/process/process_handle.h" |
| 16 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 17 #include "ipc/ipc.mojom.h" | 18 #include "ipc/ipc.mojom.h" |
| 18 #include "ipc/ipc_export.h" | 19 #include "ipc/ipc_export.h" |
| 19 #include "ipc/ipc_message.h" | 20 #include "ipc/ipc_message.h" |
| 20 #include "mojo/public/cpp/bindings/associated_binding.h" | 21 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 21 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 22 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
| 22 #include "mojo/public/cpp/system/core.h" | 23 #include "mojo/public/cpp/system/core.h" |
| 23 #include "mojo/public/cpp/system/message_pipe.h" | 24 #include "mojo/public/cpp/system/message_pipe.h" |
| 24 | 25 |
| 25 namespace IPC { | 26 namespace IPC { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 40 // The constructor automatically start listening on the pipe. | 41 // The constructor automatically start listening on the pipe. |
| 41 // | 42 // |
| 42 // All functions must be called on the IO thread, except for Send(), which can | 43 // 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 | 44 // be called on any thread. All |Delegate| functions will be called on the IO |
| 44 // thread. | 45 // thread. |
| 45 // | 46 // |
| 46 class IPC_EXPORT MessagePipeReader : public NON_EXPORTED_BASE(mojom::Channel) { | 47 class IPC_EXPORT MessagePipeReader : public NON_EXPORTED_BASE(mojom::Channel) { |
| 47 public: | 48 public: |
| 48 class Delegate { | 49 class Delegate { |
| 49 public: | 50 public: |
| 51 virtual void OnPeerPidReceived() = 0; |
| 50 virtual void OnMessageReceived(const Message& message) = 0; | 52 virtual void OnMessageReceived(const Message& message) = 0; |
| 51 virtual void OnPipeError() = 0; | 53 virtual void OnPipeError() = 0; |
| 52 virtual void OnAssociatedInterfaceRequest( | 54 virtual void OnAssociatedInterfaceRequest( |
| 53 const std::string& name, | 55 const std::string& name, |
| 54 mojo::ScopedInterfaceEndpointHandle handle) = 0; | 56 mojo::ScopedInterfaceEndpointHandle handle) = 0; |
| 55 }; | 57 }; |
| 56 | 58 |
| 57 // Delay the object deletion using the current message loop. | |
| 58 // This is intended to used by MessagePipeReader owners. | |
| 59 class DelayedDeleter { | |
| 60 public: | |
| 61 typedef std::default_delete<MessagePipeReader> DefaultType; | |
| 62 | |
| 63 static void DeleteNow(MessagePipeReader* ptr) { delete ptr; } | |
| 64 | |
| 65 DelayedDeleter() {} | |
| 66 explicit DelayedDeleter(const DefaultType&) {} | |
| 67 DelayedDeleter& operator=(const DefaultType&) { return *this; } | |
| 68 | |
| 69 void operator()(MessagePipeReader* ptr) const; | |
| 70 }; | |
| 71 | |
| 72 // Builds a reader that reads messages from |receive_handle| and lets | 59 // Builds a reader that reads messages from |receive_handle| and lets |
| 73 // |delegate| know. | 60 // |delegate| know. |
| 74 // | 61 // |
| 75 // |pipe| is the message pipe handle corresponding to the channel's master | 62 // |pipe| is the message pipe handle corresponding to the channel's master |
| 76 // interface. This is the message pipe underlying both |sender| and | 63 // interface. This is the message pipe underlying both |sender| and |
| 77 // |receiver|. | 64 // |receiver|. |
| 78 // | 65 // |
| 79 // Both |sender| and |receiver| must be non-null. | 66 // Both |sender| and |receiver| must be non-null. |
| 80 // | 67 // |
| 81 // Note that MessagePipeReader doesn't delete |delegate|. | 68 // Note that MessagePipeReader doesn't delete |delegate|. |
| 82 MessagePipeReader(mojo::MessagePipeHandle pipe, | 69 MessagePipeReader(mojo::MessagePipeHandle pipe, |
| 83 mojom::ChannelAssociatedPtr sender, | 70 mojom::ChannelAssociatedPtr sender, |
| 84 mojo::AssociatedInterfaceRequest<mojom::Channel> receiver, | 71 mojo::AssociatedInterfaceRequest<mojom::Channel> receiver, |
| 85 base::ProcessId peer_pid, | |
| 86 Delegate* delegate); | 72 Delegate* delegate); |
| 87 ~MessagePipeReader() override; | 73 ~MessagePipeReader() override; |
| 88 | 74 |
| 89 // Close and destroy the MessagePipe. | 75 // Close and destroy the MessagePipe. |
| 90 void Close(); | 76 void Close(); |
| 91 | 77 |
| 92 // Return true if the MessagePipe is alive. | 78 // Return true if the MessagePipe is alive. |
| 93 bool IsValid() { return sender_; } | 79 bool IsValid() { return sender_; } |
| 94 | 80 |
| 95 // Sends an IPC::Message to the other end of the pipe. Safe to call from any | 81 // Sends an IPC::Message to the other end of the pipe. Safe to call from any |
| 96 // thread. | 82 // thread. |
| 97 bool Send(std::unique_ptr<Message> message); | 83 bool Send(std::unique_ptr<Message> message); |
| 98 | 84 |
| 99 // Requests an associated interface from the other end of the pipe. | 85 // Requests an associated interface from the other end of the pipe. |
| 100 void GetRemoteInterface(const std::string& name, | 86 void GetRemoteInterface(const std::string& name, |
| 101 mojo::ScopedInterfaceEndpointHandle handle); | 87 mojo::ScopedInterfaceEndpointHandle handle); |
| 102 | 88 |
| 103 base::ProcessId GetPeerPid() const { return peer_pid_; } | 89 base::ProcessId GetPeerPid() const { return peer_pid_; } |
| 104 | 90 |
| 105 protected: | 91 protected: |
| 106 void OnPipeClosed(); | 92 void OnPipeClosed(); |
| 107 void OnPipeError(MojoResult error); | 93 void OnPipeError(MojoResult error); |
| 108 | 94 |
| 109 private: | 95 private: |
| 110 // mojom::Channel: | 96 // mojom::Channel: |
| 97 void SetPeerPid(int32_t peer_pid) override; |
| 111 void Receive(mojo::Array<uint8_t> data, | 98 void Receive(mojo::Array<uint8_t> data, |
| 112 mojo::Array<mojom::SerializedHandlePtr> handles) override; | 99 mojo::Array<mojom::SerializedHandlePtr> handles) override; |
| 113 void GetAssociatedInterface( | 100 void GetAssociatedInterface( |
| 114 const mojo::String& name, | 101 const mojo::String& name, |
| 115 mojom::GenericInterfaceAssociatedRequest request) override; | 102 mojom::GenericInterfaceAssociatedRequest request) override; |
| 116 | 103 |
| 117 // |delegate_| is null once the message pipe is closed. | 104 // |delegate_| is null once the message pipe is closed. |
| 118 Delegate* delegate_; | 105 Delegate* delegate_; |
| 119 base::ProcessId peer_pid_; | 106 base::ProcessId peer_pid_ = base::kNullProcessId; |
| 120 mojom::ChannelAssociatedPtr sender_; | 107 mojom::ChannelAssociatedPtr sender_; |
| 121 mojo::AssociatedBinding<mojom::Channel> binding_; | 108 mojo::AssociatedBinding<mojom::Channel> binding_; |
| 122 | 109 |
| 123 // Raw message pipe handle and interface ID we use to send legacy IPC messages | 110 // Raw message pipe handle and interface ID we use to send legacy IPC messages |
| 124 // over the associated pipe. | 111 // over the associated pipe. |
| 125 const uint32_t sender_interface_id_; | 112 const uint32_t sender_interface_id_; |
| 126 const mojo::MessagePipeHandle sender_pipe_; | 113 const mojo::MessagePipeHandle sender_pipe_; |
| 127 | 114 |
| 128 base::ThreadChecker thread_checker_; | 115 base::ThreadChecker thread_checker_; |
| 129 | 116 |
| 130 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader); | 117 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader); |
| 131 }; | 118 }; |
| 132 | 119 |
| 133 } // namespace internal | 120 } // namespace internal |
| 134 } // namespace IPC | 121 } // namespace IPC |
| 135 | 122 |
| 136 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_ | 123 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_ |
| OLD | NEW |