OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/npapi/webplugin_delegate_impl.h" | 5 #include "content/child/npapi/webplugin_delegate_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <set> | 11 #include <set> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
17 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
22 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
23 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
24 #include "base/version.h" | 24 #include "base/version.h" |
25 #include "base/win/iat_patch_function.h" | |
26 #include "base/win/registry.h" | |
27 #include "base/win/windows_version.h" | 25 #include "base/win/windows_version.h" |
28 #include "content/child/npapi/plugin_instance.h" | 26 #include "content/child/npapi/plugin_instance.h" |
29 #include "content/child/npapi/plugin_lib.h" | 27 #include "content/child/npapi/plugin_lib.h" |
30 #include "content/child/npapi/webplugin.h" | 28 #include "content/child/npapi/webplugin.h" |
31 #include "content/child/npapi/webplugin_ime_win.h" | |
32 #include "content/common/cursors/webcursor.h" | 29 #include "content/common/cursors/webcursor.h" |
33 #include "content/common/plugin_constants_win.h" | 30 #include "content/common/plugin_constants_win.h" |
34 #include "content/public/common/content_constants.h" | 31 #include "content/public/common/content_constants.h" |
35 #include "skia/ext/platform_canvas.h" | 32 #include "skia/ext/platform_canvas.h" |
36 #include "third_party/WebKit/public/web/WebInputEvent.h" | 33 #include "third_party/WebKit/public/web/WebInputEvent.h" |
37 #include "ui/gfx/win/dpi.h" | 34 #include "ui/gfx/win/dpi.h" |
38 #include "ui/gfx/win/hwnd_util.h" | 35 #include "ui/gfx/win/hwnd_util.h" |
39 | 36 |
40 using blink::WebKeyboardEvent; | 37 using blink::WebKeyboardEvent; |
41 using blink::WebInputEvent; | 38 using blink::WebInputEvent; |
42 using blink::WebMouseEvent; | 39 using blink::WebMouseEvent; |
43 | 40 |
44 namespace content { | 41 namespace content { |
45 | 42 |
46 namespace { | 43 namespace { |
47 | 44 |
48 const wchar_t kWebPluginDelegateProperty[] = L"WebPluginDelegateProperty"; | |
49 | |
50 // The fastest we are willing to process WM_USER+1 events for Flash. | |
51 // Flash can easily exceed the limits of our CPU if we don't throttle it. | |
52 // The throttle has been chosen by testing various delays and compromising | |
53 // on acceptable Flash performance and reasonable CPU consumption. | |
54 // | |
55 // I'd like to make the throttle delay variable, based on the amount of | |
56 // time currently required to paint Flash plugins. There isn't a good | |
57 // way to count the time spent in aggregate plugin painting, however, so | |
58 // this seems to work well enough. | |
59 const int kFlashWMUSERMessageThrottleDelayMs = 5; | |
60 | |
61 // The current instance of the plugin which entered the modal loop. | |
62 WebPluginDelegateImpl* g_current_plugin_instance = NULL; | |
63 | |
64 typedef std::deque<MSG> ThrottleQueue; | |
65 base::LazyInstance<ThrottleQueue> g_throttle_queue = LAZY_INSTANCE_INITIALIZER; | |
66 | |
67 base::LazyInstance<std::map<HWND, WNDPROC> > g_window_handle_proc_map = | |
68 LAZY_INSTANCE_INITIALIZER; | |
69 | |
70 // Helper object for patching the TrackPopupMenu API. | |
71 base::LazyInstance<base::win::IATPatchFunction> g_iat_patch_track_popup_menu = | |
72 LAZY_INSTANCE_INITIALIZER; | |
73 | |
74 // Helper object for patching the SetCursor API. | |
75 base::LazyInstance<base::win::IATPatchFunction> g_iat_patch_set_cursor = | |
76 LAZY_INSTANCE_INITIALIZER; | |
77 | |
78 // Helper object for patching the RegEnumKeyExW API. | |
79 base::LazyInstance<base::win::IATPatchFunction> g_iat_patch_reg_enum_key_ex_w = | |
80 LAZY_INSTANCE_INITIALIZER; | |
81 | |
82 // Helper object for patching the GetProcAddress API. | |
83 base::LazyInstance<base::win::IATPatchFunction> g_iat_patch_get_proc_address = | |
84 LAZY_INSTANCE_INITIALIZER; | |
85 | |
86 base::LazyInstance<base::win::IATPatchFunction> g_iat_patch_window_from_point = | |
87 LAZY_INSTANCE_INITIALIZER; | |
88 | |
89 // http://crbug.com/16114 | 45 // http://crbug.com/16114 |
90 // Enforces providing a valid device context in NPWindow, so that NPP_SetWindow | 46 // Enforces providing a valid device context in NPWindow, so that NPP_SetWindow |
91 // is never called with NPNWindoTypeDrawable and NPWindow set to NULL. | 47 // is never called with NPNWindoTypeDrawable and NPWindow set to NULL. |
92 // Doing so allows removing NPP_SetWindow call during painting a windowless | 48 // Doing so allows removing NPP_SetWindow call during painting a windowless |
93 // plugin, which otherwise could trigger layout change while painting by | 49 // plugin, which otherwise could trigger layout change while painting by |
94 // invoking NPN_Evaluate. Which would cause bad, bad crashes. Bad crashes. | 50 // invoking NPN_Evaluate. Which would cause bad, bad crashes. Bad crashes. |
95 // TODO(dglazkov): If this approach doesn't produce regressions, move class to | 51 // TODO(dglazkov): If this approach doesn't produce regressions, move class to |
96 // webplugin_delegate_impl.h and implement for other platforms. | 52 // webplugin_delegate_impl.h and implement for other platforms. |
97 class DrawableContextEnforcer { | 53 class DrawableContextEnforcer { |
98 public: | 54 public: |
(...skipping 12 matching lines...) Expand all Loading... |
111 | 67 |
112 DeleteDC(static_cast<HDC>(window_->window)); | 68 DeleteDC(static_cast<HDC>(window_->window)); |
113 window_->window = NULL; | 69 window_->window = NULL; |
114 } | 70 } |
115 | 71 |
116 private: | 72 private: |
117 NPWindow* window_; | 73 NPWindow* window_; |
118 bool disposable_dc_; | 74 bool disposable_dc_; |
119 }; | 75 }; |
120 | 76 |
121 // These are from ntddk.h | |
122 typedef LONG NTSTATUS; | |
123 | |
124 #ifndef STATUS_SUCCESS | |
125 #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) | |
126 #endif | |
127 | |
128 #ifndef STATUS_BUFFER_TOO_SMALL | |
129 #define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023L) | |
130 #endif | |
131 | |
132 typedef enum _KEY_INFORMATION_CLASS { | |
133 KeyBasicInformation, | |
134 KeyNodeInformation, | |
135 KeyFullInformation, | |
136 KeyNameInformation, | |
137 KeyCachedInformation, | |
138 KeyVirtualizationInformation | |
139 } KEY_INFORMATION_CLASS; | |
140 | |
141 typedef struct _KEY_NAME_INFORMATION { | |
142 ULONG NameLength; | |
143 WCHAR Name[1]; | |
144 } KEY_NAME_INFORMATION, *PKEY_NAME_INFORMATION; | |
145 | |
146 typedef DWORD (__stdcall *ZwQueryKeyType)( | |
147 HANDLE key_handle, | |
148 int key_information_class, | |
149 PVOID key_information, | |
150 ULONG length, | |
151 PULONG result_length); | |
152 | |
153 // Returns a key's full path. | |
154 std::wstring GetKeyPath(HKEY key) { | |
155 if (key == NULL) | |
156 return L""; | |
157 | |
158 HMODULE dll = GetModuleHandle(L"ntdll.dll"); | |
159 if (dll == NULL) | |
160 return L""; | |
161 | |
162 ZwQueryKeyType func = reinterpret_cast<ZwQueryKeyType>( | |
163 ::GetProcAddress(dll, "ZwQueryKey")); | |
164 if (func == NULL) | |
165 return L""; | |
166 | |
167 DWORD size = 0; | |
168 DWORD result = 0; | |
169 result = func(key, KeyNameInformation, 0, 0, &size); | |
170 if (result != static_cast<DWORD>(STATUS_BUFFER_TOO_SMALL)) | |
171 return L""; | |
172 | |
173 scoped_ptr<char[]> buffer(new char[size]); | |
174 if (buffer.get() == NULL) | |
175 return L""; | |
176 | |
177 result = func(key, KeyNameInformation, buffer.get(), size, &size); | |
178 if (result != static_cast<DWORD>(STATUS_SUCCESS)) | |
179 return L""; | |
180 | |
181 KEY_NAME_INFORMATION* info = | |
182 reinterpret_cast<KEY_NAME_INFORMATION*>(buffer.get()); | |
183 return std::wstring(info->Name, info->NameLength / sizeof(wchar_t)); | |
184 } | |
185 | |
186 uint32_t GetPluginMajorVersion(const WebPluginInfo& plugin_info) { | |
187 Version plugin_version; | |
188 WebPluginInfo::CreateVersionFromString(plugin_info.version, &plugin_version); | |
189 | |
190 uint32_t major_version = 0; | |
191 if (plugin_version.IsValid()) | |
192 major_version = plugin_version.components()[0]; | |
193 | |
194 return major_version; | |
195 } | |
196 | |
197 } // namespace | 77 } // namespace |
198 | 78 |
199 LRESULT CALLBACK WebPluginDelegateImpl::HandleEventMessageFilterHook( | |
200 int code, WPARAM wParam, LPARAM lParam) { | |
201 if (g_current_plugin_instance) { | |
202 g_current_plugin_instance->OnModalLoopEntered(); | |
203 } else { | |
204 NOTREACHED(); | |
205 } | |
206 return CallNextHookEx(NULL, code, wParam, lParam); | |
207 } | |
208 | |
209 WebPluginDelegateImpl::WebPluginDelegateImpl(WebPlugin* plugin, | 79 WebPluginDelegateImpl::WebPluginDelegateImpl(WebPlugin* plugin, |
210 PluginInstance* instance) | 80 PluginInstance* instance) |
211 : plugin_(plugin), | 81 : plugin_(plugin), |
212 instance_(instance), | 82 instance_(instance), |
213 quirks_(0), | 83 quirks_(0), |
214 dummy_window_for_activation_(NULL), | |
215 dummy_window_parent_(NULL), | |
216 old_dummy_window_proc_(NULL), | |
217 handle_event_message_filter_hook_(NULL), | |
218 handle_event_pump_messages_event_(NULL), | |
219 handle_event_depth_(0), | 84 handle_event_depth_(0), |
220 first_set_window_call_(true), | 85 first_set_window_call_(true), |
221 plugin_has_focus_(false), | 86 plugin_has_focus_(false), |
222 has_webkit_focus_(false), | 87 has_webkit_focus_(false), |
223 containing_view_has_focus_(true), | 88 containing_view_has_focus_(true), |
224 creation_succeeded_(false), | 89 creation_succeeded_(false), |
225 user_gesture_msg_factory_(this) { | 90 user_gesture_msg_factory_(this) { |
226 memset(&window_, 0, sizeof(window_)); | 91 memset(&window_, 0, sizeof(window_)); |
227 | |
228 const WebPluginInfo& plugin_info = instance_->plugin_lib()->plugin_info(); | |
229 base::string16 filename = | |
230 base::ToLowerASCII(plugin_info.path.BaseName().value()); | |
231 | |
232 if (instance_->mime_type() == kFlashPluginSwfMimeType || | |
233 filename == kFlashPlugin) { | |
234 // Flash only requests windowless plugins if we return a Mozilla user | |
235 // agent. | |
236 instance_->set_use_mozilla_user_agent(); | |
237 quirks_ |= PLUGIN_QUIRK_THROTTLE_WM_USER_PLUS_ONE; | |
238 quirks_ |= PLUGIN_QUIRK_PATCH_SETCURSOR; | |
239 quirks_ |= PLUGIN_QUIRK_ALWAYS_NOTIFY_SUCCESS; | |
240 quirks_ |= PLUGIN_QUIRK_HANDLE_MOUSE_CAPTURE; | |
241 quirks_ |= PLUGIN_QUIRK_EMULATE_IME; | |
242 quirks_ |= PLUGIN_QUIRK_FAKE_WINDOW_FROM_POINT; | |
243 } else if (filename == kAcrobatReaderPlugin) { | |
244 // Check for the version number above or equal 9. | |
245 uint32_t major_version = GetPluginMajorVersion(plugin_info); | |
246 if (major_version >= 9) { | |
247 quirks_ |= PLUGIN_QUIRK_DIE_AFTER_UNLOAD; | |
248 // 9.2 needs this. | |
249 quirks_ |= PLUGIN_QUIRK_SETWINDOW_TWICE; | |
250 } | |
251 quirks_ |= PLUGIN_QUIRK_BLOCK_NONSTANDARD_GETURL_REQUESTS; | |
252 } else if (plugin_info.name.find(L"Windows Media Player") != | |
253 std::wstring::npos) { | |
254 // Windows Media Player needs two NPP_SetWindow calls. | |
255 quirks_ |= PLUGIN_QUIRK_SETWINDOW_TWICE; | |
256 | |
257 // Windowless mode doesn't work in the WMP NPAPI plugin. | |
258 quirks_ |= PLUGIN_QUIRK_NO_WINDOWLESS; | |
259 | |
260 // The media player plugin sets its size on the first NPP_SetWindow call | |
261 // and never updates its size. We should call the underlying NPP_SetWindow | |
262 // only when we have the correct size. | |
263 quirks_ |= PLUGIN_QUIRK_IGNORE_FIRST_SETWINDOW_CALL; | |
264 | |
265 if (filename == kOldWMPPlugin) { | |
266 // Non-admin users on XP couldn't modify the key to force the new UI. | |
267 quirks_ |= PLUGIN_QUIRK_PATCH_REGENUMKEYEXW; | |
268 } | |
269 } else if (instance_->mime_type() == "audio/x-pn-realaudio-plugin" || | |
270 filename == kRealPlayerPlugin) { | |
271 quirks_ |= PLUGIN_QUIRK_DONT_CALL_WND_PROC_RECURSIVELY; | |
272 } else if (plugin_info.name.find(L"VLC Multimedia Plugin") != | |
273 std::wstring::npos || | |
274 plugin_info.name.find(L"VLC Multimedia Plug-in") != | |
275 std::wstring::npos) { | |
276 // VLC hangs on NPP_Destroy if we call NPP_SetWindow with a null window | |
277 // handle | |
278 quirks_ |= PLUGIN_QUIRK_DONT_SET_NULL_WINDOW_HANDLE_ON_DESTROY; | |
279 uint32_t major_version = GetPluginMajorVersion(plugin_info); | |
280 if (major_version == 0) { | |
281 // VLC 0.8.6d and 0.8.6e crash if multiple instances are created. | |
282 quirks_ |= PLUGIN_QUIRK_DONT_ALLOW_MULTIPLE_INSTANCES; | |
283 } | |
284 } else if (filename == kSilverlightPlugin) { | |
285 // Explanation for this quirk can be found in | |
286 // WebPluginDelegateImpl::Initialize. | |
287 quirks_ |= PLUGIN_QUIRK_PATCH_SETCURSOR; | |
288 } else if (plugin_info.name.find(L"DivX Web Player") != | |
289 std::wstring::npos) { | |
290 // The divx plugin sets its size on the first NPP_SetWindow call and never | |
291 // updates its size. We should call the underlying NPP_SetWindow only when | |
292 // we have the correct size. | |
293 quirks_ |= PLUGIN_QUIRK_IGNORE_FIRST_SETWINDOW_CALL; | |
294 } | |
295 } | 92 } |
296 | 93 |
297 WebPluginDelegateImpl::~WebPluginDelegateImpl() { | 94 WebPluginDelegateImpl::~WebPluginDelegateImpl() { |
298 if (::IsWindow(dummy_window_for_activation_)) { | |
299 WNDPROC current_wnd_proc = reinterpret_cast<WNDPROC>( | |
300 GetWindowLongPtr(dummy_window_for_activation_, GWLP_WNDPROC)); | |
301 if (current_wnd_proc == DummyWindowProc) { | |
302 SetWindowLongPtr(dummy_window_for_activation_, | |
303 GWLP_WNDPROC, | |
304 reinterpret_cast<LONG_PTR>(old_dummy_window_proc_)); | |
305 } | |
306 ::DestroyWindow(dummy_window_for_activation_); | |
307 } | |
308 | |
309 DestroyInstance(); | 95 DestroyInstance(); |
310 | |
311 if (handle_event_pump_messages_event_) { | |
312 CloseHandle(handle_event_pump_messages_event_); | |
313 } | |
314 } | 96 } |
315 | 97 |
316 bool WebPluginDelegateImpl::PlatformInitialize() { | 98 bool WebPluginDelegateImpl::PlatformInitialize() { |
317 CreateDummyWindowForActivation(); | |
318 handle_event_pump_messages_event_ = CreateEvent(NULL, TRUE, FALSE, NULL); | |
319 plugin_->SetWindowlessData( | |
320 handle_event_pump_messages_event_, | |
321 reinterpret_cast<gfx::NativeViewId>(dummy_window_for_activation_)); | |
322 | |
323 // Windowless plugins call the WindowFromPoint API and passes the result of | |
324 // that to the TrackPopupMenu API call as the owner window. This causes the | |
325 // API to fail as the API expects the window handle to live on the same | |
326 // thread as the caller. It works in the other browsers as the plugin lives | |
327 // on the browser thread. Our workaround is to intercept the TrackPopupMenu | |
328 // API and replace the window handle with the dummy activation window. | |
329 if (!g_iat_patch_track_popup_menu.Pointer()->is_patched()) { | |
330 g_iat_patch_track_popup_menu.Pointer()->Patch( | |
331 GetPluginPath().value().c_str(), "user32.dll", "TrackPopupMenu", | |
332 WebPluginDelegateImpl::TrackPopupMenuPatch); | |
333 } | |
334 | |
335 // Windowless plugins can set cursors by calling the SetCursor API. This | |
336 // works because the thread inputs of the browser UI thread and the plugin | |
337 // thread are attached. We intercept the SetCursor API for windowless | |
338 // plugins and remember the cursor being set. This is shipped over to the | |
339 // browser in the HandleEvent call, which ensures that the cursor does not | |
340 // change when a windowless plugin instance changes the cursor | |
341 // in a background tab. | |
342 if (!g_iat_patch_set_cursor.Pointer()->is_patched() && | |
343 (quirks_ & PLUGIN_QUIRK_PATCH_SETCURSOR)) { | |
344 g_iat_patch_set_cursor.Pointer()->Patch( | |
345 GetPluginPath().value().c_str(), "user32.dll", "SetCursor", | |
346 WebPluginDelegateImpl::SetCursorPatch); | |
347 } | |
348 | |
349 // On XP, WMP will use its old UI unless a registry key under HKLM has the | |
350 // name of the current process. We do it in the installer for admin users, | |
351 // for the rest patch this function. | |
352 if ((quirks_ & PLUGIN_QUIRK_PATCH_REGENUMKEYEXW) && | |
353 base::win::GetVersion() == base::win::VERSION_XP && | |
354 (base::win::RegKey().Open(HKEY_LOCAL_MACHINE, | |
355 L"SOFTWARE\\Microsoft\\MediaPlayer\\ShimInclusionList\\chrome.exe", | |
356 KEY_READ) != ERROR_SUCCESS) && | |
357 !g_iat_patch_reg_enum_key_ex_w.Pointer()->is_patched()) { | |
358 g_iat_patch_reg_enum_key_ex_w.Pointer()->Patch( | |
359 L"wmpdxm.dll", "advapi32.dll", "RegEnumKeyExW", | |
360 WebPluginDelegateImpl::RegEnumKeyExWPatch); | |
361 } | |
362 | |
363 // Flash retrieves the pointers to IMM32 functions with GetProcAddress() calls | |
364 // and use them to retrieve IME data. We add a patch to this function so we | |
365 // can dispatch these IMM32 calls to the WebPluginIMEWin class, which emulates | |
366 // IMM32 functions for Flash. | |
367 if (!g_iat_patch_get_proc_address.Pointer()->is_patched() && | |
368 (quirks_ & PLUGIN_QUIRK_EMULATE_IME)) { | |
369 g_iat_patch_get_proc_address.Pointer()->Patch( | |
370 GetPluginPath().value().c_str(), "kernel32.dll", "GetProcAddress", | |
371 GetProcAddressPatch); | |
372 } | |
373 | |
374 if (!g_iat_patch_window_from_point.Pointer()->is_patched() && | |
375 (quirks_ & PLUGIN_QUIRK_FAKE_WINDOW_FROM_POINT)) { | |
376 g_iat_patch_window_from_point.Pointer()->Patch( | |
377 GetPluginPath().value().c_str(), "user32.dll", "WindowFromPoint", | |
378 WebPluginDelegateImpl::WindowFromPointPatch); | |
379 } | |
380 | |
381 return true; | 99 return true; |
382 } | 100 } |
383 | 101 |
384 void WebPluginDelegateImpl::PlatformDestroyInstance() { | 102 void WebPluginDelegateImpl::PlatformDestroyInstance() { |
385 if (!instance_->plugin_lib()) | |
386 return; | |
387 | |
388 // Unpatch if this is the last plugin instance. | |
389 if (instance_->plugin_lib()->instance_count() != 1) | |
390 return; | |
391 | |
392 if (g_iat_patch_set_cursor.Pointer()->is_patched()) | |
393 g_iat_patch_set_cursor.Pointer()->Unpatch(); | |
394 | |
395 if (g_iat_patch_track_popup_menu.Pointer()->is_patched()) | |
396 g_iat_patch_track_popup_menu.Pointer()->Unpatch(); | |
397 | |
398 if (g_iat_patch_reg_enum_key_ex_w.Pointer()->is_patched()) | |
399 g_iat_patch_reg_enum_key_ex_w.Pointer()->Unpatch(); | |
400 | |
401 if (g_iat_patch_window_from_point.Pointer()->is_patched()) | |
402 g_iat_patch_window_from_point.Pointer()->Unpatch(); | |
403 } | 103 } |
404 | 104 |
405 void WebPluginDelegateImpl::Paint(SkCanvas* canvas, const gfx::Rect& rect) { | 105 void WebPluginDelegateImpl::Paint(SkCanvas* canvas, const gfx::Rect& rect) { |
406 if (skia::SupportsPlatformPaint(canvas)) { | 106 if (skia::SupportsPlatformPaint(canvas)) { |
407 skia::ScopedPlatformPaint scoped_platform_paint(canvas); | 107 skia::ScopedPlatformPaint scoped_platform_paint(canvas); |
408 HDC hdc = scoped_platform_paint.GetPlatformSurface(); | 108 HDC hdc = scoped_platform_paint.GetPlatformSurface(); |
409 WindowlessPaint(hdc, rect); | 109 WindowlessPaint(hdc, rect); |
410 } | 110 } |
411 } | 111 } |
412 | 112 |
413 // Erase all messages in the queue destined for a particular window. | |
414 // When windows are closing, callers should use this function to clear | |
415 // the queue. | |
416 // static | |
417 void WebPluginDelegateImpl::ClearThrottleQueueForWindow(HWND window) { | |
418 ThrottleQueue* throttle_queue = g_throttle_queue.Pointer(); | |
419 | |
420 ThrottleQueue::iterator it; | |
421 for (it = throttle_queue->begin(); it != throttle_queue->end(); ) { | |
422 if (it->hwnd == window) { | |
423 it = throttle_queue->erase(it); | |
424 } else { | |
425 it++; | |
426 } | |
427 } | |
428 } | |
429 | |
430 // Delayed callback for processing throttled messages. | |
431 // Throttled messages are aggregated globally across all plugins. | |
432 // static | |
433 void WebPluginDelegateImpl::OnThrottleMessage() { | |
434 // The current algorithm walks the list and processes the first | |
435 // message it finds for each plugin. It is important to service | |
436 // all active plugins with each pass through the throttle, otherwise | |
437 // we see video jankiness. Copy the set to notify before notifying | |
438 // since we may re-enter OnThrottleMessage from CallWindowProc! | |
439 ThrottleQueue* throttle_queue = g_throttle_queue.Pointer(); | |
440 ThrottleQueue notify_queue; | |
441 std::set<HWND> processed; | |
442 | |
443 ThrottleQueue::iterator it = throttle_queue->begin(); | |
444 while (it != throttle_queue->end()) { | |
445 const MSG& msg = *it; | |
446 if (processed.find(msg.hwnd) == processed.end()) { | |
447 processed.insert(msg.hwnd); | |
448 notify_queue.push_back(msg); | |
449 it = throttle_queue->erase(it); | |
450 } else { | |
451 it++; | |
452 } | |
453 } | |
454 | |
455 // Due to re-entrancy, we must save our queue state now. Otherwise, we may | |
456 // self-post below, and *also* start up another delayed task when the first | |
457 // entry is pushed onto the queue in ThrottleMessage(). | |
458 bool throttle_queue_was_empty = throttle_queue->empty(); | |
459 | |
460 for (it = notify_queue.begin(); it != notify_queue.end(); ++it) { | |
461 const MSG& msg = *it; | |
462 WNDPROC proc = reinterpret_cast<WNDPROC>(msg.time); | |
463 // It is possible that the window was closed after we queued | |
464 // this message. This is a rare event; just verify the window | |
465 // is alive. (see also bug 1259488) | |
466 if (IsWindow(msg.hwnd)) | |
467 CallWindowProc(proc, msg.hwnd, msg.message, msg.wParam, msg.lParam); | |
468 } | |
469 | |
470 if (!throttle_queue_was_empty) { | |
471 base::MessageLoop::current()->PostDelayedTask( | |
472 FROM_HERE, | |
473 base::Bind(&WebPluginDelegateImpl::OnThrottleMessage), | |
474 base::TimeDelta::FromMilliseconds(kFlashWMUSERMessageThrottleDelayMs)); | |
475 } | |
476 } | |
477 | |
478 // Schedule a windows message for delivery later. | |
479 // static | |
480 void WebPluginDelegateImpl::ThrottleMessage(WNDPROC proc, HWND hwnd, | |
481 UINT message, WPARAM wParam, | |
482 LPARAM lParam) { | |
483 MSG msg; | |
484 // Cast through uintptr_t and then DWORD to make the truncation explicit. | |
485 msg.time = static_cast<DWORD>(reinterpret_cast<uintptr_t>(proc)); | |
486 msg.hwnd = hwnd; | |
487 msg.message = message; | |
488 msg.wParam = wParam; | |
489 msg.lParam = lParam; | |
490 | |
491 ThrottleQueue* throttle_queue = g_throttle_queue.Pointer(); | |
492 | |
493 throttle_queue->push_back(msg); | |
494 | |
495 if (throttle_queue->size() == 1) { | |
496 base::MessageLoop::current()->PostDelayedTask( | |
497 FROM_HERE, | |
498 base::Bind(&WebPluginDelegateImpl::OnThrottleMessage), | |
499 base::TimeDelta::FromMilliseconds(kFlashWMUSERMessageThrottleDelayMs)); | |
500 } | |
501 } | |
502 | |
503 // We go out of our way to find the hidden windows created by Flash for | |
504 // windowless plugins. We throttle the rate at which they deliver messages | |
505 // so that they will not consume outrageous amounts of CPU. | |
506 // static | |
507 LRESULT CALLBACK WebPluginDelegateImpl::FlashWindowlessWndProc( | |
508 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | |
509 std::map<HWND, WNDPROC>::iterator index = | |
510 g_window_handle_proc_map.Get().find(hwnd); | |
511 | |
512 WNDPROC old_proc = (*index).second; | |
513 DCHECK(old_proc); | |
514 | |
515 switch (message) { | |
516 case WM_NCDESTROY: { | |
517 WebPluginDelegateImpl::ClearThrottleQueueForWindow(hwnd); | |
518 g_window_handle_proc_map.Get().erase(index); | |
519 break; | |
520 } | |
521 // Flash may flood the message queue with WM_USER+1 message causing 100% CPU | |
522 // usage. See https://bugzilla.mozilla.org/show_bug.cgi?id=132759. We | |
523 // prevent this by throttling the messages. | |
524 case WM_USER + 1: { | |
525 WebPluginDelegateImpl::ThrottleMessage(old_proc, hwnd, message, wparam, | |
526 lparam); | |
527 return TRUE; | |
528 } | |
529 | |
530 default: { | |
531 break; | |
532 } | |
533 } | |
534 return CallWindowProc(old_proc, hwnd, message, wparam, lparam); | |
535 } | |
536 | |
537 LRESULT CALLBACK WebPluginDelegateImpl::DummyWindowProc( | |
538 HWND hwnd, UINT message, WPARAM w_param, LPARAM l_param) { | |
539 WebPluginDelegateImpl* delegate = reinterpret_cast<WebPluginDelegateImpl*>( | |
540 GetProp(hwnd, kWebPluginDelegateProperty)); | |
541 CHECK(delegate); | |
542 if (message == WM_WINDOWPOSCHANGING) { | |
543 // We need to know when the dummy window is parented because windowless | |
544 // plugins need the parent window for things like menus. There's no message | |
545 // for a parent being changed, but a WM_WINDOWPOSCHANGING is sent so we | |
546 // check every time we get it. | |
547 // For non-aura builds, this never changes since RenderWidgetHostViewWin's | |
548 // window is constant. For aura builds, this changes every time the tab gets | |
549 // dragged to a new window. | |
550 HWND parent = GetParent(hwnd); | |
551 if (parent != delegate->dummy_window_parent_) { | |
552 delegate->dummy_window_parent_ = parent; | |
553 | |
554 // Set the containing window handle as the instance window handle. This is | |
555 // what Safari does. Not having a valid window handle causes subtle bugs | |
556 // with plugins which retrieve the window handle and use it for things | |
557 // like context menus. The window handle can be retrieved via | |
558 // NPN_GetValue of NPNVnetscapeWindow. | |
559 delegate->instance_->set_window_handle(parent); | |
560 | |
561 // The plugin caches the result of NPNVnetscapeWindow when we originally | |
562 // called NPP_SetWindow, so force it to get the new value. | |
563 delegate->WindowlessSetWindow(); | |
564 } | |
565 } else if (message == WM_NCDESTROY) { | |
566 RemoveProp(hwnd, kWebPluginDelegateProperty); | |
567 } | |
568 return CallWindowProc( | |
569 delegate->old_dummy_window_proc_, hwnd, message, w_param, l_param); | |
570 } | |
571 | |
572 // Callback for enumerating the Flash windows. | |
573 BOOL CALLBACK EnumFlashWindows(HWND window, LPARAM arg) { | |
574 WNDPROC wnd_proc = reinterpret_cast<WNDPROC>(arg); | |
575 TCHAR class_name[1024]; | |
576 if (!RealGetWindowClass(window, class_name, | |
577 sizeof(class_name)/sizeof(TCHAR))) { | |
578 LOG(ERROR) << "RealGetWindowClass failure: " << GetLastError(); | |
579 return FALSE; | |
580 } | |
581 | |
582 if (wcscmp(class_name, L"SWFlash_PlaceholderX")) | |
583 return TRUE; | |
584 | |
585 WNDPROC current_wnd_proc = reinterpret_cast<WNDPROC>( | |
586 GetWindowLongPtr(window, GWLP_WNDPROC)); | |
587 if (current_wnd_proc != wnd_proc) { | |
588 WNDPROC old_flash_proc = reinterpret_cast<WNDPROC>(SetWindowLongPtr( | |
589 window, GWLP_WNDPROC, | |
590 reinterpret_cast<LONG_PTR>(wnd_proc))); | |
591 DCHECK(old_flash_proc); | |
592 g_window_handle_proc_map.Get()[window] = old_flash_proc; | |
593 } | |
594 | |
595 return TRUE; | |
596 } | |
597 | |
598 bool WebPluginDelegateImpl::CreateDummyWindowForActivation() { | |
599 DCHECK(!dummy_window_for_activation_); | |
600 | |
601 dummy_window_for_activation_ = CreateWindowEx( | |
602 0, | |
603 L"Static", | |
604 kDummyActivationWindowName, | |
605 WS_CHILD, | |
606 0, | |
607 0, | |
608 0, | |
609 0, | |
610 // We don't know the parent of the dummy window yet, so just set it to the | |
611 // desktop and it'll get parented by the browser. | |
612 GetDesktopWindow(), | |
613 0, | |
614 GetModuleHandle(NULL), | |
615 0); | |
616 | |
617 if (dummy_window_for_activation_ == 0) | |
618 return false; | |
619 | |
620 BOOL result = SetProp(dummy_window_for_activation_, | |
621 kWebPluginDelegateProperty, this); | |
622 DCHECK(result == TRUE) << "SetProp failed, last error = " << GetLastError(); | |
623 old_dummy_window_proc_ = reinterpret_cast<WNDPROC>(SetWindowLongPtr( | |
624 dummy_window_for_activation_, GWLP_WNDPROC, | |
625 reinterpret_cast<LONG_PTR>(DummyWindowProc))); | |
626 | |
627 // Flash creates background windows which use excessive CPU in our | |
628 // environment; we wrap these windows and throttle them so that they don't | |
629 // get out of hand. | |
630 if (!EnumThreadWindows(::GetCurrentThreadId(), EnumFlashWindows, | |
631 reinterpret_cast<LPARAM>( | |
632 &WebPluginDelegateImpl::FlashWindowlessWndProc))) { | |
633 // Log that this happened. Flash will still work; it just means the | |
634 // throttle isn't installed (and Flash will use more CPU). | |
635 NOTREACHED(); | |
636 LOG(ERROR) << "Failed to wrap all windowless Flash windows"; | |
637 } | |
638 return true; | |
639 } | |
640 | |
641 // Returns true if the message passed in corresponds to a user gesture. | 113 // Returns true if the message passed in corresponds to a user gesture. |
642 static bool IsUserGestureMessage(unsigned int message) { | 114 static bool IsUserGestureMessage(unsigned int message) { |
643 switch (message) { | 115 switch (message) { |
644 case WM_LBUTTONDOWN: | 116 case WM_LBUTTONDOWN: |
645 case WM_LBUTTONUP: | 117 case WM_LBUTTONUP: |
646 case WM_RBUTTONDOWN: | 118 case WM_RBUTTONDOWN: |
647 case WM_RBUTTONUP: | 119 case WM_RBUTTONUP: |
648 case WM_MBUTTONDOWN: | 120 case WM_MBUTTONDOWN: |
649 case WM_MBUTTONUP: | 121 case WM_MBUTTONUP: |
650 case WM_KEYDOWN: | 122 case WM_KEYDOWN: |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 | 325 |
854 bool WebPluginDelegateImpl::PlatformHandleInputEvent( | 326 bool WebPluginDelegateImpl::PlatformHandleInputEvent( |
855 const WebInputEvent& event, WebCursor::CursorInfo* cursor_info) { | 327 const WebInputEvent& event, WebCursor::CursorInfo* cursor_info) { |
856 DCHECK(cursor_info != NULL); | 328 DCHECK(cursor_info != NULL); |
857 | 329 |
858 NPEvent np_event; | 330 NPEvent np_event; |
859 if (!NPEventFromWebInputEvent(event, &np_event)) { | 331 if (!NPEventFromWebInputEvent(event, &np_event)) { |
860 return false; | 332 return false; |
861 } | 333 } |
862 | 334 |
863 // Allow this plugin to access this IME emulator through IMM32 API while the | |
864 // plugin is processing this event. | |
865 if (GetQuirks() & PLUGIN_QUIRK_EMULATE_IME) { | |
866 if (!plugin_ime_) | |
867 plugin_ime_.reset(new WebPluginIMEWin); | |
868 } | |
869 WebPluginIMEWin::ScopedLock lock( | |
870 event.isKeyboardEventType(event.type) ? plugin_ime_.get() : NULL); | |
871 | |
872 HWND last_focus_window = NULL; | |
873 | |
874 if (ShouldTrackEventForModalLoops(&np_event)) { | |
875 // A windowless plugin can enter a modal loop in a NPP_HandleEvent call. | |
876 // For e.g. Flash puts up a context menu when we right click on the | |
877 // windowless plugin area. We detect this by setting up a message filter | |
878 // hook pror to calling NPP_HandleEvent on the plugin and unhook on | |
879 // return from NPP_HandleEvent. If the plugin does enter a modal loop | |
880 // in that context we unhook on receiving the first notification in | |
881 // the message filter hook. | |
882 handle_event_message_filter_hook_ = | |
883 SetWindowsHookEx(WH_MSGFILTER, HandleEventMessageFilterHook, NULL, | |
884 GetCurrentThreadId()); | |
885 // To ensure that the plugin receives keyboard events we set focus to the | |
886 // dummy window. | |
887 // TODO(iyengar) We need a framework in the renderer to identify which | |
888 // windowless plugin is under the mouse and to handle this. This would | |
889 // also require some changes in RenderWidgetHost to detect this in the | |
890 // WM_MOUSEACTIVATE handler and inform the renderer accordingly. | |
891 bool valid = GetParent(dummy_window_for_activation_) != GetDesktopWindow(); | |
892 if (valid) { | |
893 last_focus_window = ::SetFocus(dummy_window_for_activation_); | |
894 } else { | |
895 NOTREACHED() << "Dummy window not parented"; | |
896 } | |
897 } | |
898 | |
899 bool old_task_reentrancy_state = | |
900 base::MessageLoop::current()->NestableTasksAllowed(); | |
901 | |
902 // Maintain a local/global stack for the g_current_plugin_instance variable | |
903 // as this may be a nested invocation. | |
904 WebPluginDelegateImpl* last_plugin_instance = g_current_plugin_instance; | |
905 | |
906 g_current_plugin_instance = this; | |
907 | |
908 handle_event_depth_++; | 335 handle_event_depth_++; |
909 | 336 |
910 bool popups_enabled = false; | 337 bool popups_enabled = false; |
911 | 338 |
912 if (IsUserGestureMessage(np_event.event)) { | 339 if (IsUserGestureMessage(np_event.event)) { |
913 instance()->PushPopupsEnabledState(true); | 340 instance()->PushPopupsEnabledState(true); |
914 popups_enabled = true; | 341 popups_enabled = true; |
915 } | 342 } |
916 | 343 |
917 bool ret = instance()->NPP_HandleEvent(&np_event) != 0; | 344 bool ret = instance()->NPP_HandleEvent(&np_event) != 0; |
(...skipping 11 matching lines...) Expand all Loading... |
929 if (np_event.event == WM_MOUSEMOVE) { | 356 if (np_event.event == WM_MOUSEMOVE) { |
930 current_windowless_cursor_.InitFromExternalCursor(GetCursor()); | 357 current_windowless_cursor_.InitFromExternalCursor(GetCursor()); |
931 // Snag a reference to the current cursor ASAP in case the plugin modified | 358 // Snag a reference to the current cursor ASAP in case the plugin modified |
932 // it. There is a nasty race condition here with the multiprocess browser | 359 // it. There is a nasty race condition here with the multiprocess browser |
933 // as someone might be setting the cursor in the main process as well. | 360 // as someone might be setting the cursor in the main process as well. |
934 current_windowless_cursor_.GetCursorInfo(cursor_info); | 361 current_windowless_cursor_.GetCursorInfo(cursor_info); |
935 } | 362 } |
936 | 363 |
937 handle_event_depth_--; | 364 handle_event_depth_--; |
938 | 365 |
939 g_current_plugin_instance = last_plugin_instance; | |
940 | |
941 // We could have multiple NPP_HandleEvent calls nested together in case | |
942 // the plugin enters a modal loop. Reset the pump messages event when | |
943 // the outermost NPP_HandleEvent call unwinds. | |
944 if (handle_event_depth_ == 0) { | |
945 ResetEvent(handle_event_pump_messages_event_); | |
946 } | |
947 | |
948 // If we didn't enter a modal loop, need to unhook the filter. | |
949 if (handle_event_message_filter_hook_) { | |
950 UnhookWindowsHookEx(handle_event_message_filter_hook_); | |
951 handle_event_message_filter_hook_ = NULL; | |
952 } | |
953 | |
954 if (::IsWindow(last_focus_window)) { | |
955 // Restore the nestable tasks allowed state in the message loop and reset | |
956 // the os modal loop state as the plugin returned from the TrackPopupMenu | |
957 // API call. | |
958 base::MessageLoop::current()->SetNestableTasksAllowed( | |
959 old_task_reentrancy_state); | |
960 base::MessageLoop::current()->set_os_modal_loop(false); | |
961 // The Flash plugin at times sets focus to its hidden top level window | |
962 // with class name SWFlash_PlaceholderX. This causes the chrome browser | |
963 // window to receive a WM_ACTIVATEAPP message as a top level window from | |
964 // another thread is now active. We end up in a state where the chrome | |
965 // browser window is not active even though the user clicked on it. | |
966 // Our workaround for this is to send over a raw | |
967 // WM_LBUTTONDOWN/WM_LBUTTONUP combination to the last focus window, which | |
968 // does the trick. | |
969 if (dummy_window_for_activation_ != ::GetFocus()) { | |
970 INPUT input_info = {0}; | |
971 input_info.type = INPUT_MOUSE; | |
972 input_info.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; | |
973 ::SendInput(1, &input_info, sizeof(INPUT)); | |
974 | |
975 input_info.type = INPUT_MOUSE; | |
976 input_info.mi.dwFlags = MOUSEEVENTF_LEFTUP; | |
977 ::SendInput(1, &input_info, sizeof(INPUT)); | |
978 } else { | |
979 ::SetFocus(last_focus_window); | |
980 } | |
981 } | |
982 return ret; | 366 return ret; |
983 } | 367 } |
984 | 368 |
985 | |
986 void WebPluginDelegateImpl::OnModalLoopEntered() { | |
987 DCHECK(handle_event_pump_messages_event_ != NULL); | |
988 SetEvent(handle_event_pump_messages_event_); | |
989 | |
990 base::MessageLoop::current()->SetNestableTasksAllowed(true); | |
991 base::MessageLoop::current()->set_os_modal_loop(true); | |
992 | |
993 UnhookWindowsHookEx(handle_event_message_filter_hook_); | |
994 handle_event_message_filter_hook_ = NULL; | |
995 } | |
996 | |
997 bool WebPluginDelegateImpl::ShouldTrackEventForModalLoops(NPEvent* event) { | |
998 if (event->event == WM_RBUTTONDOWN) | |
999 return true; | |
1000 return false; | |
1001 } | |
1002 | |
1003 BOOL WINAPI WebPluginDelegateImpl::TrackPopupMenuPatch( | |
1004 HMENU menu, unsigned int flags, int x, int y, int reserved, | |
1005 HWND window, const RECT* rect) { | |
1006 | |
1007 if (g_current_plugin_instance) { | |
1008 unsigned long window_process_id = 0; | |
1009 unsigned long window_thread_id = | |
1010 GetWindowThreadProcessId(window, &window_process_id); | |
1011 // TrackPopupMenu fails if the window passed in belongs to a different | |
1012 // thread. | |
1013 if (::GetCurrentThreadId() != window_thread_id) { | |
1014 bool valid = | |
1015 GetParent(g_current_plugin_instance->dummy_window_for_activation_) != | |
1016 GetDesktopWindow(); | |
1017 if (valid) { | |
1018 window = g_current_plugin_instance->dummy_window_for_activation_; | |
1019 } else { | |
1020 NOTREACHED() << "Dummy window not parented"; | |
1021 } | |
1022 } | |
1023 } | |
1024 | |
1025 BOOL result = TrackPopupMenu(menu, flags, x, y, reserved, window, rect); | |
1026 return result; | |
1027 } | |
1028 | |
1029 HCURSOR WINAPI WebPluginDelegateImpl::SetCursorPatch(HCURSOR cursor) { | |
1030 // The windowless flash plugin periodically calls SetCursor in a wndproc | |
1031 // instantiated on the plugin thread. This causes annoying cursor flicker | |
1032 // when the mouse is moved on a foreground tab, with a windowless plugin | |
1033 // instance in a background tab. We just ignore the call here. | |
1034 if (!g_current_plugin_instance) { | |
1035 HCURSOR current_cursor = GetCursor(); | |
1036 if (current_cursor != cursor) { | |
1037 ::SetCursor(cursor); | |
1038 } | |
1039 return current_cursor; | |
1040 } | |
1041 return ::SetCursor(cursor); | |
1042 } | |
1043 | |
1044 LONG WINAPI WebPluginDelegateImpl::RegEnumKeyExWPatch( | |
1045 HKEY key, DWORD index, LPWSTR name, LPDWORD name_size, LPDWORD reserved, | |
1046 LPWSTR class_name, LPDWORD class_size, PFILETIME last_write_time) { | |
1047 DWORD orig_size = *name_size; | |
1048 LONG rv = RegEnumKeyExW(key, index, name, name_size, reserved, class_name, | |
1049 class_size, last_write_time); | |
1050 if (rv == ERROR_SUCCESS && | |
1051 GetKeyPath(key).find(L"Microsoft\\MediaPlayer\\ShimInclusionList") != | |
1052 std::wstring::npos) { | |
1053 static const wchar_t kChromeExeName[] = L"chrome.exe"; | |
1054 wcsncpy_s(name, orig_size, kChromeExeName, arraysize(kChromeExeName)); | |
1055 *name_size = | |
1056 std::min(orig_size, static_cast<DWORD>(arraysize(kChromeExeName))); | |
1057 } | |
1058 | |
1059 return rv; | |
1060 } | |
1061 | |
1062 void WebPluginDelegateImpl::ImeCompositionUpdated( | |
1063 const base::string16& text, | |
1064 const std::vector<int>& clauses, | |
1065 const std::vector<int>& target, | |
1066 int cursor_position) { | |
1067 if (!plugin_ime_) | |
1068 plugin_ime_.reset(new WebPluginIMEWin); | |
1069 | |
1070 plugin_ime_->CompositionUpdated(text, clauses, target, cursor_position); | |
1071 plugin_ime_->SendEvents(instance()); | |
1072 } | |
1073 | |
1074 void WebPluginDelegateImpl::ImeCompositionCompleted( | |
1075 const base::string16& text) { | |
1076 if (!plugin_ime_) | |
1077 plugin_ime_.reset(new WebPluginIMEWin); | |
1078 plugin_ime_->CompositionCompleted(text); | |
1079 plugin_ime_->SendEvents(instance()); | |
1080 } | |
1081 | |
1082 bool WebPluginDelegateImpl::GetIMEStatus(int* input_type, | |
1083 gfx::Rect* caret_rect) { | |
1084 if (!plugin_ime_) | |
1085 return false; | |
1086 return plugin_ime_->GetStatus(input_type, caret_rect); | |
1087 } | |
1088 | |
1089 // static | |
1090 FARPROC WINAPI WebPluginDelegateImpl::GetProcAddressPatch(HMODULE module, | |
1091 LPCSTR name) { | |
1092 FARPROC imm_function = WebPluginIMEWin::GetProcAddress(name); | |
1093 if (imm_function) | |
1094 return imm_function; | |
1095 return ::GetProcAddress(module, name); | |
1096 } | |
1097 | |
1098 HWND WINAPI WebPluginDelegateImpl::WindowFromPointPatch(POINT point) { | |
1099 HWND window = WindowFromPoint(point); | |
1100 if (::ScreenToClient(window, &point)) { | |
1101 HWND child = ChildWindowFromPoint(window, point); | |
1102 if (::IsWindow(child) && | |
1103 ::GetProp(child, content::kPluginDummyParentProperty)) | |
1104 return child; | |
1105 } | |
1106 return window; | |
1107 } | |
1108 | |
1109 } // namespace content | 369 } // namespace content |
OLD | NEW |