ntdll/tests: Added more registry symlink tests.
[wine.git] / dlls / mshtml / xmlhttprequest.c
blobefbb79fdbae8edabbf9d9e5adaf838e98e2c1562
1 /*
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
19 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
33 #include "initguid.h"
34 #include "msxml6.h"
35 #include "objsafe.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 static HRESULT bstr_to_nsacstr(BSTR bstr, nsACString *str)
41 char *cstr = heap_strdupWtoU(bstr);
42 if(!cstr)
43 return E_OUTOFMEMORY;
44 nsACString_Init(str, cstr);
45 heap_free(cstr);
46 return S_OK;
49 static HRESULT variant_to_nsastr(VARIANT var, nsAString *ret)
51 switch(V_VT(&var)) {
52 case VT_NULL:
53 case VT_ERROR:
54 case VT_EMPTY:
55 nsAString_Init(ret, NULL);
56 return S_OK;
57 case VT_BSTR:
58 nsAString_InitDepend(ret, V_BSTR(&var));
59 return S_OK;
60 default:
61 FIXME("Unsupported VARIANT: %s\n", debugstr_variant(&var));
62 return E_INVALIDARG;
66 static HRESULT return_nscstr(nsresult nsres, nsACString *nscstr, BSTR *p)
68 const char *str;
69 int len;
71 if(NS_FAILED(nsres)) {
72 ERR("failed: %08x\n", nsres);
73 nsACString_Finish(nscstr);
74 return E_FAIL;
77 nsACString_GetData(nscstr, &str);
79 if(*str) {
80 len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
81 *p = SysAllocStringLen(NULL, len);
82 if(!*p) {
83 nsACString_Finish(nscstr);
84 return E_OUTOFMEMORY;
86 MultiByteToWideChar(CP_UTF8, 0, str, -1, *p, len);
87 }else {
88 *p = NULL;
91 nsACString_Finish(nscstr);
92 return S_OK;
95 typedef struct XMLHttpReqEventListener XMLHttpReqEventListener;
97 typedef struct {
98 EventTarget event_target;
99 IHTMLXMLHttpRequest IHTMLXMLHttpRequest_iface;
100 LONG ref;
101 nsIXMLHttpRequest *nsxhr;
102 XMLHttpReqEventListener *event_listener;
103 } HTMLXMLHttpRequest;
105 struct XMLHttpReqEventListener {
106 nsIDOMEventListener nsIDOMEventListener_iface;
107 LONG ref;
108 HTMLXMLHttpRequest *xhr;
111 static void detach_xhr_event_listener(XMLHttpReqEventListener *event_listener)
113 nsIDOMEventTarget *event_target;
114 nsAString str;
115 nsresult nsres;
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;
150 }else {
151 *result = NULL;
152 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
153 return NS_NOINTERFACE;
156 nsIDOMEventListener_AddRef(&This->nsIDOMEventListener_iface);
157 return NS_OK;
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);
167 return 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);
177 if(!ref) {
178 assert(!This->xhr);
179 heap_free(This);
182 return ref;
185 static nsresult NSAPI XMLHttpReqEventListener_HandleEvent(nsIDOMEventListener *iface, nsIDOMEvent *event)
187 XMLHttpReqEventListener *This = impl_from_nsIDOMEventListener(iface);
189 TRACE("(%p)\n", This);
191 if(!This->xhr)
192 return NS_OK;
194 call_event_handlers(NULL, NULL, &This->xhr->event_target, NULL, EVENTID_READYSTATECHANGE,
195 (IDispatch*)&This->xhr->IHTMLXMLHttpRequest_iface);
196 return NS_OK;
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;
225 }else {
226 *ppv = NULL;
227 WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
228 return E_NOINTERFACE;
231 IUnknown_AddRef((IUnknown*)*ppv);
232 return S_OK;
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);
242 return 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);
252 if(!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);
257 heap_free(This);
260 return ref;
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,
283 lcid, rgDispId);
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);
298 UINT16 val;
299 nsresult nsres;
301 TRACE("(%p)->(%p)\n", This, p);
303 if(!p)
304 return E_POINTER;
305 nsres = nsIXMLHttpRequest_GetReadyState(This->nsxhr, &val);
306 if(NS_FAILED(nsres)) {
307 ERR("nsIXMLHttpRequest_GetReadyState failed: %08x\n", nsres);
308 return E_FAIL;
310 *p = val;
311 return S_OK;
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);
318 return E_NOTIMPL;
321 static HRESULT WINAPI HTMLXMLHttpRequest_get_responseText(IHTMLXMLHttpRequest *iface, BSTR *p)
323 HTMLXMLHttpRequest *This = impl_from_IHTMLXMLHttpRequest(iface);
324 nsAString nsstr;
325 nsresult nsres;
327 TRACE("(%p)->(%p)\n", This, p);
329 if(!p)
330 return E_POINTER;
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;
341 BSTR str;
342 HRESULT hres;
343 VARIANT_BOOL vbool;
344 IObjectSafety *safety;
346 TRACE("(%p)->(%p)\n", This, p);
348 hres = CoCreateInstance(&CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument, (void**)&xmldoc);
349 if(FAILED(hres)) {
350 ERR("CoCreateInstance failed: %08x\n", hres);
351 return hres;
354 hres = IHTMLXMLHttpRequest_get_responseText(iface, &str);
355 if(FAILED(hres)) {
356 IXMLDOMDocument_Release(xmldoc);
357 ERR("get_responseText failed: %08x\n", hres);
358 return hres;
361 hres = IXMLDOMDocument_loadXML(xmldoc, str, &vbool);
362 SysFreeString(str);
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;
375 return S_OK;
378 static HRESULT WINAPI HTMLXMLHttpRequest_get_status(IHTMLXMLHttpRequest *iface, LONG *p)
380 HTMLXMLHttpRequest *This = impl_from_IHTMLXMLHttpRequest(iface);
381 DWORD val;
382 nsresult nsres;
383 TRACE("(%p)->(%p)\n", This, p);
385 if(!p)
386 return E_POINTER;
388 nsres = nsIXMLHttpRequest_GetStatus(This->nsxhr, &val);
389 if(NS_FAILED(nsres)) {
390 ERR("nsIXMLHttpRequest_GetStatus failed: %08x\n", nsres);
391 return E_FAIL;
393 *p = val;
394 if(val == 0)
395 return E_FAIL; /* WinAPI thinks this is an error */
397 return S_OK;
400 static HRESULT WINAPI HTMLXMLHttpRequest_get_statusText(IHTMLXMLHttpRequest *iface, BSTR *p)
402 HTMLXMLHttpRequest *This = impl_from_IHTMLXMLHttpRequest(iface);
403 nsACString nscstr;
404 nsresult nsres;
405 HRESULT hres;
406 LONG state;
408 TRACE("(%p)->(%p)\n", This, p);
410 if(!p)
411 return E_POINTER;
413 hres = IHTMLXMLHttpRequest_get_readyState(iface, &state);
414 if(FAILED(hres))
415 return hres;
417 if(state < 2) {
418 *p = NULL;
419 return E_FAIL;
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);
448 nsresult nsres;
450 TRACE("(%p)->()\n", This);
452 nsres = nsIXMLHttpRequest_SlowAbort(This->nsxhr);
453 if(NS_FAILED(nsres)) {
454 ERR("nsIXMLHttpRequest_SlowAbort failed: %08x\n", nsres);
455 return E_FAIL;
458 return S_OK;
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;
466 nsresult nsres;
467 HRESULT hres;
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));
473 return E_FAIL;
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");
482 return E_FAIL;
485 hres = variant_to_nsastr(varUser, &user);
486 if(FAILED(hres))
487 return hres;
488 hres = variant_to_nsastr(varPassword, &password);
489 if(FAILED(hres)) {
490 nsAString_Finish(&user);
491 return hres;
494 hres = bstr_to_nsacstr(bstrMethod, &method);
495 if(FAILED(hres)) {
496 nsAString_Finish(&user);
497 nsAString_Finish(&password);
498 return hres;
500 hres = bstr_to_nsacstr(bstrUrl, &url);
501 if(FAILED(hres)) {
502 nsAString_Finish(&user);
503 nsAString_Finish(&password);
504 nsACString_Finish(&method);
505 return hres;
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);
518 return E_FAIL;
521 return S_OK;
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)) {
533 case VT_NULL:
534 case VT_EMPTY:
535 case VT_ERROR:
536 break;
537 case VT_BSTR: {
538 nsAString nsstr;
540 nsbody = create_nsvariant();
541 if(!nsbody)
542 return E_OUTOFMEMORY;
544 nsAString_InitDepend(&nsstr, V_BSTR(&varBody));
545 nsres = nsIWritableVariant_SetAsAString(nsbody, &nsstr);
546 nsAString_Finish(&nsstr);
547 break;
549 default:
550 FIXME("unsupported body type %s\n", debugstr_variant(&varBody));
551 return E_NOTIMPL;
554 if(NS_SUCCEEDED(nsres))
555 nsres = nsIXMLHttpRequest_Send(This->nsxhr, (nsIVariant*)nsbody);
556 if(nsbody)
557 nsIWritableVariant_Release(nsbody);
558 if(NS_FAILED(nsres)) {
559 ERR("nsIXMLHttpRequest_Send failed: %08x\n", nsres);
560 return E_FAIL;
563 return S_OK;
566 static HRESULT WINAPI HTMLXMLHttpRequest_getAllResponseHeaders(IHTMLXMLHttpRequest *iface, BSTR *p)
568 HTMLXMLHttpRequest *This = impl_from_IHTMLXMLHttpRequest(iface);
569 nsACString nscstr;
570 nsresult nsres;
571 HRESULT hres;
572 LONG state;
574 TRACE("(%p)->(%p)\n", This, p);
576 if(!p)
577 return E_POINTER;
579 hres = IHTMLXMLHttpRequest_get_readyState(iface, &state);
580 if(FAILED(hres))
581 return hres;
583 if(state < 2) {
584 *p = NULL;
585 return E_FAIL;
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;
597 char *cstr;
598 nsresult nsres;
599 HRESULT hres;
600 LONG state;
601 TRACE("(%p)->(%s %p)\n", This, debugstr_w(bstrHeader), p);
603 if(!p)
604 return E_POINTER;
605 if(!bstrHeader)
606 return E_INVALIDARG;
608 hres = IHTMLXMLHttpRequest_get_readyState(iface, &state);
609 if(FAILED(hres))
610 return hres;
612 if(state < 2) {
613 *p = NULL;
614 return E_FAIL;
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);
624 heap_free(cstr);
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;
633 nsresult nsres;
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) {
643 heap_free(header_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);
652 heap_free(header_u);
653 heap_free(value_u);
654 if(NS_FAILED(nsres)) {
655 ERR("SetRequestHeader failed: %08x\n", nsres);
656 return E_FAIL;
659 return S_OK;
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;
695 nsAString type_str;
696 nsresult nsres;
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)
705 return;
707 This->event_listener = heap_alloc(sizeof(*This->event_listener));
708 if(!This->event_listener)
709 return;
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);
722 if(NS_FAILED(nsres))
723 ERR("AddEventListener failed: %08x\n", nsres);
726 static dispex_static_data_vtbl_t HTMLXMLHttpRequest_dispex_vtbl = {
727 NULL,
728 NULL,
729 NULL,
730 NULL,
731 NULL,
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;
766 }else {
767 *ppv = NULL;
768 WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
769 return E_NOINTERFACE;
772 IUnknown_AddRef((IUnknown*)*ppv);
773 return S_OK;
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);
783 return 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);
793 if(!ref) {
794 release_dispex(&This->dispex);
795 heap_free(This);
798 return ref;
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,
821 lcid, rgDispId);
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);
842 if(!nsxhr)
843 return E_FAIL;
845 ret = heap_alloc_zero(sizeof(*ret));
846 if(!ret) {
847 nsIXMLHttpRequest_Release(nsxhr);
848 return E_OUTOFMEMORY;
850 ret->nsxhr = nsxhr;
852 ret->IHTMLXMLHttpRequest_iface.lpVtbl = &HTMLXMLHttpRequestVtbl;
853 init_dispex(&ret->event_target.dispex, (IUnknown*)&ret->IHTMLXMLHttpRequest_iface,
854 &HTMLXMLHttpRequest_dispex);
855 ret->ref = 1;
857 *p = &ret->IHTMLXMLHttpRequest_iface;
858 return S_OK;
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;
882 HRESULT hres;
884 TRACE("\n");
886 if(flags != DISPATCH_CONSTRUCT) {
887 FIXME("flags %x not supported\n", flags);
888 return E_NOTIMPL;
891 hres = IHTMLXMLHttpRequestFactory_create(&This->IHTMLXMLHttpRequestFactory_iface, &xhr);
892 if(FAILED(hres))
893 return hres;
895 V_VT(res) = VT_DISPATCH;
896 V_DISPATCH(res) = (IDispatch*)xhr;
897 return S_OK;
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));
919 if(!ret)
920 return E_OUTOFMEMORY;
922 ret->IHTMLXMLHttpRequestFactory_iface.lpVtbl = &HTMLXMLHttpRequestFactoryVtbl;
923 ret->ref = 1;
924 ret->window = window;
926 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLXMLHttpRequestFactory_iface,
927 &HTMLXMLHttpRequestFactory_dispex);
929 *ret_ptr = ret;
930 return S_OK;