summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/plugin_dispatcher.cc
blob: 1997dc43eec5f5578d82e230be689ebd24b99ea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/plugin_dispatcher.h"

#include <map>

#include "base/logging.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_sync_channel.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/proxy/interface_proxy.h"
#include "ppapi/proxy/plugin_var_serialization_rules.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/ppp_class_proxy.h"

namespace pp {
namespace proxy {

namespace {

PluginDispatcher* g_dispatcher = NULL;

const void* GetInterfaceFromDispatcher(const char* interface) {
  // TODO(brettw) need some kind of lock for multi-thread access.
  return pp::proxy::PluginDispatcher::Get()->GetProxiedInterface(interface);
}

}  // namespace

PluginDispatcher::PluginDispatcher(GetInterfaceFunc get_interface,
                                   InitModuleFunc init_module,
                                   ShutdownModuleFunc shutdown_module)
    : Dispatcher(get_interface),
      init_module_(init_module),
      shutdown_module_(shutdown_module),
      plugin_resource_tracker_(new PluginResourceTracker(this)),
      plugin_var_tracker_(new PluginVarTracker(this)) {
  SetSerializationRules(
      new PluginVarSerializationRules(plugin_var_tracker_.get()));

  // As a plugin, we always support the PPP_Class interface. There's no
  // GetInterface call or name for it, so we insert it into our table now.
  InjectProxy(INTERFACE_ID_PPP_CLASS, "$Internal_PPP_Class",
              new PPP_Class_Proxy(this));
}

PluginDispatcher::~PluginDispatcher() {
  if (shutdown_module_)
    shutdown_module_();
}

// static
PluginDispatcher* PluginDispatcher::Get() {
  return g_dispatcher;
}

// static
void PluginDispatcher::SetGlobal(PluginDispatcher* dispatcher) {
  DCHECK(!dispatcher || !g_dispatcher);
  g_dispatcher = dispatcher;
}

void PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
  if (msg.routing_id() == MSG_ROUTING_CONTROL) {
    // Handle some plugin-specific control messages.
    IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
      IPC_MESSAGE_HANDLER(PpapiMsg_InitializeModule, OnInitializeModule)

      // Forward all other control messages to the superclass.
      IPC_MESSAGE_UNHANDLED(Dispatcher::OnMessageReceived(msg))
    IPC_END_MESSAGE_MAP()
    return;
  }

  // All non-control messages get handled by the superclass.
  Dispatcher::OnMessageReceived(msg);
}

void PluginDispatcher::OnInitializeModule(PP_Module pp_module, bool* result) {
  set_pp_module(pp_module);
  *result = init_module_(pp_module, &GetInterfaceFromDispatcher) == PP_OK;
}

}  // namespace proxy
}  // namespace pp