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