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