- add sources.
[platform/framework/web/crosswalk.git] / src / content / child / npapi / webplugin_delegate_impl.h
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.
4
5 #ifndef CONTENT_CHILD_NPAPI_WEBPLUGIN_DELEGATE_IMPL_H_
6 #define CONTENT_CHILD_NPAPI_WEBPLUGIN_DELEGATE_IMPL_H_
7
8 #include <string>
9 #include <vector>
10
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"
22
23 #if defined(USE_X11)
24 #include "ui/base/x/x11_util.h"
25
26 typedef struct _GdkDrawable GdkPixmap;
27 #endif
28
29 namespace base {
30 class FilePath;
31 }
32
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
42
43 namespace content {
44 class PluginInstance;
45 class WebPlugin;
46
47 #if defined(OS_MACOSX)
48 class WebPluginAcceleratedSurface;
49 class ExternalDragTracker;
50 #endif  // OS_MACOSX
51
52 #if defined(OS_WIN)
53 class WebPluginIMEWin;
54 #endif  // OS_WIN
55
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   };
79
80   static WebPluginDelegateImpl* Create(WebPlugin* plugin,
81                                        const base::FilePath& filename,
82                                        const std::string& mime_type);
83
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,
100                                        NPReason reason,
101                                        int notify_id) OVERRIDE;
102   virtual int GetProcessId() OVERRIDE;
103   virtual void SendJavaScriptStream(const GURL& url,
104                                     const std::string& result,
105                                     bool success,
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,
120                         int notify_id,
121                         const GURL& url,
122                         const GURL& first_party_for_cookies,
123                         const std::string& method,
124                         const char* buf,
125                         unsigned int len,
126                         const GURL& referrer,
127                         bool notify_redirects,
128                         bool is_plugin_src_load,
129                         int origin_pid,
130                         int render_view_id) OVERRIDE;
131   // End of WebPluginDelegate implementation.
132
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_; }
138
139   // Returns the path for the library implementing this plugin.
140   base::FilePath GetPluginPath();
141
142   // Returns a combination of PluginQuirks.
143   int GetQuirks() const { return quirks_; }
144
145   // Informs the plugin that the view it is in has gained or lost focus.
146   void SetContentAreaHasFocus(bool has_focus);
147
148 #if defined(OS_WIN)
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);
154
155   // Informs the plugin that IME composition has completed./ If |text| is empty,
156   // IME was cancelled.
157   void ImeCompositionCompleted(const base::string16& text);
158
159   // Returns the IME status retrieved from a plug-in.
160   bool GetIMEStatus(int* input_type, gfx::Rect* caret_rect);
161 #endif
162
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);
187
188   // Indicates that the windowless plugins will draw directly to the window
189   // context instead of a buffer context.
190   void SetNoBufferContext();
191
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
197
198 #if defined(USE_X11)
199   void SetWindowlessShmPixmap(XID shm_pixmap) {
200     windowless_shm_pixmap_ = shm_pixmap;
201   }
202 #endif
203
204  private:
205   friend class base::DeleteHelper<WebPluginDelegateImpl>;
206   friend class WebPluginDelegate;
207
208   WebPluginDelegateImpl(WebPlugin* plugin, PluginInstance* instance);
209   virtual ~WebPluginDelegateImpl();
210
211   // Called by Initialize() for platform-specific initialization.
212   // If this returns false, the plugin shouldn't be started--see Initialize().
213   bool PlatformInitialize();
214
215   // Called by DestroyInstance(), used for platform-specific destruction.
216   void PlatformDestroyInstance();
217
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();
226
227   // Destroy the native window.
228   void WindowedDestroyWindow();
229
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);
234
235   // Tells the plugin about the current state of the window.
236   // See NPAPI NPP_SetWindow for more information.
237   void WindowedSetWindow();
238
239 #if defined(OS_WIN)
240   // Registers the window class for our window
241   ATOM RegisterNativeWindowClass();
242
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);
252
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);
258 #endif
259
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);
265
266   // Tells the plugin about the current state of the window.
267   // See NPAPI NPP_SetWindow for more information.
268   void WindowlessSetWindow();
269
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);
273
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);
277
278   //-----------------------------------------
279   // used for windowed and windowless plugins
280
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);
285
286   // Closes down and destroys our plugin instance.
287   void DestroyInstance();
288
289
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
294   // work offscreen.
295   gfx::PluginWindowHandle windowed_handle_;
296   gfx::Rect windowed_last_pos_;
297
298   bool windowed_did_set_window_;
299
300   // used by windowed and windowless plugins
301   bool windowless_;
302
303   WebPlugin* plugin_;
304   scoped_refptr<PluginInstance> instance_;
305
306 #if defined(OS_WIN)
307   // Original wndproc before we subclassed.
308   WNDPROC plugin_wnd_proc_;
309
310   // Used to throttle WM_USER+1 messages in Flash.
311   uint32 last_message_;
312   bool is_calling_wndproc;
313
314   // An IME emulator used by a windowless plug-in to retrieve IME data through
315   // IMM32 functions.
316   scoped_ptr<WebPluginIMEWin> plugin_ime_;
317 #endif  // defined(OS_WIN)
318
319 #if defined(USE_X11)
320   // The SHM pixmap for a windowless plugin.
321   XID windowless_shm_pixmap_;
322 #endif
323
324 #if defined(TOOLKIT_GTK)
325   // The pixmap we're drawing into, for a windowless plugin.
326   GdkPixmap* pixmap_;
327   double first_event_time_;
328
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
332   // process.
333   GtkWidget* plug_;
334   GtkWidget* socket_;
335
336   // Ensure pixmap_ exists and is at least width by height pixels.
337   void EnsurePixmapAtLeastSize(int width, int height);
338 #endif
339
340   NPWindow window_;
341   gfx::Rect window_rect_;
342   gfx::Rect clip_rect_;
343   int quirks_;
344
345 #if defined(OS_WIN)
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.
356
357   HWND dummy_window_for_activation_;
358   HWND dummy_window_parent_;
359   WNDPROC old_dummy_window_proc_;
360   bool CreateDummyWindowForActivation();
361
362   // Returns true if the event passed in needs to be tracked for a potential
363   // modal loop.
364   static bool ShouldTrackEventForModalLoops(NPEvent* event);
365
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,
369                                                        LPARAM lParam);
370
371   // TrackPopupMenu interceptor. Parameters are the same as the Win32 function
372   // TrackPopupMenu.
373   static BOOL WINAPI TrackPopupMenuPatch(HMENU menu, unsigned int flags, int x,
374                                          int y, int reserved, HWND window,
375                                          const RECT* rect);
376
377   // SetCursor interceptor for windowless plugins.
378   static HCURSOR WINAPI SetCursorPatch(HCURSOR cursor);
379
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);
384
385   // GetProcAddress intercepter for windowless plugins.
386   static FARPROC WINAPI GetProcAddressPatch(HMODULE module, LPCSTR name);
387
388   // The mouse hook proc which handles mouse capture in windowed plugins.
389   static LRESULT CALLBACK MouseHookProc(int code, WPARAM wParam,
390                                         LPARAM lParam);
391
392   // Calls SetCapture/ReleaseCapture based on the message type.
393   static void HandleCaptureForMessage(HWND window, UINT message);
394
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();
404
405   // Starts an IME session.
406   void StartIme();
407
408   // Informs the browser about the updated accelerated drawing surface.
409   void UpdateAcceleratedSurface();
410
411   // Uses a CARenderer to draw the plug-in's layer in our OpenGL surface.
412   void DrawLayerInSurface();
413
414   bool use_buffer_context_;
415   CGContextRef buffer_context_;  // Weak ref.
416
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_;
421
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_;
425
426   bool containing_window_has_focus_;
427   bool initial_window_focus_;
428   bool container_is_visible_;
429   bool have_called_set_window_;
430
431   gfx::Rect cached_clip_rect_;
432
433   bool ime_enabled_;
434   int keyup_ignore_count_;
435
436   scoped_ptr<ExternalDragTracker> external_drag_tracker_;
437 #endif  // OS_MACOSX && !USE_AURA
438
439   // Called by the message filter hook when the plugin enters a modal loop.
440   void OnModalLoopEntered();
441
442   // Returns true if the message passed in corresponds to a user gesture.
443   static bool IsUserGesture(const WebKit::WebInputEvent& event);
444
445   // The url with which the plugin was instantiated.
446   std::string plugin_url_;
447
448 #if defined(OS_WIN)
449   // Indicates the end of a user gesture period.
450   void OnUserGestureEnd();
451
452   // Handle to the message filter hook
453   HHOOK handle_event_message_filter_hook_;
454
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_;
458
459   // This flag indicates whether we started tracking a user gesture message.
460   bool user_gesture_message_posted_;
461
462   // Runnable Method Factory used to invoke the OnUserGestureEnd method
463   // asynchronously.
464   base::WeakPtrFactory<WebPluginDelegateImpl> user_gesture_msg_factory_;
465
466   // Handle to the mouse hook installed for certain windowed plugins like
467   // flash.
468   HHOOK mouse_hook_;
469 #endif
470
471   // Holds the depth of the HandleEvent callstack.
472   int handle_event_depth_;
473
474   // Holds the current cursor set by the windowless plugin.
475   WebCursor current_windowless_cursor_;
476
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_;
480
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_;
491
492   // True if NPP_New did not return an error.
493   bool creation_succeeded_;
494
495   DISALLOW_COPY_AND_ASSIGN(WebPluginDelegateImpl);
496 };
497
498 }  // namespace content
499
500 #endif  // CONTENT_CHILD_NPAPI_WEBPLUGIN_DELEGATE_IMPL_H_