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"
45 const nsIInputStreamVtbl *lpInputStreamVtbl;
53 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
56 void (*destroy)(BSCallback*);
57 HRESULT (*init_bindinfo)(BSCallback*);
58 HRESULT (*start_binding)(BSCallback*);
59 HRESULT (*stop_binding)(BSCallback*,HRESULT);
60 HRESULT (*read_data)(BSCallback*,IStream*);
61 HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
62 HRESULT (*on_response)(BSCallback*,DWORD);
66 const IBindStatusCallbackVtbl *lpBindStatusCallbackVtbl;
67 const IServiceProviderVtbl *lpServiceProviderVtbl;
68 const IHttpNegotiate2Vtbl *lpHttpNegotiate2Vtbl;
69 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
71 const BSCallbackVtbl *vtbl;
90 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
92 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
95 nsProtocolStream *This = NSINSTREAM_THIS(iface);
99 if(IsEqualGUID(&IID_nsISupports, riid)) {
100 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
101 *result = NSINSTREAM(This);
102 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
103 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
104 *result = NSINSTREAM(This);
108 nsIInputStream_AddRef(NSINSTREAM(This));
112 WARN("unsupported interface %s\n", debugstr_guid(riid));
113 return NS_NOINTERFACE;
116 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
118 nsProtocolStream *This = NSINSTREAM_THIS(iface);
119 LONG ref = InterlockedIncrement(&This->ref);
121 TRACE("(%p) ref=%d\n", This, ref);
127 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
129 nsProtocolStream *This = NSINSTREAM_THIS(iface);
130 LONG ref = InterlockedDecrement(&This->ref);
132 TRACE("(%p) ref=%d\n", This, ref);
140 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
142 nsProtocolStream *This = NSINSTREAM_THIS(iface);
143 FIXME("(%p)\n", This);
144 return NS_ERROR_NOT_IMPLEMENTED;
147 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
149 nsProtocolStream *This = NSINSTREAM_THIS(iface);
150 FIXME("(%p)->(%p)\n", This, _retval);
151 return NS_ERROR_NOT_IMPLEMENTED;
154 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
157 nsProtocolStream *This = NSINSTREAM_THIS(iface);
160 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
162 if(read > This->buf_size)
163 read = This->buf_size;
166 memcpy(aBuf, This->buf, read);
167 if(read < This->buf_size)
168 memmove(This->buf, This->buf+read, This->buf_size-read);
169 This->buf_size -= read;
176 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
177 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
178 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
180 nsProtocolStream *This = NSINSTREAM_THIS(iface);
181 PRUint32 written = 0;
184 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
189 if(aCount > This->buf_size)
190 aCount = This->buf_size;
192 nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, aCount, &written);
194 TRACE("aWritter failed: %08x\n", nsres);
195 else if(written != This->buf_size)
196 FIXME("written %d != buf_size %d\n", written, This->buf_size);
198 This->buf_size -= written;
204 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
206 nsProtocolStream *This = NSINSTREAM_THIS(iface);
207 FIXME("(%p)->(%p)\n", This, _retval);
208 return NS_ERROR_NOT_IMPLEMENTED;
211 #undef NSINSTREAM_THIS
213 static const nsIInputStreamVtbl nsInputStreamVtbl = {
214 nsInputStream_QueryInterface,
215 nsInputStream_AddRef,
216 nsInputStream_Release,
218 nsInputStream_Available,
220 nsInputStream_ReadSegments,
221 nsInputStream_IsNonBlocking
224 static nsProtocolStream *create_nsprotocol_stream(void)
226 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
228 ret->lpInputStreamVtbl = &nsInputStreamVtbl;
235 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
237 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
238 REFIID riid, void **ppv)
240 BSCallback *This = STATUSCLB_THIS(iface);
243 if(IsEqualGUID(&IID_IUnknown, riid)) {
244 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
245 *ppv = STATUSCLB(This);
246 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
247 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
248 *ppv = STATUSCLB(This);
249 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
250 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
251 *ppv = SERVPROV(This);
252 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
253 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
254 *ppv = HTTPNEG(This);
255 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
256 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
257 *ppv = HTTPNEG(This);
258 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
259 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
260 *ppv = BINDINFO(This);
264 IBindStatusCallback_AddRef(STATUSCLB(This));
268 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
269 return E_NOINTERFACE;
272 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
274 BSCallback *This = STATUSCLB_THIS(iface);
275 LONG ref = InterlockedIncrement(&This->ref);
277 TRACE("(%p) ref = %d\n", This, ref);
282 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
284 BSCallback *This = STATUSCLB_THIS(iface);
285 LONG ref = InterlockedDecrement(&This->ref);
287 TRACE("(%p) ref = %d\n", This, ref);
291 GlobalFree(This->post_data);
293 IMoniker_Release(This->mon);
295 IBinding_Release(This->binding);
296 list_remove(&This->entry);
297 heap_free(This->headers);
299 This->vtbl->destroy(This);
305 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
306 DWORD dwReserved, IBinding *pbind)
308 BSCallback *This = STATUSCLB_THIS(iface);
310 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
312 IBinding_AddRef(pbind);
313 This->binding = pbind;
316 list_add_head(&This->doc->bindings, &This->entry);
318 return This->vtbl->start_binding(This);
321 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
323 BSCallback *This = STATUSCLB_THIS(iface);
324 FIXME("(%p)->(%p)\n", This, pnPriority);
328 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
330 BSCallback *This = STATUSCLB_THIS(iface);
331 FIXME("(%p)->(%d)\n", This, reserved);
335 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
336 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
338 BSCallback *This = STATUSCLB_THIS(iface);
340 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
341 debugstr_w(szStatusText));
343 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
346 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
347 HRESULT hresult, LPCWSTR szError)
349 BSCallback *This = STATUSCLB_THIS(iface);
352 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
354 /* NOTE: IE7 calls GetBindResult here */
356 hres = This->vtbl->stop_binding(This, hresult);
359 IBinding_Release(This->binding);
360 This->binding = NULL;
363 list_remove(&This->entry);
369 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
370 DWORD *grfBINDF, BINDINFO *pbindinfo)
372 BSCallback *This = STATUSCLB_THIS(iface);
375 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
377 if(!This->bindinfo_ready) {
380 hres = This->vtbl->init_bindinfo(This);
384 This->bindinfo_ready = TRUE;
387 *grfBINDF = This->bindf;
389 size = pbindinfo->cbSize;
390 memset(pbindinfo, 0, size);
391 pbindinfo->cbSize = size;
393 pbindinfo->cbstgmedData = This->post_data_len;
394 pbindinfo->dwCodePage = CP_UTF8;
395 pbindinfo->dwOptions = 0x80000;
397 if(This->post_data) {
398 pbindinfo->dwBindVerb = BINDVERB_POST;
400 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
401 pbindinfo->stgmedData.u.hGlobal = This->post_data;
402 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
403 IBindStatusCallback_AddRef(STATUSCLB(This));
409 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
410 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
412 BSCallback *This = STATUSCLB_THIS(iface);
414 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
416 return This->vtbl->read_data(This, pstgmed->u.pstm);
419 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
420 REFIID riid, IUnknown *punk)
422 BSCallback *This = STATUSCLB_THIS(iface);
423 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
427 #undef STATUSCLB_THIS
429 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
430 BindStatusCallback_QueryInterface,
431 BindStatusCallback_AddRef,
432 BindStatusCallback_Release,
433 BindStatusCallback_OnStartBinding,
434 BindStatusCallback_GetPriority,
435 BindStatusCallback_OnLowResource,
436 BindStatusCallback_OnProgress,
437 BindStatusCallback_OnStopBinding,
438 BindStatusCallback_GetBindInfo,
439 BindStatusCallback_OnDataAvailable,
440 BindStatusCallback_OnObjectAvailable
443 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
445 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
446 REFIID riid, void **ppv)
448 BSCallback *This = HTTPNEG_THIS(iface);
449 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
452 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
454 BSCallback *This = HTTPNEG_THIS(iface);
455 return IBindStatusCallback_AddRef(STATUSCLB(This));
458 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
460 BSCallback *This = HTTPNEG_THIS(iface);
461 return IBindStatusCallback_Release(STATUSCLB(This));
464 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
465 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
467 BSCallback *This = HTTPNEG_THIS(iface);
470 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
471 dwReserved, pszAdditionalHeaders);
474 *pszAdditionalHeaders = NULL;
478 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
479 *pszAdditionalHeaders = CoTaskMemAlloc(size);
480 memcpy(*pszAdditionalHeaders, This->headers, size);
485 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
486 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
488 BSCallback *This = HTTPNEG_THIS(iface);
490 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
491 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
493 return This->vtbl->on_response(This, dwResponseCode);
496 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
497 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
499 BSCallback *This = HTTPNEG_THIS(iface);
500 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
506 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
507 HttpNegotiate_QueryInterface,
508 HttpNegotiate_AddRef,
509 HttpNegotiate_Release,
510 HttpNegotiate_BeginningTransaction,
511 HttpNegotiate_OnResponse,
512 HttpNegotiate_GetRootSecurityId
515 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
517 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
518 REFIID riid, void **ppv)
520 BSCallback *This = BINDINFO_THIS(iface);
521 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
524 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
526 BSCallback *This = BINDINFO_THIS(iface);
527 return IBindStatusCallback_AddRef(STATUSCLB(This));
530 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
532 BSCallback *This = BINDINFO_THIS(iface);
533 return IBindStatusCallback_Release(STATUSCLB(This));
536 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
537 DWORD *grfBINDF, BINDINFO *pbindinfo)
539 BSCallback *This = BINDINFO_THIS(iface);
540 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
544 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
545 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
547 BSCallback *This = BINDINFO_THIS(iface);
548 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
554 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
555 InternetBindInfo_QueryInterface,
556 InternetBindInfo_AddRef,
557 InternetBindInfo_Release,
558 InternetBindInfo_GetBindInfo,
559 InternetBindInfo_GetBindString
562 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
564 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
565 REFIID riid, void **ppv)
567 BSCallback *This = SERVPROV_THIS(iface);
568 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
571 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
573 BSCallback *This = SERVPROV_THIS(iface);
574 return IBindStatusCallback_AddRef(STATUSCLB(This));
577 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
579 BSCallback *This = SERVPROV_THIS(iface);
580 return IBindStatusCallback_Release(STATUSCLB(This));
583 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
584 REFGUID guidService, REFIID riid, void **ppv)
586 BSCallback *This = SERVPROV_THIS(iface);
587 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
588 return E_NOINTERFACE;
593 static const IServiceProviderVtbl ServiceProviderVtbl = {
594 BSCServiceProvider_QueryInterface,
595 BSCServiceProvider_AddRef,
596 BSCServiceProvider_Release,
597 BSCServiceProvider_QueryService
600 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
602 This->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
603 This->lpServiceProviderVtbl = &ServiceProviderVtbl;
604 This->lpHttpNegotiate2Vtbl = &HttpNegotiate2Vtbl;
605 This->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
610 list_init(&This->entry);
613 IMoniker_AddRef(mon);
617 /* Calls undocumented 84 cmd of CGID_ShellDocView */
618 static void call_docview_84(HTMLDocument *doc)
620 IOleCommandTarget *olecmd;
627 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
632 hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
633 IOleCommandTarget_Release(olecmd);
634 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
635 FIXME("handle result\n");
638 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
639 HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
641 PRUint32 post_data_len = 0, available = 0;
642 HGLOBAL post_data = NULL;
643 LPWSTR headers = NULL;
644 DWORD headers_len = 0, len;
645 const char *ptr, *ptr2, *post_data_end;
647 nsIInputStream_Available(post_data_stream, &available);
648 post_data = GlobalAlloc(0, available+1);
649 nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
651 TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
653 ptr = ptr2 = post_data;
654 post_data_end = (const char*)post_data+post_data_len;
656 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
657 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
667 if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
668 && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
669 CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
670 ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
675 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
678 headers = heap_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
680 headers = heap_alloc((len+1)*sizeof(WCHAR));
682 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, len);
688 headers[headers_len] = 0;
689 *headers_ret = headers;
691 if(ptr >= post_data_end-2) {
692 GlobalFree(post_data);
699 post_data_len -= ptr-(const char*)post_data;
700 memmove(post_data, ptr, post_data_len);
701 post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
704 *post_data_ret = post_data;
705 *post_data_len_ret = post_data_len;
708 HRESULT start_binding(HTMLDocument *doc, BSCallback *bscallback, IBindCtx *bctx)
713 bscallback->doc = doc;
715 /* NOTE: IE7 calls IsSystemMoniker here*/
717 call_docview_84(doc);
720 RegisterBindStatusCallback(bctx, STATUSCLB(bscallback), NULL, 0);
721 IBindCtx_AddRef(bctx);
723 hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
725 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
726 bscallback->vtbl->stop_binding(bscallback, hres);
731 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
732 IBindCtx_Release(bctx);
734 WARN("BindToStorage failed: %08x\n", hres);
735 bscallback->vtbl->stop_binding(bscallback, hres);
740 IStream_Release(str);
742 IMoniker_Release(bscallback->mon);
743 bscallback->mon = NULL;
756 #define BUFFERBSC_THIS(bsc) ((BufferBSC*) bsc)
758 static void BufferBSC_destroy(BSCallback *bsc)
760 BufferBSC *This = BUFFERBSC_THIS(bsc);
762 heap_free(This->buf);
766 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
771 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
776 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
778 BufferBSC *This = BUFFERBSC_THIS(bsc);
783 heap_free(This->buf);
791 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
793 BufferBSC *This = BUFFERBSC_THIS(bsc);
799 This->buf = heap_alloc(This->size);
803 if(This->bsc.readed == This->size) {
805 This->buf = heap_realloc(This->buf, This->size);
809 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
810 This->bsc.readed += readed;
811 }while(hres == S_OK);
816 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
821 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code)
826 #undef BUFFERBSC_THIS
828 static const BSCallbackVtbl BufferBSCVtbl = {
830 BufferBSC_init_bindinfo,
831 BufferBSC_start_binding,
832 BufferBSC_stop_binding,
834 BufferBSC_on_progress,
835 BufferBSC_on_response
839 static BufferBSC *create_bufferbsc(IMoniker *mon)
841 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
843 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
849 HRESULT bind_mon_to_buffer(HTMLDocument *doc, IMoniker *mon, void **buf, DWORD *size)
851 BufferBSC *bsc = create_bufferbsc(mon);
856 hres = start_binding(doc, &bsc->bsc, NULL);
857 if(SUCCEEDED(hres)) {
859 if(SUCCEEDED(hres)) {
862 *size = bsc->bsc.readed;
867 IBindStatusCallback_Release(STATUSCLB(&bsc->bsc));
872 struct nsChannelBSC {
875 nsChannel *nschannel;
876 nsIStreamListener *nslistener;
877 nsISupports *nscontext;
879 nsProtocolStream *nsstream;
882 static void on_start_nsrequest(nsChannelBSC *This)
886 /* FIXME: it's needed for http connections from BindToObject. */
887 if(!This->nschannel->response_status)
888 This->nschannel->response_status = 200;
890 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
891 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
893 FIXME("OnStartRequest failed: %08x\n", nsres);
896 static void on_stop_nsrequest(nsChannelBSC *This)
900 if(!This->nslistener)
903 if(!This->bsc.readed) {
904 TRACE("No data read! Calling OnStartRequest\n");
905 on_start_nsrequest(This);
908 nsres = nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
909 This->nscontext, NS_OK);
911 WARN("OnStopRequest failed: %08x\n", nsres);
914 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
920 if(!This->nslistener) {
925 hres = IStream_Read(stream, buf, sizeof(buf), &read);
926 }while(hres == S_OK && read);
932 This->nsstream = create_nsprotocol_stream();
936 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
937 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
941 This->nsstream->buf_size += read;
943 if(!This->bsc.readed) {
944 if(This->nsstream->buf_size >= 2
945 && (BYTE)This->nsstream->buf[0] == 0xff
946 && (BYTE)This->nsstream->buf[1] == 0xfe)
947 This->nschannel->charset = heap_strdupA(UTF16_STR);
949 on_start_nsrequest(This);
951 /* events are reset when a new document URI is loaded, so re-initialise them here */
952 if(This->bsc.doc && This->bsc.doc->bscallback == This && This->bsc.doc->nscontainer) {
953 update_nsdocument(This->bsc.doc);
954 init_nsevents(This->bsc.doc->nscontainer);
958 This->bsc.readed += This->nsstream->buf_size;
960 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
961 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
962 NSINSTREAM(This->nsstream), This->bsc.readed-This->nsstream->buf_size,
963 This->nsstream->buf_size);
965 ERR("OnDataAvailable failed: %08x\n", nsres);
967 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
968 ERR("buffer is full\n");
971 }while(hres == S_OK);
976 static void add_nsrequest(nsChannelBSC *This)
980 if(!This->nschannel || !This->nschannel->load_group)
983 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
984 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
987 ERR("AddRequest failed:%08x\n", nsres);
990 #define NSCHANNELBSC_THIS(bsc) ((nsChannelBSC*) bsc)
992 static void nsChannelBSC_destroy(BSCallback *bsc)
994 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
997 nsIChannel_Release(NSCHANNEL(This->nschannel));
999 nsIStreamListener_Release(This->nslistener);
1001 nsISupports_Release(This->nscontext);
1003 nsIInputStream_Release(NSINSTREAM(This->nsstream));
1007 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1009 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1011 add_nsrequest(This);
1016 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1018 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1020 if(This->nschannel && This->nschannel->post_data_stream) {
1021 parse_post_data(This->nschannel->post_data_stream, &This->bsc.headers, &This->bsc.post_data, &This->bsc.post_data_len);
1022 TRACE("headers = %s post_data = %s\n", debugstr_w(This->bsc.headers),
1023 debugstr_an(This->bsc.post_data, This->bsc.post_data_len));
1029 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1031 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1033 on_stop_nsrequest(This);
1035 if(This->nslistener) {
1036 if(This->nschannel->load_group) {
1039 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1040 (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
1041 if(NS_FAILED(nsres))
1042 ERR("RemoveRequest failed: %08x\n", nsres);
1049 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1051 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1053 return read_stream_data(This, stream);
1056 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1058 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1060 switch(status_code) {
1061 case BINDSTATUS_MIMETYPEAVAILABLE:
1062 if(!This->nschannel)
1065 heap_free(This->nschannel->content_type);
1066 This->nschannel->content_type = heap_strdupWtoA(status_text);
1072 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code)
1074 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1076 This->nschannel->response_status = response_code;
1080 #undef NSCHANNELBSC_THIS
1082 static const BSCallbackVtbl nsChannelBSCVtbl = {
1083 nsChannelBSC_destroy,
1084 nsChannelBSC_init_bindinfo,
1085 nsChannelBSC_start_binding,
1086 nsChannelBSC_stop_binding,
1087 nsChannelBSC_read_data,
1088 nsChannelBSC_on_progress,
1089 nsChannelBSC_on_response
1092 nsChannelBSC *create_channelbsc(IMoniker *mon)
1094 nsChannelBSC *ret = heap_alloc_zero(sizeof(*ret));
1096 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1101 IMoniker *get_channelbsc_mon(nsChannelBSC *This)
1104 IMoniker_AddRef(This->bsc.mon);
1105 return This->bsc.mon;
1108 void set_document_bscallback(HTMLDocument *doc, nsChannelBSC *callback)
1112 if(doc->bscallback) {
1113 if(doc->bscallback->bsc.binding)
1114 IBinding_Abort(doc->bscallback->bsc.binding);
1115 doc->bscallback->bsc.doc = NULL;
1116 IBindStatusCallback_Release(STATUSCLB(&doc->bscallback->bsc));
1119 LIST_FOR_EACH_ENTRY(iter, &doc->bindings, BSCallback, entry) {
1121 list_remove(&iter->entry);
1124 doc->bscallback = callback;
1127 IBindStatusCallback_AddRef(STATUSCLB(&callback->bsc));
1128 callback->bsc.doc = doc;
1132 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1136 const char text_html[] = "text/html";
1138 add_nsrequest(bscallback);
1140 if(bscallback->nschannel)
1141 bscallback->nschannel->content_type = heap_strdupA(text_html);
1143 hres = read_stream_data(bscallback, stream);
1144 IBindStatusCallback_OnStopBinding(STATUSCLB(&bscallback->bsc), hres, ERROR_SUCCESS);
1149 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1151 nsIChannel_AddRef(NSCHANNEL(channel));
1152 This->nschannel = channel;
1154 nsIStreamListener_AddRef(listener);
1155 This->nslistener = listener;
1158 nsISupports_AddRef(context);
1159 This->nscontext = context;
1163 void hlink_frame_navigate(HTMLDocument *doc, IHlinkFrame *hlink_frame,
1164 LPCWSTR uri, nsIInputStream *post_data_stream, DWORD hlnf)
1166 BSCallback *callback;
1172 callback = &create_channelbsc(NULL)->bsc;
1174 if(post_data_stream) {
1175 parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
1176 &callback->post_data_len);
1177 TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
1178 debugstr_an(callback->post_data, callback->post_data_len));
1181 hr = CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
1183 IBindStatusCallback_Release(STATUSCLB(callback));
1187 hr = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER, &IID_IHlink, (LPVOID*)&hlink);
1189 IBindCtx_Release(bindctx);
1190 IBindStatusCallback_Release(STATUSCLB(callback));
1194 hr = CreateURLMoniker(NULL, uri, &mon);
1195 if (SUCCEEDED(hr)) {
1196 IHlink_SetMonikerReference(hlink, 0, mon, NULL);
1198 if(hlnf & HLNF_OPENINNEWWINDOW) {
1199 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1200 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1203 IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
1205 IMoniker_Release(mon);
1208 IBindCtx_Release(bindctx);
1209 IBindStatusCallback_Release(STATUSCLB(callback));