IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / child / npapi / webplugin_delegate_impl.h
blobe072b6cbf948733be797df9355d60d9fe7d24488
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_
8 #include <string>
9 #include <vector>
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"
23 #if defined(USE_X11)
24 #include "ui/base/x/x11_util.h"
26 typedef struct _GdkDrawable GdkPixmap;
27 #endif
29 namespace base {
30 class FilePath;
33 #if defined(OS_MACOSX)
34 #ifdef __OBJC__
35 @class CALayer;
36 @class CARenderer;
37 #else
38 class CALayer;
39 class CARenderer;
40 #endif
41 #endif
43 namespace content {
44 class PluginInstance;
45 class WebPlugin;
47 #if defined(OS_MACOSX)
48 class WebPluginAcceleratedSurface;
49 class ExternalDragTracker;
50 #endif // OS_MACOSX
52 #if defined(OS_WIN)
53 class WebPluginIMEWin;
54 #endif // OS_WIN
56 // An implementation of WebPluginDelegate that runs in the plugin process,
57 // proxied from the renderer by WebPluginDelegateProxy.
58 class WebPluginDelegateImpl : public WebPluginDelegate {
59 public:
60 enum PluginQuirks {
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.
78 PLUGIN_QUIRK_FAKE_WINDOW_FROM_POINT = 262144, // Windows.
81 static WebPluginDelegateImpl* Create(WebPlugin* plugin,
82 const base::FilePath& filename,
83 const std::string& mime_type);
85 // WebPluginDelegate implementation
86 virtual bool Initialize(const GURL& url,
87 const std::vector<std::string>& arg_names,
88 const std::vector<std::string>& arg_values,
89 bool load_manually) OVERRIDE;
90 virtual void PluginDestroyed() OVERRIDE;
91 virtual void UpdateGeometry(const gfx::Rect& window_rect,
92 const gfx::Rect& clip_rect) OVERRIDE;
93 virtual void Paint(SkCanvas* canvas, const gfx::Rect& rect) OVERRIDE;
94 virtual void SetFocus(bool focused) OVERRIDE;
95 virtual bool HandleInputEvent(const blink::WebInputEvent& event,
96 WebCursor::CursorInfo* cursor_info) OVERRIDE;
97 virtual NPObject* GetPluginScriptableObject() OVERRIDE;
98 virtual NPP GetPluginNPP() OVERRIDE;
99 virtual bool GetFormValue(base::string16* value) OVERRIDE;
100 virtual void DidFinishLoadWithReason(const GURL& url,
101 NPReason reason,
102 int notify_id) OVERRIDE;
103 virtual int GetProcessId() OVERRIDE;
104 virtual void SendJavaScriptStream(const GURL& url,
105 const std::string& result,
106 bool success,
107 int notify_id) OVERRIDE;
108 virtual void DidReceiveManualResponse(const GURL& url,
109 const std::string& mime_type,
110 const std::string& headers,
111 uint32 expected_length,
112 uint32 last_modified) OVERRIDE;
113 virtual void DidReceiveManualData(const char* buffer, int length) OVERRIDE;
114 virtual void DidFinishManualLoading() OVERRIDE;
115 virtual void DidManualLoadFail() OVERRIDE;
116 virtual WebPluginResourceClient* CreateResourceClient(
117 unsigned long resource_id, const GURL& url, int notify_id) OVERRIDE;
118 virtual WebPluginResourceClient* CreateSeekableResourceClient(
119 unsigned long resource_id, int range_request_id) OVERRIDE;
120 virtual void FetchURL(unsigned long resource_id,
121 int notify_id,
122 const GURL& url,
123 const GURL& first_party_for_cookies,
124 const std::string& method,
125 const char* buf,
126 unsigned int len,
127 const GURL& referrer,
128 bool notify_redirects,
129 bool is_plugin_src_load,
130 int origin_pid,
131 int render_frame_id) OVERRIDE;
132 // End of WebPluginDelegate implementation.
134 gfx::PluginWindowHandle windowed_handle() const { return windowed_handle_; }
135 bool IsWindowless() const { return windowless_; }
136 PluginInstance* instance() { return instance_.get(); }
137 gfx::Rect GetRect() const { return window_rect_; }
138 gfx::Rect GetClipRect() const { return clip_rect_; }
140 // Returns the path for the library implementing this plugin.
141 base::FilePath GetPluginPath();
143 // Returns a combination of PluginQuirks.
144 int GetQuirks() const { return quirks_; }
146 // Informs the plugin that the view it is in has gained or lost focus.
147 void SetContentAreaHasFocus(bool has_focus);
149 #if defined(OS_WIN)
150 // Informs the plug-in that an IME has changed its status.
151 void ImeCompositionUpdated(const base::string16& text,
152 const std::vector<int>& clauses,
153 const std::vector<int>& target,
154 int cursor_position);
156 // Informs the plugin that IME composition has completed./ If |text| is empty,
157 // IME was cancelled.
158 void ImeCompositionCompleted(const base::string16& text);
160 // Returns the IME status retrieved from a plug-in.
161 bool GetIMEStatus(int* input_type, gfx::Rect* caret_rect);
162 #endif
164 #if defined(OS_MACOSX) && !defined(USE_AURA)
165 // Informs the plugin that the geometry has changed, as with UpdateGeometry,
166 // but also includes the new buffer context for that new geometry.
167 void UpdateGeometryAndContext(const gfx::Rect& window_rect,
168 const gfx::Rect& clip_rect,
169 gfx::NativeDrawingContext context);
170 // Informs the delegate that the plugin called NPN_Invalidate*. Used as a
171 // trigger for Core Animation drawing.
172 void PluginDidInvalidate();
173 // Returns the delegate currently processing events.
174 static WebPluginDelegateImpl* GetActiveDelegate();
175 // Informs the plugin that the window it is in has gained or lost focus.
176 void SetWindowHasFocus(bool has_focus);
177 // Informs the plugin that its tab or window has been hidden or shown.
178 void SetContainerVisibility(bool is_visible);
179 // Informs the plugin that its containing window's frame has changed.
180 // Frames are in screen coordinates.
181 void WindowFrameChanged(const gfx::Rect& window_frame,
182 const gfx::Rect& view_frame);
183 // Informs the plugin that IME composition has completed.
184 // If |text| is empty, IME was cancelled.
185 void ImeCompositionCompleted(const base::string16& text);
186 // Informs the delegate that the plugin set a Cocoa NSCursor.
187 void SetNSCursor(NSCursor* cursor);
189 // Indicates that the windowless plugins will draw directly to the window
190 // context instead of a buffer context.
191 void SetNoBufferContext();
193 // TODO(caryclark): This is a temporary workaround to allow the Darwin / Skia
194 // port to share code with the Darwin / CG port. Later, this will be removed
195 // and all callers will use the Paint defined above.
196 void CGPaint(CGContextRef context, const gfx::Rect& rect);
197 #endif // OS_MACOSX && !USE_AURA
199 #if defined(USE_X11)
200 void SetWindowlessShmPixmap(XID shm_pixmap) {
201 windowless_shm_pixmap_ = shm_pixmap;
203 #endif
205 private:
206 friend class base::DeleteHelper<WebPluginDelegateImpl>;
207 friend class WebPluginDelegate;
209 WebPluginDelegateImpl(WebPlugin* plugin, PluginInstance* instance);
210 virtual ~WebPluginDelegateImpl();
212 // Called by Initialize() for platform-specific initialization.
213 // If this returns false, the plugin shouldn't be started--see Initialize().
214 bool PlatformInitialize();
216 // Called by DestroyInstance(), used for platform-specific destruction.
217 void PlatformDestroyInstance();
219 //--------------------------
220 // used for windowed plugins
221 void WindowedUpdateGeometry(const gfx::Rect& window_rect,
222 const gfx::Rect& clip_rect);
223 // Create the native window.
224 // Returns true if the window is created (or already exists).
225 // Returns false if unable to create the window.
226 bool WindowedCreatePlugin();
228 // Destroy the native window.
229 void WindowedDestroyWindow();
231 // Reposition the native window to be in sync with the given geometry.
232 // Returns true if the native window has moved or been clipped differently.
233 bool WindowedReposition(const gfx::Rect& window_rect,
234 const gfx::Rect& clip_rect);
236 // Tells the plugin about the current state of the window.
237 // See NPAPI NPP_SetWindow for more information.
238 void WindowedSetWindow();
240 #if defined(OS_WIN)
241 // Registers the window class for our window
242 ATOM RegisterNativeWindowClass();
244 // Our WndProc functions.
245 static LRESULT CALLBACK WrapperWindowProc(
246 HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
247 static LRESULT CALLBACK NativeWndProc(
248 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
249 static LRESULT CALLBACK FlashWindowlessWndProc(
250 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
251 static LRESULT CALLBACK DummyWindowProc(
252 HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
254 // Used for throttling Flash messages.
255 static void ClearThrottleQueueForWindow(HWND window);
256 static void OnThrottleMessage();
257 static void ThrottleMessage(WNDPROC proc, HWND hwnd, UINT message,
258 WPARAM wParam, LPARAM lParam);
259 #endif
261 //----------------------------
262 // used for windowless plugins
263 void WindowlessUpdateGeometry(const gfx::Rect& window_rect,
264 const gfx::Rect& clip_rect);
265 void WindowlessPaint(gfx::NativeDrawingContext hdc, const gfx::Rect& rect);
267 // Tells the plugin about the current state of the window.
268 // See NPAPI NPP_SetWindow for more information.
269 void WindowlessSetWindow();
271 // Informs the plugin that it has gained or lost keyboard focus (on the Mac,
272 // this just means window first responder status).
273 void SetPluginHasFocus(bool focused);
275 // Handles the platform specific details of setting plugin focus. Returns
276 // false if the platform cancelled the focus tranfer.
277 bool PlatformSetPluginHasFocus(bool focused);
279 //-----------------------------------------
280 // used for windowed and windowless plugins
282 // Does platform-specific event handling. Arguments and return are identical
283 // to HandleInputEvent.
284 bool PlatformHandleInputEvent(const blink::WebInputEvent& event,
285 WebCursor::CursorInfo* cursor_info);
287 // Closes down and destroys our plugin instance.
288 void DestroyInstance();
291 // used for windowed plugins
292 // Note: on Mac OS X, the only time the windowed handle is non-zero
293 // is the case of accelerated rendering, which uses a fake window handle to
294 // identify itself back to the browser. It still performs all of its
295 // work offscreen.
296 gfx::PluginWindowHandle windowed_handle_;
297 gfx::Rect windowed_last_pos_;
299 bool windowed_did_set_window_;
301 // used by windowed and windowless plugins
302 bool windowless_;
304 WebPlugin* plugin_;
305 scoped_refptr<PluginInstance> instance_;
307 #if defined(OS_WIN)
308 // Original wndproc before we subclassed.
309 WNDPROC plugin_wnd_proc_;
311 // Used to throttle WM_USER+1 messages in Flash.
312 uint32 last_message_;
313 bool is_calling_wndproc;
315 // An IME emulator used by a windowless plug-in to retrieve IME data through
316 // IMM32 functions.
317 scoped_ptr<WebPluginIMEWin> plugin_ime_;
318 #endif // defined(OS_WIN)
320 #if defined(USE_X11)
321 // The SHM pixmap for a windowless plugin.
322 XID windowless_shm_pixmap_;
323 #endif
325 #if defined(TOOLKIT_GTK)
326 // The pixmap we're drawing into, for a windowless plugin.
327 GdkPixmap* pixmap_;
328 double first_event_time_;
330 // On Linux some plugins assume that the GtkSocket container is in the same
331 // process. So we create a GtkPlug to plug into the browser's container, and
332 // a GtkSocket to hold the plugin. We then send the GtkPlug to the browser
333 // process.
334 GtkWidget* plug_;
335 GtkWidget* socket_;
337 // Ensure pixmap_ exists and is at least width by height pixels.
338 void EnsurePixmapAtLeastSize(int width, int height);
339 #endif
341 NPWindow window_;
342 gfx::Rect window_rect_;
343 gfx::Rect clip_rect_;
344 int quirks_;
346 #if defined(OS_WIN)
347 // Windowless plugins don't have keyboard focus causing issues with the
348 // plugin not receiving keyboard events if the plugin enters a modal
349 // loop like TrackPopupMenuEx or MessageBox, etc.
350 // This is a basic issue with windows activation and focus arising due to
351 // the fact that these windows are created by different threads. Activation
352 // and focus are thread specific states, and if the browser has focus,
353 // the plugin may not have focus.
354 // To fix a majority of these activation issues we create a dummy visible
355 // child window to which we set focus whenever the windowless plugin
356 // receives a WM_LBUTTONDOWN/WM_RBUTTONDOWN message via NPP_HandleEvent.
358 HWND dummy_window_for_activation_;
359 HWND dummy_window_parent_;
360 WNDPROC old_dummy_window_proc_;
361 bool CreateDummyWindowForActivation();
363 // Returns true if the event passed in needs to be tracked for a potential
364 // modal loop.
365 static bool ShouldTrackEventForModalLoops(NPEvent* event);
367 // The message filter hook procedure, which tracks modal loops entered by
368 // a plugin in the course of a NPP_HandleEvent call.
369 static LRESULT CALLBACK HandleEventMessageFilterHook(int code, WPARAM wParam,
370 LPARAM lParam);
372 // TrackPopupMenu interceptor. Parameters are the same as the Win32 function
373 // TrackPopupMenu.
374 static BOOL WINAPI TrackPopupMenuPatch(HMENU menu, unsigned int flags, int x,
375 int y, int reserved, HWND window,
376 const RECT* rect);
378 // SetCursor interceptor for windowless plugins.
379 static HCURSOR WINAPI SetCursorPatch(HCURSOR cursor);
381 // RegEnumKeyExW interceptor.
382 static LONG WINAPI RegEnumKeyExWPatch(
383 HKEY key, DWORD index, LPWSTR name, LPDWORD name_size, LPDWORD reserved,
384 LPWSTR class_name, LPDWORD class_size, PFILETIME last_write_time);
386 // GetProcAddress intercepter for windowless plugins.
387 static FARPROC WINAPI GetProcAddressPatch(HMODULE module, LPCSTR name);
389 #if defined(USE_AURA)
390 // WindowFromPoint patch for Flash windowless plugins. When flash receives
391 // mouse move messages it calls the WindowFromPoint API to eventually convert
392 // the mouse coordinates to screen. We need to return the dummy plugin parent
393 // window for Aura to ensure that these conversions occur correctly.
394 static HWND WINAPI WindowFromPointPatch(POINT point);
395 #endif
397 // The mouse hook proc which handles mouse capture in windowed plugins.
398 static LRESULT CALLBACK MouseHookProc(int code, WPARAM wParam,
399 LPARAM lParam);
401 // Calls SetCapture/ReleaseCapture based on the message type.
402 static void HandleCaptureForMessage(HWND window, UINT message);
404 #elif defined(OS_MACOSX) && !defined(USE_AURA)
405 // Sets window_rect_ to |rect|
406 void SetPluginRect(const gfx::Rect& rect);
407 // Sets content_area_origin to |origin|
408 void SetContentAreaOrigin(const gfx::Point& origin);
409 // Updates everything that depends on the plugin's absolute screen location.
410 void PluginScreenLocationChanged();
411 // Updates anything that depends on plugin visibility.
412 void PluginVisibilityChanged();
414 // Starts an IME session.
415 void StartIme();
417 // Informs the browser about the updated accelerated drawing surface.
418 void UpdateAcceleratedSurface();
420 // Uses a CARenderer to draw the plug-in's layer in our OpenGL surface.
421 void DrawLayerInSurface();
423 bool use_buffer_context_;
424 CGContextRef buffer_context_; // Weak ref.
426 CALayer* layer_; // Used for CA drawing mode. Weak, retained by plug-in.
427 WebPluginAcceleratedSurface* surface_; // Weak ref.
428 CARenderer* renderer_; // Renders layer_ to surface_.
429 scoped_ptr<base::RepeatingTimer<WebPluginDelegateImpl> > redraw_timer_;
431 // The upper-left corner of the web content area in screen coordinates,
432 // relative to an upper-left (0,0).
433 gfx::Point content_area_origin_;
435 bool containing_window_has_focus_;
436 bool initial_window_focus_;
437 bool container_is_visible_;
438 bool have_called_set_window_;
440 gfx::Rect cached_clip_rect_;
442 bool ime_enabled_;
443 int keyup_ignore_count_;
445 scoped_ptr<ExternalDragTracker> external_drag_tracker_;
446 #endif // OS_MACOSX && !USE_AURA
448 // Called by the message filter hook when the plugin enters a modal loop.
449 void OnModalLoopEntered();
451 // Returns true if the message passed in corresponds to a user gesture.
452 static bool IsUserGesture(const blink::WebInputEvent& event);
454 // The url with which the plugin was instantiated.
455 std::string plugin_url_;
457 #if defined(OS_WIN)
458 // Indicates the end of a user gesture period.
459 void OnUserGestureEnd();
461 // Handle to the message filter hook
462 HHOOK handle_event_message_filter_hook_;
464 // Event which is set when the plugin enters a modal loop in the course
465 // of a NPP_HandleEvent call.
466 HANDLE handle_event_pump_messages_event_;
468 // This flag indicates whether we started tracking a user gesture message.
469 bool user_gesture_message_posted_;
471 // Runnable Method Factory used to invoke the OnUserGestureEnd method
472 // asynchronously.
473 base::WeakPtrFactory<WebPluginDelegateImpl> user_gesture_msg_factory_;
475 // Handle to the mouse hook installed for certain windowed plugins like
476 // flash.
477 HHOOK mouse_hook_;
478 #endif
480 // Holds the depth of the HandleEvent callstack.
481 int handle_event_depth_;
483 // Holds the current cursor set by the windowless plugin.
484 WebCursor current_windowless_cursor_;
486 // Set to true initially and indicates if this is the first npp_setwindow
487 // call received by the plugin.
488 bool first_set_window_call_;
490 // True if the plugin thinks it has keyboard focus
491 bool plugin_has_focus_;
492 // True if the plugin element has focus within the web content, regardless of
493 // whether its containing view currently has focus.
494 bool has_webkit_focus_;
495 // True if the containing view currently has focus.
496 // Initially set to true so that plugin focus still works in environments
497 // where SetContentAreaHasFocus is never called. See
498 // https://bugs.webkit.org/show_bug.cgi?id=46013 for details.
499 bool containing_view_has_focus_;
501 // True if NPP_New did not return an error.
502 bool creation_succeeded_;
504 DISALLOW_COPY_AND_ASSIGN(WebPluginDelegateImpl);
507 } // namespace content
509 #endif // CONTENT_CHILD_NPAPI_WEBPLUGIN_DELEGATE_IMPL_H_