blob: 390794a8d237979f62294c1584af725a8e60b896 [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
[email protected]4d2efd22011-08-18 21:58:0216namespace ppapi {
[email protected]b20df1c2011-08-03 14:38:2417namespace 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
[email protected]5c966022011-09-13 18:09:3740InterfaceProxy* CreateMessagingProxy(Dispatcher* dispatcher) {
41 return new PPP_Messaging_Proxy(dispatcher);
[email protected]b20df1c2011-08-03 14:38:2442}
43
44} // namespace
45
[email protected]5c966022011-09-13 18:09:3746PPP_Messaging_Proxy::PPP_Messaging_Proxy(Dispatcher* dispatcher)
47 : InterfaceProxy(dispatcher),
48 ppp_messaging_impl_(NULL) {
49 if (dispatcher->IsPlugin()) {
50 ppp_messaging_impl_ = static_cast<const PPP_Messaging*>(
51 dispatcher->local_get_interface()(PPP_MESSAGING_INTERFACE));
52 }
[email protected]b20df1c2011-08-03 14:38:2453}
54
55PPP_Messaging_Proxy::~PPP_Messaging_Proxy() {
56}
57
58// static
59const InterfaceProxy::Info* PPP_Messaging_Proxy::GetInfo() {
60 static const Info info = {
61 &messaging_interface,
62 PPP_MESSAGING_INTERFACE,
63 INTERFACE_ID_PPP_MESSAGING,
64 false,
65 &CreateMessagingProxy,
66 };
67 return &info;
68}
69
70bool PPP_Messaging_Proxy::OnMessageReceived(const IPC::Message& msg) {
71 bool handled = true;
72 IPC_BEGIN_MESSAGE_MAP(PPP_Messaging_Proxy, msg)
73 IPC_MESSAGE_HANDLER(PpapiMsg_PPPMessaging_HandleMessage,
74 OnMsgHandleMessage)
75 IPC_MESSAGE_UNHANDLED(handled = false)
76 IPC_END_MESSAGE_MAP()
77 return handled;
78}
79
80void PPP_Messaging_Proxy::OnMsgHandleMessage(
81 PP_Instance instance, SerializedVarReceiveInput message_data) {
82 PP_Var received_var(message_data.Get(dispatcher()));
83 // SerializedVarReceiveInput will decrement the reference count, but we want
84 // to give the recipient a reference.
[email protected]2bbd2c672011-08-09 23:14:1385 PluginResourceTracker::GetInstance()->var_tracker().AddRefVar(received_var);
[email protected]5c966022011-09-13 18:09:3786 ppp_messaging_impl_->HandleMessage(instance, received_var);
[email protected]b20df1c2011-08-03 14:38:2487}
88
89} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:0290} // namespace ppapi