blob: 085c15c3d21982ad271f6d517c207811b9b25691 [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
33PluginDispatcher::PluginDispatcher(GetInterfaceFunc get_interface,
34 InitModuleFunc init_module,
35 ShutdownModuleFunc shutdown_module)
36 : Dispatcher(get_interface),
37 init_module_(init_module),
38 shutdown_module_(shutdown_module),
[email protected]709a847e2010-11-10 01:16:1139 plugin_resource_tracker_(new PluginResourceTracker(
40 ALLOW_THIS_IN_INITIALIZER_LIST(this))),
41 plugin_var_tracker_(new PluginVarTracker(
42 ALLOW_THIS_IN_INITIALIZER_LIST(this))) {
[email protected]c2932f5e2010-11-03 03:22:3343 SetSerializationRules(
44 new PluginVarSerializationRules(plugin_var_tracker_.get()));
45
46 // As a plugin, we always support the PPP_Class interface. There's no
47 // GetInterface call or name for it, so we insert it into our table now.
48 InjectProxy(INTERFACE_ID_PPP_CLASS, "$Internal_PPP_Class",
49 new PPP_Class_Proxy(this));
50}
51
52PluginDispatcher::~PluginDispatcher() {
53 if (shutdown_module_)
54 shutdown_module_();
55}
56
57// static
58PluginDispatcher* PluginDispatcher::Get() {
59 return g_dispatcher;
60}
61
62// static
63void PluginDispatcher::SetGlobal(PluginDispatcher* dispatcher) {
64 DCHECK(!dispatcher || !g_dispatcher);
65 g_dispatcher = dispatcher;
66}
67
68void PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
69 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
70 // Handle some plugin-specific control messages.
71 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
72 IPC_MESSAGE_HANDLER(PpapiMsg_InitializeModule, OnInitializeModule)
73
74 // Forward all other control messages to the superclass.
75 IPC_MESSAGE_UNHANDLED(Dispatcher::OnMessageReceived(msg))
76 IPC_END_MESSAGE_MAP()
77 return;
78 }
79
80 // All non-control messages get handled by the superclass.
81 Dispatcher::OnMessageReceived(msg);
82}
83
84void PluginDispatcher::OnInitializeModule(PP_Module pp_module, bool* result) {
85 set_pp_module(pp_module);
86 *result = init_module_(pp_module, &GetInterfaceFromDispatcher) == PP_OK;
87}
88
89} // namespace proxy
90} // namespace pp
91