blob: d35f98a8628fcd9f19d88591e670b376ee7cb706 [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
8#include "base/memory/scoped_ptr.h"
9#include "base/process.h"
[email protected]c47317e2012-06-20 22:35:3110#include "ipc/ipc_listener.h"
[email protected]e2614c62011-04-16 22:12:4511#include "ipc/ipc_platform_file.h"
[email protected]c47317e2012-06-20 22:35:3112#include "ipc/ipc_sender.h"
[email protected]e2614c62011-04-16 22:12:4513#include "ipc/ipc_sync_channel.h"
[email protected]f0a04c42011-08-26 22:43:2014#include "ppapi/proxy/ppapi_proxy_export.h"
[email protected]e2614c62011-04-16 22:12:4515
[email protected]e2614c62011-04-16 22:12:4516namespace base {
[email protected]92bf9062011-05-02 18:00:4917class MessageLoopProxy;
[email protected]e2614c62011-04-16 22:12:4518class WaitableEvent;
19}
20
21namespace IPC {
22class TestSink;
23}
24
[email protected]4d2efd22011-08-18 21:58:0225namespace ppapi {
[email protected]e2614c62011-04-16 22:12:4526namespace proxy {
27
[email protected]f0a04c42011-08-26 22:43:2028class PPAPI_PROXY_EXPORT ProxyChannel
[email protected]c47317e2012-06-20 22:35:3129 : public IPC::Listener,
30 public IPC::Sender {
[email protected]e2614c62011-04-16 22:12:4531 public:
[email protected]59bea132011-09-15 16:46:5332 class PPAPI_PROXY_EXPORT Delegate {
[email protected]e2614c62011-04-16 22:12:4533 public:
[email protected]59bea132011-09-15 16:46:5334 virtual ~Delegate() {}
35
[email protected]e2614c62011-04-16 22:12:4536 // Returns the dedicated message loop for processing IPC requests.
[email protected]92bf9062011-05-02 18:00:4937 virtual base::MessageLoopProxy* GetIPCMessageLoop() = 0;
[email protected]e2614c62011-04-16 22:12:4538
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]f0ecb552012-05-11 22:09:1142
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]108fd342013-01-04 20:46:5450 base::ProcessId remote_pid,
[email protected]f0ecb552012-05-11 22:09:1151 bool should_close_source) = 0;
[email protected]e2614c62011-04-16 22:12:4552 };
53
54 virtual ~ProxyChannel();
55
56 // Alternative to InitWithChannel() for unit tests that want to send all
[email protected]4b417e52011-04-18 22:51:0857 // messages sent via this channel to the given test sink. The test sink
[email protected]108fd342013-01-04 20:46:5458 // must outlive this class. In this case, the peer PID will be the current
59 // process ID.
[email protected]e2614c62011-04-16 22:12:4560 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]c47317e2012-06-20 22:35:3172 // IPC::Sender implementation.
73 virtual bool Send(IPC::Message* msg) OVERRIDE;
[email protected]e2614c62011-04-16 22:12:4574
[email protected]c47317e2012-06-20 22:35:3175 // IPC::Listener implementation.
76 virtual void OnChannelError() OVERRIDE;
[email protected]e2614c62011-04-16 22:12:4577
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]4e8f0a762012-05-31 19:37:5483#if defined(OS_POSIX) && !defined(OS_NACL)
[email protected]a3a459d2011-11-10 20:12:3784 int TakeRendererFD();
[email protected]e2614c62011-04-16 22:12:4585#endif
86
87 protected:
[email protected]f0ecb552012-05-11 22:09:1188 explicit ProxyChannel();
[email protected]e2614c62011-04-16 22:12:4589
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]108fd342013-01-04 20:46:5494 base::ProcessId peer_pid,
[email protected]e2614c62011-04-16 22:12:4595 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]108fd342013-01-04 20:46:54106 // 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]e2614c62011-04-16 22:12:45111 // 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]4d2efd22011-08-18 21:58:02124} // namespace ppapi
[email protected]e2614c62011-04-16 22:12:45125
126#endif // PPAPI_PROXY_PROXY_CHANNEL_H_