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