blob: b3b520d46978f7327e8ead5017b1834f4537b294 [file] [log] [blame]
[email protected]c2932f5e2010-11-03 03:22:331// Copyright (c) 2010 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_DISPATCHER_H_
6#define PPAPI_PROXY_DISPATCHER_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/linked_ptr.h"
[email protected]5d84d012010-12-02 17:17:2113#include "base/process.h"
[email protected]c2932f5e2010-11-03 03:22:3314#include "base/scoped_ptr.h"
15#include "ipc/ipc_channel.h"
16#include "ipc/ipc_channel_handle.h"
17#include "ipc/ipc_message.h"
18#include "ppapi/c/pp_module.h"
19#include "ppapi/proxy/callback_tracker.h"
20#include "ppapi/proxy/interface_id.h"
[email protected]465faa22011-02-08 16:31:4621#include "ppapi/proxy/interface_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3322#include "ppapi/proxy/plugin_var_tracker.h"
23
24class MessageLoop;
25struct PPB_Var_Deprecated;
26
27namespace base {
28class WaitableEvent;
29}
30
31namespace IPC {
32class SyncChannel;
[email protected]f24448db2011-01-27 20:40:3933class TestSink;
[email protected]c2932f5e2010-11-03 03:22:3334}
35
36namespace pp {
37namespace proxy {
38
[email protected]c2932f5e2010-11-03 03:22:3339class VarSerializationRules;
40
41// An interface proxy can represent either end of a cross-process interface
42// call. The "source" side is where the call is invoked, and the "target" side
43// is where the call ends up being executed.
44//
45// Plugin side | Browser side
46// -------------------------------------|--------------------------------------
47// |
48// "Source" | "Target"
49// InterfaceProxy ----------------------> InterfaceProxy
50// |
51// |
52// "Target" | "Source"
53// InterfaceProxy <---------------------- InterfaceProxy
54// |
55class Dispatcher : public IPC::Channel::Listener,
56 public IPC::Message::Sender {
57 public:
58 typedef const void* (*GetInterfaceFunc)(const char*);
[email protected]9c7baaef2010-11-10 05:42:4659 typedef int32_t (*InitModuleFunc)(PP_Module, GetInterfaceFunc);
60 typedef void (*ShutdownModuleFunc)();
[email protected]c2932f5e2010-11-03 03:22:3361
62 ~Dispatcher();
63
[email protected]f24448db2011-01-27 20:40:3964 // You must call this function before anything else. Returns true on success.
[email protected]c2932f5e2010-11-03 03:22:3365 bool InitWithChannel(MessageLoop* ipc_message_loop,
[email protected]42ce94e2010-12-08 19:28:0966 const IPC::ChannelHandle& channel_handle,
[email protected]c2932f5e2010-11-03 03:22:3367 bool is_client,
68 base::WaitableEvent* shutdown_event);
69
[email protected]f24448db2011-01-27 20:40:3970 // Alternative to InitWithChannel() for unit tests that want to send all
71 // messages sent via this dispatcher to the given test sink. The test sink
72 // must outlive this class.
73 void InitWithTestSink(IPC::TestSink* test_sink);
74
[email protected]c2932f5e2010-11-03 03:22:3375 // Returns true if the dispatcher is on the plugin side, or false if it's the
76 // browser side.
77 virtual bool IsPlugin() const = 0;
78
79 VarSerializationRules* serialization_rules() const {
80 return serialization_rules_.get();
81 }
82 PP_Module pp_module() const {
83 return pp_module_;
84 }
85
86 // Wrapper for calling the local GetInterface function.
87 const void* GetLocalInterface(const char* interface);
88
[email protected]5d84d012010-12-02 17:17:2189 // Returns the remote process' handle. For the host dispatcher, this will be
90 // the plugin process, and for the plugin dispatcher, this will be the
91 // renderer process. This is used for sharing memory and such and is
92 // guaranteed valid (unless the remote process has suddenly died).
93 base::ProcessHandle remote_process_handle() const {
94 return remote_process_handle_;
95 }
96
[email protected]c2932f5e2010-11-03 03:22:3397 // Called if the remote side is declaring to us which interfaces it supports
98 // so we don't have to query for each one. We'll pre-create proxies for
99 // each of the given interfaces.
100
101 // IPC::Message::Sender implementation.
102 virtual bool Send(IPC::Message* msg);
103
104 // IPC::Channel::Listener implementation.
[email protected]a95986a82010-12-24 06:19:28105 virtual bool OnMessageReceived(const IPC::Message& msg);
[email protected]c2932f5e2010-11-03 03:22:33106
[email protected]f24448db2011-01-27 20:40:39107 // Will be NULL in some unit tests.
[email protected]c2932f5e2010-11-03 03:22:33108 IPC::SyncChannel* channel() const {
109 return channel_.get();
110 }
111
112 CallbackTracker& callback_tracker() {
113 return callback_tracker_;
114 }
115
[email protected]465faa22011-02-08 16:31:46116 // Retrieves the information associated with the given interface, identified
117 // either by name or ID. Each function searches either PPP or PPB interfaces.
118 static const InterfaceProxy::Info* GetPPBInterfaceInfo(
119 const std::string& name);
120 static const InterfaceProxy::Info* GetPPBInterfaceInfo(
121 InterfaceID id);
122 static const InterfaceProxy::Info* GetPPPInterfaceInfo(
123 const std::string& name);
124 static const InterfaceProxy::Info* GetPPPInterfaceInfo(
125 InterfaceID id);
126
[email protected]c2932f5e2010-11-03 03:22:33127 protected:
[email protected]5d84d012010-12-02 17:17:21128 Dispatcher(base::ProcessHandle remote_process_handle,
129 GetInterfaceFunc local_get_interface);
[email protected]c2932f5e2010-11-03 03:22:33130
131 // Setter for the derived classes to set the appropriate var serialization.
132 // Takes ownership of the given pointer, which must be on the heap.
133 void SetSerializationRules(VarSerializationRules* var_serialization_rules);
134
135 void set_pp_module(PP_Module module) {
136 pp_module_ = module;
137 }
138
[email protected]465faa22011-02-08 16:31:46139 bool disallow_trusted_interfaces() const {
140 return disallow_trusted_interfaces_;
141 }
[email protected]c2932f5e2010-11-03 03:22:33142
143 private:
[email protected]c2932f5e2010-11-03 03:22:33144 // Set by the derived classed to indicate the module ID corresponding to
145 // this dispatcher.
146 PP_Module pp_module_;
147
[email protected]5d84d012010-12-02 17:17:21148 base::ProcessHandle remote_process_handle_; // See getter above.
[email protected]f24448db2011-01-27 20:40:39149
150 // When we're unit testing, this will indicate the sink for the messages to
151 // be deposited so they can be inspected by the test. When non-NULL, this
152 // indicates that the channel should not be used.
153 IPC::TestSink* test_sink_;
154
155 // Will be null for some tests when there is a test_sink_.
[email protected]c2932f5e2010-11-03 03:22:33156 scoped_ptr<IPC::SyncChannel> channel_;
157
158 bool disallow_trusted_interfaces_;
159
160 GetInterfaceFunc local_get_interface_;
161
[email protected]c2932f5e2010-11-03 03:22:33162 CallbackTracker callback_tracker_;
163
164 scoped_ptr<VarSerializationRules> serialization_rules_;
165
166 DISALLOW_COPY_AND_ASSIGN(Dispatcher);
167};
168
169} // namespace proxy
170} // namespace pp
171
172#endif // PPAPI_PROXY_DISPATCHER_H_