2 * Copyright 2015 Zhenbo Li
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
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
39 static HRESULT
bstr_to_nsacstr(BSTR bstr
, nsACString
*str
)
41 char *cstr
= heap_strdupWtoU(bstr
);
44 nsACString_Init(str
, cstr
);
49 static HRESULT
variant_to_nsastr(VARIANT var
, nsAString
*ret
)
55 nsAString_Init(ret
, NULL
);
58 nsAString_InitDepend(ret
, V_BSTR(&var
));
61 FIXME("Unsupported VARIANT: %s\n", debugstr_variant(&var
));
66 static HRESULT
return_nscstr(nsresult nsres
, nsACString
*nscstr
, BSTR
*p
)
71 if(NS_FAILED(nsres
)) {
72 ERR("failed: %08x\n", nsres
);
73 nsACString_Finish(nscstr
);
77 nsACString_GetData(nscstr
, &str
);
80 len
= MultiByteToWideChar(CP_UTF8
, 0, str
, -1, NULL
, 0);
81 *p
= SysAllocStringLen(NULL
, len
);
83 nsACString_Finish(nscstr
);
86 MultiByteToWideChar(CP_UTF8
, 0, str
, -1, *p
, len
);
91 nsACString_Finish(nscstr
);
95 typedef struct XMLHttpReqEventListener XMLHttpReqEventListener
;
98 EventTarget event_target
;
99 IHTMLXMLHttpRequest IHTMLXMLHttpRequest_iface
;
101 nsIXMLHttpRequest
*nsxhr
;
102 XMLHttpReqEventListener
*event_listener
;
103 } HTMLXMLHttpRequest
;
105 struct XMLHttpReqEventListener
{
106 nsIDOMEventListener nsIDOMEventListener_iface
;
108 HTMLXMLHttpRequest
*xhr
;
111 static void detach_xhr_event_listener(XMLHttpReqEventListener
*event_listener
)
113 nsIDOMEventTarget
*event_target
;
117 static const WCHAR readystatechangeW
[] =
118 {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
120 nsres
= nsIXMLHttpRequest_QueryInterface(event_listener
->xhr
->nsxhr
, &IID_nsIDOMEventTarget
, (void**)&event_target
);
121 assert(nsres
== NS_OK
);
123 nsAString_InitDepend(&str
, readystatechangeW
);
124 nsres
= nsIDOMEventTarget_RemoveEventListener(event_target
, &str
, &event_listener
->nsIDOMEventListener_iface
, FALSE
);
125 nsAString_Finish(&str
);
126 nsIDOMEventTarget_Release(event_target
);
128 event_listener
->xhr
->event_listener
= NULL
;
129 event_listener
->xhr
= NULL
;
130 nsIDOMEventListener_Release(&event_listener
->nsIDOMEventListener_iface
);
134 static inline XMLHttpReqEventListener
*impl_from_nsIDOMEventListener(nsIDOMEventListener
*iface
)
136 return CONTAINING_RECORD(iface
, XMLHttpReqEventListener
, nsIDOMEventListener_iface
);
139 static nsresult NSAPI
XMLHttpReqEventListener_QueryInterface(nsIDOMEventListener
*iface
,
140 nsIIDRef riid
, void **result
)
142 XMLHttpReqEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
144 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
145 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
146 *result
= &This
->nsIDOMEventListener_iface
;
147 }else if(IsEqualGUID(&IID_nsIDOMEventListener
, riid
)) {
148 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This
, result
);
149 *result
= &This
->nsIDOMEventListener_iface
;
152 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
153 return NS_NOINTERFACE
;
156 nsIDOMEventListener_AddRef(&This
->nsIDOMEventListener_iface
);
160 static nsrefcnt NSAPI
XMLHttpReqEventListener_AddRef(nsIDOMEventListener
*iface
)
162 XMLHttpReqEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
163 LONG ref
= InterlockedIncrement(&This
->ref
);
165 TRACE("(%p) ref=%d\n", This
, ref
);
170 static nsrefcnt NSAPI
XMLHttpReqEventListener_Release(nsIDOMEventListener
*iface
)
172 XMLHttpReqEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
173 LONG ref
= InterlockedDecrement(&This
->ref
);
175 TRACE("(%p) ref=%d\n", This
, ref
);
185 static nsresult NSAPI
XMLHttpReqEventListener_HandleEvent(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
187 XMLHttpReqEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
189 TRACE("(%p)\n", This
);
194 call_event_handlers(NULL
, NULL
, &This
->xhr
->event_target
, NULL
, EVENTID_READYSTATECHANGE
,
195 (IDispatch
*)&This
->xhr
->IHTMLXMLHttpRequest_iface
);
199 static const nsIDOMEventListenerVtbl XMLHttpReqEventListenerVtbl
= {
200 XMLHttpReqEventListener_QueryInterface
,
201 XMLHttpReqEventListener_AddRef
,
202 XMLHttpReqEventListener_Release
,
203 XMLHttpReqEventListener_HandleEvent
206 static inline HTMLXMLHttpRequest
*impl_from_IHTMLXMLHttpRequest(IHTMLXMLHttpRequest
*iface
)
208 return CONTAINING_RECORD(iface
, HTMLXMLHttpRequest
, IHTMLXMLHttpRequest_iface
);
211 static HRESULT WINAPI
HTMLXMLHttpRequest_QueryInterface(IHTMLXMLHttpRequest
*iface
, REFIID riid
, void **ppv
)
213 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
215 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
217 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
218 *ppv
= &This
->IHTMLXMLHttpRequest_iface
;
219 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
220 *ppv
= &This
->IHTMLXMLHttpRequest_iface
;
221 }else if(IsEqualGUID(&IID_IHTMLXMLHttpRequest
, riid
)) {
222 *ppv
= &This
->IHTMLXMLHttpRequest_iface
;
223 }else if(dispex_query_interface(&This
->event_target
.dispex
, riid
, ppv
)) {
224 return *ppv
? S_OK
: E_NOINTERFACE
;
227 WARN("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
228 return E_NOINTERFACE
;
231 IUnknown_AddRef((IUnknown
*)*ppv
);
235 static ULONG WINAPI
HTMLXMLHttpRequest_AddRef(IHTMLXMLHttpRequest
*iface
)
237 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
238 LONG ref
= InterlockedIncrement(&This
->ref
);
240 TRACE("(%p) ref=%d\n", This
, ref
);
245 static ULONG WINAPI
HTMLXMLHttpRequest_Release(IHTMLXMLHttpRequest
*iface
)
247 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
248 LONG ref
= InterlockedDecrement(&This
->ref
);
250 TRACE("(%p) ref=%d\n", This
, ref
);
253 if(This
->event_listener
)
254 detach_xhr_event_listener(This
->event_listener
);
255 release_dispex(&This
->event_target
.dispex
);
256 nsIXMLHttpRequest_Release(This
->nsxhr
);
263 static HRESULT WINAPI
HTMLXMLHttpRequest_GetTypeInfoCount(IHTMLXMLHttpRequest
*iface
, UINT
*pctinfo
)
265 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
266 return IDispatchEx_GetTypeInfoCount(&This
->event_target
.dispex
.IDispatchEx_iface
, pctinfo
);
269 static HRESULT WINAPI
HTMLXMLHttpRequest_GetTypeInfo(IHTMLXMLHttpRequest
*iface
, UINT iTInfo
,
270 LCID lcid
, ITypeInfo
**ppTInfo
)
272 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
274 return IDispatchEx_GetTypeInfo(&This
->event_target
.dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
277 static HRESULT WINAPI
HTMLXMLHttpRequest_GetIDsOfNames(IHTMLXMLHttpRequest
*iface
, REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
,
278 LCID lcid
, DISPID
*rgDispId
)
280 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
282 return IDispatchEx_GetIDsOfNames(&This
->event_target
.dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
286 static HRESULT WINAPI
HTMLXMLHttpRequest_Invoke(IHTMLXMLHttpRequest
*iface
, DISPID dispIdMember
, REFIID riid
, LCID lcid
,
287 WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
289 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
291 return IDispatchEx_Invoke(&This
->event_target
.dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
, wFlags
,
292 pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
295 static HRESULT WINAPI
HTMLXMLHttpRequest_get_readyState(IHTMLXMLHttpRequest
*iface
, LONG
*p
)
297 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
301 TRACE("(%p)->(%p)\n", This
, p
);
305 nsres
= nsIXMLHttpRequest_GetReadyState(This
->nsxhr
, &val
);
306 if(NS_FAILED(nsres
)) {
307 ERR("nsIXMLHttpRequest_GetReadyState failed: %08x\n", nsres
);
314 static HRESULT WINAPI
HTMLXMLHttpRequest_get_responseBody(IHTMLXMLHttpRequest
*iface
, VARIANT
*p
)
316 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
317 FIXME("(%p)->(%p)\n", This
, p
);
321 static HRESULT WINAPI
HTMLXMLHttpRequest_get_responseText(IHTMLXMLHttpRequest
*iface
, BSTR
*p
)
323 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
327 TRACE("(%p)->(%p)\n", This
, p
);
332 nsAString_Init(&nsstr
, NULL
);
333 nsres
= nsIXMLHttpRequest_GetResponseText(This
->nsxhr
, &nsstr
);
334 return return_nsstr(nsres
, &nsstr
, p
);
337 static HRESULT WINAPI
HTMLXMLHttpRequest_get_responseXML(IHTMLXMLHttpRequest
*iface
, IDispatch
**p
)
339 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
340 IXMLDOMDocument
*xmldoc
= NULL
;
344 IObjectSafety
*safety
;
346 TRACE("(%p)->(%p)\n", This
, p
);
348 hres
= CoCreateInstance(&CLSID_DOMDocument
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IXMLDOMDocument
, (void**)&xmldoc
);
350 ERR("CoCreateInstance failed: %08x\n", hres
);
354 hres
= IHTMLXMLHttpRequest_get_responseText(iface
, &str
);
356 IXMLDOMDocument_Release(xmldoc
);
357 ERR("get_responseText failed: %08x\n", hres
);
361 hres
= IXMLDOMDocument_loadXML(xmldoc
, str
, &vbool
);
363 if(hres
!= S_OK
|| vbool
!= VARIANT_TRUE
)
364 WARN("loadXML failed: %08x, returning an empty xmldoc\n", hres
);
366 hres
= IXMLDOMDocument_QueryInterface(xmldoc
, &IID_IObjectSafety
, (void**)&safety
);
367 assert(SUCCEEDED(hres
));
368 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, NULL
,
369 INTERFACESAFE_FOR_UNTRUSTED_CALLER
| INTERFACESAFE_FOR_UNTRUSTED_DATA
| INTERFACE_USES_SECURITY_MANAGER
,
370 INTERFACESAFE_FOR_UNTRUSTED_CALLER
| INTERFACESAFE_FOR_UNTRUSTED_DATA
| INTERFACE_USES_SECURITY_MANAGER
);
371 assert(SUCCEEDED(hres
));
372 IObjectSafety_Release(safety
);
374 *p
= (IDispatch
*)xmldoc
;
378 static HRESULT WINAPI
HTMLXMLHttpRequest_get_status(IHTMLXMLHttpRequest
*iface
, LONG
*p
)
380 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
383 TRACE("(%p)->(%p)\n", This
, p
);
388 nsres
= nsIXMLHttpRequest_GetStatus(This
->nsxhr
, &val
);
389 if(NS_FAILED(nsres
)) {
390 ERR("nsIXMLHttpRequest_GetStatus failed: %08x\n", nsres
);
395 return E_FAIL
; /* WinAPI thinks this is an error */
400 static HRESULT WINAPI
HTMLXMLHttpRequest_get_statusText(IHTMLXMLHttpRequest
*iface
, BSTR
*p
)
402 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
408 TRACE("(%p)->(%p)\n", This
, p
);
413 hres
= IHTMLXMLHttpRequest_get_readyState(iface
, &state
);
422 nsACString_Init(&nscstr
, NULL
);
423 nsres
= nsIXMLHttpRequest_GetStatusText(This
->nsxhr
, &nscstr
);
424 return return_nscstr(nsres
, &nscstr
, p
);
427 static HRESULT WINAPI
HTMLXMLHttpRequest_put_onreadystatechange(IHTMLXMLHttpRequest
*iface
, VARIANT v
)
429 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
431 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&v
));
433 return set_event_handler(&This
->event_target
, EVENTID_READYSTATECHANGE
, &v
);
436 static HRESULT WINAPI
HTMLXMLHttpRequest_get_onreadystatechange(IHTMLXMLHttpRequest
*iface
, VARIANT
*p
)
438 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
440 TRACE("(%p)->(%p)\n", This
, p
);
442 return get_event_handler(&This
->event_target
, EVENTID_READYSTATECHANGE
, p
);
445 static HRESULT WINAPI
HTMLXMLHttpRequest_abort(IHTMLXMLHttpRequest
*iface
)
447 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
450 TRACE("(%p)->()\n", This
);
452 nsres
= nsIXMLHttpRequest_SlowAbort(This
->nsxhr
);
453 if(NS_FAILED(nsres
)) {
454 ERR("nsIXMLHttpRequest_SlowAbort failed: %08x\n", nsres
);
461 static HRESULT WINAPI
HTMLXMLHttpRequest_open(IHTMLXMLHttpRequest
*iface
, BSTR bstrMethod
, BSTR bstrUrl
, VARIANT varAsync
, VARIANT varUser
, VARIANT varPassword
)
463 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
464 nsACString method
, url
;
465 nsAString user
, password
;
469 TRACE("(%p)->(%s %s %s %s %s)\n", This
, debugstr_w(bstrMethod
), debugstr_w(bstrUrl
), debugstr_variant(&varAsync
), debugstr_variant(&varUser
), debugstr_variant(&varPassword
));
471 if(V_VT(&varAsync
) != VT_BOOL
) {
472 FIXME("varAsync not supported: %s\n", debugstr_variant(&varAsync
));
476 /* Note: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27),
477 * synchronous requests on the main thread have been deprecated due to the negative
478 * effects to the user experience.
480 if(!V_BOOL(&varAsync
)) {
481 FIXME("Synchronous request is not supported yet\n");
485 hres
= variant_to_nsastr(varUser
, &user
);
488 hres
= variant_to_nsastr(varPassword
, &password
);
490 nsAString_Finish(&user
);
494 hres
= bstr_to_nsacstr(bstrMethod
, &method
);
496 nsAString_Finish(&user
);
497 nsAString_Finish(&password
);
500 hres
= bstr_to_nsacstr(bstrUrl
, &url
);
502 nsAString_Finish(&user
);
503 nsAString_Finish(&password
);
504 nsACString_Finish(&method
);
508 nsres
= nsIXMLHttpRequest_Open(This
->nsxhr
, &method
, &url
, TRUE
,
509 &user
, &password
, 0);
511 nsACString_Finish(&method
);
512 nsACString_Finish(&url
);
513 nsAString_Finish(&user
);
514 nsAString_Finish(&password
);
516 if(NS_FAILED(nsres
)) {
517 ERR("nsIXMLHttpRequest_Open failed: %08x\n", nsres
);
524 static HRESULT WINAPI
HTMLXMLHttpRequest_send(IHTMLXMLHttpRequest
*iface
, VARIANT varBody
)
526 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
527 nsIWritableVariant
*nsbody
= NULL
;
528 nsresult nsres
= NS_OK
;
530 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&varBody
));
532 switch(V_VT(&varBody
)) {
540 nsbody
= create_nsvariant();
542 return E_OUTOFMEMORY
;
544 nsAString_InitDepend(&nsstr
, V_BSTR(&varBody
));
545 nsres
= nsIWritableVariant_SetAsAString(nsbody
, &nsstr
);
546 nsAString_Finish(&nsstr
);
550 FIXME("unsupported body type %s\n", debugstr_variant(&varBody
));
554 if(NS_SUCCEEDED(nsres
))
555 nsres
= nsIXMLHttpRequest_Send(This
->nsxhr
, (nsIVariant
*)nsbody
);
557 nsIWritableVariant_Release(nsbody
);
558 if(NS_FAILED(nsres
)) {
559 ERR("nsIXMLHttpRequest_Send failed: %08x\n", nsres
);
566 static HRESULT WINAPI
HTMLXMLHttpRequest_getAllResponseHeaders(IHTMLXMLHttpRequest
*iface
, BSTR
*p
)
568 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
574 TRACE("(%p)->(%p)\n", This
, p
);
579 hres
= IHTMLXMLHttpRequest_get_readyState(iface
, &state
);
588 nsACString_Init(&nscstr
, NULL
);
589 nsres
= nsIXMLHttpRequest_GetAllResponseHeaders(This
->nsxhr
, &nscstr
);
590 return return_nscstr(nsres
, &nscstr
, p
);
593 static HRESULT WINAPI
HTMLXMLHttpRequest_getResponseHeader(IHTMLXMLHttpRequest
*iface
, BSTR bstrHeader
, BSTR
*p
)
595 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
596 nsACString header
, ret
;
601 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(bstrHeader
), p
);
608 hres
= IHTMLXMLHttpRequest_get_readyState(iface
, &state
);
617 cstr
= heap_strdupWtoU(bstrHeader
);
618 nsACString_InitDepend(&header
, cstr
);
619 nsACString_Init(&ret
, NULL
);
621 nsres
= nsIXMLHttpRequest_GetResponseHeader(This
->nsxhr
, &header
, &ret
);
623 nsACString_Finish(&header
);
625 return return_nscstr(nsres
, &ret
, p
);
628 static HRESULT WINAPI
HTMLXMLHttpRequest_setRequestHeader(IHTMLXMLHttpRequest
*iface
, BSTR bstrHeader
, BSTR bstrValue
)
630 HTMLXMLHttpRequest
*This
= impl_from_IHTMLXMLHttpRequest(iface
);
631 char *header_u
, *value_u
;
632 nsACString header
, value
;
635 TRACE("(%p)->(%s %s)\n", This
, debugstr_w(bstrHeader
), debugstr_w(bstrValue
));
637 header_u
= heap_strdupWtoU(bstrHeader
);
638 if(bstrHeader
&& !header_u
)
639 return E_OUTOFMEMORY
;
641 value_u
= heap_strdupWtoU(bstrValue
);
642 if(bstrValue
&& !value_u
) {
644 return E_OUTOFMEMORY
;
647 nsACString_InitDepend(&header
, header_u
);
648 nsACString_InitDepend(&value
, value_u
);
649 nsres
= nsIXMLHttpRequest_SetRequestHeader(This
->nsxhr
, &header
, &value
);
650 nsACString_Finish(&header
);
651 nsACString_Finish(&value
);
654 if(NS_FAILED(nsres
)) {
655 ERR("SetRequestHeader failed: %08x\n", nsres
);
662 static const IHTMLXMLHttpRequestVtbl HTMLXMLHttpRequestVtbl
= {
663 HTMLXMLHttpRequest_QueryInterface
,
664 HTMLXMLHttpRequest_AddRef
,
665 HTMLXMLHttpRequest_Release
,
666 HTMLXMLHttpRequest_GetTypeInfoCount
,
667 HTMLXMLHttpRequest_GetTypeInfo
,
668 HTMLXMLHttpRequest_GetIDsOfNames
,
669 HTMLXMLHttpRequest_Invoke
,
670 HTMLXMLHttpRequest_get_readyState
,
671 HTMLXMLHttpRequest_get_responseBody
,
672 HTMLXMLHttpRequest_get_responseText
,
673 HTMLXMLHttpRequest_get_responseXML
,
674 HTMLXMLHttpRequest_get_status
,
675 HTMLXMLHttpRequest_get_statusText
,
676 HTMLXMLHttpRequest_put_onreadystatechange
,
677 HTMLXMLHttpRequest_get_onreadystatechange
,
678 HTMLXMLHttpRequest_abort
,
679 HTMLXMLHttpRequest_open
,
680 HTMLXMLHttpRequest_send
,
681 HTMLXMLHttpRequest_getAllResponseHeaders
,
682 HTMLXMLHttpRequest_getResponseHeader
,
683 HTMLXMLHttpRequest_setRequestHeader
686 static inline HTMLXMLHttpRequest
*impl_from_DispatchEx(DispatchEx
*iface
)
688 return CONTAINING_RECORD(iface
, HTMLXMLHttpRequest
, event_target
.dispex
);
691 static void HTMLXMLHttpRequest_bind_event(DispatchEx
*dispex
, int eid
)
693 HTMLXMLHttpRequest
*This
= impl_from_DispatchEx(dispex
);
694 nsIDOMEventTarget
*nstarget
;
698 static const WCHAR readystatechangeW
[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
700 TRACE("(%p)\n", This
);
702 assert(eid
== EVENTID_READYSTATECHANGE
);
704 if(This
->event_listener
)
707 This
->event_listener
= heap_alloc(sizeof(*This
->event_listener
));
708 if(!This
->event_listener
)
711 This
->event_listener
->nsIDOMEventListener_iface
.lpVtbl
= &XMLHttpReqEventListenerVtbl
;
712 This
->event_listener
->ref
= 1;
713 This
->event_listener
->xhr
= This
;
715 nsres
= nsIXMLHttpRequest_QueryInterface(This
->nsxhr
, &IID_nsIDOMEventTarget
, (void**)&nstarget
);
716 assert(nsres
== NS_OK
);
718 nsAString_InitDepend(&type_str
, readystatechangeW
);
719 nsres
= nsIDOMEventTarget_AddEventListener(nstarget
, &type_str
, &This
->event_listener
->nsIDOMEventListener_iface
, FALSE
, TRUE
, 2);
720 nsAString_Finish(&type_str
);
721 nsIDOMEventTarget_Release(nstarget
);
723 ERR("AddEventListener failed: %08x\n", nsres
);
726 static dispex_static_data_vtbl_t HTMLXMLHttpRequest_dispex_vtbl
= {
732 HTMLXMLHttpRequest_bind_event
735 static const tid_t HTMLXMLHttpRequest_iface_tids
[] = {
736 IHTMLXMLHttpRequest_tid
,
739 static dispex_static_data_t HTMLXMLHttpRequest_dispex
= {
740 &HTMLXMLHttpRequest_dispex_vtbl
,
741 DispHTMLXMLHttpRequest_tid
,
742 HTMLXMLHttpRequest_iface_tids
746 /* IHTMLXMLHttpRequestFactory */
747 static inline HTMLXMLHttpRequestFactory
*impl_from_IHTMLXMLHttpRequestFactory(IHTMLXMLHttpRequestFactory
*iface
)
749 return CONTAINING_RECORD(iface
, HTMLXMLHttpRequestFactory
, IHTMLXMLHttpRequestFactory_iface
);
752 static HRESULT WINAPI
HTMLXMLHttpRequestFactory_QueryInterface(IHTMLXMLHttpRequestFactory
*iface
, REFIID riid
, void **ppv
)
754 HTMLXMLHttpRequestFactory
*This
= impl_from_IHTMLXMLHttpRequestFactory(iface
);
756 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
758 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
759 *ppv
= &This
->IHTMLXMLHttpRequestFactory_iface
;
760 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
761 *ppv
= &This
->IHTMLXMLHttpRequestFactory_iface
;
762 }else if(IsEqualGUID(&IID_IHTMLXMLHttpRequestFactory
, riid
)) {
763 *ppv
= &This
->IHTMLXMLHttpRequestFactory_iface
;
764 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
765 return *ppv
? S_OK
: E_NOINTERFACE
;
768 WARN("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
769 return E_NOINTERFACE
;
772 IUnknown_AddRef((IUnknown
*)*ppv
);
776 static ULONG WINAPI
HTMLXMLHttpRequestFactory_AddRef(IHTMLXMLHttpRequestFactory
*iface
)
778 HTMLXMLHttpRequestFactory
*This
= impl_from_IHTMLXMLHttpRequestFactory(iface
);
779 LONG ref
= InterlockedIncrement(&This
->ref
);
781 TRACE("(%p) ref=%d\n", This
, ref
);
786 static ULONG WINAPI
HTMLXMLHttpRequestFactory_Release(IHTMLXMLHttpRequestFactory
*iface
)
788 HTMLXMLHttpRequestFactory
*This
= impl_from_IHTMLXMLHttpRequestFactory(iface
);
789 LONG ref
= InterlockedDecrement(&This
->ref
);
791 TRACE("(%p) ref=%d\n", This
, ref
);
794 release_dispex(&This
->dispex
);
801 static HRESULT WINAPI
HTMLXMLHttpRequestFactory_GetTypeInfoCount(IHTMLXMLHttpRequestFactory
*iface
, UINT
*pctinfo
)
803 HTMLXMLHttpRequestFactory
*This
= impl_from_IHTMLXMLHttpRequestFactory(iface
);
804 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
807 static HRESULT WINAPI
HTMLXMLHttpRequestFactory_GetTypeInfo(IHTMLXMLHttpRequestFactory
*iface
, UINT iTInfo
,
808 LCID lcid
, ITypeInfo
**ppTInfo
)
810 HTMLXMLHttpRequestFactory
*This
= impl_from_IHTMLXMLHttpRequestFactory(iface
);
812 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
815 static HRESULT WINAPI
HTMLXMLHttpRequestFactory_GetIDsOfNames(IHTMLXMLHttpRequestFactory
*iface
, REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
,
816 LCID lcid
, DISPID
*rgDispId
)
818 HTMLXMLHttpRequestFactory
*This
= impl_from_IHTMLXMLHttpRequestFactory(iface
);
820 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
824 static HRESULT WINAPI
HTMLXMLHttpRequestFactory_Invoke(IHTMLXMLHttpRequestFactory
*iface
, DISPID dispIdMember
, REFIID riid
, LCID lcid
,
825 WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
827 HTMLXMLHttpRequestFactory
*This
= impl_from_IHTMLXMLHttpRequestFactory(iface
);
829 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
, wFlags
,
830 pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
833 static HRESULT WINAPI
HTMLXMLHttpRequestFactory_create(IHTMLXMLHttpRequestFactory
*iface
, IHTMLXMLHttpRequest
**p
)
835 HTMLXMLHttpRequestFactory
*This
= impl_from_IHTMLXMLHttpRequestFactory(iface
);
836 HTMLXMLHttpRequest
*ret
;
837 nsIXMLHttpRequest
*nsxhr
;
839 TRACE("(%p)->(%p)\n", This
, p
);
841 nsxhr
= create_nsxhr(This
->window
->base
.outer_window
->nswindow
);
845 ret
= heap_alloc_zero(sizeof(*ret
));
847 nsIXMLHttpRequest_Release(nsxhr
);
848 return E_OUTOFMEMORY
;
852 ret
->IHTMLXMLHttpRequest_iface
.lpVtbl
= &HTMLXMLHttpRequestVtbl
;
853 init_dispex(&ret
->event_target
.dispex
, (IUnknown
*)&ret
->IHTMLXMLHttpRequest_iface
,
854 &HTMLXMLHttpRequest_dispex
);
857 *p
= &ret
->IHTMLXMLHttpRequest_iface
;
861 static const IHTMLXMLHttpRequestFactoryVtbl HTMLXMLHttpRequestFactoryVtbl
= {
862 HTMLXMLHttpRequestFactory_QueryInterface
,
863 HTMLXMLHttpRequestFactory_AddRef
,
864 HTMLXMLHttpRequestFactory_Release
,
865 HTMLXMLHttpRequestFactory_GetTypeInfoCount
,
866 HTMLXMLHttpRequestFactory_GetTypeInfo
,
867 HTMLXMLHttpRequestFactory_GetIDsOfNames
,
868 HTMLXMLHttpRequestFactory_Invoke
,
869 HTMLXMLHttpRequestFactory_create
872 static inline HTMLXMLHttpRequestFactory
*factory_from_DispatchEx(DispatchEx
*iface
)
874 return CONTAINING_RECORD(iface
, HTMLXMLHttpRequestFactory
, dispex
);
877 static HRESULT
HTMLXMLHttpRequestFactory_value(DispatchEx
*iface
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
878 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
880 HTMLXMLHttpRequestFactory
*This
= factory_from_DispatchEx(iface
);
881 IHTMLXMLHttpRequest
*xhr
;
886 if(flags
!= DISPATCH_CONSTRUCT
) {
887 FIXME("flags %x not supported\n", flags
);
891 hres
= IHTMLXMLHttpRequestFactory_create(&This
->IHTMLXMLHttpRequestFactory_iface
, &xhr
);
895 V_VT(res
) = VT_DISPATCH
;
896 V_DISPATCH(res
) = (IDispatch
*)xhr
;
900 static const dispex_static_data_vtbl_t HTMLXMLHttpRequestFactory_dispex_vtbl
= {
901 HTMLXMLHttpRequestFactory_value
904 static const tid_t HTMLXMLHttpRequestFactory_iface_tids
[] = {
905 IHTMLXMLHttpRequestFactory_tid
,
908 static dispex_static_data_t HTMLXMLHttpRequestFactory_dispex
= {
909 &HTMLXMLHttpRequestFactory_dispex_vtbl
,
910 IHTMLXMLHttpRequestFactory_tid
,
911 HTMLXMLHttpRequestFactory_iface_tids
914 HRESULT
HTMLXMLHttpRequestFactory_Create(HTMLInnerWindow
* window
, HTMLXMLHttpRequestFactory
**ret_ptr
)
916 HTMLXMLHttpRequestFactory
*ret
;
918 ret
= heap_alloc(sizeof(*ret
));
920 return E_OUTOFMEMORY
;
922 ret
->IHTMLXMLHttpRequestFactory_iface
.lpVtbl
= &HTMLXMLHttpRequestFactoryVtbl
;
924 ret
->window
= window
;
926 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLXMLHttpRequestFactory_iface
,
927 &HTMLXMLHttpRequestFactory_dispex
);