2 * Copyright 2007-2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
33 #include "htmlscript.h"
34 #include "htmlevent.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
40 typedef nsresult (*handler_t
)(HTMLDocumentNode
*,nsIDOMEvent
*);
43 nsIDOMEventListener nsIDOMEventListener_iface
;
44 nsDocumentEventListener
*This
;
48 static nsresult
handle_blur(HTMLDocumentNode
*,nsIDOMEvent
*);
49 static nsresult
handle_focus(HTMLDocumentNode
*,nsIDOMEvent
*);
50 static nsresult
handle_keypress(HTMLDocumentNode
*,nsIDOMEvent
*);
51 static nsresult
handle_dom_content_loaded(HTMLDocumentNode
*,nsIDOMEvent
*);
52 static nsresult
handle_pageshow(HTMLDocumentNode
*,nsIDOMEvent
*);
53 static nsresult
handle_pagehide(HTMLDocumentNode
*,nsIDOMEvent
*);
54 static nsresult
handle_load(HTMLDocumentNode
*,nsIDOMEvent
*);
55 static nsresult
handle_beforeunload(HTMLDocumentNode
*,nsIDOMEvent
*);
56 static nsresult
handle_unload(HTMLDocumentNode
*,nsIDOMEvent
*);
58 enum doc_event_listener_flags
{
65 enum doc_event_listener_flags flags
;
67 } doc_event_listeners
[] = {
68 { EVENTID_BLUR
, 0, handle_blur
},
69 { EVENTID_FOCUS
, 0, handle_focus
},
70 { EVENTID_KEYPRESS
, BUBBLES
, handle_keypress
},
71 { EVENTID_DOMCONTENTLOADED
, OVERRIDE
, handle_dom_content_loaded
},
72 { EVENTID_PAGESHOW
, OVERRIDE
, handle_pageshow
},
73 { EVENTID_PAGEHIDE
, OVERRIDE
, handle_pagehide
},
74 { EVENTID_LOAD
, OVERRIDE
, handle_load
},
75 { EVENTID_BEFOREUNLOAD
, OVERRIDE
, handle_beforeunload
},
76 { EVENTID_UNLOAD
, OVERRIDE
, handle_unload
},
79 struct nsDocumentEventListener
{
80 nsEventListener listener
[ARRAY_SIZE(doc_event_listeners
)];
81 nsEventListener htmlevent_listener
;
85 HTMLDocumentNode
*doc
;
88 static LONG
release_listener(nsDocumentEventListener
*This
)
90 LONG ref
= InterlockedDecrement(&This
->ref
);
92 TRACE("(%p) ref=%ld\n", This
, ref
);
100 static inline nsEventListener
*impl_from_nsIDOMEventListener(nsIDOMEventListener
*iface
)
102 return CONTAINING_RECORD(iface
, nsEventListener
, nsIDOMEventListener_iface
);
105 static nsresult NSAPI
nsDOMEventListener_QueryInterface(nsIDOMEventListener
*iface
,
106 nsIIDRef riid
, void **result
)
108 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
112 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
113 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
114 *result
= &This
->nsIDOMEventListener_iface
;
115 }else if(IsEqualGUID(&IID_nsIDOMEventListener
, riid
)) {
116 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This
, result
);
117 *result
= &This
->nsIDOMEventListener_iface
;
121 nsIDOMEventListener_AddRef(&This
->nsIDOMEventListener_iface
);
125 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
126 return NS_NOINTERFACE
;
129 static nsrefcnt NSAPI
nsDOMEventListener_AddRef(nsIDOMEventListener
*iface
)
131 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
132 LONG ref
= InterlockedIncrement(&This
->This
->ref
);
134 TRACE("(%p) ref=%ld\n", This
->This
, ref
);
139 static nsrefcnt NSAPI
nsDOMEventListener_Release(nsIDOMEventListener
*iface
)
141 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
143 return release_listener(This
->This
);
146 static nsresult NSAPI
nsDOMEventListener_HandleEvent(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
148 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
149 HTMLDocumentNode
*doc
= This
->This
->doc
;
150 HTMLInnerWindow
*window
;
155 return NS_ERROR_FAILURE
;
158 /* Hold a ref to the window, as some apps load another document during handlers */
159 window
= doc
->window
;
161 IHTMLWindow2_AddRef(&window
->base
.IHTMLWindow2_iface
);
163 nsres
= This
->handler(doc
, event
);
166 IHTMLWindow2_Release(&window
->base
.IHTMLWindow2_iface
);
171 static const nsIDOMEventListenerVtbl nsDOMEventListenerVtbl
= {
172 nsDOMEventListener_QueryInterface
,
173 nsDOMEventListener_AddRef
,
174 nsDOMEventListener_Release
,
175 nsDOMEventListener_HandleEvent
178 static BOOL
is_doc_child_focus(GeckoBrowser
*nscontainer
)
182 for(hwnd
= GetFocus(); hwnd
&& hwnd
!= nscontainer
->hwnd
; hwnd
= GetParent(hwnd
));
187 static nsresult
handle_blur(HTMLDocumentNode
*doc
, nsIDOMEvent
*event
)
189 HTMLDocumentObj
*doc_obj
;
191 TRACE("(%p)\n", doc
);
194 return NS_ERROR_FAILURE
;
195 doc_obj
= doc
->doc_obj
;
197 if(doc_obj
->focus
&& !is_doc_child_focus(doc_obj
->nscontainer
)) {
198 doc_obj
->focus
= FALSE
;
199 notif_focus(doc_obj
);
205 static nsresult
handle_focus(HTMLDocumentNode
*doc
, nsIDOMEvent
*event
)
207 HTMLDocumentObj
*doc_obj
;
209 TRACE("(%p)\n", doc
);
211 doc_obj
= doc
->doc_obj
;
213 if(!doc_obj
->focus
) {
214 doc_obj
->focus
= TRUE
;
215 notif_focus(doc_obj
);
221 static nsresult
handle_keypress(HTMLDocumentNode
*doc
, nsIDOMEvent
*event
)
224 return NS_ERROR_FAILURE
;
226 TRACE("(%p)->(%p)\n", doc
, event
);
228 update_doc(doc
->browser
->doc
, UPDATE_UI
);
229 if(doc
->browser
->usermode
== EDITMODE
)
230 handle_edit_event(doc
, event
);
235 static nsresult
handle_dom_content_loaded(HTMLDocumentNode
*doc
, nsIDOMEvent
*nsevent
)
241 doc
->window
->dom_content_loaded_event_start_time
= get_time_stamp();
243 hres
= create_event_from_nsevent(nsevent
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &event
);
244 if(SUCCEEDED(hres
)) {
245 dispatch_event(&doc
->node
.event_target
, event
);
246 IDOMEvent_Release(&event
->IDOMEvent_iface
);
250 doc
->window
->dom_content_loaded_event_end_time
= get_time_stamp();
255 static nsresult
handle_pageshow(HTMLDocumentNode
*doc
, nsIDOMEvent
*nsevent
)
257 HTMLInnerWindow
*window
;
261 if(!(window
= doc
->window
) || !doc
->dom_document
|| doc
->document_mode
< COMPAT_MODE_IE11
)
264 hres
= create_document_event(doc
, EVENTID_PAGESHOW
, &event
);
265 if(SUCCEEDED(hres
)) {
266 dispatch_event(&window
->event_target
, event
);
267 IDOMEvent_Release(&event
->IDOMEvent_iface
);
273 static nsresult
handle_pagehide(HTMLDocumentNode
*doc
, nsIDOMEvent
*nsevent
)
275 HTMLInnerWindow
*window
;
279 if(!(window
= doc
->window
) || !doc
->dom_document
|| doc
->document_mode
< COMPAT_MODE_IE11
|| doc
->unload_sent
)
282 hres
= create_document_event(doc
, EVENTID_PAGEHIDE
, &event
);
283 if(SUCCEEDED(hres
)) {
284 dispatch_event(&window
->event_target
, event
);
285 IDOMEvent_Release(&event
->IDOMEvent_iface
);
291 static void handle_docobj_load(HTMLDocumentObj
*doc
)
293 IOleCommandTarget
*olecmd
= NULL
;
296 unlink_ref(&doc
->nscontainer
->editor_controller
);
298 if(doc
->nscontainer
->usermode
== EDITMODE
)
299 setup_editor_controller(doc
->nscontainer
);
302 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&olecmd
);
307 if(doc
->download_state
) {
311 V_VT(&progress
) = VT_I4
;
313 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_SETPROGRESSPOS
,
314 OLECMDEXECOPT_DONTPROMPTUSER
, &progress
, NULL
);
317 set_download_state(doc
, 0);
321 IOleCommandTarget_Exec(olecmd
, &CGID_ShellDocView
, 103, 0, NULL
, NULL
);
322 IOleCommandTarget_Exec(olecmd
, &CGID_MSHTML
, IDM_PARSECOMPLETE
, 0, NULL
, NULL
);
323 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_HTTPEQUIV_DONE
, 0, NULL
, NULL
);
325 IOleCommandTarget_Release(olecmd
);
329 static nsresult
handle_load(HTMLDocumentNode
*doc
, nsIDOMEvent
*event
)
331 HTMLDocumentObj
*doc_obj
= NULL
;
332 DOMEvent
*load_event
;
335 TRACE("(%p)\n", doc
);
337 if(!doc
->outer_window
)
338 return NS_ERROR_FAILURE
;
339 if(doc
->doc_obj
&& doc
->doc_obj
->doc_node
== doc
) {
340 doc_obj
= doc
->doc_obj
;
341 IUnknown_AddRef(doc_obj
->outer_unk
);
343 connect_scripts(doc
->window
);
346 handle_docobj_load(doc_obj
);
348 doc
->window
->dom_complete_time
= get_time_stamp();
349 set_ready_state(doc
->outer_window
, READYSTATE_COMPLETE
);
352 if(doc_obj
->view_sink
)
353 IAdviseSink_OnViewChange(doc_obj
->view_sink
, DVASPECT_CONTENT
, -1);
355 set_statustext(doc_obj
, IDS_STATUS_DONE
, NULL
);
357 update_title(doc_obj
);
359 if(doc_obj
->doc_object_service
&& !(doc
->outer_window
->load_flags
& BINDING_REFRESH
))
360 IDocObjectService_FireDocumentComplete(doc_obj
->doc_object_service
,
361 &doc
->outer_window
->base
.IHTMLWindow2_iface
, 0);
363 IUnknown_Release(doc_obj
->outer_unk
);
366 doc
->window
->load_event_start_time
= get_time_stamp();
368 if(doc
->dom_document
) {
369 hres
= create_document_event(doc
, EVENTID_LOAD
, &load_event
);
370 if(SUCCEEDED(hres
)) {
371 dispatch_event(&doc
->node
.event_target
, load_event
);
372 IDOMEvent_Release(&load_event
->IDOMEvent_iface
);
375 WARN("no dom_document\n");
378 hres
= create_event_from_nsevent(event
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &load_event
);
379 if(SUCCEEDED(hres
)) {
380 dispatch_event(&doc
->window
->event_target
, load_event
);
381 IDOMEvent_Release(&load_event
->IDOMEvent_iface
);
384 doc
->window
->load_event_end_time
= get_time_stamp();
388 static nsresult
handle_beforeunload(HTMLDocumentNode
*doc
, nsIDOMEvent
*nsevent
)
390 HTMLInnerWindow
*window
;
394 if(!(window
= doc
->window
))
397 /* Gecko dispatches this to the document, but IE dispatches it to the window */
398 hres
= create_event_from_nsevent(nsevent
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &event
);
399 if(SUCCEEDED(hres
)) {
400 dispatch_event(&window
->event_target
, event
);
401 IDOMEvent_Release(&event
->IDOMEvent_iface
);
407 static nsresult
handle_unload(HTMLDocumentNode
*doc
, nsIDOMEvent
*nsevent
)
409 HTMLInnerWindow
*window
, *pending_window
;
413 if(!(window
= doc
->window
) || doc
->unload_sent
)
415 doc
->unload_sent
= TRUE
;
417 pending_window
= window
->base
.outer_window
->pending_window
;
419 pending_window
->unload_event_start_time
= get_time_stamp();
421 hres
= create_event_from_nsevent(nsevent
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &event
);
422 if(SUCCEEDED(hres
)) {
423 dispatch_event(&window
->event_target
, event
);
424 IDOMEvent_Release(&event
->IDOMEvent_iface
);
428 pending_window
->unload_event_end_time
= get_time_stamp();
433 static nsresult
handle_htmlevent(HTMLDocumentNode
*doc
, nsIDOMEvent
*nsevent
)
435 nsIDOMEventTarget
*event_target
;
444 nsres
= nsIDOMEvent_GetTarget(nsevent
, &event_target
);
445 if(NS_FAILED(nsres
) || !event_target
) {
446 ERR("GetEventTarget failed: %08lx\n", nsres
);
450 nsres
= nsIDOMEventTarget_QueryInterface(event_target
, &IID_nsIDOMNode
, (void**)&nsnode
);
451 nsIDOMEventTarget_Release(event_target
);
452 if(NS_FAILED(nsres
)) {
455 target
= &doc
->window
->event_target
;
456 IHTMLWindow2_AddRef(&doc
->window
->base
.IHTMLWindow2_iface
);
459 hres
= get_node(nsnode
, TRUE
, &node
);
460 nsIDOMNode_Release(nsnode
);
463 target
= &node
->event_target
;
466 hres
= create_event_from_nsevent(nsevent
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &event
);
468 IEventTarget_Release(&target
->IEventTarget_iface
);
472 /* If we fine need for more special cases here, we may consider handling it in a more generic way. */
473 if(event
->event_id
== EVENTID_FOCUS
|| event
->event_id
== EVENTID_BLUR
) {
474 DOMEvent
*focus_event
;
476 hres
= create_document_event(doc
, event
->event_id
== EVENTID_FOCUS
? EVENTID_FOCUSIN
: EVENTID_FOCUSOUT
, &focus_event
);
477 if(SUCCEEDED(hres
)) {
478 dispatch_event(target
, focus_event
);
479 IDOMEvent_Release(&focus_event
->IDOMEvent_iface
);
483 dispatch_event(target
, event
);
485 IDOMEvent_Release(&event
->IDOMEvent_iface
);
486 IEventTarget_Release(&target
->IEventTarget_iface
);
490 static void init_event(nsIDOMEventTarget
*target
, const PRUnichar
*type
,
491 nsIDOMEventListener
*listener
, BOOL capture
)
496 nsAString_InitDepend(&type_str
, type
);
497 nsres
= nsIDOMEventTarget_AddEventListener(target
, &type_str
, listener
, capture
, FALSE
, 1);
498 nsAString_Finish(&type_str
);
500 ERR("AddEventTarget failed: %08lx\n", nsres
);
504 static void init_listener(nsEventListener
*This
, nsDocumentEventListener
*listener
, handler_t handler
)
506 This
->nsIDOMEventListener_iface
.lpVtbl
= &nsDOMEventListenerVtbl
;
507 This
->handler
= handler
;
508 This
->This
= listener
;
511 static nsIDOMEventTarget
*get_default_document_target(HTMLDocumentNode
*doc
)
513 nsIDOMEventTarget
*target
;
514 nsISupports
*target_iface
;
517 target_iface
= doc
->window
? (nsISupports
*)doc
->outer_window
->nswindow
: (nsISupports
*)doc
->dom_document
;
518 nsres
= nsISupports_QueryInterface(target_iface
, &IID_nsIDOMEventTarget
, (void**)&target
);
519 return NS_SUCCEEDED(nsres
) ? target
: NULL
;
522 void add_nsevent_listener(HTMLDocumentNode
*doc
, nsIDOMNode
*nsnode
, LPCWSTR type
)
524 nsIDOMEventTarget
*target
;
528 nsres
= nsIDOMNode_QueryInterface(nsnode
, &IID_nsIDOMEventTarget
, (void**)&target
);
529 assert(nsres
== NS_OK
);
531 target
= get_default_document_target(doc
);
536 init_event(target
, type
, &doc
->nsevent_listener
->htmlevent_listener
.nsIDOMEventListener_iface
,
538 nsIDOMEventTarget_Release(target
);
541 static void detach_nslistener(HTMLDocumentNode
*doc
, const WCHAR
*type
, nsEventListener
*listener
, cpp_bool is_capture
)
543 nsIDOMEventTarget
*target
;
547 target
= get_default_document_target(doc
);
551 nsAString_InitDepend(&type_str
, type
);
552 nsres
= nsIDOMEventTarget_RemoveEventListener(target
, &type_str
,
553 &listener
->nsIDOMEventListener_iface
, is_capture
);
554 nsAString_Finish(&type_str
);
555 nsIDOMEventTarget_Release(target
);
557 ERR("RemoveEventTarget failed: %08lx\n", nsres
);
560 void detach_nsevent(HTMLDocumentNode
*doc
, const WCHAR
*type
)
562 detach_nslistener(doc
, type
, &doc
->nsevent_listener
->htmlevent_listener
, TRUE
);
565 void release_nsevents(HTMLDocumentNode
*doc
)
567 nsDocumentEventListener
*listener
= doc
->nsevent_listener
;
570 TRACE("%p %p\n", doc
, doc
->nsevent_listener
);
575 for(i
= 0; i
< ARRAY_SIZE(doc_event_listeners
); i
++)
576 detach_nslistener(doc
, get_event_name(doc_event_listeners
[i
].id
), &listener
->listener
[i
], !(doc_event_listeners
[i
].flags
& BUBBLES
));
578 listener
->doc
= NULL
;
579 release_listener(listener
);
580 doc
->nsevent_listener
= NULL
;
583 void init_nsevents(HTMLDocumentNode
*doc
)
585 nsDocumentEventListener
*listener
;
586 nsIDOMEventTarget
*target
;
589 listener
= malloc(sizeof(nsDocumentEventListener
));
593 TRACE("%p %p\n", doc
, listener
);
598 for(i
= 0; i
< ARRAY_SIZE(doc_event_listeners
); i
++)
599 init_listener(&listener
->listener
[i
], listener
, doc_event_listeners
[i
].handler
);
600 init_listener(&listener
->htmlevent_listener
, listener
, handle_htmlevent
);
602 doc
->nsevent_listener
= listener
;
604 target
= get_default_document_target(doc
);
608 for(i
= 0; i
< ARRAY_SIZE(doc_event_listeners
); i
++) {
609 init_event(target
, get_event_name(doc_event_listeners
[i
].id
), &listener
->listener
[i
].nsIDOMEventListener_iface
,
610 !(doc_event_listeners
[i
].flags
& BUBBLES
));
612 if(doc_event_listeners
[i
].flags
& OVERRIDE
)
613 doc
->event_vector
[doc_event_listeners
[i
].id
] = TRUE
;
616 nsIDOMEventTarget_Release(target
);