[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 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" |
| 10 | #include "ipc/ipc_message.h" |
| 11 | #include "ipc/ipc_platform_file.h" |
| 12 | #include "ipc/ipc_sync_channel.h" |
[email protected] | f0a04c4 | 2011-08-26 22:43:20 | [diff] [blame^] | 13 | #include "ppapi/proxy/ppapi_proxy_export.h" |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 14 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 15 | namespace base { |
[email protected] | 92bf906 | 2011-05-02 18:00:49 | [diff] [blame] | 16 | class MessageLoopProxy; |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 17 | class WaitableEvent; |
| 18 | } |
| 19 | |
| 20 | namespace IPC { |
| 21 | class TestSink; |
| 22 | } |
| 23 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 24 | namespace ppapi { |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 25 | namespace proxy { |
| 26 | |
| 27 | class VarSerializationRules; |
| 28 | |
[email protected] | f0a04c4 | 2011-08-26 22:43:20 | [diff] [blame^] | 29 | class PPAPI_PROXY_EXPORT ProxyChannel |
| 30 | : public IPC::Channel::Listener, |
| 31 | public IPC::Message::Sender { |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 32 | public: |
| 33 | typedef void (*ShutdownModuleFunc)(); |
| 34 | |
| 35 | class Delegate { |
| 36 | public: |
| 37 | // Returns the dedicated message loop for processing IPC requests. |
[email protected] | 92bf906 | 2011-05-02 18:00:49 | [diff] [blame] | 38 | virtual base::MessageLoopProxy* GetIPCMessageLoop() = 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; |
| 43 | }; |
| 44 | |
| 45 | virtual ~ProxyChannel(); |
| 46 | |
| 47 | // Alternative to InitWithChannel() for unit tests that want to send all |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 48 | // messages sent via this channel to the given test sink. The test sink |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 49 | // must outlive this class. |
| 50 | void InitWithTestSink(IPC::TestSink* test_sink); |
| 51 | |
| 52 | // Shares a file handle (HANDLE / file descriptor) with the remote side. It |
| 53 | // returns a handle that should be sent in exactly one IPC message. Upon |
| 54 | // receipt, the remote side then owns that handle. Note: if sending the |
| 55 | // message fails, the returned handle is properly closed by the IPC system. If |
| 56 | // should_close_source is set to true, the original handle is closed by this |
| 57 | // operation and should not be used again. |
| 58 | IPC::PlatformFileForTransit ShareHandleWithRemote( |
| 59 | base::PlatformFile handle, |
| 60 | bool should_close_source); |
| 61 | |
| 62 | // IPC::Message::Sender implementation. |
| 63 | virtual bool Send(IPC::Message* msg); |
| 64 | |
| 65 | // IPC::Channel::Listener implementation. |
| 66 | virtual void OnChannelError(); |
| 67 | |
| 68 | // Will be NULL in some unit tests and if the remote side has crashed. |
| 69 | IPC::SyncChannel* channel() const { |
| 70 | return channel_.get(); |
| 71 | } |
| 72 | |
| 73 | #if defined(OS_POSIX) |
| 74 | int GetRendererFD(); |
| 75 | #endif |
| 76 | |
| 77 | protected: |
| 78 | ProxyChannel(base::ProcessHandle remote_process_handle); |
| 79 | |
| 80 | // You must call this function before anything else. Returns true on success. |
| 81 | // The delegate pointer must outlive this class, ownership is not |
| 82 | // transferred. |
| 83 | virtual bool InitWithChannel(Delegate* delegate, |
| 84 | const IPC::ChannelHandle& channel_handle, |
| 85 | bool is_client); |
| 86 | |
| 87 | ProxyChannel::Delegate* delegate() const { |
| 88 | return delegate_; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | // Non-owning pointer. Guaranteed non-NULL after init is called. |
| 93 | ProxyChannel::Delegate* delegate_; |
| 94 | |
| 95 | base::ProcessHandle remote_process_handle_; // See getter above. |
| 96 | |
| 97 | // When we're unit testing, this will indicate the sink for the messages to |
| 98 | // be deposited so they can be inspected by the test. When non-NULL, this |
| 99 | // indicates that the channel should not be used. |
| 100 | IPC::TestSink* test_sink_; |
| 101 | |
| 102 | // Will be null for some tests when there is a test_sink_, and if the |
| 103 | // remote side has crashed. |
| 104 | scoped_ptr<IPC::SyncChannel> channel_; |
| 105 | |
| 106 | DISALLOW_COPY_AND_ASSIGN(ProxyChannel); |
| 107 | }; |
| 108 | |
| 109 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 110 | } // namespace ppapi |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 111 | |
| 112 | #endif // PPAPI_PROXY_PROXY_CHANNEL_H_ |