summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppp_messaging_proxy.cc
blob: 59f4574d83e50fda3a5c8cbd9ec664735afb5032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/ppp_messaging_proxy.h"

#include <algorithm>

#include "ppapi/c/ppp_messaging.h"
#include "ppapi/proxy/host_dispatcher.h"
#include "ppapi/proxy/plugin_resource_tracker.h"
#include "ppapi/proxy/plugin_var_tracker.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/serialized_var.h"

namespace ppapi {
namespace proxy {

namespace {

void HandleMessage(PP_Instance instance, PP_Var message_data) {
  HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance);
  if (!dispatcher || (message_data.type == PP_VARTYPE_OBJECT)) {
    // The dispatcher should always be valid, and the browser should never send
    // an 'object' var over PPP_Messaging.
    NOTREACHED();
    return;
  }

  dispatcher->Send(new PpapiMsg_PPPMessaging_HandleMessage(
      INTERFACE_ID_PPP_MESSAGING,
      instance,
      SerializedVarSendInput(dispatcher, message_data)));
}

static const PPP_Messaging messaging_interface = {
  &HandleMessage
};

InterfaceProxy* CreateMessagingProxy(Dispatcher* dispatcher,
                                     const void* target_interface) {
  return new PPP_Messaging_Proxy(dispatcher, target_interface);
}

}  // namespace

PPP_Messaging_Proxy::PPP_Messaging_Proxy(Dispatcher* dispatcher,
                                         const void* target_interface)
    : InterfaceProxy(dispatcher, target_interface) {
}

PPP_Messaging_Proxy::~PPP_Messaging_Proxy() {
}

// static
const InterfaceProxy::Info* PPP_Messaging_Proxy::GetInfo() {
  static const Info info = {
    &messaging_interface,
    PPP_MESSAGING_INTERFACE,
    INTERFACE_ID_PPP_MESSAGING,
    false,
    &CreateMessagingProxy,
  };
  return &info;
}

bool PPP_Messaging_Proxy::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PPP_Messaging_Proxy, msg)
    IPC_MESSAGE_HANDLER(PpapiMsg_PPPMessaging_HandleMessage,
                        OnMsgHandleMessage)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void PPP_Messaging_Proxy::OnMsgHandleMessage(
    PP_Instance instance, SerializedVarReceiveInput message_data) {
  PP_Var received_var(message_data.Get(dispatcher()));
  // SerializedVarReceiveInput will decrement the reference count, but we want
  // to give the recipient a reference.
  PluginResourceTracker::GetInstance()->var_tracker().AddRefVar(received_var);
  ppp_messaging_target()->HandleMessage(instance, received_var);
}

}  // namespace proxy
}  // namespace ppapi