[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 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 PPAPI_PROXY_PROXY_CHANNEL_H_ |
| 6 | #define PPAPI_PROXY_PROXY_CHANNEL_H_ |
| 7 | |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 8 | #include "base/files/scoped_file.h" |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
[email protected] | 1575659 | 2013-07-25 14:17:53 | [diff] [blame] | 10 | #include "base/process/process.h" |
[email protected] | c47317e | 2012-06-20 22:35:31 | [diff] [blame] | 11 | #include "ipc/ipc_listener.h" |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 12 | #include "ipc/ipc_platform_file.h" |
[email protected] | c47317e | 2012-06-20 22:35:31 | [diff] [blame] | 13 | #include "ipc/ipc_sender.h" |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 14 | #include "ipc/ipc_sync_channel.h" |
[email protected] | f0a04c4 | 2011-08-26 22:43:20 | [diff] [blame] | 15 | #include "ppapi/proxy/ppapi_proxy_export.h" |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 16 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 17 | namespace base { |
skyostil | 12262cf | 2015-05-21 14:49:31 | [diff] [blame^] | 18 | class SingleThreadTaskRunner; |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 19 | class WaitableEvent; |
| 20 | } |
| 21 | |
| 22 | namespace IPC { |
| 23 | class TestSink; |
| 24 | } |
| 25 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 26 | namespace ppapi { |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 27 | namespace proxy { |
| 28 | |
[email protected] | f0a04c4 | 2011-08-26 22:43:20 | [diff] [blame] | 29 | class PPAPI_PROXY_EXPORT ProxyChannel |
[email protected] | c47317e | 2012-06-20 22:35:31 | [diff] [blame] | 30 | : public IPC::Listener, |
| 31 | public IPC::Sender { |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 32 | public: |
[email protected] | 59bea13 | 2011-09-15 16:46:53 | [diff] [blame] | 33 | class PPAPI_PROXY_EXPORT Delegate { |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 34 | public: |
[email protected] | 59bea13 | 2011-09-15 16:46:53 | [diff] [blame] | 35 | virtual ~Delegate() {} |
| 36 | |
skyostil | 12262cf | 2015-05-21 14:49:31 | [diff] [blame^] | 37 | // Returns the task runner for processing IPC requests. |
| 38 | virtual base::SingleThreadTaskRunner* GetIPCTaskRunner() = 0; |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 39 | |
| 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] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 43 | |
| 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] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 51 | base::ProcessId remote_pid, |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 52 | bool should_close_source) = 0; |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 53 | }; |
| 54 | |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 55 | ~ProxyChannel() override; |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 56 | |
[email protected] | f7b7eb7c | 2014-02-27 23:54:15 | [diff] [blame] | 57 | // Alternative to InitWithChannel() for unit tests that want to send all |
| 58 | // messages sent via this channel to the given test sink. The test sink |
| 59 | // must outlive this class. In this case, the peer PID will be the current |
| 60 | // process ID. |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 61 | void InitWithTestSink(IPC::TestSink* test_sink); |
| 62 | |
| 63 | // Shares a file handle (HANDLE / file descriptor) with the remote side. It |
| 64 | // returns a handle that should be sent in exactly one IPC message. Upon |
| 65 | // receipt, the remote side then owns that handle. Note: if sending the |
| 66 | // message fails, the returned handle is properly closed by the IPC system. If |
| 67 | // should_close_source is set to true, the original handle is closed by this |
| 68 | // operation and should not be used again. |
| 69 | IPC::PlatformFileForTransit ShareHandleWithRemote( |
| 70 | base::PlatformFile handle, |
| 71 | bool should_close_source); |
| 72 | |
[email protected] | c47317e | 2012-06-20 22:35:31 | [diff] [blame] | 73 | // IPC::Sender implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 74 | bool Send(IPC::Message* msg) override; |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 75 | |
[email protected] | c47317e | 2012-06-20 22:35:31 | [diff] [blame] | 76 | // IPC::Listener implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 77 | void OnChannelError() override; |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 78 | |
| 79 | // Will be NULL in some unit tests and if the remote side has crashed. |
| 80 | IPC::SyncChannel* channel() const { |
| 81 | return channel_.get(); |
| 82 | } |
| 83 | |
[email protected] | 4e8f0a76 | 2012-05-31 19:37:54 | [diff] [blame] | 84 | #if defined(OS_POSIX) && !defined(OS_NACL) |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 85 | base::ScopedFD TakeRendererFD(); |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 86 | #endif |
| 87 | |
| 88 | protected: |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 89 | explicit ProxyChannel(); |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 90 | |
[email protected] | f7b7eb7c | 2014-02-27 23:54:15 | [diff] [blame] | 91 | // You must call this function before anything else. Returns true on success. |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 92 | // The delegate pointer must outlive this class, ownership is not |
| 93 | // transferred. |
[email protected] | f7b7eb7c | 2014-02-27 23:54:15 | [diff] [blame] | 94 | virtual bool InitWithChannel(Delegate* delegate, |
| 95 | base::ProcessId peer_pid, |
| 96 | const IPC::ChannelHandle& channel_handle, |
| 97 | bool is_client); |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 98 | |
| 99 | ProxyChannel::Delegate* delegate() const { |
| 100 | return delegate_; |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | // Non-owning pointer. Guaranteed non-NULL after init is called. |
| 105 | ProxyChannel::Delegate* delegate_; |
| 106 | |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 107 | // PID of the remote process. Use this instead of the Channel::peer_pid since |
| 108 | // this is set synchronously on construction rather than waiting on the |
| 109 | // "hello" message from the peer (which introduces a race condition). |
| 110 | base::ProcessId peer_pid_; |
| 111 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 112 | // When we're unit testing, this will indicate the sink for the messages to |
| 113 | // be deposited so they can be inspected by the test. When non-NULL, this |
| 114 | // indicates that the channel should not be used. |
| 115 | IPC::TestSink* test_sink_; |
| 116 | |
| 117 | // Will be null for some tests when there is a test_sink_, and if the |
| 118 | // remote side has crashed. |
| 119 | scoped_ptr<IPC::SyncChannel> channel_; |
| 120 | |
| 121 | DISALLOW_COPY_AND_ASSIGN(ProxyChannel); |
| 122 | }; |
| 123 | |
| 124 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 125 | } // namespace ppapi |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 126 | |
| 127 | #endif // PPAPI_PROXY_PROXY_CHANNEL_H_ |