Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: ppapi/proxy/dispatcher.cc

Issue 6286070: Remove all uses of the global Dispatcher Get function. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/proxy/dispatcher.h" 5 #include "ppapi/proxy/dispatcher.h"
6 6
7 #include <string.h> // For memset. 7 #include <string.h> // For memset.
8 8
9 #include <map> 9 #include <map>
10 10
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/singleton.h"
13 #include "ipc/ipc_message.h" 14 #include "ipc/ipc_message.h"
14 #include "ipc/ipc_sync_channel.h" 15 #include "ipc/ipc_sync_channel.h"
15 #include "ipc/ipc_test_sink.h" 16 #include "ipc/ipc_test_sink.h"
16 #include "ppapi/c/dev/ppb_buffer_dev.h" 17 #include "ppapi/c/dev/ppb_buffer_dev.h"
17 #include "ppapi/c/dev/ppb_char_set_dev.h" 18 #include "ppapi/c/dev/ppb_char_set_dev.h"
18 #include "ppapi/c/dev/ppb_context_3d_dev.h" 19 #include "ppapi/c/dev/ppb_context_3d_dev.h"
19 #include "ppapi/c/dev/ppb_cursor_control_dev.h" 20 #include "ppapi/c/dev/ppb_cursor_control_dev.h"
20 #include "ppapi/c/dev/ppb_gles_chromium_texture_mapping_dev.h" 21 #include "ppapi/c/dev/ppb_gles_chromium_texture_mapping_dev.h"
21 #include "ppapi/c/dev/ppb_font_dev.h" 22 #include "ppapi/c/dev/ppb_font_dev.h"
22 #include "ppapi/c/dev/ppb_fullscreen_dev.h" 23 #include "ppapi/c/dev/ppb_fullscreen_dev.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 #include "ppapi/proxy/ppb_url_request_info_proxy.h" 62 #include "ppapi/proxy/ppb_url_request_info_proxy.h"
62 #include "ppapi/proxy/ppb_url_response_info_proxy.h" 63 #include "ppapi/proxy/ppb_url_response_info_proxy.h"
63 #include "ppapi/proxy/ppb_var_deprecated_proxy.h" 64 #include "ppapi/proxy/ppb_var_deprecated_proxy.h"
64 #include "ppapi/proxy/ppp_class_proxy.h" 65 #include "ppapi/proxy/ppp_class_proxy.h"
65 #include "ppapi/proxy/ppp_instance_proxy.h" 66 #include "ppapi/proxy/ppp_instance_proxy.h"
66 #include "ppapi/proxy/var_serialization_rules.h" 67 #include "ppapi/proxy/var_serialization_rules.h"
67 68
68 namespace pp { 69 namespace pp {
69 namespace proxy { 70 namespace proxy {
70 71
72 namespace {
73
74 struct InterfaceList {
75 InterfaceList();
76
77 static InterfaceList* GetInstance();
78
79 void AddPPP(const InterfaceProxy::Info* info);
80 void AddPPB(const InterfaceProxy::Info* info);
81
82 typedef std::map<std::string, const InterfaceProxy::Info*> NameToInfo;
83 NameToInfo name_to_plugin_info_;
84 NameToInfo name_to_browser_info_;
85
86 const InterfaceProxy::Info* id_to_plugin_info_[INTERFACE_ID_COUNT];
87 const InterfaceProxy::Info* id_to_browser_info_[INTERFACE_ID_COUNT];
88 };
89
90 InterfaceList::InterfaceList() {
91 memset(id_to_plugin_info_, 0,
92 static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceID));
93 memset(id_to_browser_info_, 0,
94 static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceID));
95
96 // PPB (browser) interfaces.
97 AddPPB(PPB_AudioConfig_Proxy::GetInfo());
98 AddPPB(PPB_Audio_Proxy::GetInfo());
99 AddPPB(PPB_Buffer_Proxy::GetInfo());
100 AddPPB(PPB_CharSet_Proxy::GetInfo());
101 AddPPB(PPB_Core_Proxy::GetInfo());
102 AddPPB(PPB_CursorControl_Proxy::GetInfo());
103 AddPPB(PPB_Flash_Proxy::GetInfo());
104 AddPPB(PPB_Font_Proxy::GetInfo());
105 AddPPB(PPB_Fullscreen_Proxy::GetInfo());
106 AddPPB(PPB_Graphics2D_Proxy::GetInfo());
107 AddPPB(PPB_ImageData_Proxy::GetInfo());
108 AddPPB(PPB_Instance_Proxy::GetInfo());
109 AddPPB(PPB_PDF_Proxy::GetInfo());
110 AddPPB(PPB_Testing_Proxy::GetInfo());
111 AddPPB(PPB_URLLoader_Proxy::GetInfo());
112 AddPPB(PPB_URLLoaderTrusted_Proxy::GetInfo());
113 AddPPB(PPB_URLRequestInfo_Proxy::GetInfo());
114 AddPPB(PPB_URLResponseInfo_Proxy::GetInfo());
115 AddPPB(PPB_Var_Deprecated_Proxy::GetInfo());
116
117 // PPP (plugin) interfaces.
118 AddPPP(PPP_Instance_Proxy::GetInfo());
119 }
120
121 void InterfaceList::AddPPP(const InterfaceProxy::Info* info) {
122 DCHECK(name_to_plugin_info_.find(info->name) ==
123 name_to_plugin_info_.end());
124 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
125 DCHECK(id_to_plugin_info_[info->id] == NULL);
126
127 name_to_plugin_info_[info->name] = info;
128 id_to_plugin_info_[info->id] = info;
129 }
130
131 void InterfaceList::AddPPB(const InterfaceProxy::Info* info) {
132 DCHECK(name_to_browser_info_.find(info->name) ==
133 name_to_browser_info_.end());
134 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
135 DCHECK(id_to_browser_info_[info->id] == NULL);
136
137 name_to_browser_info_[std::string(info->name)] = info;
138 id_to_browser_info_[info->id] = info;
139 }
140
141 // static
142 InterfaceList* InterfaceList::GetInstance() {
143 return Singleton<InterfaceList>::get();
144 }
145
146 } // namespace
147
71 Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle, 148 Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle,
72 GetInterfaceFunc local_get_interface) 149 GetInterfaceFunc local_get_interface)
73 : pp_module_(0), 150 : pp_module_(0),
74 remote_process_handle_(remote_process_handle), 151 remote_process_handle_(remote_process_handle),
75 test_sink_(NULL), 152 test_sink_(NULL),
76 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable. 153 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
77 local_get_interface_(local_get_interface), 154 local_get_interface_(local_get_interface),
78 declared_supported_remote_interfaces_(false),
79 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 155 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
80 memset(id_to_proxy_, 0,
81 static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceProxy*));
82 } 156 }
83 157
84 Dispatcher::~Dispatcher() { 158 Dispatcher::~Dispatcher() {
85 } 159 }
86 160
87 bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop, 161 bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop,
88 const IPC::ChannelHandle& channel_handle, 162 const IPC::ChannelHandle& channel_handle,
89 bool is_client, 163 bool is_client,
90 base::WaitableEvent* shutdown_event) { 164 base::WaitableEvent* shutdown_event) {
91 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT 165 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
92 : IPC::Channel::MODE_SERVER; 166 : IPC::Channel::MODE_SERVER;
93 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this, 167 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this,
94 ipc_message_loop, false, shutdown_event)); 168 ipc_message_loop, false, shutdown_event));
95 return true; 169 return true;
96 } 170 }
97 171
98 void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) { 172 void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) {
99 DCHECK(!test_sink_); 173 DCHECK(!test_sink_);
100 test_sink_ = test_sink; 174 test_sink_ = test_sink;
101 } 175 }
102 176
103 bool Dispatcher::OnMessageReceived(const IPC::Message& msg) { 177 bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
104 // Control messages. 178 // Control messages.
105 if (msg.routing_id() == MSG_ROUTING_CONTROL) { 179 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
106 bool handled = true; 180 bool handled = true;
107 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg) 181 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
108 IPC_MESSAGE_HANDLER(PpapiMsg_DeclareInterfaces,
109 OnMsgDeclareInterfaces)
110 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
111 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_, 182 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
112 CallbackTracker::ReceiveExecuteSerializedCallback) 183 CallbackTracker::ReceiveExecuteSerializedCallback)
113 IPC_MESSAGE_UNHANDLED(handled = false) 184 IPC_MESSAGE_UNHANDLED(handled = false)
114 IPC_END_MESSAGE_MAP() 185 IPC_END_MESSAGE_MAP()
115 return handled; 186 return handled;
116 } 187 }
188 return false;
189 }
117 190
118 // Interface-specific messages. 191 // static
119 if (msg.routing_id() > 0 && msg.routing_id() < INTERFACE_ID_COUNT) { 192 const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(
120 InterfaceProxy* proxy = id_to_proxy_[msg.routing_id()]; 193 const std::string& name) {
121 if (proxy) 194 const InterfaceList* list = InterfaceList::GetInstance();
122 return proxy->OnMessageReceived(msg); 195 InterfaceList::NameToInfo::const_iterator found =
196 list->name_to_browser_info_.find(name);
197 if (found == list->name_to_browser_info_.end())
198 return NULL;
199 return found->second;
200 }
123 201
124 NOTREACHED(); 202 // static
125 // TODO(brettw): kill the plugin if it starts sending invalid messages? 203 const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(InterfaceID id) {
126 } 204 if (id <= 0 || id >= INTERFACE_ID_COUNT)
205 return NULL;
206 const InterfaceList* list = InterfaceList::GetInstance();
207 return list->id_to_browser_info_[id];
208 }
127 209
128 return false; 210 // static
211 const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(
212 const std::string& name) {
213 const InterfaceList* list = InterfaceList::GetInstance();
214 InterfaceList::NameToInfo::const_iterator found =
215 list->name_to_plugin_info_.find(name);
216 if (found == list->name_to_plugin_info_.end())
217 return NULL;
218 return found->second;
219 }
220
221 // static
222 const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(InterfaceID id) {
223 if (id <= 0 || id >= INTERFACE_ID_COUNT)
224 return NULL;
225 const InterfaceList* list = InterfaceList::GetInstance();
226 return list->id_to_plugin_info_[id];
129 } 227 }
130 228
131 void Dispatcher::SetSerializationRules( 229 void Dispatcher::SetSerializationRules(
132 VarSerializationRules* var_serialization_rules) { 230 VarSerializationRules* var_serialization_rules) {
133 serialization_rules_.reset(var_serialization_rules); 231 serialization_rules_.reset(var_serialization_rules);
134 } 232 }
135 233
136 void Dispatcher::InjectProxy(InterfaceID id,
137 const std::string& name,
138 InterfaceProxy* proxy) {
139 proxies_[name] = linked_ptr<InterfaceProxy>(proxy);
140 id_to_proxy_[id] = proxy;
141 }
142
143 const void* Dispatcher::GetLocalInterface(const char* interface) { 234 const void* Dispatcher::GetLocalInterface(const char* interface) {
144 return local_get_interface_(interface); 235 return local_get_interface_(interface);
145 } 236 }
146 237
147 const void* Dispatcher::GetProxiedInterface(const std::string& interface) {
148 // See if we already know about this interface and have created a host.
149 ProxyMap::const_iterator found = proxies_.find(interface);
150 if (found != proxies_.end())
151 return found->second->GetSourceInterface();
152
153 // When the remote side has sent us a declared list of all interfaces it
154 // supports and we don't have it in our list, we know the requested interface
155 // doesn't exist and we can return failure.
156 if (declared_supported_remote_interfaces_)
157 return NULL;
158
159 if (!RemoteSupportsTargetInterface(interface))
160 return NULL;
161
162 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interface, NULL));
163 if (!proxy.get())
164 return NULL; // Don't know how to proxy this interface.
165
166 // Save our proxy.
167 proxies_[interface] = proxy;
168 InterfaceID id = proxy->GetInterfaceId();
169 if (id != INTERFACE_ID_NONE)
170 id_to_proxy_[id] = proxy.get();
171 return proxy->GetSourceInterface();
172 }
173
174 bool Dispatcher::Send(IPC::Message* msg) { 238 bool Dispatcher::Send(IPC::Message* msg) {
175 if (test_sink_) 239 if (test_sink_)
176 return test_sink_->Send(msg); 240 return test_sink_->Send(msg);
177 return channel_->Send(msg); 241 return channel_->Send(msg);
178 } 242 }
179 243
180 bool Dispatcher::RemoteSupportsTargetInterface(const std::string& interface) {
181 bool result = false;
182 Send(new PpapiMsg_SupportsInterface(interface, &result));
183 return result;
184 }
185
186 bool Dispatcher::IsInterfaceTrusted(const std::string& interface) {
187 // FIXME(brettw)
188 (void)interface;
189 return false;
190 }
191
192 bool Dispatcher::SetupProxyForTargetInterface(const std::string& interface) {
193 // If we already have a proxy that knows about the locally-implemented
194 // interface, we know it's supported and don't need to re-query.
195 ProxyMap::const_iterator found = proxies_.find(interface);
196 if (found != proxies_.end())
197 return true;
198
199 if (disallow_trusted_interfaces_ && IsInterfaceTrusted(interface))
200 return false;
201
202 // Create the proxy if it doesn't exist and set the local interface on it.
203 // This also handles the case where possibly an interface could be supported
204 // by both the local and remote side.
205 const void* interface_functions = local_get_interface_(interface.c_str());
206 if (!interface_functions)
207 return false;
208 InterfaceProxy* proxy = CreateProxyForInterface(interface,
209 interface_functions);
210 if (!proxy)
211 return false;
212
213 proxies_[interface] = linked_ptr<InterfaceProxy>(proxy);
214 id_to_proxy_[proxy->GetInterfaceId()] = proxy;
215 return true;
216 }
217
218 void Dispatcher::OnMsgSupportsInterface(const std::string& interface_name,
219 bool* result) {
220 *result = SetupProxyForTargetInterface(interface_name);
221 }
222
223 void Dispatcher::OnMsgDeclareInterfaces(
224 const std::vector<std::string>& interfaces) {
225 // Make proxies for all the interfaces it supports that we also support.
226 for (size_t i = 0; i < interfaces.size(); i++) {
227 // Possibly the plugin could request an interface before the "declare"
228 // message is received, so we could already have an entry for this
229 // interface. In this case, we can just skip to the next one.
230 if (proxies_.find(interfaces[i]) != proxies_.end())
231 continue;
232
233 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interfaces[i],
234 NULL));
235 if (!proxy.get()) {
236 // Since only the browser declares supported interfaces, we should never
237 // get one we don't support.
238 //NOTREACHED() << "Remote side declaring an unsupported proxy.";
239 continue;
240 }
241 proxies_[interfaces[i]] = proxy;
242 id_to_proxy_[proxy->GetInterfaceId()] = proxy.get();
243 }
244 }
245
246 InterfaceProxy* Dispatcher::CreateProxyForInterface(
247 const std::string& interface_name,
248 const void* interface_functions) {
249 if (interface_name == PPB_AUDIO_CONFIG_INTERFACE)
250 return new PPB_AudioConfig_Proxy(this, interface_functions);
251 if (interface_name == PPB_AUDIO_INTERFACE)
252 return new PPB_Audio_Proxy(this, interface_functions);
253 if (interface_name == PPB_BUFFER_DEV_INTERFACE)
254 return new PPB_Buffer_Proxy(this, interface_functions);
255 if (interface_name == PPB_CHAR_SET_DEV_INTERFACE)
256 return new PPB_CharSet_Proxy(this, interface_functions);
257 if (interface_name == PPB_CONTEXT_3D_DEV_INTERFACE)
258 return new PPB_Context3D_Proxy(this, interface_functions);
259 if (interface_name == PPB_CORE_INTERFACE)
260 return new PPB_Core_Proxy(this, interface_functions);
261 if (interface_name == PPB_CURSOR_CONTROL_DEV_INTERFACE)
262 return new PPB_CursorControl_Proxy(this, interface_functions);
263 if (interface_name == PPB_FONT_DEV_INTERFACE)
264 return new PPB_Font_Proxy(this, interface_functions);
265 if (interface_name == PPB_FULLSCREEN_DEV_INTERFACE)
266 return new PPB_Fullscreen_Proxy(this, interface_functions);
267 if (interface_name == PPB_GLES_CHROMIUM_TEXTURE_MAPPING_DEV_INTERFACE)
268 return new PPB_GLESChromiumTextureMapping_Proxy(this, interface_functions);
269 if (interface_name == PPB_GRAPHICS_2D_INTERFACE)
270 return new PPB_Graphics2D_Proxy(this, interface_functions);
271 if (interface_name == PPB_IMAGEDATA_INTERFACE)
272 return new PPB_ImageData_Proxy(this, interface_functions);
273 if (interface_name == PPB_INSTANCE_INTERFACE)
274 return new PPB_Instance_Proxy(this, interface_functions);
275 if (interface_name == PPB_OPENGLES2_DEV_INTERFACE)
276 return new PPB_OpenGLES2_Proxy(this, interface_functions);
277 if (interface_name == PPB_SURFACE_3D_DEV_INTERFACE)
278 return new PPB_Surface3D_Proxy(this, interface_functions);
279 if (interface_name == PPB_TESTING_DEV_INTERFACE)
280 return new PPB_Testing_Proxy(this, interface_functions);
281 if (interface_name == PPB_URLLOADER_INTERFACE)
282 return new PPB_URLLoader_Proxy(this, interface_functions);
283 if (interface_name == PPB_URLREQUESTINFO_INTERFACE)
284 return new PPB_URLRequestInfo_Proxy(this, interface_functions);
285 if (interface_name == PPB_URLRESPONSEINFO_INTERFACE)
286 return new PPB_URLResponseInfo_Proxy(this, interface_functions);
287 if (interface_name == PPB_VAR_DEPRECATED_INTERFACE)
288 return new PPB_Var_Deprecated_Proxy(this, interface_functions);
289 if (interface_name == PPP_INSTANCE_INTERFACE)
290 return new PPP_Instance_Proxy(this, interface_functions);
291
292 // Trusted interfaces.
293 if (!disallow_trusted_interfaces_) {
294 if (interface_name == PPB_FLASH_INTERFACE)
295 return new PPB_Flash_Proxy(this, interface_functions);
296 if (interface_name == PPB_PDF_INTERFACE)
297 return new PPB_PDF_Proxy(this, interface_functions);
298 if (interface_name == PPB_URLLOADERTRUSTED_INTERFACE)
299 return new PPB_URLLoaderTrusted_Proxy(this, interface_functions);
300 }
301
302 return NULL;
303 }
304
305 } // namespace proxy 244 } // namespace proxy
306 } // namespace pp 245 } // namespace pp
307 246
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698