blob: 011d4a01ff33c7595961578a9999253edab05899 [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"
Alexandr Ilinc7d975f2018-06-01 09:25:4111#include "base/memory/read_only_shared_memory_region.h"
Alexandr Ilinc7d975f2018-06-01 09:25:4112#include "base/memory/unsafe_shared_memory_region.h"
[email protected]15756592013-07-25 14:17:5313#include "base/process/process.h"
avie029c4132015-12-23 06:45:2214#include "build/build_config.h"
[email protected]c47317e2012-06-20 22:35:3115#include "ipc/ipc_listener.h"
[email protected]e2614c62011-04-16 22:12:4516#include "ipc/ipc_platform_file.h"
[email protected]c47317e2012-06-20 22:35:3117#include "ipc/ipc_sender.h"
[email protected]e2614c62011-04-16 22:12:4518#include "ipc/ipc_sync_channel.h"
[email protected]f0a04c42011-08-26 22:43:2019#include "ppapi/proxy/ppapi_proxy_export.h"
[email protected]e2614c62011-04-16 22:12:4520
[email protected]e2614c62011-04-16 22:12:4521namespace base {
skyostil12262cf2015-05-21 14:49:3122class SingleThreadTaskRunner;
[email protected]e2614c62011-04-16 22:12:4523class WaitableEvent;
24}
25
[email protected]4d2efd22011-08-18 21:58:0226namespace ppapi {
[email protected]e2614c62011-04-16 22:12:4527namespace proxy {
28
[email protected]f0a04c42011-08-26 22:43:2029class PPAPI_PROXY_EXPORT ProxyChannel
[email protected]c47317e2012-06-20 22:35:3130 : public IPC::Listener,
31 public IPC::Sender {
[email protected]e2614c62011-04-16 22:12:4532 public:
[email protected]59bea132011-09-15 16:46:5333 class PPAPI_PROXY_EXPORT Delegate {
[email protected]e2614c62011-04-16 22:12:4534 public:
[email protected]59bea132011-09-15 16:46:5335 virtual ~Delegate() {}
36
skyostil12262cf2015-05-21 14:49:3137 // Returns the task runner for processing IPC requests.
38 virtual base::SingleThreadTaskRunner* GetIPCTaskRunner() = 0;
[email protected]e2614c62011-04-16 22:12:4539
40 // Returns the event object that becomes signalled when the main thread's
41 // message loop exits.
42 virtual base::WaitableEvent* GetShutdownEvent() = 0;
[email protected]f0ecb552012-05-11 22:09:1143
44 // Duplicates a handle to the provided object, returning one that is valid
45 // on the other side of the channel. This is part of the delegate interface
46 // because both sides of the channel may not have sufficient permission to
47 // duplicate handles directly. The implementation must provide the same
48 // guarantees as ProxyChannel::ShareHandleWithRemote below.
49 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
50 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:5451 base::ProcessId remote_pid,
[email protected]f0ecb552012-05-11 22:09:1152 bool should_close_source) = 0;
erikchen4fc32d52015-06-02 02:16:3853
54 // Duplicates a shared memory handle, returning one that is valid on the
55 // other side of the channel. This is part of the delegate interface
56 // because both sides of the channel may not have sufficient permission to
57 // duplicate handles directly. The implementation must provide the same
58 // guarantees as ProxyChannel::ShareSharedMemoryHandleWithRemote below.
Alexandr Ilinc7d975f2018-06-01 09:25:4159 virtual base::UnsafeSharedMemoryRegion
60 ShareUnsafeSharedMemoryRegionWithRemote(
61 const base::UnsafeSharedMemoryRegion& region,
62 base::ProcessId remote_pid) = 0;
63 virtual base::ReadOnlySharedMemoryRegion
64 ShareReadOnlySharedMemoryRegionWithRemote(
65 const base::ReadOnlySharedMemoryRegion& region,
66 base::ProcessId remote_pid) = 0;
[email protected]e2614c62011-04-16 22:12:4567 };
68
Peter Boström3d5b3cb2021-09-23 21:35:4569 ProxyChannel(const ProxyChannel&) = delete;
70 ProxyChannel& operator=(const ProxyChannel&) = delete;
71
nicke4784432015-04-23 14:01:4872 ~ProxyChannel() override;
[email protected]e2614c62011-04-16 22:12:4573
[email protected]f7b7eb7c2014-02-27 23:54:1574 // Alternative to InitWithChannel() for unit tests that want to send all
75 // messages sent via this channel to the given test sink. The test sink
76 // must outlive this class. In this case, the peer PID will be the current
77 // process ID.
cfredric455bf842021-07-09 21:02:1878 void InitWithTestSink(IPC::Sender* sender);
[email protected]e2614c62011-04-16 22:12:4579
80 // Shares a file handle (HANDLE / file descriptor) with the remote side. It
81 // returns a handle that should be sent in exactly one IPC message. Upon
82 // receipt, the remote side then owns that handle. Note: if sending the
83 // message fails, the returned handle is properly closed by the IPC system. If
84 // should_close_source is set to true, the original handle is closed by this
85 // operation and should not be used again.
86 IPC::PlatformFileForTransit ShareHandleWithRemote(
87 base::PlatformFile handle,
88 bool should_close_source);
89
erikchen4fc32d52015-06-02 02:16:3890 // Shares a shared memory handle with the remote side. It returns a handle
91 // that should be sent in exactly one IPC message. Upon receipt, the remote
92 // side then owns that handle. Note: if sending the message fails, the
93 // returned handle is properly closed by the IPC system. The original handle
94 // is not closed by this operation.
Alexandr Ilinc7d975f2018-06-01 09:25:4195 base::UnsafeSharedMemoryRegion ShareUnsafeSharedMemoryRegionWithRemote(
96 const base::UnsafeSharedMemoryRegion& region);
97 base::ReadOnlySharedMemoryRegion ShareReadOnlySharedMemoryRegionWithRemote(
98 const base::ReadOnlySharedMemoryRegion& region);
erikchen4fc32d52015-06-02 02:16:3899
[email protected]c47317e2012-06-20 22:35:31100 // IPC::Sender implementation.
nicke4784432015-04-23 14:01:48101 bool Send(IPC::Message* msg) override;
[email protected]e2614c62011-04-16 22:12:45102
[email protected]c47317e2012-06-20 22:35:31103 // IPC::Listener implementation.
nicke4784432015-04-23 14:01:48104 void OnChannelError() override;
[email protected]e2614c62011-04-16 22:12:45105
106 // Will be NULL in some unit tests and if the remote side has crashed.
107 IPC::SyncChannel* channel() const {
108 return channel_.get();
109 }
110
sammcf810f07f2016-11-10 22:34:07111 base::ProcessId peer_pid() { return peer_pid_; }
112
[email protected]e2614c62011-04-16 22:12:45113 protected:
[email protected]f0ecb552012-05-11 22:09:11114 explicit ProxyChannel();
[email protected]e2614c62011-04-16 22:12:45115
[email protected]f7b7eb7c2014-02-27 23:54:15116 // You must call this function before anything else. Returns true on success.
[email protected]e2614c62011-04-16 22:12:45117 // The delegate pointer must outlive this class, ownership is not
118 // transferred.
Hajime Hoshi5959c54f2019-01-09 01:42:12119 virtual bool InitWithChannel(
120 Delegate* delegate,
121 base::ProcessId peer_pid,
122 const IPC::ChannelHandle& channel_handle,
123 bool is_client,
124 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
[email protected]e2614c62011-04-16 22:12:45125
126 ProxyChannel::Delegate* delegate() const {
127 return delegate_;
128 }
129
130 private:
131 // Non-owning pointer. Guaranteed non-NULL after init is called.
132 ProxyChannel::Delegate* delegate_;
133
[email protected]108fd342013-01-04 20:46:54134 // PID of the remote process. Use this instead of the Channel::peer_pid since
135 // this is set synchronously on construction rather than waiting on the
136 // "hello" message from the peer (which introduces a race condition).
137 base::ProcessId peer_pid_;
138
[email protected]e2614c62011-04-16 22:12:45139 // When we're unit testing, this will indicate the sink for the messages to
140 // be deposited so they can be inspected by the test. When non-NULL, this
141 // indicates that the channel should not be used.
cfredric455bf842021-07-09 21:02:18142 IPC::Sender* test_sink_;
[email protected]e2614c62011-04-16 22:12:45143
144 // Will be null for some tests when there is a test_sink_, and if the
145 // remote side has crashed.
dchengced92242016-04-07 00:00:12146 std::unique_ptr<IPC::SyncChannel> channel_;
[email protected]e2614c62011-04-16 22:12:45147};
148
149} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02150} // namespace ppapi
[email protected]e2614c62011-04-16 22:12:45151
152#endif // PPAPI_PROXY_PROXY_CHANNEL_H_