blob: eecc9a00bc6010bbb6f634df35149c6282d9b177 [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#include "ppapi/proxy/dispatcher.h"
6
7#include <string.h> // For memset.
8
9#include <map>
10
[email protected]709a847e2010-11-10 01:16:1111#include "base/compiler_specific.h"
[email protected]c2932f5e2010-11-03 03:22:3312#include "base/logging.h"
13#include "ipc/ipc_message.h"
14#include "ipc/ipc_sync_channel.h"
15#include "ppapi/proxy/interface_proxy.h"
16#include "ppapi/proxy/ppapi_messages.h"
[email protected]799d1ab2010-11-09 17:16:2817#include "ppapi/c/dev/ppb_testing_dev.h"
[email protected]c2932f5e2010-11-03 03:22:3318#include "ppapi/c/dev/ppb_var_deprecated.h"
19#include "ppapi/c/pp_errors.h"
20#include "ppapi/c/ppb_core.h"
21#include "ppapi/c/ppb_graphics_2d.h"
22#include "ppapi/c/ppb_image_data.h"
23#include "ppapi/c/ppb_instance.h"
24#include "ppapi/c/ppp_instance.h"
[email protected]5a3f62852010-11-10 21:43:0125#include "ppapi/c/ppb_url_loader.h"
26#include "ppapi/c/ppb_url_request_info.h"
27#include "ppapi/c/ppb_url_response_info.h"
[email protected]c2932f5e2010-11-03 03:22:3328#include "ppapi/proxy/ppb_core_proxy.h"
29#include "ppapi/proxy/ppb_graphics_2d_proxy.h"
30#include "ppapi/proxy/ppb_image_data_proxy.h"
31#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]799d1ab2010-11-09 17:16:2832#include "ppapi/proxy/ppb_testing_proxy.h"
33#include "ppapi/proxy/ppb_url_loader_proxy.h"
34#include "ppapi/proxy/ppb_url_request_info_proxy.h"
35#include "ppapi/proxy/ppb_url_response_info_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3336#include "ppapi/proxy/ppb_var_deprecated_proxy.h"
37#include "ppapi/proxy/ppp_class_proxy.h"
38#include "ppapi/proxy/ppp_instance_proxy.h"
39#include "ppapi/proxy/var_serialization_rules.h"
40
41namespace pp {
42namespace proxy {
43
44Dispatcher::Dispatcher(GetInterfaceFunc local_get_interface)
45 : pp_module_(0),
46 disallow_trusted_interfaces_(true),
47 local_get_interface_(local_get_interface),
48 declared_supported_remote_interfaces_(false),
[email protected]709a847e2010-11-10 01:16:1149 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:3350 memset(id_to_proxy_, 0,
51 static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceProxy*));
52}
53
54Dispatcher::~Dispatcher() {
55}
56
57bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop,
58 const std::string& channel_name,
59 bool is_client,
60 base::WaitableEvent* shutdown_event) {
61 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
62 : IPC::Channel::MODE_SERVER;
63 channel_.reset(new IPC::SyncChannel(channel_name, mode, this, NULL,
64 ipc_message_loop, false, shutdown_event));
65 return true;
66}
67
68void Dispatcher::OnMessageReceived(const IPC::Message& msg) {
69 // Control messages.
70 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
71 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
72 IPC_MESSAGE_HANDLER(PpapiMsg_DeclareInterfaces,
73 OnMsgDeclareInterfaces)
74 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
75 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
76 CallbackTracker::ReceiveExecuteSerializedCallback)
77 IPC_END_MESSAGE_MAP()
78 return;
79 }
80
81 // Interface-specific messages.
82 if (msg.routing_id() > 0 && msg.routing_id() < INTERFACE_ID_COUNT) {
83 InterfaceProxy* proxy = id_to_proxy_[msg.routing_id()];
84 if (proxy)
85 proxy->OnMessageReceived(msg);
86 else
87 NOTREACHED();
88 // TODO(brettw): kill the plugin if it starts sending invalid messages?
89 }
90}
91
92void Dispatcher::SetSerializationRules(
93 VarSerializationRules* var_serialization_rules) {
94 serialization_rules_.reset(var_serialization_rules);
95}
96
97void Dispatcher::InjectProxy(InterfaceID id,
98 const std::string& name,
99 InterfaceProxy* proxy) {
100 proxies_[name] = linked_ptr<InterfaceProxy>(proxy);
101 id_to_proxy_[id] = proxy;
102}
103
104const void* Dispatcher::GetLocalInterface(const char* interface) {
105 return local_get_interface_(interface);
106}
107
108const void* Dispatcher::GetProxiedInterface(const std::string& interface) {
109 // See if we already know about this interface and have created a host.
110 ProxyMap::const_iterator found = proxies_.find(interface);
111 if (found != proxies_.end())
112 return found->second->GetSourceInterface();
113
114 // When the remote side has sent us a declared list of all interfaces it
115 // supports and we don't have it in our list, we know the requested interface
116 // doesn't exist and we can return failure.
117 if (declared_supported_remote_interfaces_)
118 return NULL;
119
120 if (!RemoteSupportsTargetInterface(interface))
121 return NULL;
122
123 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interface, NULL));
124 if (!proxy.get())
125 return NULL; // Don't know how to proxy this interface.
126
127 // Save our proxy.
128 proxies_[interface] = proxy;
129 id_to_proxy_[proxy->GetInterfaceId()] = proxy.get();
130 return proxy->GetSourceInterface();
131}
132
133bool Dispatcher::Send(IPC::Message* msg) {
134 return channel_->Send(msg);
135}
136
137bool Dispatcher::RemoteSupportsTargetInterface(const std::string& interface) {
138 bool result = false;
139 Send(new PpapiMsg_SupportsInterface(interface, &result));
140 return result;
141}
142
143bool Dispatcher::IsInterfaceTrusted(const std::string& interface) {
144 // FIXME(brettw)
145 (void)interface;
146 return false;
147}
148
149bool Dispatcher::SetupProxyForTargetInterface(const std::string& interface) {
150 // If we already have a proxy that knows about the locally-implemented
151 // interface, we know it's supported and don't need to re-query.
152 ProxyMap::const_iterator found = proxies_.find(interface);
153 if (found != proxies_.end())
154 return true;
155
156 if (disallow_trusted_interfaces_ && IsInterfaceTrusted(interface))
157 return false;
158
159 // Create the proxy if it doesn't exist and set the local interface on it.
160 // This also handles the case where possibly an interface could be supported
161 // by both the local and remote side.
162 const void* interface_functions = local_get_interface_(interface.c_str());
163 if (!interface_functions)
164 return false;
165 InterfaceProxy* proxy = CreateProxyForInterface(interface,
166 interface_functions);
167 if (!proxy)
168 return false;
169
170 proxies_[interface] = linked_ptr<InterfaceProxy>(proxy);
171 id_to_proxy_[proxy->GetInterfaceId()] = proxy;
172 return true;
173}
174
175void Dispatcher::OnMsgSupportsInterface(const std::string& interface_name,
176 bool* result) {
177 *result = SetupProxyForTargetInterface(interface_name);
178}
179
180void Dispatcher::OnMsgDeclareInterfaces(
181 const std::vector<std::string>& interfaces) {
182 // Make proxies for all the interfaces it supports that we also support.
183 for (size_t i = 0; i < interfaces.size(); i++) {
184 // Possibly the plugin could request an interface before the "declare"
185 // message is received, so we could already have an entry for this
186 // interface. In this case, we can just skip to the next one.
187 if (proxies_.find(interfaces[i]) != proxies_.end())
188 continue;
189
190 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interfaces[i],
191 NULL));
192 if (!proxy.get()) {
193 // Since only the browser declares supported interfaces, we should never
194 // get one we don't support.
195 //NOTREACHED() << "Remote side declaring an unsupported proxy.";
196 continue;
197 }
198 proxies_[interfaces[i]] = proxy;
199 id_to_proxy_[proxy->GetInterfaceId()] = proxy.get();
200 }
201}
202
203InterfaceProxy* Dispatcher::CreateProxyForInterface(
204 const std::string& interface_name,
205 const void* interface_functions) {
206 if (interface_name == PPB_CORE_INTERFACE)
207 return new PPB_Core_Proxy(this, interface_functions);
208 if (interface_name == PPB_GRAPHICS_2D_INTERFACE)
209 return new PPB_Graphics2D_Proxy(this, interface_functions);
210 if (interface_name == PPB_IMAGEDATA_INTERFACE)
211 return new PPB_ImageData_Proxy(this, interface_functions);
212 if (interface_name == PPB_INSTANCE_INTERFACE)
213 return new PPB_Instance_Proxy(this, interface_functions);
[email protected]799d1ab2010-11-09 17:16:28214 if (interface_name == PPB_TESTING_DEV_INTERFACE)
215 return new PPB_Testing_Proxy(this, interface_functions);
[email protected]5a3f62852010-11-10 21:43:01216 if (interface_name == PPB_URLLOADER_INTERFACE)
[email protected]799d1ab2010-11-09 17:16:28217 return new PPB_URLLoader_Proxy(this, interface_functions);
[email protected]5a3f62852010-11-10 21:43:01218 if (interface_name == PPB_URLREQUESTINFO_INTERFACE)
[email protected]799d1ab2010-11-09 17:16:28219 return new PPB_URLRequestInfo_Proxy(this, interface_functions);
[email protected]5a3f62852010-11-10 21:43:01220 if (interface_name == PPB_URLRESPONSEINFO_INTERFACE)
[email protected]799d1ab2010-11-09 17:16:28221 return new PPB_URLResponseInfo_Proxy(this, interface_functions);
[email protected]c2932f5e2010-11-03 03:22:33222 if (interface_name == PPB_VAR_DEPRECATED_INTERFACE)
223 return new PPB_Var_Deprecated_Proxy(this, interface_functions);
224 if (interface_name == PPP_INSTANCE_INTERFACE)
225 return new PPP_Instance_Proxy(this, interface_functions);
226
227 return NULL;
228}
229
230} // namespace proxy
231} // namespace pp
232