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