1 // Copyright (c) 2012 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 #ifndef PPAPI_PROXY_PLUGIN_DISPATCHER_H_
6 #define PPAPI_PROXY_PLUGIN_DISPATCHER_H_
11 #include "base/basictypes.h"
12 #include "base/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/process.h"
16 #include "build/build_config.h"
17 #include "ppapi/c/pp_rect.h"
18 #include "ppapi/c/pp_instance.h"
19 #include "ppapi/c/ppb_console.h"
20 #include "ppapi/proxy/dispatcher.h"
21 #include "ppapi/shared_impl/ppapi_preferences.h"
22 #include "ppapi/shared_impl/ppb_view_shared.h"
23 #include "ppapi/shared_impl/singleton_resource_id.h"
24 #include "ppapi/shared_impl/tracked_callback.h"
32 class PPB_Instance_API
;
33 class ResourceCreationAPI
;
38 class ResourceMessageReplyParams
;
40 // Used to keep track of per-instance data.
47 // When non-NULL, indicates the callback to execute when mouse lock is lost.
48 scoped_refptr
<TrackedCallback
> mouse_lock_callback
;
50 // A map of singleton resources which are lazily created.
51 typedef std::map
<SingletonResourceID
, scoped_refptr
<Resource
> >
53 SingletonResourceMap singleton_resources
;
55 // Calls to |RequestSurroundingText()| are done by posted tasks. Track whether
56 // a) a task is pending, to avoid redundant calls, and b) whether we should
57 // actually call |RequestSurroundingText()|, to avoid stale calls (i.e.,
58 // calling when we shouldn't).
59 bool is_request_surrounding_text_pending
;
60 bool should_do_request_surrounding_text
;
63 class PPAPI_PROXY_EXPORT PluginDispatcher
65 public base::SupportsWeakPtr
<PluginDispatcher
> {
67 class PPAPI_PROXY_EXPORT PluginDelegate
: public ProxyChannel::Delegate
{
69 // Returns the set used for globally uniquifying PP_Instances. This same
70 // set must be returned for all channels.
72 // DEREFERENCE ONLY ON THE I/O THREAD.
73 virtual std::set
<PP_Instance
>* GetGloballySeenInstanceIDSet() = 0;
75 // Registers the plugin dispatcher and returns an ID.
76 // Plugin dispatcher IDs will be used to dispatch messages from the browser.
77 // Each call to Register() has to be matched with a call to Unregister().
78 virtual uint32
Register(PluginDispatcher
* plugin_dispatcher
) = 0;
79 virtual void Unregister(uint32 plugin_dispatcher_id
) = 0;
82 // Constructor for the plugin side. The init and shutdown functions will be
83 // will be automatically called when requested by the renderer side. The
84 // module ID will be set upon receipt of the InitializeModule message.
86 // Note about permissions: On the plugin side, the dispatcher and the plugin
87 // run in the same address space (including in nacl). This means that the
88 // permissions here are subject to malicious modification and bypass, and
89 // an exploited or malicious plugin could send any IPC messages and just
90 // bypass the permissions. All permissions must be checked "for realz" in the
91 // host process when receiving messages. We check them on the plugin side
92 // primarily to keep honest plugins honest, especially with respect to
93 // dev interfaces that they "shouldn't" be using.
95 // You must call InitPluginWithChannel after the constructor.
96 PluginDispatcher(PP_GetInterface_Func get_interface
,
97 const PpapiPermissions
& permissions
,
99 virtual ~PluginDispatcher();
101 // The plugin side maintains a mapping from PP_Instance to Dispatcher so
102 // that we can send the messages to the right channel if there are multiple
103 // renderers sharing the same plugin. This mapping is maintained by
104 // DidCreateInstance/DidDestroyInstance.
105 static PluginDispatcher
* GetForInstance(PP_Instance instance
);
107 // Same as GetForInstance but retrieves the instance from the given resource
108 // object as a convenience. Returns NULL on failure.
109 static PluginDispatcher
* GetForResource(const Resource
* resource
);
111 // Implements the GetInterface function for the plugin to call to retrieve
112 // a browser interface.
113 static const void* GetBrowserInterface(const char* interface_name
);
115 // Logs the given log message to the given instance, or, if the instance is
116 // invalid, to all instances associated with all dispatchers. Used for
117 // global log messages.
118 static void LogWithSource(PP_Instance instance
,
120 const std::string
& source
,
121 const std::string
& value
);
123 const void* GetPluginInterface(const std::string
& interface_name
);
125 // You must call this function before anything else. Returns true on success.
126 // The delegate pointer must outlive this class, ownership is not
128 bool InitPluginWithChannel(PluginDelegate
* delegate
,
129 const IPC::ChannelHandle
& channel_handle
,
132 // Dispatcher overrides.
133 virtual bool IsPlugin() const;
134 virtual bool Send(IPC::Message
* msg
);
136 // IPC::Listener implementation.
137 virtual bool OnMessageReceived(const IPC::Message
& msg
);
138 virtual void OnChannelError();
140 // Keeps track of which dispatcher to use for each instance, active instances
141 // and tracks associated data like the current size.
142 void DidCreateInstance(PP_Instance instance
);
143 void DidDestroyInstance(PP_Instance instance
);
145 // Gets the data for an existing instance, or NULL if the instance id doesn't
146 // correspond to a known instance.
147 InstanceData
* GetInstanceData(PP_Instance instance
);
149 // Returns the corresponding API. These are APIs not associated with a
150 // resource. Guaranteed non-NULL.
151 thunk::PPB_Instance_API
* GetInstanceAPI();
152 thunk::ResourceCreationAPI
* GetResourceCreationAPI();
154 // Returns the Preferences.
155 const Preferences
& preferences() const { return preferences_
; }
157 uint32
plugin_dispatcher_id() const { return plugin_dispatcher_id_
; }
158 bool incognito() const { return incognito_
; }
160 // Dispatches the given resource message to the appropriate resource in the
161 // plugin process. This should be wired to the various channels that messages
162 // come in from various other processes.
163 static void DispatchResourceReply(
164 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
165 const IPC::Message
& nested_msg
);
168 friend class PluginDispatcherTest
;
170 // Notifies all live instances that they're now closed. This is used when
171 // a renderer crashes or some other error is received.
172 void ForceFreeAllInstances();
174 // IPC message handlers.
175 void OnMsgResourceReply(
176 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
177 const IPC::Message
& nested_msg
);
178 void OnMsgSupportsInterface(const std::string
& interface_name
, bool* result
);
179 void OnMsgSetPreferences(const Preferences
& prefs
);
181 // Internal backed for DispatchResourceReply.
182 static void LockedDispatchResourceReply(
183 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
184 const IPC::Message
& nested_msg
);
186 PluginDelegate
* plugin_delegate_
;
188 // Contains all the plugin interfaces we've queried. The mapped value will
189 // be the pointer to the interface pointer supplied by the plugin if it's
190 // supported, or NULL if it's not supported. This allows us to cache failures
191 // and not req-query if a plugin doesn't support the interface.
192 typedef base::hash_map
<std::string
, const void*> InterfaceMap
;
193 InterfaceMap plugin_interfaces_
;
195 typedef base::hash_map
<PP_Instance
, InstanceData
> InstanceDataMap
;
196 InstanceDataMap instance_map_
;
198 // The preferences sent from the host. We only want to set this once, which
199 // is what the received_preferences_ indicates. See OnMsgSetPreferences.
200 bool received_preferences_
;
201 Preferences preferences_
;
203 uint32 plugin_dispatcher_id_
;
205 // Set to true when the instances associated with this dispatcher are
209 DISALLOW_COPY_AND_ASSIGN(PluginDispatcher
);
215 #endif // PPAPI_PROXY_PLUGIN_DISPATCHER_H_