windows.networking.hostname/tests: Add IHostNameFactory::CreateHostName() tests.
[wine.git] / dlls / mshtml / nsevents.c
blob90d4bc9e9bd41fe1ed409a4f5757aef440966230
1 /*
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "mshtmcid.h"
28 #include "shlguid.h"
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
33 #include "htmlscript.h"
34 #include "htmlevent.h"
35 #include "binding.h"
36 #include "resource.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 typedef nsresult (*handler_t)(HTMLDocumentNode*,nsIDOMEvent*);
42 typedef struct {
43 nsIDOMEventListener nsIDOMEventListener_iface;
44 nsDocumentEventListener *This;
45 handler_t handler;
46 } nsEventListener;
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 {
59 BUBBLES = 0x0001,
60 OVERRIDE = 0x0002,
63 static const struct {
64 eventid_t id;
65 enum doc_event_listener_flags flags;
66 handler_t handler;
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;
83 LONG ref;
85 HTMLDocumentNode *doc;
88 static LONG release_listener(nsDocumentEventListener *This)
90 LONG ref = InterlockedDecrement(&This->ref);
92 TRACE("(%p) ref=%ld\n", This, ref);
94 if(!ref)
95 free(This);
97 return 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);
110 *result = NULL;
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;
120 if(*result) {
121 nsIDOMEventListener_AddRef(&This->nsIDOMEventListener_iface);
122 return NS_OK;
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);
136 return 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;
151 nsresult nsres;
153 if(!doc) {
154 WARN("NULL doc!\n");
155 return NS_ERROR_FAILURE;
158 /* Hold a ref to the window, as some apps load another document during handlers */
159 window = doc->window;
160 if(window)
161 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
163 nsres = This->handler(doc, event);
165 if(window)
166 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
168 return nsres;
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)
180 HWND hwnd;
182 for(hwnd = GetFocus(); hwnd && hwnd != nscontainer->hwnd; hwnd = GetParent(hwnd));
184 return hwnd != NULL;
187 static nsresult handle_blur(HTMLDocumentNode *doc, nsIDOMEvent *event)
189 HTMLDocumentObj *doc_obj;
191 TRACE("(%p)\n", doc);
193 if(!doc->doc_obj)
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);
202 return NS_OK;
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);
218 return NS_OK;
221 static nsresult handle_keypress(HTMLDocumentNode *doc, nsIDOMEvent *event)
223 if(!doc->browser)
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);
232 return NS_OK;
235 static nsresult handle_dom_content_loaded(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
237 DOMEvent *event;
238 HRESULT hres;
240 if(doc->window)
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);
249 if(doc->window)
250 doc->window->performance_timing->dom_content_loaded_event_end_time = get_time_stamp();
252 return NS_OK;
255 static nsresult handle_pageshow(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
257 HTMLInnerWindow *window;
258 DOMEvent *event;
259 HRESULT hres;
261 if(!(window = doc->window) || !doc->dom_document || doc->document_mode < COMPAT_MODE_IE11)
262 return NS_OK;
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);
270 return NS_OK;
273 static nsresult handle_pagehide(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
275 HTMLInnerWindow *window;
276 DOMEvent *event;
277 HRESULT hres;
279 if(!(window = doc->window) || !doc->dom_document || doc->document_mode < COMPAT_MODE_IE11 || doc->unload_sent)
280 return NS_OK;
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);
288 return NS_OK;
291 static void handle_docobj_load(HTMLDocumentObj *doc)
293 IOleCommandTarget *olecmd = NULL;
294 HRESULT hres;
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);
304 if(doc->client) {
305 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
306 if(FAILED(hres))
307 olecmd = NULL;
310 if(doc->download_state) {
311 if(olecmd) {
312 VARIANT progress;
314 V_VT(&progress) = VT_I4;
315 V_I4(&progress) = 0;
316 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS,
317 OLECMDEXECOPT_DONTPROMPTUSER, &progress, NULL);
320 set_download_state(doc, 0);
323 if(olecmd) {
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;
336 HRESULT hres;
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);
348 if(doc_obj)
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);
354 if(doc_obj) {
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);
377 }else {
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();
388 return NS_OK;
391 static nsresult handle_beforeunload(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
393 HTMLInnerWindow *window;
394 DOMEvent *event;
395 HRESULT hres;
397 if(!(window = doc->window))
398 return NS_OK;
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);
407 return NS_OK;
410 static nsresult handle_unload(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
412 HTMLPerformanceTiming *timing = NULL;
413 HTMLInnerWindow *window;
414 DOMEvent *event;
415 HRESULT hres;
417 if(!(window = doc->window) || doc->unload_sent)
418 return NS_OK;
419 doc->unload_sent = TRUE;
421 if(window->base.outer_window->pending_window)
422 timing = window->base.outer_window->pending_window->performance_timing;
424 if(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);
433 if(timing)
434 timing->unload_event_end_time = get_time_stamp();
436 return NS_OK;
439 static nsresult handle_htmlevent(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
441 nsIDOMEventTarget *event_target;
442 EventTarget *target;
443 nsIDOMNode *nsnode;
444 DOMEvent *event;
445 nsresult nsres;
446 HRESULT hres;
448 TRACE("%p\n", doc);
450 nsres = nsIDOMEvent_GetTarget(nsevent, &event_target);
451 if(NS_FAILED(nsres) || !event_target) {
452 ERR("GetEventTarget failed: %08lx\n", nsres);
453 return NS_OK;
456 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
457 nsIDOMEventTarget_Release(event_target);
458 if(NS_FAILED(nsres)) {
459 if(!doc->window)
460 return NS_OK;
461 target = &doc->window->event_target;
462 IHTMLWindow2_AddRef(&doc->window->base.IHTMLWindow2_iface);
463 }else {
464 HTMLDOMNode *node;
465 hres = get_node(nsnode, TRUE, &node);
466 nsIDOMNode_Release(nsnode);
467 if(FAILED(hres))
468 return NS_OK;
469 target = &node->event_target;
472 hres = create_event_from_nsevent(nsevent, dispex_compat_mode(&doc->node.event_target.dispex), &event);
473 if(FAILED(hres)) {
474 IEventTarget_Release(&target->IEventTarget_iface);
475 return NS_OK;
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);
493 return NS_OK;
496 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
497 nsIDOMEventListener *listener, BOOL capture)
499 nsAString type_str;
500 nsresult nsres;
502 nsAString_InitDepend(&type_str, type);
503 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture, FALSE, 1);
504 nsAString_Finish(&type_str);
505 if(NS_FAILED(nsres))
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;
521 nsresult nsres;
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;
531 nsresult nsres;
533 if(nsnode) {
534 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMEventTarget, (void**)&target);
535 assert(nsres == NS_OK);
536 }else {
537 target = get_default_document_target(doc);
538 if(!target)
539 return;
542 init_event(target, type, &doc->nsevent_listener->htmlevent_listener.nsIDOMEventListener_iface,
543 TRUE);
544 nsIDOMEventTarget_Release(target);
547 static void detach_nslistener(HTMLDocumentNode *doc, const WCHAR *type, nsEventListener *listener, cpp_bool is_capture)
549 nsIDOMEventTarget *target;
550 nsAString type_str;
551 nsresult nsres;
553 target = get_default_document_target(doc);
554 if(!target)
555 return;
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);
562 if(NS_FAILED(nsres))
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;
574 unsigned i;
576 TRACE("%p %p\n", doc, doc->nsevent_listener);
578 if(!listener)
579 return;
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;
593 unsigned i;
595 listener = malloc(sizeof(nsDocumentEventListener));
596 if(!listener)
597 return;
599 TRACE("%p %p\n", doc, listener);
601 listener->ref = 1;
602 listener->doc = doc;
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);
611 if(!target)
612 return;
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);