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
->performance_timing
->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
->performance_timing
->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 if(doc
->nscontainer
->editor_controller
) {
297 nsIController_Release(doc
->nscontainer
->editor_controller
);
298 doc
->nscontainer
->editor_controller
= NULL
;
301 if(doc
->nscontainer
->usermode
== EDITMODE
)
302 setup_editor_controller(doc
->nscontainer
);
305 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&olecmd
);
310 if(doc
->download_state
) {
314 V_VT(&progress
) = VT_I4
;
316 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_SETPROGRESSPOS
,
317 OLECMDEXECOPT_DONTPROMPTUSER
, &progress
, NULL
);
320 set_download_state(doc
, 0);
324 IOleCommandTarget_Exec(olecmd
, &CGID_ShellDocView
, 103, 0, NULL
, NULL
);
325 IOleCommandTarget_Exec(olecmd
, &CGID_MSHTML
, IDM_PARSECOMPLETE
, 0, NULL
, NULL
);
326 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_HTTPEQUIV_DONE
, 0, NULL
, NULL
);
328 IOleCommandTarget_Release(olecmd
);
332 static nsresult
handle_load(HTMLDocumentNode
*doc
, nsIDOMEvent
*event
)
334 HTMLDocumentObj
*doc_obj
= NULL
;
335 DOMEvent
*load_event
;
338 TRACE("(%p)\n", doc
);
340 if(!doc
->outer_window
)
341 return NS_ERROR_FAILURE
;
342 if(doc
->doc_obj
&& doc
->doc_obj
->doc_node
== doc
) {
343 doc_obj
= doc
->doc_obj
;
344 IUnknown_AddRef(doc_obj
->outer_unk
);
346 connect_scripts(doc
->window
);
349 handle_docobj_load(doc_obj
);
351 doc
->window
->performance_timing
->dom_complete_time
= get_time_stamp();
352 set_ready_state(doc
->outer_window
, READYSTATE_COMPLETE
);
355 if(doc_obj
->view_sink
)
356 IAdviseSink_OnViewChange(doc_obj
->view_sink
, DVASPECT_CONTENT
, -1);
358 set_statustext(doc_obj
, IDS_STATUS_DONE
, NULL
);
360 update_title(doc_obj
);
362 if(doc_obj
->doc_object_service
&& !(doc
->outer_window
->load_flags
& BINDING_REFRESH
))
363 IDocObjectService_FireDocumentComplete(doc_obj
->doc_object_service
,
364 &doc
->outer_window
->base
.IHTMLWindow2_iface
, 0);
366 IUnknown_Release(doc_obj
->outer_unk
);
369 doc
->window
->performance_timing
->load_event_start_time
= get_time_stamp();
371 if(doc
->dom_document
) {
372 hres
= create_document_event(doc
, EVENTID_LOAD
, &load_event
);
373 if(SUCCEEDED(hres
)) {
374 dispatch_event(&doc
->node
.event_target
, load_event
);
375 IDOMEvent_Release(&load_event
->IDOMEvent_iface
);
378 WARN("no dom_document\n");
381 hres
= create_event_from_nsevent(event
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &load_event
);
382 if(SUCCEEDED(hres
)) {
383 dispatch_event(&doc
->window
->event_target
, load_event
);
384 IDOMEvent_Release(&load_event
->IDOMEvent_iface
);
387 doc
->window
->performance_timing
->load_event_end_time
= get_time_stamp();
391 static nsresult
handle_beforeunload(HTMLDocumentNode
*doc
, nsIDOMEvent
*nsevent
)
393 HTMLInnerWindow
*window
;
397 if(!(window
= doc
->window
))
400 /* Gecko dispatches this to the document, but IE dispatches it to the window */
401 hres
= create_event_from_nsevent(nsevent
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &event
);
402 if(SUCCEEDED(hres
)) {
403 dispatch_event(&window
->event_target
, event
);
404 IDOMEvent_Release(&event
->IDOMEvent_iface
);
410 static nsresult
handle_unload(HTMLDocumentNode
*doc
, nsIDOMEvent
*nsevent
)
412 HTMLPerformanceTiming
*timing
= NULL
;
413 HTMLInnerWindow
*window
;
417 if(!(window
= doc
->window
) || doc
->unload_sent
)
419 doc
->unload_sent
= TRUE
;
421 if(window
->base
.outer_window
->pending_window
)
422 timing
= window
->base
.outer_window
->pending_window
->performance_timing
;
425 timing
->unload_event_start_time
= get_time_stamp();
427 hres
= create_event_from_nsevent(nsevent
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &event
);
428 if(SUCCEEDED(hres
)) {
429 dispatch_event(&window
->event_target
, event
);
430 IDOMEvent_Release(&event
->IDOMEvent_iface
);
434 timing
->unload_event_end_time
= get_time_stamp();
439 static nsresult
handle_htmlevent(HTMLDocumentNode
*doc
, nsIDOMEvent
*nsevent
)
441 nsIDOMEventTarget
*event_target
;
450 nsres
= nsIDOMEvent_GetTarget(nsevent
, &event_target
);
451 if(NS_FAILED(nsres
) || !event_target
) {
452 ERR("GetEventTarget failed: %08lx\n", nsres
);
456 nsres
= nsIDOMEventTarget_QueryInterface(event_target
, &IID_nsIDOMNode
, (void**)&nsnode
);
457 nsIDOMEventTarget_Release(event_target
);
458 if(NS_FAILED(nsres
)) {
461 target
= &doc
->window
->event_target
;
462 IHTMLWindow2_AddRef(&doc
->window
->base
.IHTMLWindow2_iface
);
465 hres
= get_node(nsnode
, TRUE
, &node
);
466 nsIDOMNode_Release(nsnode
);
469 target
= &node
->event_target
;
472 hres
= create_event_from_nsevent(nsevent
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &event
);
474 IEventTarget_Release(&target
->IEventTarget_iface
);
478 /* If we fine need for more special cases here, we may consider handling it in a more generic way. */
479 if(event
->event_id
== EVENTID_FOCUS
|| event
->event_id
== EVENTID_BLUR
) {
480 DOMEvent
*focus_event
;
482 hres
= create_document_event(doc
, event
->event_id
== EVENTID_FOCUS
? EVENTID_FOCUSIN
: EVENTID_FOCUSOUT
, &focus_event
);
483 if(SUCCEEDED(hres
)) {
484 dispatch_event(target
, focus_event
);
485 IDOMEvent_Release(&focus_event
->IDOMEvent_iface
);
489 dispatch_event(target
, event
);
491 IDOMEvent_Release(&event
->IDOMEvent_iface
);
492 IEventTarget_Release(&target
->IEventTarget_iface
);
496 static void init_event(nsIDOMEventTarget
*target
, const PRUnichar
*type
,
497 nsIDOMEventListener
*listener
, BOOL capture
)
502 nsAString_InitDepend(&type_str
, type
);
503 nsres
= nsIDOMEventTarget_AddEventListener(target
, &type_str
, listener
, capture
, FALSE
, 1);
504 nsAString_Finish(&type_str
);
506 ERR("AddEventTarget failed: %08lx\n", nsres
);
510 static void init_listener(nsEventListener
*This
, nsDocumentEventListener
*listener
, handler_t handler
)
512 This
->nsIDOMEventListener_iface
.lpVtbl
= &nsDOMEventListenerVtbl
;
513 This
->handler
= handler
;
514 This
->This
= listener
;
517 static nsIDOMEventTarget
*get_default_document_target(HTMLDocumentNode
*doc
)
519 nsIDOMEventTarget
*target
;
520 nsISupports
*target_iface
;
523 target_iface
= doc
->window
? (nsISupports
*)doc
->outer_window
->nswindow
: (nsISupports
*)doc
->dom_document
;
524 nsres
= nsISupports_QueryInterface(target_iface
, &IID_nsIDOMEventTarget
, (void**)&target
);
525 return NS_SUCCEEDED(nsres
) ? target
: NULL
;
528 void add_nsevent_listener(HTMLDocumentNode
*doc
, nsIDOMNode
*nsnode
, LPCWSTR type
)
530 nsIDOMEventTarget
*target
;
534 nsres
= nsIDOMNode_QueryInterface(nsnode
, &IID_nsIDOMEventTarget
, (void**)&target
);
535 assert(nsres
== NS_OK
);
537 target
= get_default_document_target(doc
);
542 init_event(target
, type
, &doc
->nsevent_listener
->htmlevent_listener
.nsIDOMEventListener_iface
,
544 nsIDOMEventTarget_Release(target
);
547 static void detach_nslistener(HTMLDocumentNode
*doc
, const WCHAR
*type
, nsEventListener
*listener
, cpp_bool is_capture
)
549 nsIDOMEventTarget
*target
;
553 target
= get_default_document_target(doc
);
557 nsAString_InitDepend(&type_str
, type
);
558 nsres
= nsIDOMEventTarget_RemoveEventListener(target
, &type_str
,
559 &listener
->nsIDOMEventListener_iface
, is_capture
);
560 nsAString_Finish(&type_str
);
561 nsIDOMEventTarget_Release(target
);
563 ERR("RemoveEventTarget failed: %08lx\n", nsres
);
566 void detach_nsevent(HTMLDocumentNode
*doc
, const WCHAR
*type
)
568 detach_nslistener(doc
, type
, &doc
->nsevent_listener
->htmlevent_listener
, TRUE
);
571 void release_nsevents(HTMLDocumentNode
*doc
)
573 nsDocumentEventListener
*listener
= doc
->nsevent_listener
;
576 TRACE("%p %p\n", doc
, doc
->nsevent_listener
);
581 for(i
= 0; i
< ARRAY_SIZE(doc_event_listeners
); i
++)
582 detach_nslistener(doc
, get_event_name(doc_event_listeners
[i
].id
), &listener
->listener
[i
], !(doc_event_listeners
[i
].flags
& BUBBLES
));
584 listener
->doc
= NULL
;
585 release_listener(listener
);
586 doc
->nsevent_listener
= NULL
;
589 void init_nsevents(HTMLDocumentNode
*doc
)
591 nsDocumentEventListener
*listener
;
592 nsIDOMEventTarget
*target
;
595 listener
= malloc(sizeof(nsDocumentEventListener
));
599 TRACE("%p %p\n", doc
, listener
);
604 for(i
= 0; i
< ARRAY_SIZE(doc_event_listeners
); i
++)
605 init_listener(&listener
->listener
[i
], listener
, doc_event_listeners
[i
].handler
);
606 init_listener(&listener
->htmlevent_listener
, listener
, handle_htmlevent
);
608 doc
->nsevent_listener
= listener
;
610 target
= get_default_document_target(doc
);
614 for(i
= 0; i
< ARRAY_SIZE(doc_event_listeners
); i
++) {
615 init_event(target
, get_event_name(doc_event_listeners
[i
].id
), &listener
->listener
[i
].nsIDOMEventListener_iface
,
616 !(doc_event_listeners
[i
].flags
& BUBBLES
));
618 if(doc_event_listeners
[i
].flags
& OVERRIDE
)
619 doc
->event_vector
[doc_event_listeners
[i
].id
] = TRUE
;
622 nsIDOMEventTarget_Release(target
);