[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 1 | // 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/plugin_dispatcher.h" |
| 6 | |
| 7 | #include <map> |
| 8 | |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 10 | #include "base/logging.h" |
| 11 | #include "ipc/ipc_message.h" |
| 12 | #include "ipc/ipc_sync_channel.h" |
| 13 | #include "ppapi/c/pp_errors.h" |
| 14 | #include "ppapi/proxy/interface_proxy.h" |
| 15 | #include "ppapi/proxy/plugin_var_serialization_rules.h" |
| 16 | #include "ppapi/proxy/ppapi_messages.h" |
| 17 | #include "ppapi/proxy/ppp_class_proxy.h" |
| 18 | |
| 19 | namespace pp { |
| 20 | namespace proxy { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | PluginDispatcher* g_dispatcher = NULL; |
| 25 | |
| 26 | const void* GetInterfaceFromDispatcher(const char* interface) { |
| 27 | // TODO(brettw) need some kind of lock for multi-thread access. |
| 28 | return pp::proxy::PluginDispatcher::Get()->GetProxiedInterface(interface); |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 33 | PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle, |
| 34 | GetInterfaceFunc get_interface, |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 35 | InitModuleFunc init_module, |
| 36 | ShutdownModuleFunc shutdown_module) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 37 | : Dispatcher(remote_process_handle, get_interface), |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 38 | init_module_(init_module), |
| 39 | shutdown_module_(shutdown_module), |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 40 | plugin_resource_tracker_(new PluginResourceTracker( |
| 41 | ALLOW_THIS_IN_INITIALIZER_LIST(this))), |
| 42 | plugin_var_tracker_(new PluginVarTracker( |
| 43 | ALLOW_THIS_IN_INITIALIZER_LIST(this))) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 44 | SetSerializationRules( |
| 45 | new PluginVarSerializationRules(plugin_var_tracker_.get())); |
| 46 | |
| 47 | // As a plugin, we always support the PPP_Class interface. There's no |
| 48 | // GetInterface call or name for it, so we insert it into our table now. |
| 49 | InjectProxy(INTERFACE_ID_PPP_CLASS, "$Internal_PPP_Class", |
| 50 | new PPP_Class_Proxy(this)); |
| 51 | } |
| 52 | |
| 53 | PluginDispatcher::~PluginDispatcher() { |
| 54 | if (shutdown_module_) |
| 55 | shutdown_module_(); |
| 56 | } |
| 57 | |
| 58 | // static |
| 59 | PluginDispatcher* PluginDispatcher::Get() { |
| 60 | return g_dispatcher; |
| 61 | } |
| 62 | |
| 63 | // static |
| 64 | void PluginDispatcher::SetGlobal(PluginDispatcher* dispatcher) { |
| 65 | DCHECK(!dispatcher || !g_dispatcher); |
| 66 | g_dispatcher = dispatcher; |
| 67 | } |
| 68 | |
| 69 | void PluginDispatcher::OnMessageReceived(const IPC::Message& msg) { |
| 70 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 71 | // Handle some plugin-specific control messages. |
| 72 | IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg) |
[email protected] | 176c7392 | 2010-12-03 17:32:19 | [diff] [blame^] | 73 | IPC_MESSAGE_HANDLER(PpapiMsg_InitializeModule, OnMsgInitializeModule) |
| 74 | IPC_MESSAGE_HANDLER(PpapiMsg_Shutdown, OnMsgShutdown) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 75 | |
| 76 | // Forward all other control messages to the superclass. |
| 77 | IPC_MESSAGE_UNHANDLED(Dispatcher::OnMessageReceived(msg)) |
| 78 | IPC_END_MESSAGE_MAP() |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // All non-control messages get handled by the superclass. |
| 83 | Dispatcher::OnMessageReceived(msg); |
| 84 | } |
| 85 | |
[email protected] | 176c7392 | 2010-12-03 17:32:19 | [diff] [blame^] | 86 | void PluginDispatcher::OnMsgInitializeModule(PP_Module pp_module, |
| 87 | bool* result) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 88 | set_pp_module(pp_module); |
| 89 | *result = init_module_(pp_module, &GetInterfaceFromDispatcher) == PP_OK; |
| 90 | } |
| 91 | |
[email protected] | 176c7392 | 2010-12-03 17:32:19 | [diff] [blame^] | 92 | void PluginDispatcher::OnMsgShutdown() { |
| 93 | if (shutdown_module_) |
| 94 | shutdown_module_(); |
| 95 | MessageLoop::current()->Quit(); |
| 96 | } |
| 97 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 98 | } // namespace proxy |
| 99 | } // namespace pp |
| 100 | |