blob: 744d087cb53e080d94e87069453775e1e8662fb3 [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"
[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
27class VarSerializationRules;
28
[email protected]f0a04c42011-08-26 22:43:2029class PPAPI_PROXY_EXPORT ProxyChannel
30 : public IPC::Channel::Listener,
31 public IPC::Message::Sender {
[email protected]e2614c62011-04-16 22:12:4532 public:
33 typedef void (*ShutdownModuleFunc)();
34
35 class Delegate {
36 public:
37 // Returns the dedicated message loop for processing IPC requests.
[email protected]92bf9062011-05-02 18:00:4938 virtual base::MessageLoopProxy* GetIPCMessageLoop() = 0;
[email protected]e2614c62011-04-16 22:12:4539
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]4b417e52011-04-18 22:51:0848 // messages sent via this channel to the given test sink. The test sink
[email protected]e2614c62011-04-16 22:12:4549 // 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]4d2efd22011-08-18 21:58:02110} // namespace ppapi
[email protected]e2614c62011-04-16 22:12:45111
112#endif // PPAPI_PROXY_PROXY_CHANNEL_H_