[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 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] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 11 | #include "ppapi/proxy/plugin_resource_tracker.h" |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 12 | #include "ppapi/proxy/plugin_var_tracker.h" |
| 13 | #include "ppapi/proxy/ppapi_messages.h" |
| 14 | #include "ppapi/proxy/serialized_var.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 15 | #include "ppapi/shared_impl/ppapi_globals.h" |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 16 | #include "ppapi/shared_impl/proxy_lock.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 17 | #include "ppapi/shared_impl/var_tracker.h" |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 18 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 19 | namespace ppapi { |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 20 | namespace proxy { |
| 21 | |
| 22 | namespace { |
| 23 | |
[email protected] | 24d70dea3 | 2012-11-19 22:18:20 | [diff] [blame] | 24 | #if !defined(OS_NACL) |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 25 | void 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] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 35 | API_ID_PPP_MESSAGING, |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 36 | instance, |
[email protected] | 30e1cb75 | 2013-03-19 20:42:33 | [diff] [blame] | 37 | SerializedVarSendInputShmem(dispatcher, message_data, instance))); |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static const PPP_Messaging messaging_interface = { |
| 41 | &HandleMessage |
| 42 | }; |
[email protected] | 24d70dea3 | 2012-11-19 22:18:20 | [diff] [blame] | 43 | #else |
| 44 | // The NaCl plugin doesn't need the host side interface - stub it out. |
| 45 | static const PPP_Messaging messaging_interface = {}; |
| 46 | #endif // !defined(OS_NACL) |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 47 | |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 48 | } // namespace |
| 49 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 50 | PPP_Messaging_Proxy::PPP_Messaging_Proxy(Dispatcher* dispatcher) |
| 51 | : InterfaceProxy(dispatcher), |
| 52 | ppp_messaging_impl_(NULL) { |
| 53 | if (dispatcher->IsPlugin()) { |
| 54 | ppp_messaging_impl_ = static_cast<const PPP_Messaging*>( |
| 55 | dispatcher->local_get_interface()(PPP_MESSAGING_INTERFACE)); |
| 56 | } |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | PPP_Messaging_Proxy::~PPP_Messaging_Proxy() { |
| 60 | } |
| 61 | |
| 62 | // static |
[email protected] | 6642ebf | 2013-12-17 20:49:30 | [diff] [blame] | 63 | const PPP_Messaging* PPP_Messaging_Proxy::GetProxyInterface() { |
| 64 | return &messaging_interface; |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool PPP_Messaging_Proxy::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | d5f5dc1 | 2013-01-22 23:05:03 | [diff] [blame] | 68 | if (!dispatcher()->IsPlugin()) |
| 69 | return false; |
| 70 | |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 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 | |
| 80 | void PPP_Messaging_Proxy::OnMsgHandleMessage( |
| 81 | PP_Instance instance, SerializedVarReceiveInput message_data) { |
[email protected] | 30e1cb75 | 2013-03-19 20:42:33 | [diff] [blame] | 82 | PP_Var received_var(message_data.GetForInstance(dispatcher(), instance)); |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 83 | // SerializedVarReceiveInput will decrement the reference count, but we want |
| 84 | // to give the recipient a reference. |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 85 | PpapiGlobals::Get()->GetVarTracker()->AddRefVar(received_var); |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 86 | CallWhileUnlocked(ppp_messaging_impl_->HandleMessage, |
| 87 | instance, |
| 88 | received_var); |
[email protected] | b20df1c | 2011-08-03 14:38:24 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 92 | } // namespace ppapi |