[email protected] | b697e1a | 2011-01-06 22:20:28 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [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/dispatcher.h" | ||||
6 | |||||
7 | #include <string.h> // For memset. | ||||
8 | |||||
9 | #include <map> | ||||
10 | |||||
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/singleton.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 14 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 15 | #include "ppapi/proxy/var_serialization_rules.h" |
16 | |||||
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 17 | namespace ppapi { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 18 | namespace proxy { |
19 | |||||
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 20 | Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle, |
21 | GetInterfaceFunc local_get_interface) | ||||
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 22 | : ProxyChannel(remote_process_handle), |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 23 | disallow_trusted_interfaces_(false), // TODO(brettw) make this settable. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 24 | local_get_interface_(local_get_interface), |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 25 | callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 26 | } |
27 | |||||
28 | Dispatcher::~Dispatcher() { | ||||
29 | } | ||||
30 | |||||
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame^] | 31 | InterfaceProxy* Dispatcher::GetInterfaceProxy(InterfaceID id) { |
32 | 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 | |||||
48 | base::MessageLoopProxy* Dispatcher::GetIPCMessageLoop() { | ||||
49 | return delegate()->GetIPCMessageLoop(); | ||||
50 | } | ||||
51 | |||||
52 | void Dispatcher::AddIOThreadMessageFilter( | ||||
53 | IPC::ChannelProxy::MessageFilter* filter) { | ||||
54 | channel()->AddFilter(filter); | ||||
55 | } | ||||
56 | |||||
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 57 | bool Dispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 58 | // Control messages. |
59 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { | ||||
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 60 | bool handled = true; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 61 | IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 62 | IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_, |
63 | CallbackTracker::ReceiveExecuteSerializedCallback) | ||||
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 64 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 65 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 66 | return handled; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 67 | } |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 68 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame^] | 69 | if (msg.routing_id() <= 0 || msg.routing_id() >= INTERFACE_ID_COUNT) { |
70 | OnInvalidMessageReceived(); | ||||
71 | return true; | ||||
72 | } | ||||
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 73 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame^] | 74 | InterfaceProxy* proxy = GetInterfaceProxy( |
75 | static_cast<InterfaceID>(msg.routing_id())); | ||||
76 | if (!proxy) { | ||||
77 | NOTREACHED(); | ||||
78 | return true; | ||||
79 | } | ||||
80 | return proxy->OnMessageReceived(msg); | ||||
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 81 | } |
82 | |||||
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 83 | void Dispatcher::SetSerializationRules( |
84 | VarSerializationRules* var_serialization_rules) { | ||||
85 | serialization_rules_.reset(var_serialization_rules); | ||||
86 | } | ||||
87 | |||||
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame^] | 88 | void Dispatcher::OnInvalidMessageReceived() { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 89 | } |
90 | |||||
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 91 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 92 | } // namespace ppapi |