1 // Copyright (c) 2011 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.
5 #include "ppapi/proxy/plugin_dispatcher.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/message_loop.h"
12 #include "ipc/ipc_message.h"
13 #include "ipc/ipc_sync_channel.h"
14 #include "base/debug/trace_event.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/proxy/interface_list.h"
17 #include "ppapi/proxy/interface_proxy.h"
18 #include "ppapi/proxy/plugin_message_filter.h"
19 #include "ppapi/proxy/plugin_resource_tracker.h"
20 #include "ppapi/proxy/plugin_var_serialization_rules.h"
21 #include "ppapi/proxy/ppapi_messages.h"
22 #include "ppapi/proxy/ppb_cursor_control_proxy.h"
23 #include "ppapi/proxy/ppb_font_proxy.h"
24 #include "ppapi/proxy/ppb_instance_proxy.h"
25 #include "ppapi/proxy/ppp_class_proxy.h"
26 #include "ppapi/proxy/resource_creation_proxy.h"
27 #include "ppapi/shared_impl/resource.h"
30 #include "base/eintr_wrapper.h"
31 #include "ipc/ipc_channel_posix.h"
39 typedef std::map
<PP_Instance
, PluginDispatcher
*> InstanceToDispatcherMap
;
40 InstanceToDispatcherMap
* g_instance_to_dispatcher
= NULL
;
44 InstanceData::InstanceData()
45 : fullscreen(PP_FALSE
),
46 flash_fullscreen(PP_FALSE
),
47 mouse_lock_callback(PP_BlockUntilComplete()) {
48 memset(&position
, 0, sizeof(position
));
51 InstanceData::~InstanceData() {
52 // Run any pending mouse lock callback to prevent leaks.
53 if (mouse_lock_callback
.func
)
54 PP_RunAndClearCompletionCallback(&mouse_lock_callback
, PP_ERROR_ABORTED
);
57 PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle
,
58 GetInterfaceFunc get_interface
)
59 : Dispatcher(remote_process_handle
, get_interface
),
60 plugin_delegate_(NULL
),
61 received_preferences_(false),
62 plugin_dispatcher_id_(0) {
63 SetSerializationRules(new PluginVarSerializationRules
);
66 PluginDispatcher::~PluginDispatcher() {
68 plugin_delegate_
->Unregister(plugin_dispatcher_id_
);
72 PluginDispatcher
* PluginDispatcher::GetForInstance(PP_Instance instance
) {
73 if (!g_instance_to_dispatcher
)
75 InstanceToDispatcherMap::iterator found
= g_instance_to_dispatcher
->find(
77 if (found
== g_instance_to_dispatcher
->end())
83 PluginDispatcher
* PluginDispatcher::GetForResource(const Resource
* resource
) {
84 return GetForInstance(resource
->pp_instance());
88 const void* PluginDispatcher::GetBrowserInterface(const char* interface_name
) {
89 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name
);
92 const void* PluginDispatcher::GetPluginInterface(
93 const std::string
& interface_name
) {
94 InterfaceMap::iterator found
= plugin_interfaces_
.find(interface_name
);
95 if (found
== plugin_interfaces_
.end()) {
96 const void* ret
= local_get_interface()(interface_name
.c_str());
97 plugin_interfaces_
.insert(std::make_pair(interface_name
, ret
));
100 return found
->second
;
103 bool PluginDispatcher::InitPluginWithChannel(
104 PluginDelegate
* delegate
,
105 const IPC::ChannelHandle
& channel_handle
,
107 if (!Dispatcher::InitWithChannel(delegate
, channel_handle
, is_client
))
109 plugin_delegate_
= delegate
;
110 plugin_dispatcher_id_
= plugin_delegate_
->Register(this);
112 // The message filter will intercept and process certain messages directly
113 // on the I/O thread.
114 channel()->AddFilter(
115 new PluginMessageFilter(delegate
->GetGloballySeenInstanceIDSet()));
119 bool PluginDispatcher::IsPlugin() const {
123 bool PluginDispatcher::Send(IPC::Message
* msg
) {
124 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
125 "Class", IPC_MESSAGE_ID_CLASS(msg
->type()),
126 "Line", IPC_MESSAGE_ID_LINE(msg
->type()));
127 // We always want plugin->renderer messages to arrive in-order. If some sync
128 // and some async messages are send in response to a synchronous
129 // renderer->plugin call, the sync reply will be processed before the async
130 // reply, and everything will be confused.
132 // Allowing all async messages to unblock the renderer means more reentrancy
133 // there but gives correct ordering.
134 msg
->set_unblock(true);
135 return Dispatcher::Send(msg
);
138 bool PluginDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
139 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
140 "Class", IPC_MESSAGE_ID_CLASS(msg
.type()),
141 "Line", IPC_MESSAGE_ID_LINE(msg
.type()));
142 if (msg
.routing_id() == MSG_ROUTING_CONTROL
) {
143 // Handle some plugin-specific control messages.
145 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher
, msg
)
146 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface
, OnMsgSupportsInterface
)
147 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences
, OnMsgSetPreferences
)
148 IPC_MESSAGE_UNHANDLED(handled
= false);
149 IPC_END_MESSAGE_MAP()
153 return Dispatcher::OnMessageReceived(msg
);
156 void PluginDispatcher::OnChannelError() {
157 Dispatcher::OnChannelError();
159 // The renderer has crashed or exited. This channel and all instances
160 // associated with it are no longer valid.
161 ForceFreeAllInstances();
162 // TODO(brettw) free resources too!
166 void PluginDispatcher::DidCreateInstance(PP_Instance instance
) {
167 if (!g_instance_to_dispatcher
)
168 g_instance_to_dispatcher
= new InstanceToDispatcherMap
;
169 (*g_instance_to_dispatcher
)[instance
] = this;
171 instance_map_
[instance
] = InstanceData();
174 void PluginDispatcher::DidDestroyInstance(PP_Instance instance
) {
175 InstanceDataMap::iterator it
= instance_map_
.find(instance
);
176 if (it
!= instance_map_
.end())
177 instance_map_
.erase(it
);
179 if (g_instance_to_dispatcher
) {
180 InstanceToDispatcherMap::iterator found
= g_instance_to_dispatcher
->find(
182 if (found
!= g_instance_to_dispatcher
->end()) {
183 DCHECK(found
->second
== this);
184 g_instance_to_dispatcher
->erase(found
);
191 InstanceData
* PluginDispatcher::GetInstanceData(PP_Instance instance
) {
192 InstanceDataMap::iterator it
= instance_map_
.find(instance
);
193 return (it
== instance_map_
.end()) ? NULL
: &it
->second
;
196 void PluginDispatcher::PostToWebKitThread(
197 const tracked_objects::Location
& from_here
,
198 const base::Closure
& task
) {
199 return plugin_delegate_
->PostToWebKitThread(from_here
, task
);
202 bool PluginDispatcher::SendToBrowser(IPC::Message
* msg
) {
203 return plugin_delegate_
->SendToBrowser(msg
);
206 WebKitForwarding
* PluginDispatcher::GetWebKitForwarding() {
207 return plugin_delegate_
->GetWebKitForwarding();
210 FunctionGroupBase
* PluginDispatcher::GetFunctionAPI(ApiID id
) {
211 return GetInterfaceProxy(id
);
214 void PluginDispatcher::ForceFreeAllInstances() {
215 if (!g_instance_to_dispatcher
)
218 // Iterating will remove each item from the map, so we need to make a copy
219 // to avoid things changing out from under is.
220 InstanceToDispatcherMap temp_map
= *g_instance_to_dispatcher
;
221 for (InstanceToDispatcherMap::iterator i
= temp_map
.begin();
222 i
!= temp_map
.end(); ++i
) {
223 if (i
->second
== this) {
224 // Synthesize an "instance destroyed" message, this will notify the
225 // plugin and also remove it from our list of tracked plugins.
226 PpapiMsg_PPPInstance_DidDestroy
msg(API_ID_PPP_INSTANCE
, i
->first
);
227 OnMessageReceived(msg
);
232 void PluginDispatcher::OnMsgSupportsInterface(
233 const std::string
& interface_name
,
235 *result
= !!GetPluginInterface(interface_name
);
238 void PluginDispatcher::OnMsgSetPreferences(const Preferences
& prefs
) {
239 // The renderer may send us preferences more than once (currently this
240 // happens every time a new plugin instance is created). Since we don't have
241 // a way to signal to the plugin that the preferences have changed, changing
242 // the default fonts and such in the middle of a running plugin could be
243 // confusing to it. As a result, we never allow the preferences to be changed
244 // once they're set. The user will have to restart to get new font prefs
245 // propogated to plugins.
246 if (!received_preferences_
) {
247 received_preferences_
= true;
248 preferences_
= prefs
;