2 * Copyright 2006-2007 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
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 #include "mshtml_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41 #define CONTENT_LENGTH "Content-Length"
42 #define UTF16_STR "utf-16"
44 static WCHAR emptyW[] = {0};
47 const nsIInputStreamVtbl *lpInputStreamVtbl;
55 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
58 void (*destroy)(BSCallback*);
59 HRESULT (*init_bindinfo)(BSCallback*);
60 HRESULT (*start_binding)(BSCallback*);
61 HRESULT (*stop_binding)(BSCallback*,HRESULT);
62 HRESULT (*read_data)(BSCallback*,IStream*);
63 HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
64 HRESULT (*on_response)(BSCallback*,DWORD);
68 const IBindStatusCallbackVtbl *lpBindStatusCallbackVtbl;
69 const IServiceProviderVtbl *lpServiceProviderVtbl;
70 const IHttpNegotiate2Vtbl *lpHttpNegotiate2Vtbl;
71 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
73 const BSCallbackVtbl *vtbl;
87 HTMLDocumentNode *doc;
92 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
94 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
97 nsProtocolStream *This = NSINSTREAM_THIS(iface);
101 if(IsEqualGUID(&IID_nsISupports, riid)) {
102 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
103 *result = NSINSTREAM(This);
104 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
105 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
106 *result = NSINSTREAM(This);
110 nsIInputStream_AddRef(NSINSTREAM(This));
114 WARN("unsupported interface %s\n", debugstr_guid(riid));
115 return NS_NOINTERFACE;
118 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
120 nsProtocolStream *This = NSINSTREAM_THIS(iface);
121 LONG ref = InterlockedIncrement(&This->ref);
123 TRACE("(%p) ref=%d\n", This, ref);
129 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
131 nsProtocolStream *This = NSINSTREAM_THIS(iface);
132 LONG ref = InterlockedDecrement(&This->ref);
134 TRACE("(%p) ref=%d\n", This, ref);
142 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
144 nsProtocolStream *This = NSINSTREAM_THIS(iface);
145 FIXME("(%p)\n", This);
146 return NS_ERROR_NOT_IMPLEMENTED;
149 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
151 nsProtocolStream *This = NSINSTREAM_THIS(iface);
152 FIXME("(%p)->(%p)\n", This, _retval);
153 return NS_ERROR_NOT_IMPLEMENTED;
156 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
159 nsProtocolStream *This = NSINSTREAM_THIS(iface);
162 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
164 if(read > This->buf_size)
165 read = This->buf_size;
168 memcpy(aBuf, This->buf, read);
169 if(read < This->buf_size)
170 memmove(This->buf, This->buf+read, This->buf_size-read);
171 This->buf_size -= read;
178 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
179 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
180 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
182 nsProtocolStream *This = NSINSTREAM_THIS(iface);
183 PRUint32 written = 0;
186 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
191 if(aCount > This->buf_size)
192 aCount = This->buf_size;
194 nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, aCount, &written);
196 TRACE("aWritter failed: %08x\n", nsres);
197 else if(written != This->buf_size)
198 FIXME("written %d != buf_size %d\n", written, This->buf_size);
200 This->buf_size -= written;
206 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
208 nsProtocolStream *This = NSINSTREAM_THIS(iface);
209 FIXME("(%p)->(%p)\n", This, _retval);
210 return NS_ERROR_NOT_IMPLEMENTED;
213 #undef NSINSTREAM_THIS
215 static const nsIInputStreamVtbl nsInputStreamVtbl = {
216 nsInputStream_QueryInterface,
217 nsInputStream_AddRef,
218 nsInputStream_Release,
220 nsInputStream_Available,
222 nsInputStream_ReadSegments,
223 nsInputStream_IsNonBlocking
226 static nsProtocolStream *create_nsprotocol_stream(void)
228 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
230 ret->lpInputStreamVtbl = &nsInputStreamVtbl;
237 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
239 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
240 REFIID riid, void **ppv)
242 BSCallback *This = STATUSCLB_THIS(iface);
245 if(IsEqualGUID(&IID_IUnknown, riid)) {
246 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
247 *ppv = STATUSCLB(This);
248 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
249 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
250 *ppv = STATUSCLB(This);
251 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
252 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
253 *ppv = SERVPROV(This);
254 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
255 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
256 *ppv = HTTPNEG(This);
257 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
258 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
259 *ppv = HTTPNEG(This);
260 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
261 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
262 *ppv = BINDINFO(This);
266 IBindStatusCallback_AddRef(STATUSCLB(This));
270 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
271 return E_NOINTERFACE;
274 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
276 BSCallback *This = STATUSCLB_THIS(iface);
277 LONG ref = InterlockedIncrement(&This->ref);
279 TRACE("(%p) ref = %d\n", This, ref);
284 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
286 BSCallback *This = STATUSCLB_THIS(iface);
287 LONG ref = InterlockedDecrement(&This->ref);
289 TRACE("(%p) ref = %d\n", This, ref);
293 GlobalFree(This->post_data);
295 IMoniker_Release(This->mon);
297 IBinding_Release(This->binding);
298 list_remove(&This->entry);
299 heap_free(This->headers);
301 This->vtbl->destroy(This);
307 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
308 DWORD dwReserved, IBinding *pbind)
310 BSCallback *This = STATUSCLB_THIS(iface);
312 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
314 IBinding_AddRef(pbind);
315 This->binding = pbind;
318 list_add_head(&This->doc->bindings, &This->entry);
320 return This->vtbl->start_binding(This);
323 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
325 BSCallback *This = STATUSCLB_THIS(iface);
326 FIXME("(%p)->(%p)\n", This, pnPriority);
330 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
332 BSCallback *This = STATUSCLB_THIS(iface);
333 FIXME("(%p)->(%d)\n", This, reserved);
337 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
338 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
340 BSCallback *This = STATUSCLB_THIS(iface);
342 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
343 debugstr_w(szStatusText));
345 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
348 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
349 HRESULT hresult, LPCWSTR szError)
351 BSCallback *This = STATUSCLB_THIS(iface);
354 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
356 /* NOTE: IE7 calls GetBindResult here */
358 hres = This->vtbl->stop_binding(This, hresult);
361 IBinding_Release(This->binding);
362 This->binding = NULL;
365 list_remove(&This->entry);
371 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
372 DWORD *grfBINDF, BINDINFO *pbindinfo)
374 BSCallback *This = STATUSCLB_THIS(iface);
377 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
379 if(!This->bindinfo_ready) {
382 hres = This->vtbl->init_bindinfo(This);
386 This->bindinfo_ready = TRUE;
389 *grfBINDF = This->bindf;
391 size = pbindinfo->cbSize;
392 memset(pbindinfo, 0, size);
393 pbindinfo->cbSize = size;
395 pbindinfo->cbstgmedData = This->post_data_len;
396 pbindinfo->dwCodePage = CP_UTF8;
397 pbindinfo->dwOptions = 0x80000;
399 if(This->post_data) {
400 pbindinfo->dwBindVerb = BINDVERB_POST;
402 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
403 pbindinfo->stgmedData.u.hGlobal = This->post_data;
404 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
405 IBindStatusCallback_AddRef(STATUSCLB(This));
411 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
412 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
414 BSCallback *This = STATUSCLB_THIS(iface);
416 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
418 return This->vtbl->read_data(This, pstgmed->u.pstm);
421 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
422 REFIID riid, IUnknown *punk)
424 BSCallback *This = STATUSCLB_THIS(iface);
425 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
429 #undef STATUSCLB_THIS
431 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
432 BindStatusCallback_QueryInterface,
433 BindStatusCallback_AddRef,
434 BindStatusCallback_Release,
435 BindStatusCallback_OnStartBinding,
436 BindStatusCallback_GetPriority,
437 BindStatusCallback_OnLowResource,
438 BindStatusCallback_OnProgress,
439 BindStatusCallback_OnStopBinding,
440 BindStatusCallback_GetBindInfo,
441 BindStatusCallback_OnDataAvailable,
442 BindStatusCallback_OnObjectAvailable
445 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
447 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
448 REFIID riid, void **ppv)
450 BSCallback *This = HTTPNEG_THIS(iface);
451 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
454 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
456 BSCallback *This = HTTPNEG_THIS(iface);
457 return IBindStatusCallback_AddRef(STATUSCLB(This));
460 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
462 BSCallback *This = HTTPNEG_THIS(iface);
463 return IBindStatusCallback_Release(STATUSCLB(This));
466 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
467 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
469 BSCallback *This = HTTPNEG_THIS(iface);
472 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
473 dwReserved, pszAdditionalHeaders);
476 *pszAdditionalHeaders = NULL;
480 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
481 *pszAdditionalHeaders = CoTaskMemAlloc(size);
482 memcpy(*pszAdditionalHeaders, This->headers, size);
487 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
488 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
490 BSCallback *This = HTTPNEG_THIS(iface);
492 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
493 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
495 return This->vtbl->on_response(This, dwResponseCode);
498 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
499 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
501 BSCallback *This = HTTPNEG_THIS(iface);
502 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
508 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
509 HttpNegotiate_QueryInterface,
510 HttpNegotiate_AddRef,
511 HttpNegotiate_Release,
512 HttpNegotiate_BeginningTransaction,
513 HttpNegotiate_OnResponse,
514 HttpNegotiate_GetRootSecurityId
517 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
519 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
520 REFIID riid, void **ppv)
522 BSCallback *This = BINDINFO_THIS(iface);
523 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
526 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
528 BSCallback *This = BINDINFO_THIS(iface);
529 return IBindStatusCallback_AddRef(STATUSCLB(This));
532 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
534 BSCallback *This = BINDINFO_THIS(iface);
535 return IBindStatusCallback_Release(STATUSCLB(This));
538 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
539 DWORD *grfBINDF, BINDINFO *pbindinfo)
541 BSCallback *This = BINDINFO_THIS(iface);
542 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
546 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
547 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
549 BSCallback *This = BINDINFO_THIS(iface);
550 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
556 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
557 InternetBindInfo_QueryInterface,
558 InternetBindInfo_AddRef,
559 InternetBindInfo_Release,
560 InternetBindInfo_GetBindInfo,
561 InternetBindInfo_GetBindString
564 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
566 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
567 REFIID riid, void **ppv)
569 BSCallback *This = SERVPROV_THIS(iface);
570 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
573 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
575 BSCallback *This = SERVPROV_THIS(iface);
576 return IBindStatusCallback_AddRef(STATUSCLB(This));
579 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
581 BSCallback *This = SERVPROV_THIS(iface);
582 return IBindStatusCallback_Release(STATUSCLB(This));
585 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
586 REFGUID guidService, REFIID riid, void **ppv)
588 BSCallback *This = SERVPROV_THIS(iface);
589 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
590 return E_NOINTERFACE;
595 static const IServiceProviderVtbl ServiceProviderVtbl = {
596 BSCServiceProvider_QueryInterface,
597 BSCServiceProvider_AddRef,
598 BSCServiceProvider_Release,
599 BSCServiceProvider_QueryService
602 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
604 This->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
605 This->lpServiceProviderVtbl = &ServiceProviderVtbl;
606 This->lpHttpNegotiate2Vtbl = &HttpNegotiate2Vtbl;
607 This->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
612 list_init(&This->entry);
615 IMoniker_AddRef(mon);
619 /* Calls undocumented 84 cmd of CGID_ShellDocView */
620 static void call_docview_84(HTMLDocumentObj *doc)
622 IOleCommandTarget *olecmd;
629 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
634 hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
635 IOleCommandTarget_Release(olecmd);
636 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
637 FIXME("handle result\n");
640 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
641 HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
643 PRUint32 post_data_len = 0, available = 0;
644 HGLOBAL post_data = NULL;
645 LPWSTR headers = NULL;
646 DWORD headers_len = 0, len;
647 const char *ptr, *ptr2, *post_data_end;
649 nsIInputStream_Available(post_data_stream, &available);
650 post_data = GlobalAlloc(0, available+1);
651 nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
653 TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
655 ptr = ptr2 = post_data;
656 post_data_end = (const char*)post_data+post_data_len;
658 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
659 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
669 if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
670 && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
671 CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
672 ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
677 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
680 headers = heap_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
682 headers = heap_alloc((len+1)*sizeof(WCHAR));
684 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, len);
690 headers[headers_len] = 0;
691 *headers_ret = headers;
693 if(ptr >= post_data_end-2) {
694 GlobalFree(post_data);
701 post_data_len -= ptr-(const char*)post_data;
702 memmove(post_data, ptr, post_data_len);
703 post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
706 *post_data_ret = post_data;
707 *post_data_len_ret = post_data_len;
710 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
715 bscallback->doc = doc;
717 /* NOTE: IE7 calls IsSystemMoniker here*/
720 call_docview_84(window->doc_obj);
723 RegisterBindStatusCallback(bctx, STATUSCLB(bscallback), NULL, 0);
724 IBindCtx_AddRef(bctx);
726 hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
728 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
729 bscallback->vtbl->stop_binding(bscallback, hres);
734 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
735 IBindCtx_Release(bctx);
737 WARN("BindToStorage failed: %08x\n", hres);
738 bscallback->vtbl->stop_binding(bscallback, hres);
743 IStream_Release(str);
745 IMoniker_Release(bscallback->mon);
746 bscallback->mon = NULL;
759 #define BUFFERBSC_THIS(bsc) ((BufferBSC*) bsc)
761 static void BufferBSC_destroy(BSCallback *bsc)
763 BufferBSC *This = BUFFERBSC_THIS(bsc);
765 heap_free(This->buf);
769 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
774 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
779 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
781 BufferBSC *This = BUFFERBSC_THIS(bsc);
786 heap_free(This->buf);
794 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
796 BufferBSC *This = BUFFERBSC_THIS(bsc);
802 This->buf = heap_alloc(This->size);
806 if(This->bsc.readed == This->size) {
808 This->buf = heap_realloc(This->buf, This->size);
812 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
813 This->bsc.readed += readed;
814 }while(hres == S_OK);
819 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
824 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code)
829 #undef BUFFERBSC_THIS
831 static const BSCallbackVtbl BufferBSCVtbl = {
833 BufferBSC_init_bindinfo,
834 BufferBSC_start_binding,
835 BufferBSC_stop_binding,
837 BufferBSC_on_progress,
838 BufferBSC_on_response
842 static BufferBSC *create_bufferbsc(IMoniker *mon)
844 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
846 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
852 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
854 BufferBSC *bsc = create_bufferbsc(mon);
859 hres = start_binding(NULL, doc, &bsc->bsc, NULL);
860 if(SUCCEEDED(hres)) {
862 if(SUCCEEDED(hres)) {
865 *size = bsc->bsc.readed;
870 IBindStatusCallback_Release(STATUSCLB(&bsc->bsc));
875 struct nsChannelBSC {
880 nsChannel *nschannel;
881 nsIStreamListener *nslistener;
882 nsISupports *nscontext;
884 nsProtocolStream *nsstream;
887 static void on_start_nsrequest(nsChannelBSC *This)
891 /* FIXME: it's needed for http connections from BindToObject. */
892 if(!This->nschannel->response_status)
893 This->nschannel->response_status = 200;
895 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
896 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
898 FIXME("OnStartRequest failed: %08x\n", nsres);
901 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
905 if(!This->nslistener)
908 if(!This->bsc.readed && SUCCEEDED(result)) {
909 TRACE("No data read! Calling OnStartRequest\n");
910 on_start_nsrequest(This);
913 nsres = nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
914 This->nscontext, SUCCEEDED(result) ? NS_OK : NS_ERROR_FAILURE);
916 WARN("OnStopRequest failed: %08x\n", nsres);
919 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
925 if(!This->nslistener) {
930 hres = IStream_Read(stream, buf, sizeof(buf), &read);
931 }while(hres == S_OK && read);
937 This->nsstream = create_nsprotocol_stream();
941 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
942 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
946 This->nsstream->buf_size += read;
948 if(!This->bsc.readed) {
949 if(This->nsstream->buf_size >= 2
950 && (BYTE)This->nsstream->buf[0] == 0xff
951 && (BYTE)This->nsstream->buf[1] == 0xfe)
952 This->nschannel->charset = heap_strdupA(UTF16_STR);
954 on_start_nsrequest(This);
957 update_window_doc(This->window);
960 This->bsc.readed += This->nsstream->buf_size;
962 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
963 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
964 NSINSTREAM(This->nsstream), This->bsc.readed-This->nsstream->buf_size,
965 This->nsstream->buf_size);
967 ERR("OnDataAvailable failed: %08x\n", nsres);
969 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
970 ERR("buffer is full\n");
973 }while(hres == S_OK);
978 static void add_nsrequest(nsChannelBSC *This)
982 if(!This->nschannel || !This->nschannel->load_group)
985 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
986 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
989 ERR("AddRequest failed:%08x\n", nsres);
992 #define NSCHANNELBSC_THIS(bsc) ((nsChannelBSC*) bsc)
994 static void nsChannelBSC_destroy(BSCallback *bsc)
996 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
999 nsIChannel_Release(NSCHANNEL(This->nschannel));
1000 if(This->nslistener)
1001 nsIStreamListener_Release(This->nslistener);
1003 nsISupports_Release(This->nscontext);
1005 nsIInputStream_Release(NSINSTREAM(This->nsstream));
1009 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1011 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1013 add_nsrequest(This);
1018 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1020 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1022 if(This->nschannel && This->nschannel->post_data_stream) {
1023 parse_post_data(This->nschannel->post_data_stream, &This->bsc.headers, &This->bsc.post_data, &This->bsc.post_data_len);
1024 TRACE("headers = %s post_data = %s\n", debugstr_w(This->bsc.headers),
1025 debugstr_an(This->bsc.post_data, This->bsc.post_data_len));
1031 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1033 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1035 on_stop_nsrequest(This, result);
1037 if(This->nslistener) {
1038 if(This->nschannel->load_group) {
1041 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1042 (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
1043 if(NS_FAILED(nsres))
1044 ERR("RemoveRequest failed: %08x\n", nsres);
1051 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1053 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1055 return read_stream_data(This, stream);
1058 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1060 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1062 switch(status_code) {
1063 case BINDSTATUS_MIMETYPEAVAILABLE:
1064 if(!This->nschannel)
1067 heap_free(This->nschannel->content_type);
1068 This->nschannel->content_type = heap_strdupWtoA(status_text);
1074 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code)
1076 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1078 This->nschannel->response_status = response_code;
1082 #undef NSCHANNELBSC_THIS
1084 static const BSCallbackVtbl nsChannelBSCVtbl = {
1085 nsChannelBSC_destroy,
1086 nsChannelBSC_init_bindinfo,
1087 nsChannelBSC_start_binding,
1088 nsChannelBSC_stop_binding,
1089 nsChannelBSC_read_data,
1090 nsChannelBSC_on_progress,
1091 nsChannelBSC_on_response
1094 nsChannelBSC *create_channelbsc(IMoniker *mon)
1096 nsChannelBSC *ret = heap_alloc_zero(sizeof(*ret));
1098 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1103 IMoniker *get_channelbsc_mon(nsChannelBSC *This)
1106 IMoniker_AddRef(This->bsc.mon);
1107 return This->bsc.mon;
1110 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1112 if(window->bscallback) {
1113 if(window->bscallback->bsc.binding)
1114 IBinding_Abort(window->bscallback->bsc.binding);
1115 window->bscallback->bsc.doc = NULL;
1116 window->bscallback->window = NULL;
1117 IBindStatusCallback_Release(STATUSCLB(&window->bscallback->bsc));
1120 window->bscallback = callback;
1123 callback->window = window;
1124 IBindStatusCallback_AddRef(STATUSCLB(&callback->bsc));
1125 callback->bsc.doc = window->doc;
1129 void abort_document_bindings(HTMLDocumentNode *doc)
1133 LIST_FOR_EACH_ENTRY(iter, &doc->bindings, BSCallback, entry) {
1135 IBinding_Abort(iter->binding);
1137 list_remove(&iter->entry);
1141 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1145 if(!bscallback->nschannel) {
1146 ERR("NULL nschannel\n");
1150 bscallback->nschannel->content_type = heap_strdupA("text/html");
1151 if(!bscallback->nschannel->content_type)
1152 return E_OUTOFMEMORY;
1154 add_nsrequest(bscallback);
1156 hres = read_stream_data(bscallback, stream);
1157 IBindStatusCallback_OnStopBinding(STATUSCLB(&bscallback->bsc), hres, ERROR_SUCCESS);
1162 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1164 nsIChannel_AddRef(NSCHANNEL(channel));
1165 This->nschannel = channel;
1167 nsIStreamListener_AddRef(listener);
1168 This->nslistener = listener;
1171 nsISupports_AddRef(context);
1172 This->nscontext = context;
1176 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url,
1177 nsIInputStream *post_data_stream, DWORD hlnf)
1179 IHlinkFrame *hlink_frame;
1180 IServiceProvider *sp;
1181 BSCallback *callback;
1187 hres = IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IServiceProvider,
1192 hres = IServiceProvider_QueryService(sp, &IID_IHlinkFrame, &IID_IHlinkFrame,
1193 (void**)&hlink_frame);
1194 IServiceProvider_Release(sp);
1198 callback = &create_channelbsc(NULL)->bsc;
1200 if(post_data_stream) {
1201 parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
1202 &callback->post_data_len);
1203 TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
1204 debugstr_an(callback->post_data, callback->post_data_len));
1207 hres = CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
1209 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
1210 &IID_IHlink, (LPVOID*)&hlink);
1213 hres = CreateURLMoniker(NULL, url, &mon);
1215 if(SUCCEEDED(hres)) {
1216 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
1218 if(hlnf & HLNF_OPENINNEWWINDOW) {
1219 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1220 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1223 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
1225 IMoniker_Release(mon);
1228 IHlinkFrame_Release(hlink_frame);
1229 IBindCtx_Release(bindctx);
1230 IBindStatusCallback_Release(STATUSCLB(callback));
1234 HRESULT navigate_url(HTMLDocumentNode *doc, OLECHAR *url)
1236 OLECHAR *translated_url = NULL;
1242 if(doc->basedoc.doc_obj->hostui) {
1243 hres = IDocHostUIHandler_TranslateUrl(doc->basedoc.doc_obj->hostui, 0, url,
1246 url = translated_url;
1249 hres = hlink_frame_navigate(&doc->basedoc, url, NULL, 0);
1251 FIXME("hlink_frame_navigate failed: %08x\n", hres);
1253 CoTaskMemFree(translated_url);