blob: 77dd9ee2d94742b3c42e964f7fd58087096059ee [file] [log] [blame]
[email protected]64860882014-08-04 23:44:171// 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
avi246998d82015-12-22 02:39:048#include <stdint.h>
9
dcheng0917ec42015-11-19 07:00:2010#include <memory>
[email protected]64860882014-08-04 23:44:1711#include <vector>
12
amistry0b0e7482015-09-02 18:04:2213#include "base/atomicops.h"
morritad68bedf42014-11-25 23:35:5714#include "base/compiler_specific.h"
Ken Rockot3044d212018-01-23 02:44:3915#include "base/component_export.h"
avi246998d82015-12-22 02:39:0416#include "base/macros.h"
rockot0e4de5f2016-07-22 21:18:0717#include "base/process/process_handle.h"
amistry0b0e7482015-09-02 18:04:2218#include "base/threading/thread_checker.h"
amistryd4aa70d2016-06-23 07:52:3719#include "ipc/ipc.mojom.h"
morritad68bedf42014-11-25 23:35:5720#include "ipc/ipc_message.h"
sammce4d0abd2016-03-07 22:38:0421#include "mojo/public/cpp/bindings/associated_binding.h"
rockot7c6bf952016-07-14 00:34:1122#include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
rockot85dce0862015-11-13 01:33:5923#include "mojo/public/cpp/system/core.h"
rockot506f92fa22016-03-23 01:32:1824#include "mojo/public/cpp/system/message_pipe.h"
Ken Rockotfd907632017-09-14 04:23:4125#include "mojo/public/interfaces/bindings/native_struct.mojom.h"
[email protected]64860882014-08-04 23:44:1726
27namespace IPC {
28namespace internal {
29
30// A helper class to handle bytestream directly over mojo::MessagePipe
31// in template-method pattern. MessagePipeReader manages the lifetime
32// of given MessagePipe and participates the event loop, and
33// read the stream and call the client when it is ready.
34//
35// Each client has to:
36//
37// * Provide a subclass implemenation of a specific use of a MessagePipe
38// and implement callbacks.
39// * Create the subclass instance with a MessagePipeHandle.
40// The constructor automatically start listening on the pipe.
41//
amistry0b0e7482015-09-02 18:04:2242// 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// thread.
[email protected]64860882014-08-04 23:44:1745//
Ken Rockot3044d212018-01-23 02:44:3946class COMPONENT_EXPORT(IPC) MessagePipeReader : public mojom::Channel {
[email protected]64860882014-08-04 23:44:1747 public:
morritad68bedf42014-11-25 23:35:5748 class Delegate {
49 public:
sammcf810f07f2016-11-10 22:34:0750 virtual void OnPeerPidReceived(int32_t peer_pid) = 0;
sammce4d0abd2016-03-07 22:38:0451 virtual void OnMessageReceived(const Message& message) = 0;
Roman Karaseva43d5b4e2017-12-21 03:06:0252 virtual void OnBrokenDataReceived() = 0;
rockot506f92fa22016-03-23 01:32:1853 virtual void OnPipeError() = 0;
rockot7c6bf952016-07-14 00:34:1154 virtual void OnAssociatedInterfaceRequest(
55 const std::string& name,
56 mojo::ScopedInterfaceEndpointHandle handle) = 0;
morritad68bedf42014-11-25 23:35:5757 };
58
rockot506f92fa22016-03-23 01:32:1859 // Builds a reader that reads messages from |receive_handle| and lets
sammce4d0abd2016-03-07 22:38:0460 // |delegate| know.
rockot506f92fa22016-03-23 01:32:1861 //
62 // |pipe| is the message pipe handle corresponding to the channel's master
63 // interface. This is the message pipe underlying both |sender| and
64 // |receiver|.
65 //
66 // Both |sender| and |receiver| must be non-null.
67 //
sammce4d0abd2016-03-07 22:38:0468 // Note that MessagePipeReader doesn't delete |delegate|.
rockot506f92fa22016-03-23 01:32:1869 MessagePipeReader(mojo::MessagePipeHandle pipe,
70 mojom::ChannelAssociatedPtr sender,
sammce4d0abd2016-03-07 22:38:0471 mojo::AssociatedInterfaceRequest<mojom::Channel> receiver,
72 Delegate* delegate);
73 ~MessagePipeReader() override;
[email protected]64860882014-08-04 23:44:1774
75 // Close and destroy the MessagePipe.
76 void Close();
morritab4472142015-04-20 21:20:1277
[email protected]64860882014-08-04 23:44:1778 // Return true if the MessagePipe is alive.
Jeremy Romanc13482982017-06-14 00:53:5979 bool IsValid() { return sender_.is_bound(); }
[email protected]64860882014-08-04 23:44:1780
rockot506f92fa22016-03-23 01:32:1881 // Sends an IPC::Message to the other end of the pipe. Safe to call from any
82 // thread.
danakj03de39b22016-04-23 04:21:0983 bool Send(std::unique_ptr<Message> message);
[email protected]64860882014-08-04 23:44:1784
rockot7c6bf952016-07-14 00:34:1185 // Requests an associated interface from the other end of the pipe.
86 void GetRemoteInterface(const std::string& name,
87 mojo::ScopedInterfaceEndpointHandle handle);
88
rockota628d0b2017-02-09 08:40:1589 mojom::ChannelAssociatedPtr& sender() { return sender_; }
rockot401fb2c2016-09-06 18:35:5790
sammce4d0abd2016-03-07 22:38:0491 protected:
morritad68bedf42014-11-25 23:35:5792 void OnPipeClosed();
93 void OnPipeError(MojoResult error);
94
sammce4d0abd2016-03-07 22:38:0495 private:
rockot506f92fa22016-03-23 01:32:1896 // mojom::Channel:
rockot0e4de5f2016-07-22 21:18:0797 void SetPeerPid(int32_t peer_pid) override;
Eve Martin-Jones475e7e62018-02-13 22:57:2598 void Receive(base::span<const uint8_t> data,
99 base::Optional<std::vector<mojo::native::SerializedHandlePtr>>
100 handles) override;
rockot7c6bf952016-07-14 00:34:11101 void GetAssociatedInterface(
yzshen24b40a32016-08-24 01:10:13102 const std::string& name,
rockot7c6bf952016-07-14 00:34:11103 mojom::GenericInterfaceAssociatedRequest request) override;
[email protected]64860882014-08-04 23:44:17104
sammce4d0abd2016-03-07 22:38:04105 // |delegate_| is null once the message pipe is closed.
morritad68bedf42014-11-25 23:35:57106 Delegate* delegate_;
sammce4d0abd2016-03-07 22:38:04107 mojom::ChannelAssociatedPtr sender_;
108 mojo::AssociatedBinding<mojom::Channel> binding_;
amistry0b0e7482015-09-02 18:04:22109 base::ThreadChecker thread_checker_;
[email protected]64860882014-08-04 23:44:17110
111 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader);
112};
113
114} // namespace internal
115} // namespace IPC
116
117#endif // IPC_IPC_MESSAGE_PIPE_READER_H_