blob: 5a12419f78ae9f3291697d41e8a904559691d7a8 [file] [log] [blame]
[email protected]4c41d3f2012-02-15 01:44:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b20df1c2011-08-03 14:38:242// 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"
[email protected]794d83cd2011-10-20 19:09:2015#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]4c41d3f2012-02-15 01:44:4716#include "ppapi/shared_impl/proxy_lock.h"
[email protected]794d83cd2011-10-20 19:09:2017#include "ppapi/shared_impl/var_tracker.h"
[email protected]b20df1c2011-08-03 14:38:2418
[email protected]4d2efd22011-08-18 21:58:0219namespace ppapi {
[email protected]b20df1c2011-08-03 14:38:2420namespace proxy {
21
22namespace {
23
[email protected]24d70dea32012-11-19 22:18:2024#if !defined(OS_NACL)
[email protected]b20df1c2011-08-03 14:38:2425void HandleMessage(PP_Instance instance, PP_Var message_data) {
26 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance);
27 if (!dispatcher || (message_data.type == PP_VARTYPE_OBJECT)) {
28 // The dispatcher should always be valid, and the browser should never send
29 // an 'object' var over PPP_Messaging.
30 NOTREACHED();
31 return;
32 }
33
34 dispatcher->Send(new PpapiMsg_PPPMessaging_HandleMessage(
[email protected]ac4b54d2011-10-20 23:09:2835 API_ID_PPP_MESSAGING,
[email protected]b20df1c2011-08-03 14:38:2436 instance,
[email protected]30e1cb752013-03-19 20:42:3337 SerializedVarSendInputShmem(dispatcher, message_data, instance)));
[email protected]b20df1c2011-08-03 14:38:2438}
39
40static const PPP_Messaging messaging_interface = {
41 &HandleMessage
42};
[email protected]24d70dea32012-11-19 22:18:2043#else
44// The NaCl plugin doesn't need the host side interface - stub it out.
45static const PPP_Messaging messaging_interface = {};
46#endif // !defined(OS_NACL)
[email protected]b20df1c2011-08-03 14:38:2447
[email protected]5c966022011-09-13 18:09:3748InterfaceProxy* CreateMessagingProxy(Dispatcher* dispatcher) {
49 return new PPP_Messaging_Proxy(dispatcher);
[email protected]b20df1c2011-08-03 14:38:2450}
51
52} // namespace
53
[email protected]5c966022011-09-13 18:09:3754PPP_Messaging_Proxy::PPP_Messaging_Proxy(Dispatcher* dispatcher)
55 : InterfaceProxy(dispatcher),
56 ppp_messaging_impl_(NULL) {
57 if (dispatcher->IsPlugin()) {
58 ppp_messaging_impl_ = static_cast<const PPP_Messaging*>(
59 dispatcher->local_get_interface()(PPP_MESSAGING_INTERFACE));
60 }
[email protected]b20df1c2011-08-03 14:38:2461}
62
63PPP_Messaging_Proxy::~PPP_Messaging_Proxy() {
64}
65
66// static
67const InterfaceProxy::Info* PPP_Messaging_Proxy::GetInfo() {
68 static const Info info = {
69 &messaging_interface,
70 PPP_MESSAGING_INTERFACE,
[email protected]ac4b54d2011-10-20 23:09:2871 API_ID_PPP_MESSAGING,
[email protected]b20df1c2011-08-03 14:38:2472 false,
73 &CreateMessagingProxy,
74 };
75 return &info;
76}
77
78bool PPP_Messaging_Proxy::OnMessageReceived(const IPC::Message& msg) {
[email protected]d5f5dc12013-01-22 23:05:0379 if (!dispatcher()->IsPlugin())
80 return false;
81
[email protected]b20df1c2011-08-03 14:38:2482 bool handled = true;
83 IPC_BEGIN_MESSAGE_MAP(PPP_Messaging_Proxy, msg)
84 IPC_MESSAGE_HANDLER(PpapiMsg_PPPMessaging_HandleMessage,
85 OnMsgHandleMessage)
86 IPC_MESSAGE_UNHANDLED(handled = false)
87 IPC_END_MESSAGE_MAP()
88 return handled;
89}
90
91void PPP_Messaging_Proxy::OnMsgHandleMessage(
92 PP_Instance instance, SerializedVarReceiveInput message_data) {
[email protected]30e1cb752013-03-19 20:42:3393 PP_Var received_var(message_data.GetForInstance(dispatcher(), instance));
[email protected]b20df1c2011-08-03 14:38:2494 // SerializedVarReceiveInput will decrement the reference count, but we want
95 // to give the recipient a reference.
[email protected]794d83cd2011-10-20 19:09:2096 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(received_var);
[email protected]4c41d3f2012-02-15 01:44:4797 CallWhileUnlocked(ppp_messaging_impl_->HandleMessage,
98 instance,
99 received_var);
[email protected]b20df1c2011-08-03 14:38:24100}
101
102} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02103} // namespace ppapi