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 CONTENT_RENDERER_PEPPER_PEPPER_PLUGIN_DELEGATE_IMPL_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_PLUGIN_DELEGATE_IMPL_H_
13 #include "base/basictypes.h"
14 #include "base/id_map.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
19 #include "content/public/renderer/render_view_observer.h"
20 #include "content/renderer/mouse_lock_dispatcher.h"
21 #include "content/renderer/render_view_pepper_helper.h"
22 #include "ppapi/c/pp_file_info.h"
23 #include "ppapi/shared_impl/private/ppb_tcp_server_socket_shared.h"
24 #include "ppapi/shared_impl/private/tcp_socket_private_impl.h"
25 #include "ui/base/ime/text_input_type.h"
26 #include "webkit/plugins/ppapi/plugin_delegate.h"
38 class PPB_X509Certificate_Fields
;
39 class PpapiPermissions
;
52 struct WebCompositionUnderline
;
56 class ContextProviderCommandBuffer
;
57 class GamepadSharedMemoryReader
;
58 class PepperBrokerImpl
;
59 class PepperDeviceEnumerationEventHandler
;
62 class PepperPluginDelegateImpl
63 : public webkit::ppapi::PluginDelegate
,
64 public RenderViewPepperHelper
,
65 public base::SupportsWeakPtr
<PepperPluginDelegateImpl
>,
66 public RenderViewObserver
{
68 explicit PepperPluginDelegateImpl(RenderViewImpl
* render_view
);
69 virtual ~PepperPluginDelegateImpl();
71 RenderViewImpl
* render_view() { return render_view_
; }
73 // Sets up the renderer host and out-of-process proxy for an external plugin
74 // module. Returns the renderer host, or NULL if it couldn't be created.
75 RendererPpapiHost
* CreateExternalPluginModule(
76 scoped_refptr
<webkit::ppapi::PluginModule
> module
,
77 const base::FilePath
& path
,
78 ppapi::PpapiPermissions permissions
,
79 const IPC::ChannelHandle
& channel_handle
,
80 base::ProcessId plugin_pid
,
83 // Removes broker from pending_connect_broker_ if present. Returns true if so.
84 bool StopWaitingForBrokerConnection(PepperBrokerImpl
* broker
);
86 CONTENT_EXPORT
int GetRoutingID() const;
88 typedef base::Callback
<void (int /* request_id */,
90 const std::string
& /* label */)>
93 // Opens the specified device. The request ID passed into the callback will be
94 // the same as the return value. If successful, the label passed into the
95 // callback identifies a audio/video steam, which can be used to call
96 // CloseDevice() and GetSesssionID().
97 int OpenDevice(PP_DeviceType_Dev type
,
98 const std::string
& device_id
,
99 const OpenDeviceCallback
& callback
);
100 void CloseDevice(const std::string
& label
);
101 // Gets audio/video session ID given a label.
102 int GetSessionID(PP_DeviceType_Dev type
, const std::string
& label
);
105 // RenderViewPepperHelper implementation.
106 virtual WebKit::WebPlugin
* CreatePepperWebPlugin(
107 const webkit::WebPluginInfo
& webplugin_info
,
108 const WebKit::WebPluginParams
& params
) OVERRIDE
;
109 virtual void ViewWillInitiatePaint() OVERRIDE
;
110 virtual void ViewInitiatedPaint() OVERRIDE
;
111 virtual void ViewFlushedPaint() OVERRIDE
;
112 virtual webkit::ppapi::PluginInstance
* GetBitmapForOptimizedPluginPaint(
113 const gfx::Rect
& paint_bounds
,
117 float* scale_factor
) OVERRIDE
;
118 virtual void OnAsyncFileOpened(base::PlatformFileError error_code
,
119 base::PlatformFile file
,
120 int message_id
) OVERRIDE
;
121 virtual void OnPpapiBrokerChannelCreated(
123 base::ProcessId broker_pid
,
124 const IPC::ChannelHandle
& handle
) OVERRIDE
;
125 virtual void OnPpapiBrokerPermissionResult(int request_id
,
126 bool result
) OVERRIDE
;
127 virtual void OnSetFocus(bool has_focus
) OVERRIDE
;
128 virtual void PageVisibilityChanged(bool is_visible
) OVERRIDE
;
129 virtual bool IsPluginFocused() const OVERRIDE
;
130 virtual gfx::Rect
GetCaretBounds() const OVERRIDE
;
131 virtual ui::TextInputType
GetTextInputType() const OVERRIDE
;
132 virtual bool IsPluginAcceptingCompositionEvents() const OVERRIDE
;
133 virtual bool CanComposeInline() const OVERRIDE
;
134 virtual void GetSurroundingText(string16
* text
,
135 ui::Range
* range
) const OVERRIDE
;
136 virtual void OnImeSetComposition(
137 const string16
& text
,
138 const std::vector
<WebKit::WebCompositionUnderline
>& underlines
,
140 int selection_end
) OVERRIDE
;
141 virtual void OnImeConfirmComposition(const string16
& text
) OVERRIDE
;
142 virtual void WillHandleMouseEvent() OVERRIDE
;
144 // PluginDelegate implementation.
145 virtual void PluginFocusChanged(webkit::ppapi::PluginInstance
* instance
,
146 bool focused
) OVERRIDE
;
147 virtual void PluginTextInputTypeChanged(
148 webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
149 virtual void PluginCaretPositionChanged(
150 webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
151 virtual void PluginRequestedCancelComposition(
152 webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
153 virtual void PluginSelectionChanged(
154 webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
155 virtual void SimulateImeSetComposition(
156 const string16
& text
,
157 const std::vector
<WebKit::WebCompositionUnderline
>& underlines
,
159 int selection_end
) OVERRIDE
;
160 virtual void SimulateImeConfirmComposition(const string16
& text
) OVERRIDE
;
161 virtual void PluginCrashed(webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
162 virtual void InstanceCreated(
163 webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
164 virtual void InstanceDeleted(
165 webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
166 virtual scoped_ptr
< ::ppapi::thunk::ResourceCreationAPI
>
167 CreateResourceCreationAPI(
168 webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
169 virtual SkBitmap
* GetSadPluginBitmap() OVERRIDE
;
170 virtual WebKit::WebPlugin
* CreatePluginReplacement(
171 const base::FilePath
& file_path
) OVERRIDE
;
172 virtual uint32_t GetAudioHardwareOutputSampleRate() OVERRIDE
;
173 virtual uint32_t GetAudioHardwareOutputBufferSize() OVERRIDE
;
174 virtual PlatformAudioOutput
* CreateAudioOutput(
175 uint32_t sample_rate
,
176 uint32_t sample_count
,
177 PlatformAudioOutputClient
* client
) OVERRIDE
;
178 virtual PlatformAudioInput
* CreateAudioInput(
179 const std::string
& device_id
,
180 uint32_t sample_rate
,
181 uint32_t sample_count
,
182 PlatformAudioInputClient
* client
) OVERRIDE
;
183 virtual PlatformImage2D
* CreateImage2D(int width
, int height
) OVERRIDE
;
184 virtual PlatformGraphics2D
* GetGraphics2D(
185 webkit::ppapi::PluginInstance
* instance
,
186 PP_Resource resource
) OVERRIDE
;
187 virtual PlatformContext3D
* CreateContext3D() OVERRIDE
;
188 virtual void ReparentContext(PlatformContext3D
*) OVERRIDE
;
189 virtual PlatformVideoCapture
* CreateVideoCapture(
190 const std::string
& device_id
,
191 PlatformVideoCaptureEventHandler
* handler
) OVERRIDE
;
192 virtual PlatformVideoDecoder
* CreateVideoDecoder(
193 media::VideoDecodeAccelerator::Client
* client
,
194 int32 command_buffer_route_id
) OVERRIDE
;
195 virtual Broker
* ConnectToBroker(
196 webkit::ppapi::PPB_Broker_Impl
* client
) OVERRIDE
;
197 virtual void NumberOfFindResultsChanged(int identifier
,
199 bool final_result
) OVERRIDE
;
200 virtual void SelectedFindResultChanged(int identifier
, int index
) OVERRIDE
;
201 virtual bool AsyncOpenFile(const base::FilePath
& path
,
203 const AsyncOpenFileCallback
& callback
) OVERRIDE
;
204 virtual bool AsyncOpenFileSystemURL(
207 const AsyncOpenFileSystemURLCallback
& callback
) OVERRIDE
;
208 virtual bool IsFileSystemOpened(PP_Instance instance
,
209 PP_Resource resource
) const OVERRIDE
;
210 virtual PP_FileSystemType
GetFileSystemType(
211 PP_Instance instance
,
212 PP_Resource resource
) const OVERRIDE
;
213 virtual GURL
GetFileSystemRootUrl(PP_Instance instance
,
214 PP_Resource resource
) const OVERRIDE
;
215 virtual bool MakeDirectory(
218 fileapi::FileSystemCallbackDispatcher
* dispatcher
) OVERRIDE
;
221 fileapi::FileSystemCallbackDispatcher
* dispatcher
) OVERRIDE
;
224 const base::Time
& last_access_time
,
225 const base::Time
& last_modified_time
,
226 fileapi::FileSystemCallbackDispatcher
* dispatcher
) OVERRIDE
;
227 virtual bool SetLength(
230 fileapi::FileSystemCallbackDispatcher
* dispatcher
) OVERRIDE
;
233 fileapi::FileSystemCallbackDispatcher
* dispatcher
) OVERRIDE
;
235 const GURL
& file_path
,
236 const GURL
& new_file_path
,
237 fileapi::FileSystemCallbackDispatcher
* dispatcher
) OVERRIDE
;
238 virtual bool ReadDirectory(
239 const GURL
& directory_path
,
240 fileapi::FileSystemCallbackDispatcher
* dispatcher
) OVERRIDE
;
241 virtual void QueryAvailableSpace(
243 quota::StorageType type
,
244 const AvailableSpaceCallback
& callback
) OVERRIDE
;
245 virtual void WillUpdateFile(const GURL
& file_path
) OVERRIDE
;
246 virtual void DidUpdateFile(const GURL
& file_path
, int64_t delta
) OVERRIDE
;
247 virtual void SyncGetFileSystemPlatformPath(
249 base::FilePath
* platform_path
) OVERRIDE
;
250 virtual scoped_refptr
<base::MessageLoopProxy
>
251 GetFileThreadMessageLoopProxy() OVERRIDE
;
252 virtual uint32
TCPSocketCreate() OVERRIDE
;
253 virtual void TCPSocketConnect(
254 webkit::ppapi::PPB_TCPSocket_Private_Impl
* socket
,
256 const std::string
& host
,
257 uint16_t port
) OVERRIDE
;
258 virtual void TCPSocketConnectWithNetAddress(
259 webkit::ppapi::PPB_TCPSocket_Private_Impl
* socket
,
261 const PP_NetAddress_Private
& addr
) OVERRIDE
;
262 virtual void TCPSocketSSLHandshake(
264 const std::string
& server_name
,
265 uint16_t server_port
,
266 const std::vector
<std::vector
<char> >& trusted_certs
,
267 const std::vector
<std::vector
<char> >& untrusted_certs
) OVERRIDE
;
268 virtual void TCPSocketRead(uint32 socket_id
, int32_t bytes_to_read
) OVERRIDE
;
269 virtual void TCPSocketWrite(uint32 socket_id
,
270 const std::string
& buffer
) OVERRIDE
;
271 virtual void TCPSocketDisconnect(uint32 socket_id
) OVERRIDE
;
272 virtual void TCPSocketSetBoolOption(uint32 socket_id
,
273 PP_TCPSocketOption_Private name
,
274 bool value
) OVERRIDE
;
275 virtual void RegisterTCPSocket(
276 webkit::ppapi::PPB_TCPSocket_Private_Impl
* socket
,
277 uint32 socket_id
) OVERRIDE
;
278 virtual void TCPServerSocketListen(
279 PP_Resource socket_resource
,
280 const PP_NetAddress_Private
& addr
,
281 int32_t backlog
) OVERRIDE
;
282 virtual void TCPServerSocketAccept(uint32 server_socket_id
) OVERRIDE
;
283 virtual void TCPServerSocketStopListening(
284 PP_Resource socket_resource
,
285 uint32 socket_id
) OVERRIDE
;
286 virtual bool AddNetworkListObserver(
287 webkit_glue::NetworkListObserver
* observer
) OVERRIDE
;
288 virtual void RemoveNetworkListObserver(
289 webkit_glue::NetworkListObserver
* observer
) OVERRIDE
;
290 virtual bool X509CertificateParseDER(
291 const std::vector
<char>& der
,
292 ppapi::PPB_X509Certificate_Fields
* fields
) OVERRIDE
;
293 virtual webkit::ppapi::FullscreenContainer
*
294 CreateFullscreenContainer(
295 webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
296 virtual gfx::Size
GetScreenSize() OVERRIDE
;
297 virtual std::string
GetDefaultEncoding() OVERRIDE
;
298 virtual void ZoomLimitsChanged(double minimum_factor
, double maximum_factor
)
300 virtual void DidStartLoading() OVERRIDE
;
301 virtual void DidStopLoading() OVERRIDE
;
302 virtual void SetContentRestriction(int restrictions
) OVERRIDE
;
303 virtual void SaveURLAs(const GURL
& url
) OVERRIDE
;
304 virtual base::SharedMemory
* CreateAnonymousSharedMemory(size_t size
)
306 virtual ::ppapi::Preferences
GetPreferences() OVERRIDE
;
307 virtual bool LockMouse(webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
308 virtual void UnlockMouse(webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
309 virtual bool IsMouseLocked(webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
310 virtual void DidChangeCursor(webkit::ppapi::PluginInstance
* instance
,
311 const WebKit::WebCursorInfo
& cursor
) OVERRIDE
;
312 virtual void DidReceiveMouseEvent(
313 webkit::ppapi::PluginInstance
* instance
) OVERRIDE
;
314 virtual bool IsInFullscreenMode() OVERRIDE
;
315 virtual void SampleGamepads(WebKit::WebGamepads
* data
) OVERRIDE
;
316 virtual bool IsPageVisible() const OVERRIDE
;
317 virtual int EnumerateDevices(
318 PP_DeviceType_Dev type
,
319 const EnumerateDevicesCallback
& callback
) OVERRIDE
;
320 virtual void StopEnumerateDevices(int request_id
) OVERRIDE
;
322 // RenderViewObserver implementation.
323 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
324 virtual void OnDestruct() OVERRIDE
;
326 void OnTCPSocketConnectACK(uint32 plugin_dispatcher_id
,
329 const PP_NetAddress_Private
& local_addr
,
330 const PP_NetAddress_Private
& remote_addr
);
331 void OnTCPSocketSSLHandshakeACK(
332 uint32 plugin_dispatcher_id
,
335 const ppapi::PPB_X509Certificate_Fields
& certificate_fields
);
336 void OnTCPSocketReadACK(uint32 plugin_dispatcher_id
,
339 const std::string
& data
);
340 void OnTCPSocketWriteACK(uint32 plugin_dispatcher_id
,
343 int32_t bytes_written
);
344 void OnTCPSocketSetBoolOptionACK(uint32 plugin_dispatcher_id
,
347 void OnTCPServerSocketListenACK(uint32 plugin_dispatcher_id
,
348 PP_Resource socket_resource
,
351 void OnTCPServerSocketAcceptACK(uint32 plugin_dispatcher_id
,
353 uint32 accepted_socket_id
,
354 const PP_NetAddress_Private
& local_addr
,
355 const PP_NetAddress_Private
& remote_addr
);
357 // Attempts to create a PPAPI plugin for the given filepath. On success, it
358 // will return the newly-created module.
360 // There are two reasons for failure. The first is that the plugin isn't
361 // a PPAPI plugin. In this case, |*pepper_plugin_was_registered| will be set
362 // to false and the caller may want to fall back on creating an NPAPI plugin.
363 // the second is that the plugin failed to initialize. In this case,
364 // |*pepper_plugin_was_registered| will be set to true and the caller should
365 // not fall back on any other plugin types.
366 scoped_refptr
<webkit::ppapi::PluginModule
>
367 CreatePepperPluginModule(
368 const webkit::WebPluginInfo
& webplugin_info
,
369 bool* pepper_plugin_was_registered
);
371 // Asynchronously attempts to create a PPAPI broker for the given plugin.
372 scoped_refptr
<PepperBrokerImpl
> CreateBroker(
373 webkit::ppapi::PluginModule
* plugin_module
);
375 // Create a new HostDispatcher for proxying, hook it to the PluginModule,
376 // and perform other common initialization.
377 RendererPpapiHost
* CreateOutOfProcessModule(
378 webkit::ppapi::PluginModule
* module
,
379 const base::FilePath
& path
,
380 ppapi::PpapiPermissions permissions
,
381 const IPC::ChannelHandle
& channel_handle
,
382 base::ProcessId plugin_pid
,
386 MouseLockDispatcher::LockTarget
* GetOrCreateLockTargetAdapter(
387 webkit::ppapi::PluginInstance
* instance
);
388 void UnSetAndDeleteLockTargetAdapter(webkit::ppapi::PluginInstance
* instance
);
390 MouseLockDispatcher
* GetMouseLockDispatcher(
391 webkit::ppapi::PluginInstance
* instance
);
393 // Share a given handle with the target process.
394 virtual IPC::PlatformFileForTransit
ShareHandleWithRemote(
395 base::PlatformFile handle
,
396 base::ProcessId target_process_id
,
397 bool should_close_source
) const OVERRIDE
;
399 virtual bool IsRunningInProcess(PP_Instance instance
) const OVERRIDE
;
401 // Pointer to the RenderView that owns us.
402 RenderViewImpl
* render_view_
;
404 std::set
<webkit::ppapi::PluginInstance
*> active_instances_
;
405 typedef std::map
<webkit::ppapi::PluginInstance
*,
406 MouseLockDispatcher::LockTarget
*> LockTargetMap
;
407 LockTargetMap mouse_lock_instances_
;
409 IDMap
<AsyncOpenFileCallback
> pending_async_open_files_
;
411 IDMap
<webkit::ppapi::PPB_TCPSocket_Private_Impl
> tcp_sockets_
;
413 IDMap
<ppapi::PPB_TCPServerSocket_Shared
> tcp_server_sockets_
;
415 typedef IDMap
<scoped_refptr
<PepperBrokerImpl
>, IDMapOwnPointer
> BrokerMap
;
416 BrokerMap pending_connect_broker_
;
418 typedef IDMap
<base::WeakPtr
<webkit::ppapi::PPB_Broker_Impl
> >
419 PermissionRequestMap
;
420 PermissionRequestMap pending_permission_requests_
;
422 // Whether or not the focus is on a PPAPI plugin
423 webkit::ppapi::PluginInstance
* focused_plugin_
;
425 // Current text input composition text. Empty if no composition is in
427 string16 composition_text_
;
429 // The plugin instance that received the last mouse event. It is set to NULL
430 // if the last mouse event went to elements other than Pepper plugins.
431 // |last_mouse_event_target_| is not owned by this class. We can know about
432 // when it is destroyed via InstanceDeleted().
433 webkit::ppapi::PluginInstance
* last_mouse_event_target_
;
435 scoped_ptr
<GamepadSharedMemoryReader
> gamepad_shared_memory_reader_
;
437 scoped_ptr
<PepperDeviceEnumerationEventHandler
>
438 device_enumeration_event_handler_
;
440 scoped_refptr
<ContextProviderCommandBuffer
> offscreen_context3d_
;
442 DISALLOW_COPY_AND_ASSIGN(PepperPluginDelegateImpl
);
445 } // namespace content
447 #endif // CONTENT_RENDERER_PEPPER_PEPPER_PLUGIN_DELEGATE_IMPL_H_