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