blob: 037b470241f7cdf089635995382472f767e6d524 [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),
39 shutdown_module_(shutdown_module),
[email protected]709a847e2010-11-10 01:16:1140 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]c2932f5e2010-11-03 03:22:3344 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
53PluginDispatcher::~PluginDispatcher() {
54 if (shutdown_module_)
55 shutdown_module_();
56}
57
58// static
59PluginDispatcher* PluginDispatcher::Get() {
60 return g_dispatcher;
61}
62
63// static
64void PluginDispatcher::SetGlobal(PluginDispatcher* dispatcher) {
65 DCHECK(!dispatcher || !g_dispatcher);
66 g_dispatcher = dispatcher;
67}
68
69void 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]176c73922010-12-03 17:32:1973 IPC_MESSAGE_HANDLER(PpapiMsg_InitializeModule, OnMsgInitializeModule)
74 IPC_MESSAGE_HANDLER(PpapiMsg_Shutdown, OnMsgShutdown)
[email protected]c2932f5e2010-11-03 03:22:3375
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]176c73922010-12-03 17:32:1986void PluginDispatcher::OnMsgInitializeModule(PP_Module pp_module,
87 bool* result) {
[email protected]c2932f5e2010-11-03 03:22:3388 set_pp_module(pp_module);
89 *result = init_module_(pp_module, &GetInterfaceFromDispatcher) == PP_OK;
90}
91
[email protected]176c73922010-12-03 17:32:1992void PluginDispatcher::OnMsgShutdown() {
93 if (shutdown_module_)
94 shutdown_module_();
95 MessageLoop::current()->Quit();
96}
97
[email protected]c2932f5e2010-11-03 03:22:3398} // namespace proxy
99} // namespace pp
100