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_CHILD_NPAPI_WEBPLUGIN_DELEGATE_IMPL_H_
6 #define CONTENT_CHILD_NPAPI_WEBPLUGIN_DELEGATE_IMPL_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/sequenced_task_runner_helpers.h"
15 #include "base/timer/timer.h"
16 #include "build/build_config.h"
17 #include "content/child/npapi/webplugin_delegate.h"
18 #include "third_party/npapi/bindings/npapi.h"
19 #include "ui/gfx/native_widget_types.h"
20 #include "ui/gfx/rect.h"
21 #include "webkit/common/cursors/webcursor.h"
24 #include "ui/base/x/x11_util.h"
26 typedef struct _GdkDrawable GdkPixmap;
33 #if defined(OS_MACOSX)
47 #if defined(OS_MACOSX)
48 class WebPluginAcceleratedSurface;
49 class ExternalDragTracker;
53 class WebPluginIMEWin;
56 // An implementation of WebPluginDelegate that runs in the plugin process,
57 // proxied from the renderer by WebPluginDelegateProxy.
58 class WebPluginDelegateImpl : public WebPluginDelegate {
61 PLUGIN_QUIRK_SETWINDOW_TWICE = 1, // Win32
62 PLUGIN_QUIRK_THROTTLE_WM_USER_PLUS_ONE = 2, // Win32
63 PLUGIN_QUIRK_DONT_CALL_WND_PROC_RECURSIVELY = 4, // Win32
64 PLUGIN_QUIRK_DONT_SET_NULL_WINDOW_HANDLE_ON_DESTROY = 8, // Win32
65 PLUGIN_QUIRK_DONT_ALLOW_MULTIPLE_INSTANCES = 16, // Win32
66 PLUGIN_QUIRK_DIE_AFTER_UNLOAD = 32, // Win32
67 PLUGIN_QUIRK_PATCH_SETCURSOR = 64, // Win32
68 PLUGIN_QUIRK_BLOCK_NONSTANDARD_GETURL_REQUESTS = 128, // Win32
69 PLUGIN_QUIRK_WINDOWLESS_OFFSET_WINDOW_TO_DRAW = 256, // Linux
70 PLUGIN_QUIRK_WINDOWLESS_INVALIDATE_AFTER_SET_WINDOW = 512, // Linux
71 PLUGIN_QUIRK_NO_WINDOWLESS = 1024, // Windows
72 PLUGIN_QUIRK_PATCH_REGENUMKEYEXW = 2048, // Windows
73 PLUGIN_QUIRK_ALWAYS_NOTIFY_SUCCESS = 4096, // Windows
74 PLUGIN_QUIRK_HANDLE_MOUSE_CAPTURE = 16384, // Windows
75 PLUGIN_QUIRK_WINDOWLESS_NO_RIGHT_CLICK = 32768, // Linux
76 PLUGIN_QUIRK_IGNORE_FIRST_SETWINDOW_CALL = 65536, // Windows.
77 PLUGIN_QUIRK_EMULATE_IME = 131072, // Windows.
80 static WebPluginDelegateImpl* Create(WebPlugin* plugin,
81 const base::FilePath& filename,
82 const std::string& mime_type);
84 // WebPluginDelegate implementation
85 virtual bool Initialize(const GURL& url,
86 const std::vector<std::string>& arg_names,
87 const std::vector<std::string>& arg_values,
88 bool load_manually) OVERRIDE;
89 virtual void PluginDestroyed() OVERRIDE;
90 virtual void UpdateGeometry(const gfx::Rect& window_rect,
91 const gfx::Rect& clip_rect) OVERRIDE;
92 virtual void Paint(SkCanvas* canvas, const gfx::Rect& rect) OVERRIDE;
93 virtual void SetFocus(bool focused) OVERRIDE;
94 virtual bool HandleInputEvent(const WebKit::WebInputEvent& event,
95 WebCursor::CursorInfo* cursor_info) OVERRIDE;
96 virtual NPObject* GetPluginScriptableObject() OVERRIDE;
97 virtual NPP GetPluginNPP() OVERRIDE;
98 virtual bool GetFormValue(base::string16* value) OVERRIDE;
99 virtual void DidFinishLoadWithReason(const GURL& url,
101 int notify_id) OVERRIDE;
102 virtual int GetProcessId() OVERRIDE;
103 virtual void SendJavaScriptStream(const GURL& url,
104 const std::string& result,
106 int notify_id) OVERRIDE;
107 virtual void DidReceiveManualResponse(const GURL& url,
108 const std::string& mime_type,
109 const std::string& headers,
110 uint32 expected_length,
111 uint32 last_modified) OVERRIDE;
112 virtual void DidReceiveManualData(const char* buffer, int length) OVERRIDE;
113 virtual void DidFinishManualLoading() OVERRIDE;
114 virtual void DidManualLoadFail() OVERRIDE;
115 virtual WebPluginResourceClient* CreateResourceClient(
116 unsigned long resource_id, const GURL& url, int notify_id) OVERRIDE;
117 virtual WebPluginResourceClient* CreateSeekableResourceClient(
118 unsigned long resource_id, int range_request_id) OVERRIDE;
119 virtual void FetchURL(unsigned long resource_id,
122 const GURL& first_party_for_cookies,
123 const std::string& method,
126 const GURL& referrer,
127 bool notify_redirects,
128 bool is_plugin_src_load,
130 int render_view_id) OVERRIDE;
131 // End of WebPluginDelegate implementation.
133 gfx::PluginWindowHandle windowed_handle() const { return windowed_handle_; }
134 bool IsWindowless() const { return windowless_; }
135 PluginInstance* instance() { return instance_.get(); }
136 gfx::Rect GetRect() const { return window_rect_; }
137 gfx::Rect GetClipRect() const { return clip_rect_; }
139 // Returns the path for the library implementing this plugin.
140 base::FilePath GetPluginPath();
142 // Returns a combination of PluginQuirks.
143 int GetQuirks() const { return quirks_; }
145 // Informs the plugin that the view it is in has gained or lost focus.
146 void SetContentAreaHasFocus(bool has_focus);
149 // Informs the plug-in that an IME has changed its status.
150 void ImeCompositionUpdated(const base::string16& text,
151 const std::vector<int>& clauses,
152 const std::vector<int>& target,
153 int cursor_position);
155 // Informs the plugin that IME composition has completed./ If |text| is empty,
156 // IME was cancelled.
157 void ImeCompositionCompleted(const base::string16& text);
159 // Returns the IME status retrieved from a plug-in.
160 bool GetIMEStatus(int* input_type, gfx::Rect* caret_rect);
163 #if defined(OS_MACOSX) && !defined(USE_AURA)
164 // Informs the plugin that the geometry has changed, as with UpdateGeometry,
165 // but also includes the new buffer context for that new geometry.
166 void UpdateGeometryAndContext(const gfx::Rect& window_rect,
167 const gfx::Rect& clip_rect,
168 gfx::NativeDrawingContext context);
169 // Informs the delegate that the plugin called NPN_Invalidate*. Used as a
170 // trigger for Core Animation drawing.
171 void PluginDidInvalidate();
172 // Returns the delegate currently processing events.
173 static WebPluginDelegateImpl* GetActiveDelegate();
174 // Informs the plugin that the window it is in has gained or lost focus.
175 void SetWindowHasFocus(bool has_focus);
176 // Informs the plugin that its tab or window has been hidden or shown.
177 void SetContainerVisibility(bool is_visible);
178 // Informs the plugin that its containing window's frame has changed.
179 // Frames are in screen coordinates.
180 void WindowFrameChanged(const gfx::Rect& window_frame,
181 const gfx::Rect& view_frame);
182 // Informs the plugin that IME composition has completed.
183 // If |text| is empty, IME was cancelled.
184 void ImeCompositionCompleted(const base::string16& text);
185 // Informs the delegate that the plugin set a Cocoa NSCursor.
186 void SetNSCursor(NSCursor* cursor);
188 // Indicates that the windowless plugins will draw directly to the window
189 // context instead of a buffer context.
190 void SetNoBufferContext();
192 // TODO(caryclark): This is a temporary workaround to allow the Darwin / Skia
193 // port to share code with the Darwin / CG port. Later, this will be removed
194 // and all callers will use the Paint defined above.
195 void CGPaint(CGContextRef context, const gfx::Rect& rect);
196 #endif // OS_MACOSX && !USE_AURA
199 void SetWindowlessShmPixmap(XID shm_pixmap) {
200 windowless_shm_pixmap_ = shm_pixmap;
205 friend class base::DeleteHelper<WebPluginDelegateImpl>;
206 friend class WebPluginDelegate;
208 WebPluginDelegateImpl(WebPlugin* plugin, PluginInstance* instance);
209 virtual ~WebPluginDelegateImpl();
211 // Called by Initialize() for platform-specific initialization.
212 // If this returns false, the plugin shouldn't be started--see Initialize().
213 bool PlatformInitialize();
215 // Called by DestroyInstance(), used for platform-specific destruction.
216 void PlatformDestroyInstance();
218 //--------------------------
219 // used for windowed plugins
220 void WindowedUpdateGeometry(const gfx::Rect& window_rect,
221 const gfx::Rect& clip_rect);
222 // Create the native window.
223 // Returns true if the window is created (or already exists).
224 // Returns false if unable to create the window.
225 bool WindowedCreatePlugin();
227 // Destroy the native window.
228 void WindowedDestroyWindow();
230 // Reposition the native window to be in sync with the given geometry.
231 // Returns true if the native window has moved or been clipped differently.
232 bool WindowedReposition(const gfx::Rect& window_rect,
233 const gfx::Rect& clip_rect);
235 // Tells the plugin about the current state of the window.
236 // See NPAPI NPP_SetWindow for more information.
237 void WindowedSetWindow();
240 // Registers the window class for our window
241 ATOM RegisterNativeWindowClass();
243 // Our WndProc functions.
244 static LRESULT CALLBACK WrapperWindowProc(
245 HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
246 static LRESULT CALLBACK NativeWndProc(
247 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
248 static LRESULT CALLBACK FlashWindowlessWndProc(
249 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
250 static LRESULT CALLBACK DummyWindowProc(
251 HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
253 // Used for throttling Flash messages.
254 static void ClearThrottleQueueForWindow(HWND window);
255 static void OnThrottleMessage();
256 static void ThrottleMessage(WNDPROC proc, HWND hwnd, UINT message,
257 WPARAM wParam, LPARAM lParam);
260 //----------------------------
261 // used for windowless plugins
262 void WindowlessUpdateGeometry(const gfx::Rect& window_rect,
263 const gfx::Rect& clip_rect);
264 void WindowlessPaint(gfx::NativeDrawingContext hdc, const gfx::Rect& rect);
266 // Tells the plugin about the current state of the window.
267 // See NPAPI NPP_SetWindow for more information.
268 void WindowlessSetWindow();
270 // Informs the plugin that it has gained or lost keyboard focus (on the Mac,
271 // this just means window first responder status).
272 void SetPluginHasFocus(bool focused);
274 // Handles the platform specific details of setting plugin focus. Returns
275 // false if the platform cancelled the focus tranfer.
276 bool PlatformSetPluginHasFocus(bool focused);
278 //-----------------------------------------
279 // used for windowed and windowless plugins
281 // Does platform-specific event handling. Arguments and return are identical
282 // to HandleInputEvent.
283 bool PlatformHandleInputEvent(const WebKit::WebInputEvent& event,
284 WebCursor::CursorInfo* cursor_info);
286 // Closes down and destroys our plugin instance.
287 void DestroyInstance();
290 // used for windowed plugins
291 // Note: on Mac OS X, the only time the windowed handle is non-zero
292 // is the case of accelerated rendering, which uses a fake window handle to
293 // identify itself back to the browser. It still performs all of its
295 gfx::PluginWindowHandle windowed_handle_;
296 gfx::Rect windowed_last_pos_;
298 bool windowed_did_set_window_;
300 // used by windowed and windowless plugins
304 scoped_refptr<PluginInstance> instance_;
307 // Original wndproc before we subclassed.
308 WNDPROC plugin_wnd_proc_;
310 // Used to throttle WM_USER+1 messages in Flash.
311 uint32 last_message_;
312 bool is_calling_wndproc;
314 // An IME emulator used by a windowless plug-in to retrieve IME data through
316 scoped_ptr<WebPluginIMEWin> plugin_ime_;
317 #endif // defined(OS_WIN)
320 // The SHM pixmap for a windowless plugin.
321 XID windowless_shm_pixmap_;
324 #if defined(TOOLKIT_GTK)
325 // The pixmap we're drawing into, for a windowless plugin.
327 double first_event_time_;
329 // On Linux some plugins assume that the GtkSocket container is in the same
330 // process. So we create a GtkPlug to plug into the browser's container, and
331 // a GtkSocket to hold the plugin. We then send the GtkPlug to the browser
336 // Ensure pixmap_ exists and is at least width by height pixels.
337 void EnsurePixmapAtLeastSize(int width, int height);
341 gfx::Rect window_rect_;
342 gfx::Rect clip_rect_;
346 // Windowless plugins don't have keyboard focus causing issues with the
347 // plugin not receiving keyboard events if the plugin enters a modal
348 // loop like TrackPopupMenuEx or MessageBox, etc.
349 // This is a basic issue with windows activation and focus arising due to
350 // the fact that these windows are created by different threads. Activation
351 // and focus are thread specific states, and if the browser has focus,
352 // the plugin may not have focus.
353 // To fix a majority of these activation issues we create a dummy visible
354 // child window to which we set focus whenever the windowless plugin
355 // receives a WM_LBUTTONDOWN/WM_RBUTTONDOWN message via NPP_HandleEvent.
357 HWND dummy_window_for_activation_;
358 HWND dummy_window_parent_;
359 WNDPROC old_dummy_window_proc_;
360 bool CreateDummyWindowForActivation();
362 // Returns true if the event passed in needs to be tracked for a potential
364 static bool ShouldTrackEventForModalLoops(NPEvent* event);
366 // The message filter hook procedure, which tracks modal loops entered by
367 // a plugin in the course of a NPP_HandleEvent call.
368 static LRESULT CALLBACK HandleEventMessageFilterHook(int code, WPARAM wParam,
371 // TrackPopupMenu interceptor. Parameters are the same as the Win32 function
373 static BOOL WINAPI TrackPopupMenuPatch(HMENU menu, unsigned int flags, int x,
374 int y, int reserved, HWND window,
377 // SetCursor interceptor for windowless plugins.
378 static HCURSOR WINAPI SetCursorPatch(HCURSOR cursor);
380 // RegEnumKeyExW interceptor.
381 static LONG WINAPI RegEnumKeyExWPatch(
382 HKEY key, DWORD index, LPWSTR name, LPDWORD name_size, LPDWORD reserved,
383 LPWSTR class_name, LPDWORD class_size, PFILETIME last_write_time);
385 // GetProcAddress intercepter for windowless plugins.
386 static FARPROC WINAPI GetProcAddressPatch(HMODULE module, LPCSTR name);
388 // The mouse hook proc which handles mouse capture in windowed plugins.
389 static LRESULT CALLBACK MouseHookProc(int code, WPARAM wParam,
392 // Calls SetCapture/ReleaseCapture based on the message type.
393 static void HandleCaptureForMessage(HWND window, UINT message);
395 #elif defined(OS_MACOSX) && !defined(USE_AURA)
396 // Sets window_rect_ to |rect|
397 void SetPluginRect(const gfx::Rect& rect);
398 // Sets content_area_origin to |origin|
399 void SetContentAreaOrigin(const gfx::Point& origin);
400 // Updates everything that depends on the plugin's absolute screen location.
401 void PluginScreenLocationChanged();
402 // Updates anything that depends on plugin visibility.
403 void PluginVisibilityChanged();
405 // Starts an IME session.
408 // Informs the browser about the updated accelerated drawing surface.
409 void UpdateAcceleratedSurface();
411 // Uses a CARenderer to draw the plug-in's layer in our OpenGL surface.
412 void DrawLayerInSurface();
414 bool use_buffer_context_;
415 CGContextRef buffer_context_; // Weak ref.
417 CALayer* layer_; // Used for CA drawing mode. Weak, retained by plug-in.
418 WebPluginAcceleratedSurface* surface_; // Weak ref.
419 CARenderer* renderer_; // Renders layer_ to surface_.
420 scoped_ptr<base::RepeatingTimer<WebPluginDelegateImpl> > redraw_timer_;
422 // The upper-left corner of the web content area in screen coordinates,
423 // relative to an upper-left (0,0).
424 gfx::Point content_area_origin_;
426 bool containing_window_has_focus_;
427 bool initial_window_focus_;
428 bool container_is_visible_;
429 bool have_called_set_window_;
431 gfx::Rect cached_clip_rect_;
434 int keyup_ignore_count_;
436 scoped_ptr<ExternalDragTracker> external_drag_tracker_;
437 #endif // OS_MACOSX && !USE_AURA
439 // Called by the message filter hook when the plugin enters a modal loop.
440 void OnModalLoopEntered();
442 // Returns true if the message passed in corresponds to a user gesture.
443 static bool IsUserGesture(const WebKit::WebInputEvent& event);
445 // The url with which the plugin was instantiated.
446 std::string plugin_url_;
449 // Indicates the end of a user gesture period.
450 void OnUserGestureEnd();
452 // Handle to the message filter hook
453 HHOOK handle_event_message_filter_hook_;
455 // Event which is set when the plugin enters a modal loop in the course
456 // of a NPP_HandleEvent call.
457 HANDLE handle_event_pump_messages_event_;
459 // This flag indicates whether we started tracking a user gesture message.
460 bool user_gesture_message_posted_;
462 // Runnable Method Factory used to invoke the OnUserGestureEnd method
464 base::WeakPtrFactory<WebPluginDelegateImpl> user_gesture_msg_factory_;
466 // Handle to the mouse hook installed for certain windowed plugins like
471 // Holds the depth of the HandleEvent callstack.
472 int handle_event_depth_;
474 // Holds the current cursor set by the windowless plugin.
475 WebCursor current_windowless_cursor_;
477 // Set to true initially and indicates if this is the first npp_setwindow
478 // call received by the plugin.
479 bool first_set_window_call_;
481 // True if the plugin thinks it has keyboard focus
482 bool plugin_has_focus_;
483 // True if the plugin element has focus within the web content, regardless of
484 // whether its containing view currently has focus.
485 bool has_webkit_focus_;
486 // True if the containing view currently has focus.
487 // Initially set to true so that plugin focus still works in environments
488 // where SetContentAreaHasFocus is never called. See
489 // https://bugs.webkit.org/show_bug.cgi?id=46013 for details.
490 bool containing_view_has_focus_;
492 // True if NPP_New did not return an error.
493 bool creation_succeeded_;
495 DISALLOW_COPY_AND_ASSIGN(WebPluginDelegateImpl);
498 } // namespace content
500 #endif // CONTENT_CHILD_NPAPI_WEBPLUGIN_DELEGATE_IMPL_H_