blob: 8de9cea76665938b7c87555f07f2234e1d9cdeec [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/plugin_dispatcher.h"
6
7#include <map>
8
[email protected]709a847e2010-11-10 01:16:119#include "base/compiler_specific.h"
[email protected]c2932f5e2010-11-03 03:22:3310#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
19namespace pp {
20namespace proxy {
21
22namespace {
23
24PluginDispatcher* g_dispatcher = NULL;
25
26const 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]5d84d012010-12-02 17:17:2133PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle,
34 GetInterfaceFunc get_interface,
[email protected]c2932f5e2010-11-03 03:22:3335 InitModuleFunc init_module,
36 ShutdownModuleFunc shutdown_module)
[email protected]5d84d012010-12-02 17:17:2137 : Dispatcher(remote_process_handle, get_interface),
[email protected]c2932f5e2010-11-03 03:22:3338 init_module_(init_module),
[email protected]4614f192011-01-21 00:26:4339 shutdown_module_(shutdown_module) {
40 SetSerializationRules(new PluginVarSerializationRules);
[email protected]c2932f5e2010-11-03 03:22:3341
42 // As a plugin, we always support the PPP_Class interface. There's no
43 // GetInterface call or name for it, so we insert it into our table now.
44 InjectProxy(INTERFACE_ID_PPP_CLASS, "$Internal_PPP_Class",
45 new PPP_Class_Proxy(this));
46}
47
48PluginDispatcher::~PluginDispatcher() {
49 if (shutdown_module_)
50 shutdown_module_();
51}
52
53// static
54PluginDispatcher* PluginDispatcher::Get() {
55 return g_dispatcher;
56}
57
58// static
59void PluginDispatcher::SetGlobal(PluginDispatcher* dispatcher) {
60 DCHECK(!dispatcher || !g_dispatcher);
61 g_dispatcher = dispatcher;
62}
63
[email protected]4614f192011-01-21 00:26:4364// static
65PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
66 // TODO(brettw) implement "real" per-instance dispatcher map.
67 DCHECK(instance != 0);
68 return Get();
69}
70
[email protected]7cf40912010-12-09 18:25:0371bool PluginDispatcher::IsPlugin() const {
72 return true;
73}
74
[email protected]a95986a82010-12-24 06:19:2875bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]c2932f5e2010-11-03 03:22:3376 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
77 // Handle some plugin-specific control messages.
[email protected]a95986a82010-12-24 06:19:2878 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:3379 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
[email protected]176c73922010-12-03 17:32:1980 IPC_MESSAGE_HANDLER(PpapiMsg_InitializeModule, OnMsgInitializeModule)
81 IPC_MESSAGE_HANDLER(PpapiMsg_Shutdown, OnMsgShutdown)
[email protected]c2932f5e2010-11-03 03:22:3382
83 // Forward all other control messages to the superclass.
[email protected]a95986a82010-12-24 06:19:2884 IPC_MESSAGE_UNHANDLED(handled = Dispatcher::OnMessageReceived(msg))
[email protected]c2932f5e2010-11-03 03:22:3385 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:2886 return handled;
[email protected]c2932f5e2010-11-03 03:22:3387 }
88
89 // All non-control messages get handled by the superclass.
[email protected]a95986a82010-12-24 06:19:2890 return Dispatcher::OnMessageReceived(msg);
[email protected]c2932f5e2010-11-03 03:22:3391}
92
[email protected]176c73922010-12-03 17:32:1993void PluginDispatcher::OnMsgInitializeModule(PP_Module pp_module,
94 bool* result) {
[email protected]c2932f5e2010-11-03 03:22:3395 set_pp_module(pp_module);
96 *result = init_module_(pp_module, &GetInterfaceFromDispatcher) == PP_OK;
97}
98
[email protected]176c73922010-12-03 17:32:1999void PluginDispatcher::OnMsgShutdown() {
100 if (shutdown_module_)
101 shutdown_module_();
102 MessageLoop::current()->Quit();
103}
104
[email protected]c2932f5e2010-11-03 03:22:33105} // namespace proxy
106} // namespace pp
107