blob: ffd3f2f381ca369b1abdfb42c2fc9616645c45ff [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"
10#include "ipc/ipc_message.h"
11#include "ipc/ipc_platform_file.h"
12#include "ipc/ipc_sync_channel.h"
[email protected]f0a04c42011-08-26 22:43:2013#include "ppapi/proxy/ppapi_proxy_export.h"
[email protected]e2614c62011-04-16 22:12:4514
[email protected]e2614c62011-04-16 22:12:4515namespace base {
[email protected]92bf9062011-05-02 18:00:4916class MessageLoopProxy;
[email protected]e2614c62011-04-16 22:12:4517class WaitableEvent;
18}
19
20namespace IPC {
21class TestSink;
22}
23
[email protected]4d2efd22011-08-18 21:58:0224namespace ppapi {
[email protected]e2614c62011-04-16 22:12:4525namespace proxy {
26
[email protected]f0a04c42011-08-26 22:43:2027class PPAPI_PROXY_EXPORT ProxyChannel
28 : public IPC::Channel::Listener,
29 public IPC::Message::Sender {
[email protected]e2614c62011-04-16 22:12:4530 public:
[email protected]59bea132011-09-15 16:46:5331 class PPAPI_PROXY_EXPORT Delegate {
[email protected]e2614c62011-04-16 22:12:4532 public:
[email protected]59bea132011-09-15 16:46:5333 virtual ~Delegate() {}
34
[email protected]e2614c62011-04-16 22:12:4535 // Returns the dedicated message loop for processing IPC requests.
[email protected]92bf9062011-05-02 18:00:4936 virtual base::MessageLoopProxy* GetIPCMessageLoop() = 0;
[email protected]e2614c62011-04-16 22:12:4537
38 // Returns the event object that becomes signalled when the main thread's
39 // message loop exits.
40 virtual base::WaitableEvent* GetShutdownEvent() = 0;
[email protected]f0ecb552012-05-11 22:09:1141
42 // Duplicates a handle to the provided object, returning one that is valid
43 // on the other side of the channel. This is part of the delegate interface
44 // because both sides of the channel may not have sufficient permission to
45 // duplicate handles directly. The implementation must provide the same
46 // guarantees as ProxyChannel::ShareHandleWithRemote below.
47 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
48 base::PlatformFile handle,
49 const IPC::SyncChannel& channel,
50 bool should_close_source) = 0;
[email protected]e2614c62011-04-16 22:12:4551 };
52
53 virtual ~ProxyChannel();
54
55 // Alternative to InitWithChannel() for unit tests that want to send all
[email protected]4b417e52011-04-18 22:51:0856 // messages sent via this channel to the given test sink. The test sink
[email protected]e2614c62011-04-16 22:12:4557 // must outlive this class.
58 void InitWithTestSink(IPC::TestSink* test_sink);
59
60 // Shares a file handle (HANDLE / file descriptor) with the remote side. It
61 // returns a handle that should be sent in exactly one IPC message. Upon
62 // receipt, the remote side then owns that handle. Note: if sending the
63 // message fails, the returned handle is properly closed by the IPC system. If
64 // should_close_source is set to true, the original handle is closed by this
65 // operation and should not be used again.
66 IPC::PlatformFileForTransit ShareHandleWithRemote(
67 base::PlatformFile handle,
68 bool should_close_source);
69
70 // IPC::Message::Sender implementation.
71 virtual bool Send(IPC::Message* msg);
72
73 // IPC::Channel::Listener implementation.
74 virtual void OnChannelError();
75
76 // Will be NULL in some unit tests and if the remote side has crashed.
77 IPC::SyncChannel* channel() const {
78 return channel_.get();
79 }
80
[email protected]4e8f0a762012-05-31 19:37:5481#if defined(OS_POSIX) && !defined(OS_NACL)
[email protected]a3a459d2011-11-10 20:12:3782 int TakeRendererFD();
[email protected]e2614c62011-04-16 22:12:4583#endif
84
85 protected:
[email protected]f0ecb552012-05-11 22:09:1186 explicit ProxyChannel();
[email protected]e2614c62011-04-16 22:12:4587
88 // You must call this function before anything else. Returns true on success.
89 // The delegate pointer must outlive this class, ownership is not
90 // transferred.
91 virtual bool InitWithChannel(Delegate* delegate,
92 const IPC::ChannelHandle& channel_handle,
93 bool is_client);
94
95 ProxyChannel::Delegate* delegate() const {
96 return delegate_;
97 }
98
99 private:
100 // Non-owning pointer. Guaranteed non-NULL after init is called.
101 ProxyChannel::Delegate* delegate_;
102
[email protected]e2614c62011-04-16 22:12:45103 // When we're unit testing, this will indicate the sink for the messages to
104 // be deposited so they can be inspected by the test. When non-NULL, this
105 // indicates that the channel should not be used.
106 IPC::TestSink* test_sink_;
107
108 // Will be null for some tests when there is a test_sink_, and if the
109 // remote side has crashed.
110 scoped_ptr<IPC::SyncChannel> channel_;
111
112 DISALLOW_COPY_AND_ASSIGN(ProxyChannel);
113};
114
115} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02116} // namespace ppapi
[email protected]e2614c62011-04-16 22:12:45117
118#endif // PPAPI_PROXY_PROXY_CHANNEL_H_