ppp_messaging_proxy.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 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_resource_tracker.h"
12#include "ppapi/proxy/plugin_var_tracker.h"
13#include "ppapi/proxy/ppapi_messages.h"
14#include "ppapi/proxy/serialized_var.h"
15#include "ppapi/shared_impl/ppapi_globals.h"
16#include "ppapi/shared_impl/proxy_lock.h"
17#include "ppapi/shared_impl/var_tracker.h"
18
19namespace ppapi {
20namespace proxy {
21
22namespace {
23
24void HandleMessage(PP_Instance instance, PP_Var message_data) {
25  HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance);
26  if (!dispatcher || (message_data.type == PP_VARTYPE_OBJECT)) {
27    // The dispatcher should always be valid, and the browser should never send
28    // an 'object' var over PPP_Messaging.
29    NOTREACHED();
30    return;
31  }
32
33  dispatcher->Send(new PpapiMsg_PPPMessaging_HandleMessage(
34      API_ID_PPP_MESSAGING,
35      instance,
36      SerializedVarSendInput(dispatcher, message_data)));
37}
38
39static const PPP_Messaging messaging_interface = {
40  &HandleMessage
41};
42
43InterfaceProxy* CreateMessagingProxy(Dispatcher* dispatcher) {
44  return new PPP_Messaging_Proxy(dispatcher);
45}
46
47}  // namespace
48
49PPP_Messaging_Proxy::PPP_Messaging_Proxy(Dispatcher* dispatcher)
50    : InterfaceProxy(dispatcher),
51      ppp_messaging_impl_(NULL) {
52  if (dispatcher->IsPlugin()) {
53    ppp_messaging_impl_ = static_cast<const PPP_Messaging*>(
54        dispatcher->local_get_interface()(PPP_MESSAGING_INTERFACE));
55  }
56}
57
58PPP_Messaging_Proxy::~PPP_Messaging_Proxy() {
59}
60
61// static
62const InterfaceProxy::Info* PPP_Messaging_Proxy::GetInfo() {
63  static const Info info = {
64    &messaging_interface,
65    PPP_MESSAGING_INTERFACE,
66    API_ID_PPP_MESSAGING,
67    false,
68    &CreateMessagingProxy,
69  };
70  return &info;
71}
72
73bool PPP_Messaging_Proxy::OnMessageReceived(const IPC::Message& msg) {
74  bool handled = true;
75  IPC_BEGIN_MESSAGE_MAP(PPP_Messaging_Proxy, msg)
76    IPC_MESSAGE_HANDLER(PpapiMsg_PPPMessaging_HandleMessage,
77                        OnMsgHandleMessage)
78    IPC_MESSAGE_UNHANDLED(handled = false)
79  IPC_END_MESSAGE_MAP()
80  return handled;
81}
82
83void PPP_Messaging_Proxy::OnMsgHandleMessage(
84    PP_Instance instance, SerializedVarReceiveInput message_data) {
85  PP_Var received_var(message_data.Get(dispatcher()));
86  // SerializedVarReceiveInput will decrement the reference count, but we want
87  // to give the recipient a reference.
88  PpapiGlobals::Get()->GetVarTracker()->AddRefVar(received_var);
89  CallWhileUnlocked(ppp_messaging_impl_->HandleMessage,
90                    instance,
91                    received_var);
92}
93
94}  // namespace proxy
95}  // namespace ppapi
96