blob: a155dcfb372bcb009a9c4c2c034e1e3ed2b4fd90 [file] [log] [blame]
morrita@chromium.org15996aa2014-08-05 08:44:17 +09001// 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
avi42ebda42015-12-22 11:39:04 +09008#include <stdint.h>
9
dcheng0bdf3772015-11-19 16:00:20 +090010#include <memory>
morrita@chromium.org15996aa2014-08-05 08:44:17 +090011#include <vector>
12
amistry5dfb9c22015-09-03 03:04:22 +090013#include "base/atomicops.h"
morrita1f42dc62014-11-26 08:35:57 +090014#include "base/compiler_specific.h"
avi42ebda42015-12-22 11:39:04 +090015#include "base/macros.h"
rockota01dbc72016-07-23 06:18:07 +090016#include "base/process/process_handle.h"
amistry5dfb9c22015-09-03 03:04:22 +090017#include "base/threading/thread_checker.h"
amistry6555e692016-06-23 16:52:37 +090018#include "ipc/ipc.mojom.h"
rockota6edf832016-07-14 09:34:11 +090019#include "ipc/ipc_export.h"
morrita1f42dc62014-11-26 08:35:57 +090020#include "ipc/ipc_message.h"
sammc6194d972016-03-08 07:38:04 +090021#include "mojo/public/cpp/bindings/associated_binding.h"
rockota6edf832016-07-14 09:34:11 +090022#include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
rockotaf32acb2015-11-13 10:33:59 +090023#include "mojo/public/cpp/system/core.h"
rockot6e3d4922016-03-23 10:32:18 +090024#include "mojo/public/cpp/system/message_pipe.h"
Ken Rockotb3a77c92017-09-14 13:23:41 +090025#include "mojo/public/interfaces/bindings/native_struct.mojom.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090026
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//
amistry5dfb9c22015-09-03 03:04:22 +090042// 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.
morrita@chromium.org15996aa2014-08-05 08:44:17 +090045//
Nico Weber1615c312017-08-16 04:19:27 +090046class IPC_EXPORT MessagePipeReader : public mojom::Channel {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090047 public:
morrita1f42dc62014-11-26 08:35:57 +090048 class Delegate {
49 public:
sammcb83485d2016-11-11 07:34:07 +090050 virtual void OnPeerPidReceived(int32_t peer_pid) = 0;
sammc6194d972016-03-08 07:38:04 +090051 virtual void OnMessageReceived(const Message& message) = 0;
rockot6e3d4922016-03-23 10:32:18 +090052 virtual void OnPipeError() = 0;
rockota6edf832016-07-14 09:34:11 +090053 virtual void OnAssociatedInterfaceRequest(
54 const std::string& name,
55 mojo::ScopedInterfaceEndpointHandle handle) = 0;
morrita1f42dc62014-11-26 08:35:57 +090056 };
57
rockot6e3d4922016-03-23 10:32:18 +090058 // Builds a reader that reads messages from |receive_handle| and lets
sammc6194d972016-03-08 07:38:04 +090059 // |delegate| know.
rockot6e3d4922016-03-23 10:32:18 +090060 //
61 // |pipe| is the message pipe handle corresponding to the channel's master
62 // interface. This is the message pipe underlying both |sender| and
63 // |receiver|.
64 //
65 // Both |sender| and |receiver| must be non-null.
66 //
sammc6194d972016-03-08 07:38:04 +090067 // Note that MessagePipeReader doesn't delete |delegate|.
rockot6e3d4922016-03-23 10:32:18 +090068 MessagePipeReader(mojo::MessagePipeHandle pipe,
69 mojom::ChannelAssociatedPtr sender,
sammc6194d972016-03-08 07:38:04 +090070 mojo::AssociatedInterfaceRequest<mojom::Channel> receiver,
71 Delegate* delegate);
72 ~MessagePipeReader() override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090073
74 // Close and destroy the MessagePipe.
75 void Close();
morrita5138bf52015-04-21 06:20:12 +090076
morrita@chromium.org15996aa2014-08-05 08:44:17 +090077 // Return true if the MessagePipe is alive.
Jeremy Roman294562e2017-06-14 09:53:59 +090078 bool IsValid() { return sender_.is_bound(); }
morrita@chromium.org15996aa2014-08-05 08:44:17 +090079
rockot6e3d4922016-03-23 10:32:18 +090080 // Sends an IPC::Message to the other end of the pipe. Safe to call from any
81 // thread.
danakjc3fb6c52016-04-23 13:21:09 +090082 bool Send(std::unique_ptr<Message> message);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090083
rockota6edf832016-07-14 09:34:11 +090084 // Requests an associated interface from the other end of the pipe.
85 void GetRemoteInterface(const std::string& name,
86 mojo::ScopedInterfaceEndpointHandle handle);
87
rockot7900d342017-02-09 17:40:15 +090088 mojom::ChannelAssociatedPtr& sender() { return sender_; }
rockot685505b2016-09-07 03:35:57 +090089
sammc6194d972016-03-08 07:38:04 +090090 protected:
morrita1f42dc62014-11-26 08:35:57 +090091 void OnPipeClosed();
92 void OnPipeError(MojoResult error);
93
sammc6194d972016-03-08 07:38:04 +090094 private:
rockot6e3d4922016-03-23 10:32:18 +090095 // mojom::Channel:
rockota01dbc72016-07-23 06:18:07 +090096 void SetPeerPid(int32_t peer_pid) override;
Ken Rockotb3a77c92017-09-14 13:23:41 +090097 void Receive(base::span<const uint8_t> data,
98 base::Optional<std::vector<mojo::native::SerializedHandlePtr>>
99 handles) override;
rockota6edf832016-07-14 09:34:11 +0900100 void GetAssociatedInterface(
yzshen8af798c2016-08-24 10:10:13 +0900101 const std::string& name,
rockota6edf832016-07-14 09:34:11 +0900102 mojom::GenericInterfaceAssociatedRequest request) override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900103
sammc6194d972016-03-08 07:38:04 +0900104 // |delegate_| is null once the message pipe is closed.
morrita1f42dc62014-11-26 08:35:57 +0900105 Delegate* delegate_;
sammc6194d972016-03-08 07:38:04 +0900106 mojom::ChannelAssociatedPtr sender_;
107 mojo::AssociatedBinding<mojom::Channel> binding_;
amistry5dfb9c22015-09-03 03:04:22 +0900108 base::ThreadChecker thread_checker_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900109
110 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader);
111};
112
113} // namespace internal
114} // namespace IPC
115
116#endif // IPC_IPC_MESSAGE_PIPE_READER_H_