blob: 9d981da9e0e93d15a7a07bbc0d197cb78c86b1b4 [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"
erikchen4fc32d52015-06-02 02:16:3812#include "base/memory/shared_memory.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
26namespace IPC {
27class TestSink;
28}
29
[email protected]4d2efd22011-08-18 21:58:0230namespace ppapi {
[email protected]e2614c62011-04-16 22:12:4531namespace proxy {
32
[email protected]f0a04c42011-08-26 22:43:2033class PPAPI_PROXY_EXPORT ProxyChannel
[email protected]c47317e2012-06-20 22:35:3134 : public IPC::Listener,
35 public IPC::Sender {
[email protected]e2614c62011-04-16 22:12:4536 public:
[email protected]59bea132011-09-15 16:46:5337 class PPAPI_PROXY_EXPORT Delegate {
[email protected]e2614c62011-04-16 22:12:4538 public:
[email protected]59bea132011-09-15 16:46:5339 virtual ~Delegate() {}
40
skyostil12262cf2015-05-21 14:49:3141 // Returns the task runner for processing IPC requests.
42 virtual base::SingleThreadTaskRunner* GetIPCTaskRunner() = 0;
[email protected]e2614c62011-04-16 22:12:4543
44 // Returns the event object that becomes signalled when the main thread's
45 // message loop exits.
46 virtual base::WaitableEvent* GetShutdownEvent() = 0;
[email protected]f0ecb552012-05-11 22:09:1147
48 // Duplicates a handle to the provided object, returning one that is valid
49 // on the other side of the channel. This is part of the delegate interface
50 // because both sides of the channel may not have sufficient permission to
51 // duplicate handles directly. The implementation must provide the same
52 // guarantees as ProxyChannel::ShareHandleWithRemote below.
53 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
54 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:5455 base::ProcessId remote_pid,
[email protected]f0ecb552012-05-11 22:09:1156 bool should_close_source) = 0;
erikchen4fc32d52015-06-02 02:16:3857
58 // Duplicates a shared memory handle, returning one that is valid on the
59 // other side of the channel. This is part of the delegate interface
60 // because both sides of the channel may not have sufficient permission to
61 // duplicate handles directly. The implementation must provide the same
62 // guarantees as ProxyChannel::ShareSharedMemoryHandleWithRemote below.
63 virtual base::SharedMemoryHandle ShareSharedMemoryHandleWithRemote(
64 const base::SharedMemoryHandle& handle,
65 base::ProcessId remote_pid) = 0;
[email protected]e2614c62011-04-16 22:12:4566 };
67
nicke4784432015-04-23 14:01:4868 ~ProxyChannel() override;
[email protected]e2614c62011-04-16 22:12:4569
[email protected]f7b7eb7c2014-02-27 23:54:1570 // Alternative to InitWithChannel() for unit tests that want to send all
71 // messages sent via this channel to the given test sink. The test sink
72 // must outlive this class. In this case, the peer PID will be the current
73 // process ID.
[email protected]e2614c62011-04-16 22:12:4574 void InitWithTestSink(IPC::TestSink* test_sink);
75
76 // Shares a file handle (HANDLE / file descriptor) with the remote side. It
77 // returns a handle that should be sent in exactly one IPC message. Upon
78 // receipt, the remote side then owns that handle. Note: if sending the
79 // message fails, the returned handle is properly closed by the IPC system. If
80 // should_close_source is set to true, the original handle is closed by this
81 // operation and should not be used again.
82 IPC::PlatformFileForTransit ShareHandleWithRemote(
83 base::PlatformFile handle,
84 bool should_close_source);
85
erikchen4fc32d52015-06-02 02:16:3886 // Shares a shared memory handle with the remote side. It returns a handle
87 // that should be sent in exactly one IPC message. Upon receipt, the remote
88 // side then owns that handle. Note: if sending the message fails, the
89 // returned handle is properly closed by the IPC system. The original handle
90 // is not closed by this operation.
91 base::SharedMemoryHandle ShareSharedMemoryHandleWithRemote(
92 const base::SharedMemoryHandle& handle);
93
[email protected]c47317e2012-06-20 22:35:3194 // IPC::Sender implementation.
nicke4784432015-04-23 14:01:4895 bool Send(IPC::Message* msg) override;
[email protected]e2614c62011-04-16 22:12:4596
[email protected]c47317e2012-06-20 22:35:3197 // IPC::Listener implementation.
nicke4784432015-04-23 14:01:4898 void OnChannelError() override;
[email protected]e2614c62011-04-16 22:12:4599
100 // Will be NULL in some unit tests and if the remote side has crashed.
101 IPC::SyncChannel* channel() const {
102 return channel_.get();
103 }
104
[email protected]4e8f0a762012-05-31 19:37:54105#if defined(OS_POSIX) && !defined(OS_NACL)
morritaa409ccc2014-10-20 23:53:25106 base::ScopedFD TakeRendererFD();
[email protected]e2614c62011-04-16 22:12:45107#endif
108
109 protected:
[email protected]f0ecb552012-05-11 22:09:11110 explicit ProxyChannel();
[email protected]e2614c62011-04-16 22:12:45111
[email protected]f7b7eb7c2014-02-27 23:54:15112 // You must call this function before anything else. Returns true on success.
[email protected]e2614c62011-04-16 22:12:45113 // The delegate pointer must outlive this class, ownership is not
114 // transferred.
[email protected]f7b7eb7c2014-02-27 23:54:15115 virtual bool InitWithChannel(Delegate* delegate,
116 base::ProcessId peer_pid,
117 const IPC::ChannelHandle& channel_handle,
118 bool is_client);
[email protected]e2614c62011-04-16 22:12:45119
120 ProxyChannel::Delegate* delegate() const {
121 return delegate_;
122 }
123
124 private:
125 // Non-owning pointer. Guaranteed non-NULL after init is called.
126 ProxyChannel::Delegate* delegate_;
127
[email protected]108fd342013-01-04 20:46:54128 // PID of the remote process. Use this instead of the Channel::peer_pid since
129 // this is set synchronously on construction rather than waiting on the
130 // "hello" message from the peer (which introduces a race condition).
131 base::ProcessId peer_pid_;
132
[email protected]e2614c62011-04-16 22:12:45133 // When we're unit testing, this will indicate the sink for the messages to
134 // be deposited so they can be inspected by the test. When non-NULL, this
135 // indicates that the channel should not be used.
136 IPC::TestSink* test_sink_;
137
138 // Will be null for some tests when there is a test_sink_, and if the
139 // remote side has crashed.
dchengced92242016-04-07 00:00:12140 std::unique_ptr<IPC::SyncChannel> channel_;
[email protected]e2614c62011-04-16 22:12:45141
142 DISALLOW_COPY_AND_ASSIGN(ProxyChannel);
143};
144
145} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02146} // namespace ppapi
[email protected]e2614c62011-04-16 22:12:45147
148#endif // PPAPI_PROXY_PROXY_CHANNEL_H_