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