blob: a2a03b90722320132ac82687b07a2807e2dc05ce [file] [log] [blame]
[email protected]f0ecb552012-05-11 22:09:111// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e2614c62011-04-16 22:12:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef PPAPI_PROXY_PROXY_CHANNEL_H_
6#define PPAPI_PROXY_PROXY_CHANNEL_H_
7
dchengced92242016-04-07 00:00:128#include <memory>
9
morritaa409ccc2014-10-20 23:53:2510#include "base/files/scoped_file.h"
avie029c4132015-12-23 06:45:2211#include "base/macros.h"
Alexandr Ilinc7d975f2018-06-01 09:25:4112#include "base/memory/read_only_shared_memory_region.h"
Alexandr Ilinc7d975f2018-06-01 09:25:4113#include "base/memory/unsafe_shared_memory_region.h"
[email protected]15756592013-07-25 14:17:5314#include "base/process/process.h"
avie029c4132015-12-23 06:45:2215#include "build/build_config.h"
[email protected]c47317e2012-06-20 22:35:3116#include "ipc/ipc_listener.h"
[email protected]e2614c62011-04-16 22:12:4517#include "ipc/ipc_platform_file.h"
[email protected]c47317e2012-06-20 22:35:3118#include "ipc/ipc_sender.h"
[email protected]e2614c62011-04-16 22:12:4519#include "ipc/ipc_sync_channel.h"
[email protected]f0a04c42011-08-26 22:43:2020#include "ppapi/proxy/ppapi_proxy_export.h"
[email protected]e2614c62011-04-16 22:12:4521
[email protected]e2614c62011-04-16 22:12:4522namespace base {
skyostil12262cf2015-05-21 14:49:3123class SingleThreadTaskRunner;
[email protected]e2614c62011-04-16 22:12:4524class WaitableEvent;
25}
26
[email protected]4d2efd22011-08-18 21:58:0227namespace ppapi {
[email protected]e2614c62011-04-16 22:12:4528namespace proxy {
29
[email protected]f0a04c42011-08-26 22:43:2030class PPAPI_PROXY_EXPORT ProxyChannel
[email protected]c47317e2012-06-20 22:35:3131 : public IPC::Listener,
32 public IPC::Sender {
[email protected]e2614c62011-04-16 22:12:4533 public:
[email protected]59bea132011-09-15 16:46:5334 class PPAPI_PROXY_EXPORT Delegate {
[email protected]e2614c62011-04-16 22:12:4535 public:
[email protected]59bea132011-09-15 16:46:5336 virtual ~Delegate() {}
37
skyostil12262cf2015-05-21 14:49:3138 // Returns the task runner for processing IPC requests.
39 virtual base::SingleThreadTaskRunner* GetIPCTaskRunner() = 0;
[email protected]e2614c62011-04-16 22:12:4540
41 // Returns the event object that becomes signalled when the main thread's
42 // message loop exits.
43 virtual base::WaitableEvent* GetShutdownEvent() = 0;
[email protected]f0ecb552012-05-11 22:09:1144
45 // Duplicates a handle to the provided object, returning one that is valid
46 // on the other side of the channel. This is part of the delegate interface
47 // because both sides of the channel may not have sufficient permission to
48 // duplicate handles directly. The implementation must provide the same
49 // guarantees as ProxyChannel::ShareHandleWithRemote below.
50 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
51 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:5452 base::ProcessId remote_pid,
[email protected]f0ecb552012-05-11 22:09:1153 bool should_close_source) = 0;
erikchen4fc32d52015-06-02 02:16:3854
55 // Duplicates a shared memory handle, returning one that is valid on the
56 // other side of the channel. This is part of the delegate interface
57 // because both sides of the channel may not have sufficient permission to
58 // duplicate handles directly. The implementation must provide the same
59 // guarantees as ProxyChannel::ShareSharedMemoryHandleWithRemote below.
Alexandr Ilinc7d975f2018-06-01 09:25:4160 virtual base::UnsafeSharedMemoryRegion
61 ShareUnsafeSharedMemoryRegionWithRemote(
62 const base::UnsafeSharedMemoryRegion& region,
63 base::ProcessId remote_pid) = 0;
64 virtual base::ReadOnlySharedMemoryRegion
65 ShareReadOnlySharedMemoryRegionWithRemote(
66 const base::ReadOnlySharedMemoryRegion& region,
67 base::ProcessId remote_pid) = 0;
[email protected]e2614c62011-04-16 22:12:4568 };
69
Peter Boström3d5b3cb2021-09-23 21:35:4570 ProxyChannel(const ProxyChannel&) = delete;
71 ProxyChannel& operator=(const ProxyChannel&) = delete;
72
nicke4784432015-04-23 14:01:4873 ~ProxyChannel() override;
[email protected]e2614c62011-04-16 22:12:4574
[email protected]f7b7eb7c2014-02-27 23:54:1575 // Alternative to InitWithChannel() for unit tests that want to send all
76 // messages sent via this channel to the given test sink. The test sink
77 // must outlive this class. In this case, the peer PID will be the current
78 // process ID.
cfredric455bf842021-07-09 21:02:1879 void InitWithTestSink(IPC::Sender* sender);
[email protected]e2614c62011-04-16 22:12:4580
81 // Shares a file handle (HANDLE / file descriptor) with the remote side. It
82 // returns a handle that should be sent in exactly one IPC message. Upon
83 // receipt, the remote side then owns that handle. Note: if sending the
84 // message fails, the returned handle is properly closed by the IPC system. If
85 // should_close_source is set to true, the original handle is closed by this
86 // operation and should not be used again.
87 IPC::PlatformFileForTransit ShareHandleWithRemote(
88 base::PlatformFile handle,
89 bool should_close_source);
90
erikchen4fc32d52015-06-02 02:16:3891 // Shares a shared memory handle with the remote side. It returns a handle
92 // that should be sent in exactly one IPC message. Upon receipt, the remote
93 // side then owns that handle. Note: if sending the message fails, the
94 // returned handle is properly closed by the IPC system. The original handle
95 // is not closed by this operation.
Alexandr Ilinc7d975f2018-06-01 09:25:4196 base::UnsafeSharedMemoryRegion ShareUnsafeSharedMemoryRegionWithRemote(
97 const base::UnsafeSharedMemoryRegion& region);
98 base::ReadOnlySharedMemoryRegion ShareReadOnlySharedMemoryRegionWithRemote(
99 const base::ReadOnlySharedMemoryRegion& region);
erikchen4fc32d52015-06-02 02:16:38100
[email protected]c47317e2012-06-20 22:35:31101 // IPC::Sender implementation.
nicke4784432015-04-23 14:01:48102 bool Send(IPC::Message* msg) override;
[email protected]e2614c62011-04-16 22:12:45103
[email protected]c47317e2012-06-20 22:35:31104 // IPC::Listener implementation.
nicke4784432015-04-23 14:01:48105 void OnChannelError() override;
[email protected]e2614c62011-04-16 22:12:45106
107 // Will be NULL in some unit tests and if the remote side has crashed.
108 IPC::SyncChannel* channel() const {
109 return channel_.get();
110 }
111
sammcf810f07f2016-11-10 22:34:07112 base::ProcessId peer_pid() { return peer_pid_; }
113
[email protected]e2614c62011-04-16 22:12:45114 protected:
[email protected]f0ecb552012-05-11 22:09:11115 explicit ProxyChannel();
[email protected]e2614c62011-04-16 22:12:45116
[email protected]f7b7eb7c2014-02-27 23:54:15117 // You must call this function before anything else. Returns true on success.
[email protected]e2614c62011-04-16 22:12:45118 // The delegate pointer must outlive this class, ownership is not
119 // transferred.
Hajime Hoshi5959c54f2019-01-09 01:42:12120 virtual bool InitWithChannel(
121 Delegate* delegate,
122 base::ProcessId peer_pid,
123 const IPC::ChannelHandle& channel_handle,
124 bool is_client,
125 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
[email protected]e2614c62011-04-16 22:12:45126
127 ProxyChannel::Delegate* delegate() const {
128 return delegate_;
129 }
130
131 private:
132 // Non-owning pointer. Guaranteed non-NULL after init is called.
133 ProxyChannel::Delegate* delegate_;
134
[email protected]108fd342013-01-04 20:46:54135 // PID of the remote process. Use this instead of the Channel::peer_pid since
136 // this is set synchronously on construction rather than waiting on the
137 // "hello" message from the peer (which introduces a race condition).
138 base::ProcessId peer_pid_;
139
[email protected]e2614c62011-04-16 22:12:45140 // When we're unit testing, this will indicate the sink for the messages to
141 // be deposited so they can be inspected by the test. When non-NULL, this
142 // indicates that the channel should not be used.
cfredric455bf842021-07-09 21:02:18143 IPC::Sender* test_sink_;
[email protected]e2614c62011-04-16 22:12:45144
145 // Will be null for some tests when there is a test_sink_, and if the
146 // remote side has crashed.
dchengced92242016-04-07 00:00:12147 std::unique_ptr<IPC::SyncChannel> channel_;
[email protected]e2614c62011-04-16 22:12:45148};
149
150} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02151} // namespace ppapi
[email protected]e2614c62011-04-16 22:12:45152
153#endif // PPAPI_PROXY_PROXY_CHANNEL_H_