[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [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/plugin_dispatcher.h" |
| 6 | |
| 7 | #include <map> |
Gyuyoung Kim | b480aba | 2018-01-27 07:00:04 | [diff] [blame^] | 8 | #include <memory> |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 9 | |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | a7629597 | 2013-07-18 00:42:32 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
gab | 751416e1c | 2015-03-26 17:20:58 | [diff] [blame] | 13 | #include "base/metrics/histogram_macros.h" |
primiano | 3ca2296 | 2015-01-30 17:08:37 | [diff] [blame] | 14 | #include "base/trace_event/trace_event.h" |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 16 | #include "ipc/ipc_message.h" |
| 17 | #include "ipc/ipc_sync_channel.h" |
[email protected] | 83ad1c4 | 2013-05-01 16:56:27 | [diff] [blame] | 18 | #include "ipc/ipc_sync_message_filter.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 19 | #include "ppapi/c/pp_errors.h" |
[email protected] | e8f07ac | 2012-01-03 17:43:36 | [diff] [blame] | 20 | #include "ppapi/c/ppp_instance.h" |
[email protected] | a1686e27 | 2012-11-01 23:39:34 | [diff] [blame] | 21 | #include "ppapi/proxy/flash_clipboard_resource.h" |
[email protected] | 0c92b0d | 2012-12-08 00:46:23 | [diff] [blame] | 22 | #include "ppapi/proxy/flash_file_resource.h" |
[email protected] | a7629597 | 2013-07-18 00:42:32 | [diff] [blame] | 23 | #include "ppapi/proxy/flash_resource.h" |
[email protected] | f511881 | 2012-08-24 19:54:30 | [diff] [blame] | 24 | #include "ppapi/proxy/gamepad_resource.h" |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 25 | #include "ppapi/proxy/interface_list.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 26 | #include "ppapi/proxy/interface_proxy.h" |
[email protected] | 6018116 | 2013-04-26 22:02:53 | [diff] [blame] | 27 | #include "ppapi/proxy/plugin_globals.h" |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 28 | #include "ppapi/proxy/plugin_message_filter.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 29 | #include "ppapi/proxy/plugin_resource_tracker.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 30 | #include "ppapi/proxy/plugin_var_serialization_rules.h" |
| 31 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | ceadc39 | 2011-06-15 23:04:24 | [diff] [blame] | 32 | #include "ppapi/proxy/ppb_instance_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 33 | #include "ppapi/proxy/ppp_class_proxy.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 34 | #include "ppapi/proxy/resource_creation_proxy.h" |
[email protected] | 511c58e | 2013-12-12 12:25:33 | [diff] [blame] | 35 | #include "ppapi/proxy/resource_reply_thread_registrar.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 36 | #include "ppapi/shared_impl/ppapi_globals.h" |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 37 | #include "ppapi/shared_impl/proxy_lock.h" |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 38 | #include "ppapi/shared_impl/resource.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 39 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 40 | namespace ppapi { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 41 | namespace proxy { |
| 42 | |
| 43 | namespace { |
| 44 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 45 | typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap; |
| 46 | InstanceToDispatcherMap* g_instance_to_dispatcher = NULL; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 47 | |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 48 | typedef std::set<PluginDispatcher*> DispatcherSet; |
| 49 | DispatcherSet* g_live_dispatchers = NULL; |
| 50 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 51 | } // namespace |
| 52 | |
[email protected] | 06e0a34 | 2011-09-27 04:24:30 | [diff] [blame] | 53 | InstanceData::InstanceData() |
[email protected] | 22fdaa6 | 2012-11-30 01:55:44 | [diff] [blame] | 54 | : is_request_surrounding_text_pending(false), |
[email protected] | a8e7214 | 2012-08-21 17:24:20 | [diff] [blame] | 55 | should_do_request_surrounding_text(false) { |
[email protected] | c7bf745 | 2011-09-12 21:31:50 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | 0f41c01 | 2011-10-21 19:49:20 | [diff] [blame] | 58 | InstanceData::~InstanceData() { |
| 59 | // Run any pending mouse lock callback to prevent leaks. |
[email protected] | f0c8624 | 2013-06-02 21:25:43 | [diff] [blame] | 60 | if (mouse_lock_callback.get()) |
[email protected] | aed9653 | 2012-06-23 14:27:42 | [diff] [blame] | 61 | mouse_lock_callback->Abort(); |
[email protected] | 0f41c01 | 2011-10-21 19:49:20 | [diff] [blame] | 62 | } |
| 63 | |
penghuang | 3634509a | 2015-06-15 15:06:49 | [diff] [blame] | 64 | InstanceData::FlushInfo::FlushInfo() |
| 65 | : flush_pending(false), |
| 66 | put_offset(0) { |
| 67 | } |
| 68 | |
| 69 | InstanceData::FlushInfo::~FlushInfo() { |
| 70 | } |
| 71 | |
Raymes Khoury | 982b7eb2 | 2017-07-26 09:40:24 | [diff] [blame] | 72 | PluginDispatcher::Sender::Sender( |
| 73 | base::WeakPtr<PluginDispatcher> plugin_dispatcher, |
| 74 | scoped_refptr<IPC::SyncMessageFilter> sync_filter) |
| 75 | : plugin_dispatcher_(plugin_dispatcher), sync_filter_(sync_filter) {} |
| 76 | |
| 77 | PluginDispatcher::Sender::~Sender() {} |
| 78 | |
| 79 | bool PluginDispatcher::Sender::SendMessage(IPC::Message* msg) { |
| 80 | // Currently we need to choose between two different mechanisms for sending. |
| 81 | // On the main thread we use the regular dispatch Send() method, on another |
| 82 | // thread we use SyncMessageFilter. |
| 83 | if (PpapiGlobals::Get() |
| 84 | ->GetMainThreadMessageLoop() |
| 85 | ->BelongsToCurrentThread()) { |
| 86 | // The PluginDispatcher may have been destroyed if the channel is gone, but |
| 87 | // resources are leaked and may still send messages. We ignore those |
| 88 | // messages. See crbug.com/725033. |
| 89 | if (plugin_dispatcher_) { |
| 90 | return plugin_dispatcher_.get()->Dispatcher::Send(msg); |
| 91 | } else { |
| 92 | delete msg; |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | return sync_filter_->Send(msg); |
| 97 | } |
| 98 | |
| 99 | bool PluginDispatcher::Sender::Send(IPC::Message* msg) { |
| 100 | TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send", "Class", |
| 101 | IPC_MESSAGE_ID_CLASS(msg->type()), "Line", |
| 102 | IPC_MESSAGE_ID_LINE(msg->type())); |
| 103 | // We always want plugin->renderer messages to arrive in-order. If some sync |
| 104 | // and some async messages are sent in response to a synchronous |
| 105 | // renderer->plugin call, the sync reply will be processed before the async |
| 106 | // reply, and everything will be confused. |
| 107 | // |
| 108 | // Allowing all async messages to unblock the renderer means more reentrancy |
| 109 | // there but gives correct ordering. |
| 110 | // |
| 111 | // We don't want reply messages to unblock however, as they will potentially |
| 112 | // end up on the wrong queue - see crbug.com/122443 |
| 113 | if (!msg->is_reply()) |
| 114 | msg->set_unblock(true); |
| 115 | if (msg->is_sync()) { |
| 116 | // Synchronous messages might be re-entrant, so we need to drop the lock. |
| 117 | ProxyAutoUnlock unlock; |
| 118 | SCOPED_UMA_HISTOGRAM_TIMER("Plugin.PpapiSyncIPCTime"); |
| 119 | return SendMessage(msg); |
| 120 | } |
| 121 | return SendMessage(msg); |
| 122 | } |
| 123 | |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 124 | PluginDispatcher::PluginDispatcher(PP_GetInterface_Func get_interface, |
[email protected] | 195d4cde | 2012-10-02 18:12:41 | [diff] [blame] | 125 | const PpapiPermissions& permissions, |
[email protected] | bc2eeb4 | 2012-05-02 22:35:53 | [diff] [blame] | 126 | bool incognito) |
[email protected] | 195d4cde | 2012-10-02 18:12:41 | [diff] [blame] | 127 | : Dispatcher(get_interface, permissions), |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 128 | plugin_delegate_(NULL), |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 129 | received_preferences_(false), |
[email protected] | bc2eeb4 | 2012-05-02 22:35:53 | [diff] [blame] | 130 | plugin_dispatcher_id_(0), |
Raymes Khoury | 982b7eb2 | 2017-07-26 09:40:24 | [diff] [blame] | 131 | incognito_(incognito), |
| 132 | sender_( |
| 133 | new Sender(AsWeakPtr(), scoped_refptr<IPC::SyncMessageFilter>())) { |
[email protected] | 67600b9 | 2012-03-10 06:51:48 | [diff] [blame] | 134 | SetSerializationRules(new PluginVarSerializationRules(AsWeakPtr())); |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 135 | |
| 136 | if (!g_live_dispatchers) |
| 137 | g_live_dispatchers = new DispatcherSet; |
| 138 | g_live_dispatchers->insert(this); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | PluginDispatcher::~PluginDispatcher() { |
[email protected] | 6018116 | 2013-04-26 22:02:53 | [diff] [blame] | 142 | PluginGlobals::Get()->plugin_var_tracker()->DidDeleteDispatcher(this); |
| 143 | |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 144 | if (plugin_delegate_) |
| 145 | plugin_delegate_->Unregister(plugin_dispatcher_id_); |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 146 | |
| 147 | g_live_dispatchers->erase(this); |
| 148 | if (g_live_dispatchers->empty()) { |
| 149 | delete g_live_dispatchers; |
| 150 | g_live_dispatchers = NULL; |
| 151 | } |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // static |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 155 | PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 156 | if (!g_instance_to_dispatcher) |
| 157 | return NULL; |
| 158 | InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find( |
| 159 | instance); |
| 160 | if (found == g_instance_to_dispatcher->end()) |
| 161 | return NULL; |
| 162 | return found->second; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 165 | // static |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 166 | PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) { |
| 167 | return GetForInstance(resource->pp_instance()); |
| 168 | } |
| 169 | |
| 170 | // static |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 171 | const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) { |
dmichael | fee3a51 | 2014-09-18 21:32:13 | [diff] [blame] | 172 | // CAUTION: This function is called directly from the plugin, but we *don't* |
| 173 | // lock the ProxyLock to avoid excessive locking from C++ wrappers. |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 174 | return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name); |
| 175 | } |
| 176 | |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 177 | // static |
| 178 | void PluginDispatcher::LogWithSource(PP_Instance instance, |
[email protected] | 598816ad | 2012-12-13 01:34:32 | [diff] [blame] | 179 | PP_LogLevel level, |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 180 | const std::string& source, |
| 181 | const std::string& value) { |
| 182 | if (!g_live_dispatchers || !g_instance_to_dispatcher) |
| 183 | return; |
| 184 | |
| 185 | if (instance) { |
| 186 | InstanceToDispatcherMap::iterator found = |
| 187 | g_instance_to_dispatcher->find(instance); |
| 188 | if (found != g_instance_to_dispatcher->end()) { |
| 189 | // Send just to this specific dispatcher. |
| 190 | found->second->Send(new PpapiHostMsg_LogWithSource( |
| 191 | instance, static_cast<int>(level), source, value)); |
| 192 | return; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Instance 0 or invalid, send to all dispatchers. |
| 197 | for (DispatcherSet::iterator i = g_live_dispatchers->begin(); |
| 198 | i != g_live_dispatchers->end(); ++i) { |
| 199 | (*i)->Send(new PpapiHostMsg_LogWithSource( |
| 200 | instance, static_cast<int>(level), source, value)); |
| 201 | } |
| 202 | } |
| 203 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 204 | const void* PluginDispatcher::GetPluginInterface( |
| 205 | const std::string& interface_name) { |
| 206 | InterfaceMap::iterator found = plugin_interfaces_.find(interface_name); |
| 207 | if (found == plugin_interfaces_.end()) { |
| 208 | const void* ret = local_get_interface()(interface_name.c_str()); |
| 209 | plugin_interfaces_.insert(std::make_pair(interface_name, ret)); |
| 210 | return ret; |
| 211 | } |
| 212 | return found->second; |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 215 | bool PluginDispatcher::InitPluginWithChannel( |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 216 | PluginDelegate* delegate, |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 217 | base::ProcessId peer_pid, |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 218 | const IPC::ChannelHandle& channel_handle, |
| 219 | bool is_client) { |
[email protected] | f7b7eb7c | 2014-02-27 23:54:15 | [diff] [blame] | 220 | if (!Dispatcher::InitWithChannel(delegate, peer_pid, channel_handle, |
| 221 | is_client)) |
| 222 | return false; |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 223 | plugin_delegate_ = delegate; |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 224 | plugin_dispatcher_id_ = plugin_delegate_->Register(this); |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 225 | |
Raymes Khoury | 982b7eb2 | 2017-07-26 09:40:24 | [diff] [blame] | 226 | sender_ = new Sender(AsWeakPtr(), channel()->CreateSyncMessageFilter()); |
[email protected] | 83ad1c4 | 2013-05-01 16:56:27 | [diff] [blame] | 227 | |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 228 | // The message filter will intercept and process certain messages directly |
| 229 | // on the I/O thread. |
| 230 | channel()->AddFilter( |
[email protected] | 511c58e | 2013-12-12 12:25:33 | [diff] [blame] | 231 | new PluginMessageFilter( |
| 232 | delegate->GetGloballySeenInstanceIDSet(), |
| 233 | PluginGlobals::Get()->resource_reply_thread_registrar())); |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 234 | return true; |
| 235 | } |
| 236 | |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 237 | bool PluginDispatcher::IsPlugin() const { |
| 238 | return true; |
| 239 | } |
| 240 | |
[email protected] | b00bbb3 | 2011-03-30 19:02:14 | [diff] [blame] | 241 | bool PluginDispatcher::Send(IPC::Message* msg) { |
Raymes Khoury | 982b7eb2 | 2017-07-26 09:40:24 | [diff] [blame] | 242 | return sender_->Send(msg); |
[email protected] | b00bbb3 | 2011-03-30 19:02:14 | [diff] [blame] | 243 | } |
| 244 | |
dmichael | 06b3b7f | 2015-04-03 05:22:26 | [diff] [blame] | 245 | bool PluginDispatcher::SendAndStayLocked(IPC::Message* msg) { |
| 246 | TRACE_EVENT2("ppapi proxy", "PluginDispatcher::SendAndStayLocked", |
| 247 | "Class", IPC_MESSAGE_ID_CLASS(msg->type()), |
| 248 | "Line", IPC_MESSAGE_ID_LINE(msg->type())); |
| 249 | if (!msg->is_reply()) |
| 250 | msg->set_unblock(true); |
Raymes Khoury | 982b7eb2 | 2017-07-26 09:40:24 | [diff] [blame] | 251 | return sender_->SendMessage(msg); |
dmichael | 06b3b7f | 2015-04-03 05:22:26 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 254 | bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | 4c41d3f | 2012-02-15 01:44:47 | [diff] [blame] | 255 | // We need to grab the proxy lock to ensure that we don't collide with the |
| 256 | // plugin making pepper calls on a different thread. |
| 257 | ProxyAutoLock lock; |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 258 | TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived", |
| 259 | "Class", IPC_MESSAGE_ID_CLASS(msg.type()), |
| 260 | "Line", IPC_MESSAGE_ID_LINE(msg.type())); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 261 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 262 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 263 | // Handle some plugin-specific control messages. |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 264 | bool handled = true; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 265 | IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg) |
kareng | 1c62eeb | 2014-11-08 16:35:03 | [diff] [blame] | 266 | IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface) |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 267 | IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences) |
philipj | c842a08 | 2016-01-27 03:36:11 | [diff] [blame] | 268 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 269 | IPC_END_MESSAGE_MAP() |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 270 | if (handled) |
[email protected] | 37fe036 | 2011-09-13 04:00:33 | [diff] [blame] | 271 | return true; |
[email protected] | 37fe036 | 2011-09-13 04:00:33 | [diff] [blame] | 272 | } |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 273 | return Dispatcher::OnMessageReceived(msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 274 | } |
| 275 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 276 | void PluginDispatcher::OnChannelError() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 277 | Dispatcher::OnChannelError(); |
| 278 | |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 279 | // The renderer has crashed or exited. This channel and all instances |
| 280 | // associated with it are no longer valid. |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 281 | ForceFreeAllInstances(); |
| 282 | // TODO(brettw) free resources too! |
| 283 | delete this; |
| 284 | } |
| 285 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 286 | void PluginDispatcher::DidCreateInstance(PP_Instance instance) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 287 | if (!g_instance_to_dispatcher) |
| 288 | g_instance_to_dispatcher = new InstanceToDispatcherMap; |
| 289 | (*g_instance_to_dispatcher)[instance] = this; |
Gyuyoung Kim | b480aba | 2018-01-27 07:00:04 | [diff] [blame^] | 290 | instance_map_[instance] = std::make_unique<InstanceData>(); |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void PluginDispatcher::DidDestroyInstance(PP_Instance instance) { |
[email protected] | 4111b99 | 2014-05-15 17:11:20 | [diff] [blame] | 294 | instance_map_.erase(instance); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 295 | |
| 296 | if (g_instance_to_dispatcher) { |
| 297 | InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find( |
| 298 | instance); |
| 299 | if (found != g_instance_to_dispatcher->end()) { |
| 300 | DCHECK(found->second == this); |
| 301 | g_instance_to_dispatcher->erase(found); |
| 302 | } else { |
| 303 | NOTREACHED(); |
| 304 | } |
| 305 | } |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) { |
avi | 6e1b4e7 | 2016-12-29 22:02:57 | [diff] [blame] | 309 | auto it = instance_map_.find(instance); |
| 310 | if (it == instance_map_.end()) |
| 311 | return nullptr; |
| 312 | return it->second.get(); |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 4f200612 | 2012-04-30 05:13:17 | [diff] [blame] | 315 | thunk::PPB_Instance_API* PluginDispatcher::GetInstanceAPI() { |
| 316 | return static_cast<PPB_Instance_Proxy*>( |
| 317 | GetInterfaceProxy(API_ID_PPB_INSTANCE)); |
| 318 | } |
| 319 | |
| 320 | thunk::ResourceCreationAPI* PluginDispatcher::GetResourceCreationAPI() { |
| 321 | return static_cast<ResourceCreationProxy*>( |
| 322 | GetInterfaceProxy(API_ID_RESOURCE_CREATION)); |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 325 | void PluginDispatcher::ForceFreeAllInstances() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 326 | if (!g_instance_to_dispatcher) |
| 327 | return; |
| 328 | |
| 329 | // Iterating will remove each item from the map, so we need to make a copy |
| 330 | // to avoid things changing out from under is. |
| 331 | InstanceToDispatcherMap temp_map = *g_instance_to_dispatcher; |
| 332 | for (InstanceToDispatcherMap::iterator i = temp_map.begin(); |
| 333 | i != temp_map.end(); ++i) { |
| 334 | if (i->second == this) { |
| 335 | // Synthesize an "instance destroyed" message, this will notify the |
| 336 | // plugin and also remove it from our list of tracked plugins. |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 337 | PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first); |
[email protected] | 4585fbc | 2011-06-13 17:17:56 | [diff] [blame] | 338 | OnMessageReceived(msg); |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 339 | } |
| 340 | } |
[email protected] | 176c7392 | 2010-12-03 17:32:19 | [diff] [blame] | 341 | } |
| 342 | |
kareng | 1c62eeb | 2014-11-08 16:35:03 | [diff] [blame] | 343 | void PluginDispatcher::OnMsgSupportsInterface( |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 344 | const std::string& interface_name, |
| 345 | bool* result) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 346 | *result = !!GetPluginInterface(interface_name); |
[email protected] | e8f07ac | 2012-01-03 17:43:36 | [diff] [blame] | 347 | |
| 348 | // Do fallback for PPP_Instance. This is a hack here and if we have more |
| 349 | // cases like this it should be generalized. The PPP_Instance proxy always |
| 350 | // proxies the 1.1 interface, and then does fallback to 1.0 inside the |
| 351 | // plugin process (see PPP_Instance_Proxy). So here we return true for |
| 352 | // supporting the 1.1 interface if either 1.1 or 1.0 is supported. |
| 353 | if (!*result && interface_name == PPP_INSTANCE_INTERFACE) |
| 354 | *result = !!GetPluginInterface(PPP_INSTANCE_INTERFACE_1_0); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 355 | } |
| 356 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 357 | void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) { |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 358 | // The renderer may send us preferences more than once (currently this |
| 359 | // happens every time a new plugin instance is created). Since we don't have |
| 360 | // a way to signal to the plugin that the preferences have changed, changing |
| 361 | // the default fonts and such in the middle of a running plugin could be |
| 362 | // confusing to it. As a result, we never allow the preferences to be changed |
| 363 | // once they're set. The user will have to restart to get new font prefs |
| 364 | // propogated to plugins. |
| 365 | if (!received_preferences_) { |
| 366 | received_preferences_ = true; |
| 367 | preferences_ = prefs; |
| 368 | } |
| 369 | } |
| 370 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 371 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 372 | } // namespace ppapi |