Threaded data provider: Support main thread data notifications (Chrome side)
[chromium-blink-merge.git] / content / child / npapi / webplugin_delegate_impl.h
blobdf705843199a4e6ce8e33e8f7b33065db0950f3d
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 "content/common/cursors/webcursor.h"
19 #include "third_party/npapi/bindings/npapi.h"
20 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gfx/native_widget_types.h"
23 namespace base {
24 class FilePath;
27 #if defined(OS_MACOSX)
28 #ifdef __OBJC__
29 @class CALayer;
30 @class CARenderer;
31 #else
32 class CALayer;
33 class CARenderer;
34 #endif
35 #endif
37 namespace content {
38 class PluginInstance;
39 class WebPlugin;
41 #if defined(OS_MACOSX)
42 class WebPluginAcceleratedSurface;
43 class ExternalDragTracker;
44 #endif // OS_MACOSX
46 #if defined(OS_WIN)
47 class WebPluginIMEWin;
48 #endif // OS_WIN
50 // An implementation of WebPluginDelegate that runs in the plugin process,
51 // proxied from the renderer by WebPluginDelegateProxy.
52 class WebPluginDelegateImpl : public WebPluginDelegate {
53 public:
54 enum PluginQuirks {
55 PLUGIN_QUIRK_SETWINDOW_TWICE = 1, // Win32
56 PLUGIN_QUIRK_THROTTLE_WM_USER_PLUS_ONE = 2, // Win32
57 PLUGIN_QUIRK_DONT_CALL_WND_PROC_RECURSIVELY = 4, // Win32
58 PLUGIN_QUIRK_DONT_SET_NULL_WINDOW_HANDLE_ON_DESTROY = 8, // Win32
59 PLUGIN_QUIRK_DONT_ALLOW_MULTIPLE_INSTANCES = 16, // Win32
60 PLUGIN_QUIRK_DIE_AFTER_UNLOAD = 32, // Win32
61 PLUGIN_QUIRK_PATCH_SETCURSOR = 64, // Win32
62 PLUGIN_QUIRK_BLOCK_NONSTANDARD_GETURL_REQUESTS = 128, // Win32
63 PLUGIN_QUIRK_WINDOWLESS_OFFSET_WINDOW_TO_DRAW = 256, // Linux
64 PLUGIN_QUIRK_WINDOWLESS_INVALIDATE_AFTER_SET_WINDOW = 512, // Linux
65 PLUGIN_QUIRK_NO_WINDOWLESS = 1024, // Windows
66 PLUGIN_QUIRK_PATCH_REGENUMKEYEXW = 2048, // Windows
67 PLUGIN_QUIRK_ALWAYS_NOTIFY_SUCCESS = 4096, // Windows
68 PLUGIN_QUIRK_HANDLE_MOUSE_CAPTURE = 16384, // Windows
69 PLUGIN_QUIRK_WINDOWLESS_NO_RIGHT_CLICK = 32768, // Linux
70 PLUGIN_QUIRK_IGNORE_FIRST_SETWINDOW_CALL = 65536, // Windows.
71 PLUGIN_QUIRK_EMULATE_IME = 131072, // Windows.
72 PLUGIN_QUIRK_FAKE_WINDOW_FROM_POINT = 262144, // Windows.
73 PLUGIN_QUIRK_COPY_STREAM_DATA = 524288, // All platforms
76 static WebPluginDelegateImpl* Create(WebPlugin* plugin,
77 const base::FilePath& filename,
78 const std::string& mime_type);
80 // WebPluginDelegate implementation
81 bool Initialize(const GURL& url,
82 const std::vector<std::string>& arg_names,
83 const std::vector<std::string>& arg_values,
84 bool load_manually) override;
85 void PluginDestroyed() override;
86 void UpdateGeometry(const gfx::Rect& window_rect,
87 const gfx::Rect& clip_rect) override;
88 void Paint(SkCanvas* canvas, const gfx::Rect& rect) override;
89 void SetFocus(bool focused) override;
90 bool HandleInputEvent(const blink::WebInputEvent& event,
91 WebCursor::CursorInfo* cursor_info) override;
92 NPObject* GetPluginScriptableObject() override;
93 NPP GetPluginNPP() override;
94 bool GetFormValue(base::string16* value) override;
95 void DidFinishLoadWithReason(const GURL& url,
96 NPReason reason,
97 int notify_id) override;
98 int GetProcessId() override;
99 void SendJavaScriptStream(const GURL& url,
100 const std::string& result,
101 bool success,
102 int notify_id) override;
103 void DidReceiveManualResponse(const GURL& url,
104 const std::string& mime_type,
105 const std::string& headers,
106 uint32 expected_length,
107 uint32 last_modified) override;
108 void DidReceiveManualData(const char* buffer, int length) override;
109 void DidFinishManualLoading() override;
110 void DidManualLoadFail() override;
111 WebPluginResourceClient* CreateResourceClient(unsigned long resource_id,
112 const GURL& url,
113 int notify_id) override;
114 WebPluginResourceClient* CreateSeekableResourceClient(
115 unsigned long resource_id,
116 int range_request_id) override;
117 void FetchURL(unsigned long resource_id,
118 int notify_id,
119 const GURL& url,
120 const GURL& first_party_for_cookies,
121 const std::string& method,
122 const char* buf,
123 unsigned int len,
124 const Referrer& referrer,
125 bool notify_redirects,
126 bool is_plugin_src_load,
127 int origin_pid,
128 int render_frame_id,
129 int render_view_id) override;
130 // End of WebPluginDelegate implementation.
132 gfx::PluginWindowHandle windowed_handle() const { return windowed_handle_; }
133 bool IsWindowless() const { return windowless_; }
134 PluginInstance* instance() { return instance_.get(); }
135 gfx::Rect GetRect() const { return window_rect_; }
136 gfx::Rect GetClipRect() const { return clip_rect_; }
138 // Returns the path for the library implementing this plugin.
139 base::FilePath GetPluginPath();
141 // Returns a combination of PluginQuirks.
142 int GetQuirks() const { return quirks_; }
144 // Informs the plugin that the view it is in has gained or lost focus.
145 void SetContentAreaHasFocus(bool has_focus);
147 #if defined(OS_WIN)
148 // Informs the plug-in that an IME has changed its status.
149 void ImeCompositionUpdated(const base::string16& text,
150 const std::vector<int>& clauses,
151 const std::vector<int>& target,
152 int cursor_position);
154 // Informs the plugin that IME composition has completed./ If |text| is empty,
155 // IME was cancelled.
156 void ImeCompositionCompleted(const base::string16& text);
158 // Returns the IME status retrieved from a plug-in.
159 bool GetIMEStatus(int* input_type, gfx::Rect* caret_rect);
160 #endif
162 #if defined(OS_MACOSX) && !defined(USE_AURA)
163 // Informs the plugin that the geometry has changed, as with UpdateGeometry,
164 // but also includes the new buffer context for that new geometry.
165 void UpdateGeometryAndContext(const gfx::Rect& window_rect,
166 const gfx::Rect& clip_rect,
167 gfx::NativeDrawingContext context);
168 // Informs the delegate that the plugin called NPN_Invalidate*. Used as a
169 // trigger for Core Animation drawing.
170 void PluginDidInvalidate();
171 // Returns the delegate currently processing events.
172 static WebPluginDelegateImpl* GetActiveDelegate();
173 // Informs the plugin that the window it is in has gained or lost focus.
174 void SetWindowHasFocus(bool has_focus);
175 // Informs the plugin that its tab or window has been hidden or shown.
176 void SetContainerVisibility(bool is_visible);
177 // Informs the plugin that its containing window's frame has changed.
178 // Frames are in screen coordinates.
179 void WindowFrameChanged(const gfx::Rect& window_frame,
180 const gfx::Rect& view_frame);
181 // Informs the plugin that IME composition has completed.
182 // If |text| is empty, IME was cancelled.
183 void ImeCompositionCompleted(const base::string16& text);
184 // Informs the delegate that the plugin set a Cocoa NSCursor.
185 void SetNSCursor(NSCursor* cursor);
187 // Indicates that the windowless plugins will draw directly to the window
188 // context instead of a buffer context.
189 void SetNoBufferContext();
191 // TODO(caryclark): This is a temporary workaround to allow the Darwin / Skia
192 // port to share code with the Darwin / CG port. Later, this will be removed
193 // and all callers will use the Paint defined above.
194 void CGPaint(CGContextRef context, const gfx::Rect& rect);
195 #endif // OS_MACOSX && !USE_AURA
197 private:
198 friend class base::DeleteHelper<WebPluginDelegateImpl>;
199 friend class WebPluginDelegate;
201 WebPluginDelegateImpl(WebPlugin* plugin, PluginInstance* instance);
202 ~WebPluginDelegateImpl() override;
204 // Called by Initialize() for platform-specific initialization.
205 // If this returns false, the plugin shouldn't be started--see Initialize().
206 bool PlatformInitialize();
208 // Called by DestroyInstance(), used for platform-specific destruction.
209 void PlatformDestroyInstance();
211 //--------------------------
212 // used for windowed plugins
213 void WindowedUpdateGeometry(const gfx::Rect& window_rect,
214 const gfx::Rect& clip_rect);
215 // Create the native window.
216 // Returns true if the window is created (or already exists).
217 // Returns false if unable to create the window.
218 bool WindowedCreatePlugin();
220 // Destroy the native window.
221 void WindowedDestroyWindow();
223 // Reposition the native window to be in sync with the given geometry.
224 // Returns true if the native window has moved or been clipped differently.
225 bool WindowedReposition(const gfx::Rect& window_rect,
226 const gfx::Rect& clip_rect);
228 // Tells the plugin about the current state of the window.
229 // See NPAPI NPP_SetWindow for more information.
230 void WindowedSetWindow();
232 #if defined(OS_WIN)
233 // Registers the window class for our window
234 ATOM RegisterNativeWindowClass();
236 // Our WndProc functions.
237 static LRESULT CALLBACK WrapperWindowProc(
238 HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
239 static LRESULT CALLBACK NativeWndProc(
240 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
241 static LRESULT CALLBACK FlashWindowlessWndProc(
242 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
243 static LRESULT CALLBACK DummyWindowProc(
244 HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
246 // Used for throttling Flash messages.
247 static void ClearThrottleQueueForWindow(HWND window);
248 static void OnThrottleMessage();
249 static void ThrottleMessage(WNDPROC proc, HWND hwnd, UINT message,
250 WPARAM wParam, LPARAM lParam);
251 #endif
253 //----------------------------
254 // used for windowless plugins
255 void WindowlessUpdateGeometry(const gfx::Rect& window_rect,
256 const gfx::Rect& clip_rect);
257 void WindowlessPaint(gfx::NativeDrawingContext hdc, const gfx::Rect& rect);
259 // Tells the plugin about the current state of the window.
260 // See NPAPI NPP_SetWindow for more information.
261 void WindowlessSetWindow();
263 // Informs the plugin that it has gained or lost keyboard focus (on the Mac,
264 // this just means window first responder status).
265 void SetPluginHasFocus(bool focused);
267 // Handles the platform specific details of setting plugin focus. Returns
268 // false if the platform cancelled the focus tranfer.
269 bool PlatformSetPluginHasFocus(bool focused);
271 //-----------------------------------------
272 // used for windowed and windowless plugins
274 // Does platform-specific event handling. Arguments and return are identical
275 // to HandleInputEvent.
276 bool PlatformHandleInputEvent(const blink::WebInputEvent& event,
277 WebCursor::CursorInfo* cursor_info);
279 // Closes down and destroys our plugin instance.
280 void DestroyInstance();
283 // used for windowed plugins
284 // Note: on Mac OS X, the only time the windowed handle is non-zero
285 // is the case of accelerated rendering, which uses a fake window handle to
286 // identify itself back to the browser. It still performs all of its
287 // work offscreen.
288 gfx::PluginWindowHandle windowed_handle_;
289 gfx::Rect windowed_last_pos_;
291 bool windowed_did_set_window_;
293 // used by windowed and windowless plugins
294 bool windowless_;
296 WebPlugin* plugin_;
297 scoped_refptr<PluginInstance> instance_;
299 #if defined(OS_WIN)
300 // Original wndproc before we subclassed.
301 WNDPROC plugin_wnd_proc_;
303 // Used to throttle WM_USER+1 messages in Flash.
304 uint32 last_message_;
305 bool is_calling_wndproc;
307 // An IME emulator used by a windowless plug-in to retrieve IME data through
308 // IMM32 functions.
309 scoped_ptr<WebPluginIMEWin> plugin_ime_;
310 #endif // defined(OS_WIN)
312 NPWindow window_;
313 gfx::Rect window_rect_;
314 gfx::Rect clip_rect_;
315 int quirks_;
317 #if defined(OS_WIN)
318 // Windowless plugins don't have keyboard focus causing issues with the
319 // plugin not receiving keyboard events if the plugin enters a modal
320 // loop like TrackPopupMenuEx or MessageBox, etc.
321 // This is a basic issue with windows activation and focus arising due to
322 // the fact that these windows are created by different threads. Activation
323 // and focus are thread specific states, and if the browser has focus,
324 // the plugin may not have focus.
325 // To fix a majority of these activation issues we create a dummy visible
326 // child window to which we set focus whenever the windowless plugin
327 // receives a WM_LBUTTONDOWN/WM_RBUTTONDOWN message via NPP_HandleEvent.
329 HWND dummy_window_for_activation_;
330 HWND dummy_window_parent_;
331 WNDPROC old_dummy_window_proc_;
332 bool CreateDummyWindowForActivation();
334 // Returns true if the event passed in needs to be tracked for a potential
335 // modal loop.
336 static bool ShouldTrackEventForModalLoops(NPEvent* event);
338 // The message filter hook procedure, which tracks modal loops entered by
339 // a plugin in the course of a NPP_HandleEvent call.
340 static LRESULT CALLBACK HandleEventMessageFilterHook(int code, WPARAM wParam,
341 LPARAM lParam);
343 // TrackPopupMenu interceptor. Parameters are the same as the Win32 function
344 // TrackPopupMenu.
345 static BOOL WINAPI TrackPopupMenuPatch(HMENU menu, unsigned int flags, int x,
346 int y, int reserved, HWND window,
347 const RECT* rect);
349 // SetCursor interceptor for windowless plugins.
350 static HCURSOR WINAPI SetCursorPatch(HCURSOR cursor);
352 // RegEnumKeyExW interceptor.
353 static LONG WINAPI RegEnumKeyExWPatch(
354 HKEY key, DWORD index, LPWSTR name, LPDWORD name_size, LPDWORD reserved,
355 LPWSTR class_name, LPDWORD class_size, PFILETIME last_write_time);
357 // GetProcAddress intercepter for windowless plugins.
358 static FARPROC WINAPI GetProcAddressPatch(HMODULE module, LPCSTR name);
360 // WindowFromPoint patch for Flash windowless plugins. When flash receives
361 // mouse move messages it calls the WindowFromPoint API to eventually convert
362 // the mouse coordinates to screen. We need to return the dummy plugin parent
363 // window for Aura to ensure that these conversions occur correctly.
364 static HWND WINAPI WindowFromPointPatch(POINT point);
366 // The mouse hook proc which handles mouse capture in windowed plugins.
367 static LRESULT CALLBACK MouseHookProc(int code, WPARAM wParam,
368 LPARAM lParam);
370 // Calls SetCapture/ReleaseCapture based on the message type.
371 static void HandleCaptureForMessage(HWND window, UINT message);
373 #elif defined(OS_MACOSX) && !defined(USE_AURA)
374 // Sets window_rect_ to |rect|
375 void SetPluginRect(const gfx::Rect& rect);
376 // Sets content_area_origin to |origin|
377 void SetContentAreaOrigin(const gfx::Point& origin);
378 // Updates everything that depends on the plugin's absolute screen location.
379 void PluginScreenLocationChanged();
380 // Updates anything that depends on plugin visibility.
381 void PluginVisibilityChanged();
383 // Starts an IME session.
384 void StartIme();
386 // Informs the browser about the updated accelerated drawing surface.
387 void UpdateAcceleratedSurface();
389 // Uses a CARenderer to draw the plug-in's layer in our OpenGL surface.
390 void DrawLayerInSurface();
392 bool use_buffer_context_;
393 CGContextRef buffer_context_; // Weak ref.
395 CALayer* layer_; // Used for CA drawing mode. Weak, retained by plug-in.
396 WebPluginAcceleratedSurface* surface_; // Weak ref.
397 CARenderer* renderer_; // Renders layer_ to surface_.
398 scoped_ptr<base::RepeatingTimer<WebPluginDelegateImpl> > redraw_timer_;
400 // The upper-left corner of the web content area in screen coordinates,
401 // relative to an upper-left (0,0).
402 gfx::Point content_area_origin_;
404 bool containing_window_has_focus_;
405 bool initial_window_focus_;
406 bool container_is_visible_;
407 bool have_called_set_window_;
409 gfx::Rect cached_clip_rect_;
411 bool ime_enabled_;
412 int keyup_ignore_count_;
414 scoped_ptr<ExternalDragTracker> external_drag_tracker_;
415 #endif // OS_MACOSX && !USE_AURA
417 // Called by the message filter hook when the plugin enters a modal loop.
418 void OnModalLoopEntered();
420 // Returns true if the message passed in corresponds to a user gesture.
421 static bool IsUserGesture(const blink::WebInputEvent& event);
423 // The url with which the plugin was instantiated.
424 std::string plugin_url_;
426 #if defined(OS_WIN)
427 // Indicates the end of a user gesture period.
428 void OnUserGestureEnd();
430 // Handle to the message filter hook
431 HHOOK handle_event_message_filter_hook_;
433 // Event which is set when the plugin enters a modal loop in the course
434 // of a NPP_HandleEvent call.
435 HANDLE handle_event_pump_messages_event_;
437 // This flag indicates whether we started tracking a user gesture message.
438 bool user_gesture_message_posted_;
440 // Runnable Method Factory used to invoke the OnUserGestureEnd method
441 // asynchronously.
442 base::WeakPtrFactory<WebPluginDelegateImpl> user_gesture_msg_factory_;
444 // Handle to the mouse hook installed for certain windowed plugins like
445 // flash.
446 HHOOK mouse_hook_;
447 #endif
449 // Holds the depth of the HandleEvent callstack.
450 int handle_event_depth_;
452 // Holds the current cursor set by the windowless plugin.
453 WebCursor current_windowless_cursor_;
455 // Set to true initially and indicates if this is the first npp_setwindow
456 // call received by the plugin.
457 bool first_set_window_call_;
459 // True if the plugin thinks it has keyboard focus
460 bool plugin_has_focus_;
461 // True if the plugin element has focus within the web content, regardless of
462 // whether its containing view currently has focus.
463 bool has_webkit_focus_;
464 // True if the containing view currently has focus.
465 // Initially set to true so that plugin focus still works in environments
466 // where SetContentAreaHasFocus is never called. See
467 // https://bugs.webkit.org/show_bug.cgi?id=46013 for details.
468 bool containing_view_has_focus_;
470 // True if NPP_New did not return an error.
471 bool creation_succeeded_;
473 DISALLOW_COPY_AND_ASSIGN(WebPluginDelegateImpl);
476 } // namespace content
478 #endif // CONTENT_CHILD_NPAPI_WEBPLUGIN_DELEGATE_IMPL_H_