[email protected] | a08ebea | 2011-02-13 17:50:20 | [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/plugin_dispatcher.h" |
| 6 | |
| 7 | #include <map> |
| 8 | |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | aabfa65 | 2011-07-13 23:28:44 | [diff] [blame] | 11 | #include "base/message_loop.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 12 | #include "ipc/ipc_message.h" |
| 13 | #include "ipc/ipc_sync_channel.h" |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 14 | #include "base/debug/trace_event.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 15 | #include "ppapi/c/pp_errors.h" |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 16 | #include "ppapi/proxy/interface_list.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 17 | #include "ppapi/proxy/interface_proxy.h" |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 18 | #include "ppapi/proxy/plugin_message_filter.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 19 | #include "ppapi/proxy/plugin_resource_tracker.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 20 | #include "ppapi/proxy/plugin_var_serialization_rules.h" |
| 21 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 22 | #include "ppapi/proxy/ppb_cursor_control_proxy.h" |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 23 | #include "ppapi/proxy/ppb_font_proxy.h" |
[email protected] | ceadc39 | 2011-06-15 23:04:24 | [diff] [blame] | 24 | #include "ppapi/proxy/ppb_instance_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 25 | #include "ppapi/proxy/ppp_class_proxy.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 26 | #include "ppapi/proxy/resource_creation_proxy.h" |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 27 | #include "ppapi/shared_impl/resource.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 28 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 29 | #if defined(OS_POSIX) |
| 30 | #include "base/eintr_wrapper.h" |
| 31 | #include "ipc/ipc_channel_posix.h" |
| 32 | #endif |
| 33 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 34 | namespace ppapi { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 35 | namespace proxy { |
| 36 | |
| 37 | namespace { |
| 38 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 39 | typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap; |
| 40 | InstanceToDispatcherMap* g_instance_to_dispatcher = NULL; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 41 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 42 | } // namespace |
| 43 | |
[email protected] | 06e0a34 | 2011-09-27 04:24:30 | [diff] [blame] | 44 | InstanceData::InstanceData() |
[email protected] | 0f41c01 | 2011-10-21 19:49:20 | [diff] [blame^] | 45 | : fullscreen(PP_FALSE), |
| 46 | flash_fullscreen(PP_FALSE), |
| 47 | mouse_lock_callback(PP_BlockUntilComplete()) { |
[email protected] | c7bf745 | 2011-09-12 21:31:50 | [diff] [blame] | 48 | memset(&position, 0, sizeof(position)); |
| 49 | } |
| 50 | |
[email protected] | 0f41c01 | 2011-10-21 19:49:20 | [diff] [blame^] | 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); |
| 55 | } |
| 56 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 57 | PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle, |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 58 | GetInterfaceFunc get_interface) |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 59 | : Dispatcher(remote_process_handle, get_interface), |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 60 | plugin_delegate_(NULL), |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 61 | received_preferences_(false), |
| 62 | plugin_dispatcher_id_(0) { |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 63 | SetSerializationRules(new PluginVarSerializationRules); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | PluginDispatcher::~PluginDispatcher() { |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 67 | if (plugin_delegate_) |
| 68 | plugin_delegate_->Unregister(plugin_dispatcher_id_); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // static |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 72 | PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 73 | if (!g_instance_to_dispatcher) |
| 74 | return NULL; |
| 75 | InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find( |
| 76 | instance); |
| 77 | if (found == g_instance_to_dispatcher->end()) |
| 78 | return NULL; |
| 79 | return found->second; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 82 | // static |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 83 | PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) { |
| 84 | return GetForInstance(resource->pp_instance()); |
| 85 | } |
| 86 | |
| 87 | // static |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 88 | const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) { |
| 89 | return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name); |
| 90 | } |
| 91 | |
| 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)); |
| 98 | return ret; |
| 99 | } |
| 100 | return found->second; |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 103 | bool PluginDispatcher::InitPluginWithChannel( |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 104 | PluginDelegate* delegate, |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 105 | const IPC::ChannelHandle& channel_handle, |
| 106 | bool is_client) { |
| 107 | if (!Dispatcher::InitWithChannel(delegate, channel_handle, is_client)) |
| 108 | return false; |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 109 | plugin_delegate_ = delegate; |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 110 | plugin_dispatcher_id_ = plugin_delegate_->Register(this); |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 111 | |
| 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())); |
| 116 | return true; |
| 117 | } |
| 118 | |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 119 | bool PluginDispatcher::IsPlugin() const { |
| 120 | return true; |
| 121 | } |
| 122 | |
[email protected] | b00bbb3 | 2011-03-30 19:02:14 | [diff] [blame] | 123 | bool PluginDispatcher::Send(IPC::Message* msg) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 124 | TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send", |
| 125 | "Class", IPC_MESSAGE_ID_CLASS(msg->type()), |
| 126 | "Line", IPC_MESSAGE_ID_LINE(msg->type())); |
[email protected] | b00bbb3 | 2011-03-30 19:02:14 | [diff] [blame] | 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. |
| 131 | // |
| 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); |
| 136 | } |
| 137 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 138 | bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 139 | TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived", |
| 140 | "Class", IPC_MESSAGE_ID_CLASS(msg.type()), |
| 141 | "Line", IPC_MESSAGE_ID_LINE(msg.type())); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 142 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 143 | // Handle some plugin-specific control messages. |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 144 | bool handled = true; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 145 | IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg) |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 146 | IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface) |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 147 | IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences) |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 148 | IPC_MESSAGE_UNHANDLED(handled = false); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 149 | IPC_END_MESSAGE_MAP() |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 150 | if (handled) |
[email protected] | 37fe036 | 2011-09-13 04:00:33 | [diff] [blame] | 151 | return true; |
[email protected] | 37fe036 | 2011-09-13 04:00:33 | [diff] [blame] | 152 | } |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 153 | return Dispatcher::OnMessageReceived(msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 156 | void PluginDispatcher::OnChannelError() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 157 | Dispatcher::OnChannelError(); |
| 158 | |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 159 | // The renderer has crashed or exited. This channel and all instances |
| 160 | // associated with it are no longer valid. |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 161 | ForceFreeAllInstances(); |
| 162 | // TODO(brettw) free resources too! |
| 163 | delete this; |
| 164 | } |
| 165 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 166 | void PluginDispatcher::DidCreateInstance(PP_Instance instance) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 167 | if (!g_instance_to_dispatcher) |
| 168 | g_instance_to_dispatcher = new InstanceToDispatcherMap; |
| 169 | (*g_instance_to_dispatcher)[instance] = this; |
| 170 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 171 | instance_map_[instance] = InstanceData(); |
| 172 | } |
| 173 | |
| 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); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 178 | |
| 179 | if (g_instance_to_dispatcher) { |
| 180 | InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find( |
| 181 | instance); |
| 182 | if (found != g_instance_to_dispatcher->end()) { |
| 183 | DCHECK(found->second == this); |
| 184 | g_instance_to_dispatcher->erase(found); |
| 185 | } else { |
| 186 | NOTREACHED(); |
| 187 | } |
| 188 | } |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) { |
| 192 | InstanceDataMap::iterator it = instance_map_.find(instance); |
| 193 | return (it == instance_map_.end()) ? NULL : &it->second; |
| 194 | } |
| 195 | |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 196 | void PluginDispatcher::PostToWebKitThread( |
| 197 | const tracked_objects::Location& from_here, |
| 198 | const base::Closure& task) { |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 199 | return plugin_delegate_->PostToWebKitThread(from_here, task); |
| 200 | } |
| 201 | |
| 202 | bool PluginDispatcher::SendToBrowser(IPC::Message* msg) { |
| 203 | return plugin_delegate_->SendToBrowser(msg); |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 206 | WebKitForwarding* PluginDispatcher::GetWebKitForwarding() { |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 207 | return plugin_delegate_->GetWebKitForwarding(); |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 210 | FunctionGroupBase* PluginDispatcher::GetFunctionAPI(ApiID id) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 211 | return GetInterfaceProxy(id); |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 214 | void PluginDispatcher::ForceFreeAllInstances() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 215 | if (!g_instance_to_dispatcher) |
| 216 | return; |
| 217 | |
| 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. |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 226 | PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first); |
[email protected] | 4585fbc | 2011-06-13 17:17:56 | [diff] [blame] | 227 | OnMessageReceived(msg); |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 228 | } |
| 229 | } |
[email protected] | 176c7392 | 2010-12-03 17:32:19 | [diff] [blame] | 230 | } |
| 231 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 232 | void PluginDispatcher::OnMsgSupportsInterface( |
| 233 | const std::string& interface_name, |
| 234 | bool* result) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 235 | *result = !!GetPluginInterface(interface_name); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 236 | } |
| 237 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 238 | void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) { |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 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; |
| 249 | } |
| 250 | } |
| 251 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 252 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 253 | } // namespace ppapi |