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