blob: 1d8614da38694c183a9b76029427799780ff17af [file] [log] [blame]
[email protected]b697e1a2011-01-06 22:20:281// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]c2932f5e2010-11-03 03:22:332// 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/dispatcher.h"
6
7#include <string.h> // For memset.
8
9#include <map>
10
[email protected]709a847e2010-11-10 01:16:1111#include "base/compiler_specific.h"
[email protected]c2932f5e2010-11-03 03:22:3312#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/singleton.h"
[email protected]9ca952d2010-11-11 20:43:5014#include "ppapi/proxy/ppapi_messages.h"
[email protected]c2932f5e2010-11-03 03:22:3315#include "ppapi/proxy/var_serialization_rules.h"
16
[email protected]4d2efd22011-08-18 21:58:0217namespace ppapi {
[email protected]c2932f5e2010-11-03 03:22:3318namespace proxy {
19
[email protected]5d84d012010-12-02 17:17:2120Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle,
21 GetInterfaceFunc local_get_interface)
[email protected]e2614c62011-04-16 22:12:4522 : ProxyChannel(remote_process_handle),
[email protected]5f2517612010-12-02 22:36:4823 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
[email protected]c2932f5e2010-11-03 03:22:3324 local_get_interface_(local_get_interface),
[email protected]709a847e2010-11-10 01:16:1125 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:3326}
27
28Dispatcher::~Dispatcher() {
29}
30
[email protected]ac4b54d2011-10-20 23:09:2831InterfaceProxy* Dispatcher::GetInterfaceProxy(ApiID id) {
[email protected]5c966022011-09-13 18:09:3732 InterfaceProxy* proxy = proxies_[id].get();
33 if (!proxy) {
34 // Handle the first time for a given API by creating the proxy for it.
35 InterfaceProxy::Factory factory =
36 InterfaceList::GetInstance()->GetFactoryForID(id);
37 if (!factory) {
38 NOTREACHED();
39 return NULL;
40 }
41 proxy = factory(this);
42 DCHECK(proxy);
43 proxies_[id].reset(proxy);
44 }
45 return proxy;
46}
47
48base::MessageLoopProxy* Dispatcher::GetIPCMessageLoop() {
49 return delegate()->GetIPCMessageLoop();
50}
51
52void Dispatcher::AddIOThreadMessageFilter(
53 IPC::ChannelProxy::MessageFilter* filter) {
54 channel()->AddFilter(filter);
55}
56
[email protected]a95986a82010-12-24 06:19:2857bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]c2932f5e2010-11-03 03:22:3358 // Control messages.
59 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
[email protected]a95986a82010-12-24 06:19:2860 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:3361 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
[email protected]c2932f5e2010-11-03 03:22:3362 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
63 CallbackTracker::ReceiveExecuteSerializedCallback)
[email protected]a95986a82010-12-24 06:19:2864 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:3365 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:2866 return handled;
[email protected]c2932f5e2010-11-03 03:22:3367 }
[email protected]c2932f5e2010-11-03 03:22:3368
[email protected]ac4b54d2011-10-20 23:09:2869 if (msg.routing_id() <= 0 || msg.routing_id() >= API_ID_COUNT) {
[email protected]5c966022011-09-13 18:09:3770 OnInvalidMessageReceived();
71 return true;
72 }
[email protected]465faa22011-02-08 16:31:4673
[email protected]5c966022011-09-13 18:09:3774 InterfaceProxy* proxy = GetInterfaceProxy(
[email protected]ac4b54d2011-10-20 23:09:2875 static_cast<ApiID>(msg.routing_id()));
[email protected]5c966022011-09-13 18:09:3776 if (!proxy) {
77 NOTREACHED();
78 return true;
79 }
80 return proxy->OnMessageReceived(msg);
[email protected]465faa22011-02-08 16:31:4681}
82
[email protected]c2932f5e2010-11-03 03:22:3383void Dispatcher::SetSerializationRules(
84 VarSerializationRules* var_serialization_rules) {
85 serialization_rules_.reset(var_serialization_rules);
86}
87
[email protected]5c966022011-09-13 18:09:3788void Dispatcher::OnInvalidMessageReceived() {
[email protected]c2932f5e2010-11-03 03:22:3389}
90
[email protected]c2932f5e2010-11-03 03:22:3391} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:0292} // namespace ppapi