blob: a0f467f90789b73c3f0a753726378faef5111f9a [file] [log] [blame]
[email protected]b20df1c2011-08-03 14:38:241// Copyright (c) 2011 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/ppp_messaging_proxy.h"
6
7#include <algorithm>
8
9#include "ppapi/c/ppp_messaging.h"
10#include "ppapi/proxy/host_dispatcher.h"
[email protected]2bbd2c672011-08-09 23:14:1311#include "ppapi/proxy/plugin_resource_tracker.h"
[email protected]b20df1c2011-08-03 14:38:2412#include "ppapi/proxy/plugin_var_tracker.h"
13#include "ppapi/proxy/ppapi_messages.h"
14#include "ppapi/proxy/serialized_var.h"
15
16namespace pp {
17namespace proxy {
18
19namespace {
20
21void HandleMessage(PP_Instance instance, PP_Var message_data) {
22 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance);
23 if (!dispatcher || (message_data.type == PP_VARTYPE_OBJECT)) {
24 // The dispatcher should always be valid, and the browser should never send
25 // an 'object' var over PPP_Messaging.
26 NOTREACHED();
27 return;
28 }
29
30 dispatcher->Send(new PpapiMsg_PPPMessaging_HandleMessage(
31 INTERFACE_ID_PPP_MESSAGING,
32 instance,
33 SerializedVarSendInput(dispatcher, message_data)));
34}
35
36static const PPP_Messaging messaging_interface = {
37 &HandleMessage
38};
39
40InterfaceProxy* CreateMessagingProxy(Dispatcher* dispatcher,
41 const void* target_interface) {
42 return new PPP_Messaging_Proxy(dispatcher, target_interface);
43}
44
45} // namespace
46
47PPP_Messaging_Proxy::PPP_Messaging_Proxy(Dispatcher* dispatcher,
48 const void* target_interface)
49 : InterfaceProxy(dispatcher, target_interface) {
50}
51
52PPP_Messaging_Proxy::~PPP_Messaging_Proxy() {
53}
54
55// static
56const InterfaceProxy::Info* PPP_Messaging_Proxy::GetInfo() {
57 static const Info info = {
58 &messaging_interface,
59 PPP_MESSAGING_INTERFACE,
60 INTERFACE_ID_PPP_MESSAGING,
61 false,
62 &CreateMessagingProxy,
63 };
64 return &info;
65}
66
67bool PPP_Messaging_Proxy::OnMessageReceived(const IPC::Message& msg) {
68 bool handled = true;
69 IPC_BEGIN_MESSAGE_MAP(PPP_Messaging_Proxy, msg)
70 IPC_MESSAGE_HANDLER(PpapiMsg_PPPMessaging_HandleMessage,
71 OnMsgHandleMessage)
72 IPC_MESSAGE_UNHANDLED(handled = false)
73 IPC_END_MESSAGE_MAP()
74 return handled;
75}
76
77void PPP_Messaging_Proxy::OnMsgHandleMessage(
78 PP_Instance instance, SerializedVarReceiveInput message_data) {
79 PP_Var received_var(message_data.Get(dispatcher()));
80 // SerializedVarReceiveInput will decrement the reference count, but we want
81 // to give the recipient a reference.
[email protected]2bbd2c672011-08-09 23:14:1382 PluginResourceTracker::GetInstance()->var_tracker().AddRefVar(received_var);
[email protected]b20df1c2011-08-03 14:38:2483 ppp_messaging_target()->HandleMessage(instance, received_var);
84}
85
86} // namespace proxy
87} // namespace pp