blob: ca41f58734fab801b4f2e1bd22d810e52711e473 [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"
erikchen4fc32d52015-06-02 02:16:3813#include "base/memory/shared_memory.h"
Alexandr Ilinc7d975f2018-06-01 09:25:4114#include "base/memory/unsafe_shared_memory_region.h"
[email protected]15756592013-07-25 14:17:5315#include "base/process/process.h"
avie029c4132015-12-23 06:45:2216#include "build/build_config.h"
[email protected]c47317e2012-06-20 22:35:3117#include "ipc/ipc_listener.h"
[email protected]e2614c62011-04-16 22:12:4518#include "ipc/ipc_platform_file.h"
[email protected]c47317e2012-06-20 22:35:3119#include "ipc/ipc_sender.h"
[email protected]e2614c62011-04-16 22:12:4520#include "ipc/ipc_sync_channel.h"
[email protected]f0a04c42011-08-26 22:43:2021#include "ppapi/proxy/ppapi_proxy_export.h"
[email protected]e2614c62011-04-16 22:12:4522
[email protected]e2614c62011-04-16 22:12:4523namespace base {
skyostil12262cf2015-05-21 14:49:3124class SingleThreadTaskRunner;
[email protected]e2614c62011-04-16 22:12:4525class WaitableEvent;
26}
27
28namespace IPC {
29class TestSink;
30}
31
[email protected]4d2efd22011-08-18 21:58:0232namespace ppapi {
[email protected]e2614c62011-04-16 22:12:4533namespace proxy {
34
[email protected]f0a04c42011-08-26 22:43:2035class PPAPI_PROXY_EXPORT ProxyChannel
[email protected]c47317e2012-06-20 22:35:3136 : public IPC::Listener,
37 public IPC::Sender {
[email protected]e2614c62011-04-16 22:12:4538 public:
[email protected]59bea132011-09-15 16:46:5339 class PPAPI_PROXY_EXPORT Delegate {
[email protected]e2614c62011-04-16 22:12:4540 public:
[email protected]59bea132011-09-15 16:46:5341 virtual ~Delegate() {}
42
skyostil12262cf2015-05-21 14:49:3143 // Returns the task runner for processing IPC requests.
44 virtual base::SingleThreadTaskRunner* GetIPCTaskRunner() = 0;
[email protected]e2614c62011-04-16 22:12:4545
46 // Returns the event object that becomes signalled when the main thread's
47 // message loop exits.
48 virtual base::WaitableEvent* GetShutdownEvent() = 0;
[email protected]f0ecb552012-05-11 22:09:1149
50 // Duplicates a handle to the provided object, returning one that is valid
51 // on the other side of the channel. This is part of the delegate interface
52 // because both sides of the channel may not have sufficient permission to
53 // duplicate handles directly. The implementation must provide the same
54 // guarantees as ProxyChannel::ShareHandleWithRemote below.
55 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
56 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:5457 base::ProcessId remote_pid,
[email protected]f0ecb552012-05-11 22:09:1158 bool should_close_source) = 0;
erikchen4fc32d52015-06-02 02:16:3859
60 // Duplicates a shared memory handle, returning one that is valid on the
61 // other side of the channel. This is part of the delegate interface
62 // because both sides of the channel may not have sufficient permission to
63 // duplicate handles directly. The implementation must provide the same
64 // guarantees as ProxyChannel::ShareSharedMemoryHandleWithRemote below.
65 virtual base::SharedMemoryHandle ShareSharedMemoryHandleWithRemote(
66 const base::SharedMemoryHandle& handle,
67 base::ProcessId remote_pid) = 0;
Alexandr Ilinc7d975f2018-06-01 09:25:4168 virtual base::UnsafeSharedMemoryRegion
69 ShareUnsafeSharedMemoryRegionWithRemote(
70 const base::UnsafeSharedMemoryRegion& region,
71 base::ProcessId remote_pid) = 0;
72 virtual base::ReadOnlySharedMemoryRegion
73 ShareReadOnlySharedMemoryRegionWithRemote(
74 const base::ReadOnlySharedMemoryRegion& region,
75 base::ProcessId remote_pid) = 0;
[email protected]e2614c62011-04-16 22:12:4576 };
77
nicke4784432015-04-23 14:01:4878 ~ProxyChannel() override;
[email protected]e2614c62011-04-16 22:12:4579
[email protected]f7b7eb7c2014-02-27 23:54:1580 // Alternative to InitWithChannel() for unit tests that want to send all
81 // messages sent via this channel to the given test sink. The test sink
82 // must outlive this class. In this case, the peer PID will be the current
83 // process ID.
[email protected]e2614c62011-04-16 22:12:4584 void InitWithTestSink(IPC::TestSink* test_sink);
85
86 // Shares a file handle (HANDLE / file descriptor) with the remote side. It
87 // returns a handle that should be sent in exactly one IPC message. Upon
88 // receipt, the remote side then owns that handle. Note: if sending the
89 // message fails, the returned handle is properly closed by the IPC system. If
90 // should_close_source is set to true, the original handle is closed by this
91 // operation and should not be used again.
92 IPC::PlatformFileForTransit ShareHandleWithRemote(
93 base::PlatformFile handle,
94 bool should_close_source);
95
erikchen4fc32d52015-06-02 02:16:3896 // Shares a shared memory handle with the remote side. It returns a handle
97 // that should be sent in exactly one IPC message. Upon receipt, the remote
98 // side then owns that handle. Note: if sending the message fails, the
99 // returned handle is properly closed by the IPC system. The original handle
100 // is not closed by this operation.
101 base::SharedMemoryHandle ShareSharedMemoryHandleWithRemote(
102 const base::SharedMemoryHandle& handle);
Alexandr Ilinc7d975f2018-06-01 09:25:41103 base::UnsafeSharedMemoryRegion ShareUnsafeSharedMemoryRegionWithRemote(
104 const base::UnsafeSharedMemoryRegion& region);
105 base::ReadOnlySharedMemoryRegion ShareReadOnlySharedMemoryRegionWithRemote(
106 const base::ReadOnlySharedMemoryRegion& region);
erikchen4fc32d52015-06-02 02:16:38107
[email protected]c47317e2012-06-20 22:35:31108 // IPC::Sender implementation.
nicke4784432015-04-23 14:01:48109 bool Send(IPC::Message* msg) override;
[email protected]e2614c62011-04-16 22:12:45110
[email protected]c47317e2012-06-20 22:35:31111 // IPC::Listener implementation.
nicke4784432015-04-23 14:01:48112 void OnChannelError() override;
[email protected]e2614c62011-04-16 22:12:45113
114 // Will be NULL in some unit tests and if the remote side has crashed.
115 IPC::SyncChannel* channel() const {
116 return channel_.get();
117 }
118
sammcf810f07f2016-11-10 22:34:07119 base::ProcessId peer_pid() { return peer_pid_; }
120
[email protected]e2614c62011-04-16 22:12:45121 protected:
[email protected]f0ecb552012-05-11 22:09:11122 explicit ProxyChannel();
[email protected]e2614c62011-04-16 22:12:45123
[email protected]f7b7eb7c2014-02-27 23:54:15124 // You must call this function before anything else. Returns true on success.
[email protected]e2614c62011-04-16 22:12:45125 // The delegate pointer must outlive this class, ownership is not
126 // transferred.
[email protected]f7b7eb7c2014-02-27 23:54:15127 virtual bool InitWithChannel(Delegate* delegate,
128 base::ProcessId peer_pid,
129 const IPC::ChannelHandle& channel_handle,
130 bool is_client);
[email protected]e2614c62011-04-16 22:12:45131
132 ProxyChannel::Delegate* delegate() const {
133 return delegate_;
134 }
135
136 private:
137 // Non-owning pointer. Guaranteed non-NULL after init is called.
138 ProxyChannel::Delegate* delegate_;
139
[email protected]108fd342013-01-04 20:46:54140 // PID of the remote process. Use this instead of the Channel::peer_pid since
141 // this is set synchronously on construction rather than waiting on the
142 // "hello" message from the peer (which introduces a race condition).
143 base::ProcessId peer_pid_;
144
[email protected]e2614c62011-04-16 22:12:45145 // When we're unit testing, this will indicate the sink for the messages to
146 // be deposited so they can be inspected by the test. When non-NULL, this
147 // indicates that the channel should not be used.
148 IPC::TestSink* test_sink_;
149
150 // Will be null for some tests when there is a test_sink_, and if the
151 // remote side has crashed.
dchengced92242016-04-07 00:00:12152 std::unique_ptr<IPC::SyncChannel> channel_;
[email protected]e2614c62011-04-16 22:12:45153
154 DISALLOW_COPY_AND_ASSIGN(ProxyChannel);
155};
156
157} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02158} // namespace ppapi
[email protected]e2614c62011-04-16 22:12:45159
160#endif // PPAPI_PROXY_PROXY_CHANNEL_H_