[email protected] | 67600b9 | 2012-03-10 06:51:48 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | 0f41c01 | 2011-10-21 19:49:20 | [diff] [blame] | 24 | local_get_interface_(local_get_interface) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 25 | } |
26 | |||||
27 | Dispatcher::~Dispatcher() { | ||||
28 | } | ||||
29 | |||||
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 30 | InterfaceProxy* Dispatcher::GetInterfaceProxy(ApiID id) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 31 | InterfaceProxy* proxy = proxies_[id].get(); |
32 | if (!proxy) { | ||||
33 | // Handle the first time for a given API by creating the proxy for it. | ||||
34 | InterfaceProxy::Factory factory = | ||||
35 | InterfaceList::GetInstance()->GetFactoryForID(id); | ||||
36 | if (!factory) { | ||||
37 | NOTREACHED(); | ||||
38 | return NULL; | ||||
39 | } | ||||
40 | proxy = factory(this); | ||||
41 | DCHECK(proxy); | ||||
42 | proxies_[id].reset(proxy); | ||||
43 | } | ||||
44 | return proxy; | ||||
45 | } | ||||
46 | |||||
47 | base::MessageLoopProxy* Dispatcher::GetIPCMessageLoop() { | ||||
48 | return delegate()->GetIPCMessageLoop(); | ||||
49 | } | ||||
50 | |||||
51 | void Dispatcher::AddIOThreadMessageFilter( | ||||
52 | IPC::ChannelProxy::MessageFilter* filter) { | ||||
[email protected] | 8be4584 | 2012-04-13 19:49:29 | [diff] [blame^] | 53 | // Our filter is refcounted. The channel will call the destruct method on the |
54 | // filter when the channel is done with it, so the corresponding Release() | ||||
55 | // happens there. | ||||
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 56 | channel()->AddFilter(filter); |
57 | } | ||||
58 | |||||
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 59 | bool Dispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 60 | if (msg.routing_id() <= 0 || msg.routing_id() >= API_ID_COUNT) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 61 | OnInvalidMessageReceived(); |
62 | return true; | ||||
63 | } | ||||
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 64 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 65 | InterfaceProxy* proxy = GetInterfaceProxy( |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 66 | static_cast<ApiID>(msg.routing_id())); |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 67 | if (!proxy) { |
68 | NOTREACHED(); | ||||
69 | return true; | ||||
70 | } | ||||
71 | return proxy->OnMessageReceived(msg); | ||||
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 72 | } |
73 | |||||
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 74 | void Dispatcher::SetSerializationRules( |
75 | VarSerializationRules* var_serialization_rules) { | ||||
[email protected] | 67600b9 | 2012-03-10 06:51:48 | [diff] [blame] | 76 | serialization_rules_ = var_serialization_rules; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 77 | } |
78 | |||||
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 79 | void Dispatcher::OnInvalidMessageReceived() { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 80 | } |
81 | |||||
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 82 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 83 | } // namespace ppapi |