2 * Copyright 2006-2010 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
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 #include "mshtml_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44 #define CONTENT_LENGTH "Content-Length"
45 #define UTF16_STR "utf-16"
48 nsIInputStream nsIInputStream_iface;
57 void (*destroy)(BSCallback*);
58 HRESULT (*init_bindinfo)(BSCallback*);
59 HRESULT (*start_binding)(BSCallback*);
60 HRESULT (*stop_binding)(BSCallback*,HRESULT);
61 HRESULT (*read_data)(BSCallback*,IStream*);
62 HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
63 HRESULT (*on_response)(BSCallback*,DWORD,LPCWSTR);
64 HRESULT (*beginning_transaction)(BSCallback*,WCHAR**);
68 IBindStatusCallback IBindStatusCallback_iface;
69 IServiceProvider IServiceProvider_iface;
70 IHttpNegotiate2 IHttpNegotiate2_iface;
71 IInternetBindInfo IInternetBindInfo_iface;
73 const BSCallbackVtbl *vtbl;
87 HTMLDocumentNode *doc;
92 static inline nsProtocolStream *impl_from_nsIInputStream(nsIInputStream *iface)
94 return CONTAINING_RECORD(iface, nsProtocolStream, nsIInputStream_iface);
97 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
100 nsProtocolStream *This = impl_from_nsIInputStream(iface);
104 if(IsEqualGUID(&IID_nsISupports, riid)) {
105 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
106 *result = &This->nsIInputStream_iface;
107 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
108 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
109 *result = &This->nsIInputStream_iface;
113 nsIInputStream_AddRef(&This->nsIInputStream_iface);
117 WARN("unsupported interface %s\n", debugstr_guid(riid));
118 return NS_NOINTERFACE;
121 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
123 nsProtocolStream *This = impl_from_nsIInputStream(iface);
124 LONG ref = InterlockedIncrement(&This->ref);
126 TRACE("(%p) ref=%d\n", This, ref);
132 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
134 nsProtocolStream *This = impl_from_nsIInputStream(iface);
135 LONG ref = InterlockedDecrement(&This->ref);
137 TRACE("(%p) ref=%d\n", This, ref);
145 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
147 nsProtocolStream *This = impl_from_nsIInputStream(iface);
148 FIXME("(%p)\n", This);
149 return NS_ERROR_NOT_IMPLEMENTED;
152 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
154 nsProtocolStream *This = impl_from_nsIInputStream(iface);
155 FIXME("(%p)->(%p)\n", This, _retval);
156 return NS_ERROR_NOT_IMPLEMENTED;
159 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
162 nsProtocolStream *This = impl_from_nsIInputStream(iface);
165 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
167 if(read > This->buf_size)
168 read = This->buf_size;
171 memcpy(aBuf, This->buf, read);
172 if(read < This->buf_size)
173 memmove(This->buf, This->buf+read, This->buf_size-read);
174 This->buf_size -= read;
181 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
182 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
183 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
185 nsProtocolStream *This = impl_from_nsIInputStream(iface);
186 PRUint32 written = 0;
189 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
194 if(aCount > This->buf_size)
195 aCount = This->buf_size;
197 nsres = aWriter(&This->nsIInputStream_iface, aClousure, This->buf, 0, aCount, &written);
199 TRACE("aWritter failed: %08x\n", nsres);
200 else if(written != This->buf_size)
201 FIXME("written %d != buf_size %d\n", written, This->buf_size);
203 This->buf_size -= written;
209 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
211 nsProtocolStream *This = impl_from_nsIInputStream(iface);
212 FIXME("(%p)->(%p)\n", This, _retval);
213 return NS_ERROR_NOT_IMPLEMENTED;
216 static const nsIInputStreamVtbl nsInputStreamVtbl = {
217 nsInputStream_QueryInterface,
218 nsInputStream_AddRef,
219 nsInputStream_Release,
221 nsInputStream_Available,
223 nsInputStream_ReadSegments,
224 nsInputStream_IsNonBlocking
227 static nsProtocolStream *create_nsprotocol_stream(void)
229 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
231 ret->nsIInputStream_iface.lpVtbl = &nsInputStreamVtbl;
238 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
240 return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
243 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
244 REFIID riid, void **ppv)
246 BSCallback *This = impl_from_IBindStatusCallback(iface);
249 if(IsEqualGUID(&IID_IUnknown, riid)) {
250 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
251 *ppv = &This->IBindStatusCallback_iface;
252 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
253 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
254 *ppv = &This->IBindStatusCallback_iface;
255 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
256 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
257 *ppv = &This->IServiceProvider_iface;
258 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
259 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
260 *ppv = &This->IHttpNegotiate2_iface;
261 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
262 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
263 *ppv = &This->IHttpNegotiate2_iface;
264 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
265 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
266 *ppv = &This->IInternetBindInfo_iface;
270 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
274 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
275 return E_NOINTERFACE;
278 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
280 BSCallback *This = impl_from_IBindStatusCallback(iface);
281 LONG ref = InterlockedIncrement(&This->ref);
283 TRACE("(%p) ref = %d\n", This, ref);
288 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
290 BSCallback *This = impl_from_IBindStatusCallback(iface);
291 LONG ref = InterlockedDecrement(&This->ref);
293 TRACE("(%p) ref = %d\n", This, ref);
297 GlobalFree(This->post_data);
299 IMoniker_Release(This->mon);
301 IBinding_Release(This->binding);
302 list_remove(&This->entry);
303 list_init(&This->entry);
304 heap_free(This->headers);
306 This->vtbl->destroy(This);
312 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
313 DWORD dwReserved, IBinding *pbind)
315 BSCallback *This = impl_from_IBindStatusCallback(iface);
317 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
319 IBinding_AddRef(pbind);
320 This->binding = pbind;
323 list_add_head(&This->doc->bindings, &This->entry);
325 return This->vtbl->start_binding(This);
328 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
330 BSCallback *This = impl_from_IBindStatusCallback(iface);
331 FIXME("(%p)->(%p)\n", This, pnPriority);
335 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
337 BSCallback *This = impl_from_IBindStatusCallback(iface);
338 FIXME("(%p)->(%d)\n", This, reserved);
342 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
343 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
345 BSCallback *This = impl_from_IBindStatusCallback(iface);
347 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
348 debugstr_w(szStatusText));
350 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
353 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
354 HRESULT hresult, LPCWSTR szError)
356 BSCallback *This = impl_from_IBindStatusCallback(iface);
359 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
361 /* NOTE: IE7 calls GetBindResult here */
363 hres = This->vtbl->stop_binding(This, hresult);
366 IBinding_Release(This->binding);
367 This->binding = NULL;
370 list_remove(&This->entry);
371 list_init(&This->entry);
377 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
378 DWORD *grfBINDF, BINDINFO *pbindinfo)
380 BSCallback *This = impl_from_IBindStatusCallback(iface);
383 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
385 if(!This->bindinfo_ready) {
388 hres = This->vtbl->init_bindinfo(This);
392 This->bindinfo_ready = TRUE;
395 *grfBINDF = This->bindf;
397 size = pbindinfo->cbSize;
398 memset(pbindinfo, 0, size);
399 pbindinfo->cbSize = size;
401 pbindinfo->cbstgmedData = This->post_data_len;
402 pbindinfo->dwCodePage = CP_UTF8;
403 pbindinfo->dwOptions = 0x80000;
405 if(This->post_data) {
406 pbindinfo->dwBindVerb = BINDVERB_POST;
408 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
409 pbindinfo->stgmedData.u.hGlobal = This->post_data;
410 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
411 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
417 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
418 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
420 BSCallback *This = impl_from_IBindStatusCallback(iface);
422 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
424 return This->vtbl->read_data(This, pstgmed->u.pstm);
427 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
428 REFIID riid, IUnknown *punk)
430 BSCallback *This = impl_from_IBindStatusCallback(iface);
431 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
435 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
436 BindStatusCallback_QueryInterface,
437 BindStatusCallback_AddRef,
438 BindStatusCallback_Release,
439 BindStatusCallback_OnStartBinding,
440 BindStatusCallback_GetPriority,
441 BindStatusCallback_OnLowResource,
442 BindStatusCallback_OnProgress,
443 BindStatusCallback_OnStopBinding,
444 BindStatusCallback_GetBindInfo,
445 BindStatusCallback_OnDataAvailable,
446 BindStatusCallback_OnObjectAvailable
449 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
451 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
454 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
455 REFIID riid, void **ppv)
457 BSCallback *This = impl_from_IHttpNegotiate2(iface);
458 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
461 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
463 BSCallback *This = impl_from_IHttpNegotiate2(iface);
464 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
467 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
469 BSCallback *This = impl_from_IHttpNegotiate2(iface);
470 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
473 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
474 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
476 BSCallback *This = impl_from_IHttpNegotiate2(iface);
479 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
480 dwReserved, pszAdditionalHeaders);
482 *pszAdditionalHeaders = NULL;
484 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
491 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
492 *pszAdditionalHeaders = CoTaskMemAlloc(size);
493 if(!*pszAdditionalHeaders)
494 return E_OUTOFMEMORY;
495 memcpy(*pszAdditionalHeaders, This->headers, size);
501 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
502 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
504 BSCallback *This = impl_from_IHttpNegotiate2(iface);
506 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
507 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
509 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
512 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
513 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
515 BSCallback *This = impl_from_IHttpNegotiate2(iface);
516 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
520 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
521 HttpNegotiate_QueryInterface,
522 HttpNegotiate_AddRef,
523 HttpNegotiate_Release,
524 HttpNegotiate_BeginningTransaction,
525 HttpNegotiate_OnResponse,
526 HttpNegotiate_GetRootSecurityId
529 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
531 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
534 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
535 REFIID riid, void **ppv)
537 BSCallback *This = impl_from_IInternetBindInfo(iface);
538 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
541 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
543 BSCallback *This = impl_from_IInternetBindInfo(iface);
544 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
547 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
549 BSCallback *This = impl_from_IInternetBindInfo(iface);
550 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
553 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
554 DWORD *grfBINDF, BINDINFO *pbindinfo)
556 BSCallback *This = impl_from_IInternetBindInfo(iface);
557 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
561 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
562 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
564 BSCallback *This = impl_from_IInternetBindInfo(iface);
565 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
569 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
570 InternetBindInfo_QueryInterface,
571 InternetBindInfo_AddRef,
572 InternetBindInfo_Release,
573 InternetBindInfo_GetBindInfo,
574 InternetBindInfo_GetBindString
577 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
579 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
582 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
583 REFIID riid, void **ppv)
585 BSCallback *This = impl_from_IServiceProvider(iface);
586 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
589 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
591 BSCallback *This = impl_from_IServiceProvider(iface);
592 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
595 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
597 BSCallback *This = impl_from_IServiceProvider(iface);
598 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
601 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
602 REFGUID guidService, REFIID riid, void **ppv)
604 BSCallback *This = impl_from_IServiceProvider(iface);
605 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
606 return E_NOINTERFACE;
609 static const IServiceProviderVtbl ServiceProviderVtbl = {
610 BSCServiceProvider_QueryInterface,
611 BSCServiceProvider_AddRef,
612 BSCServiceProvider_Release,
613 BSCServiceProvider_QueryService
616 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
618 This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
619 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
620 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
621 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
626 list_init(&This->entry);
629 IMoniker_AddRef(mon);
633 /* Calls undocumented 84 cmd of CGID_ShellDocView */
634 static void call_docview_84(HTMLDocumentObj *doc)
636 IOleCommandTarget *olecmd;
643 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
648 hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
649 IOleCommandTarget_Release(olecmd);
650 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
651 FIXME("handle result\n");
654 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
656 const WCHAR *header, *header_end, *colon, *value;
661 if(header[0] == '\r' && header[1] == '\n' && !header[2])
663 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
673 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
675 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
680 if(header[0] == '\r' && header[1] == '\n')
687 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
692 TRACE("(%p %p %p %p)\n", window, doc, bscallback, bctx);
694 bscallback->doc = doc;
696 /* NOTE: IE7 calls IsSystemMoniker here*/
699 if(bscallback->mon != window->mon)
700 set_current_mon(window, bscallback->mon);
701 call_docview_84(window->doc_obj);
705 RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
706 IBindCtx_AddRef(bctx);
708 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
710 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
711 bscallback->vtbl->stop_binding(bscallback, hres);
716 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
717 IBindCtx_Release(bctx);
719 WARN("BindToStorage failed: %08x\n", hres);
720 bscallback->vtbl->stop_binding(bscallback, hres);
725 IStream_Release(str);
727 IMoniker_Release(bscallback->mon);
728 bscallback->mon = NULL;
741 static inline BufferBSC *BufferBSC_from_BSCallback(BSCallback *iface)
743 return CONTAINING_RECORD(iface, BufferBSC, bsc);
746 static void BufferBSC_destroy(BSCallback *bsc)
748 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
750 heap_free(This->buf);
754 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
759 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
764 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
766 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
771 heap_free(This->buf);
779 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
781 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
787 This->buf = heap_alloc(This->size);
791 if(This->bsc.readed == This->size) {
793 This->buf = heap_realloc(This->buf, This->size);
797 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
798 This->bsc.readed += readed;
799 }while(hres == S_OK);
804 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
809 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
810 LPCWSTR response_headers)
815 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
820 static const BSCallbackVtbl BufferBSCVtbl = {
822 BufferBSC_init_bindinfo,
823 BufferBSC_start_binding,
824 BufferBSC_stop_binding,
826 BufferBSC_on_progress,
827 BufferBSC_on_response,
828 BufferBSC_beginning_transaction
832 static BufferBSC *create_bufferbsc(IMoniker *mon)
834 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
836 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
842 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
844 BufferBSC *bsc = create_bufferbsc(mon);
849 hres = start_binding(NULL, doc, &bsc->bsc, NULL);
850 if(SUCCEEDED(hres)) {
852 if(SUCCEEDED(hres)) {
855 *size = bsc->bsc.readed;
860 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
865 struct nsChannelBSC {
870 nsChannel *nschannel;
871 nsIStreamListener *nslistener;
872 nsISupports *nscontext;
874 nsProtocolStream *nsstream;
877 static HRESULT read_post_data_stream(nsChannelBSC *This, nsChannel *nschannel)
879 PRUint32 data_len = 0, available = 0;
880 char *data, *post_data;
884 if(!nschannel->post_data_stream)
887 nsres = nsIInputStream_Available(nschannel->post_data_stream, &available);
891 post_data = data = GlobalAlloc(0, available);
893 return E_OUTOFMEMORY;
895 nsres = nsIInputStream_Read(nschannel->post_data_stream, data, available, &data_len);
896 if(NS_FAILED(nsres)) {
901 if(nschannel->post_data_contains_headers) {
902 if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
910 post_data += data_len;
911 for(ptr = data; ptr+4 < data+data_len; ptr++) {
912 if(!memcmp(ptr, "\r\n\r\n", 4)) {
918 data_len -= post_data-data;
920 size = MultiByteToWideChar(CP_ACP, 0, data, post_data-data, NULL, 0);
921 headers = heap_alloc((size+1)*sizeof(WCHAR));
923 MultiByteToWideChar(CP_ACP, 0, data, post_data-data, headers, size);
925 hres = parse_headers(headers , &nschannel->request_headers);
927 This->bsc.headers = headers;
931 hres = E_OUTOFMEMORY;
944 }else if(post_data != data) {
947 new_data = GlobalAlloc(0, data_len);
949 memcpy(new_data, post_data, data_len);
952 return E_OUTOFMEMORY;
953 post_data = new_data;
956 This->bsc.post_data = post_data;
957 This->bsc.post_data_len = data_len;
958 TRACE("post_data = %s\n", debugstr_a(This->bsc.post_data));
962 static HRESULT on_start_nsrequest(nsChannelBSC *This)
966 /* FIXME: it's needed for http connections from BindToObject. */
967 if(!This->nschannel->response_status)
968 This->nschannel->response_status = 200;
970 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
971 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
972 if(NS_FAILED(nsres)) {
973 FIXME("OnStartRequest failed: %08x\n", nsres);
978 list_remove(&This->bsc.entry);
979 list_init(&This->bsc.entry);
980 update_window_doc(This->window);
981 if(This->window->doc != This->bsc.doc)
982 This->bsc.doc = This->window->doc;
983 list_add_head(&This->bsc.doc->bindings, &This->bsc.entry);
984 if(This->window->readystate != READYSTATE_LOADING)
985 set_ready_state(This->window, READYSTATE_LOADING);
991 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
993 nsresult nsres, request_result;
997 request_result = NS_OK;
1000 request_result = NS_BINDING_ABORTED;
1003 request_result = NS_ERROR_FAILURE;
1006 if(This->nslistener) {
1007 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
1008 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1010 if(NS_FAILED(nsres))
1011 WARN("OnStopRequet failed: %08x\n", nsres);
1014 if(This->nschannel->load_group) {
1015 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1016 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
1017 if(NS_FAILED(nsres))
1018 ERR("RemoveRequest failed: %08x\n", nsres);
1022 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
1028 if(!This->nslistener) {
1033 hres = IStream_Read(stream, buf, sizeof(buf), &read);
1034 }while(hres == S_OK && read);
1040 This->nsstream = create_nsprotocol_stream();
1044 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
1045 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1049 This->nsstream->buf_size += read;
1051 if(!This->bsc.readed) {
1052 if(This->nsstream->buf_size >= 2
1053 && (BYTE)This->nsstream->buf[0] == 0xff
1054 && (BYTE)This->nsstream->buf[1] == 0xfe)
1055 This->nschannel->charset = heap_strdupA(UTF16_STR);
1057 if(!This->nschannel->content_type) {
1060 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
1064 TRACE("Found MIME %s\n", debugstr_w(mime));
1066 This->nschannel->content_type = heap_strdupWtoA(mime);
1067 CoTaskMemFree(mime);
1068 if(!This->nschannel->content_type)
1069 return E_OUTOFMEMORY;
1072 on_start_nsrequest(This);
1075 This->bsc.readed += This->nsstream->buf_size;
1077 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1078 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1079 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1080 This->nsstream->buf_size);
1081 if(NS_FAILED(nsres))
1082 ERR("OnDataAvailable failed: %08x\n", nsres);
1084 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1085 ERR("buffer is full\n");
1088 }while(hres == S_OK);
1094 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1098 nsChannel *nschannel;
1100 } nsRedirectCallback;
1102 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1104 return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1107 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1108 nsIIDRef riid, void **result)
1110 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1112 if(IsEqualGUID(&IID_nsISupports, riid)) {
1113 TRACE("(%p)->(IID_nsISuports %p)\n", This, result);
1114 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1115 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1116 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1117 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1120 WARN("unimplemented iface %s\n", debugstr_guid(riid));
1121 return NS_NOINTERFACE;
1124 nsISupports_AddRef((nsISupports*)*result);
1128 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1130 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1131 LONG ref = InterlockedIncrement(&This->ref);
1133 TRACE("(%p) ref=%d\n", This, ref);
1138 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1140 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1141 LONG ref = InterlockedDecrement(&This->ref);
1143 TRACE("(%p) ref=%d\n", This, ref);
1146 IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1147 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1154 static nsresult NSAPI nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1156 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1158 TRACE("(%p)->(%08x)\n", This, result);
1160 if(This->bsc->nschannel)
1161 nsIHttpChannel_Release(&This->bsc->nschannel->nsIHttpChannel_iface);
1162 nsIChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1163 This->bsc->nschannel = This->nschannel;
1165 if(This->nschannel->load_group) {
1168 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1170 if(NS_FAILED(nsres))
1171 ERR("AddRequest failed: %08x\n", nsres);
1177 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1178 nsAsyncVerifyRedirectCallback_QueryInterface,
1179 nsAsyncVerifyRedirectCallback_AddRef,
1180 nsAsyncVerifyRedirectCallback_Release,
1181 nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1184 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1186 nsRedirectCallback *callback;
1188 callback = heap_alloc(sizeof(*callback));
1190 return E_OUTOFMEMORY;
1192 callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1195 nsIChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1196 callback->nschannel = nschannel;
1198 IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1199 callback->bsc = bsc;
1205 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1207 return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1210 static void nsChannelBSC_destroy(BSCallback *bsc)
1212 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1215 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1216 if(This->nslistener)
1217 nsIStreamListener_Release(This->nslistener);
1219 nsISupports_Release(This->nscontext);
1221 nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1225 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1227 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1230 This->window->doc->skip_mutation_notif = FALSE;
1235 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1237 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1240 if(This->nschannel && This->nschannel->post_data_stream) {
1241 hres = read_post_data_stream(This, This->nschannel);
1252 } stop_request_task_t;
1254 static void stop_request_proc(task_t *_task)
1256 stop_request_task_t *task = (stop_request_task_t*)_task;
1258 TRACE("(%p)\n", task->bsc);
1260 list_remove(&task->bsc->bsc.entry);
1261 list_init(&task->bsc->bsc.entry);
1262 on_stop_nsrequest(task->bsc, S_OK);
1263 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1266 static HRESULT async_stop_request(nsChannelBSC *This)
1268 stop_request_task_t *task;
1270 if(!This->bsc.readed) {
1271 TRACE("No data read, calling OnStartRequest\n");
1272 on_start_nsrequest(This);
1275 task = heap_alloc(sizeof(*task));
1277 return E_OUTOFMEMORY;
1279 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1281 push_task(&task->header, stop_request_proc, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1285 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1287 HTMLDocumentObj *doc;
1288 IOleCommandTarget *olecmd;
1291 SAFEARRAYBOUND bound;
1292 VARIANT var, varOut;
1300 doc = This->window->doc_obj;
1301 if(!doc || !doc->doc_object_service || !doc->client)
1304 hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1305 This->window->url, &is_error_url);
1306 if(FAILED(hres) || is_error_url)
1309 hres = IOleClientSite_QueryInterface(doc->client,
1310 &IID_IOleCommandTarget, (void**)&olecmd);
1315 bound.cElements = 8;
1316 sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1318 IOleCommandTarget_Release(olecmd);
1324 V_I4(&var) = result;
1325 SafeArrayPutElement(sa, &ind, &var);
1328 V_VT(&var) = VT_BSTR;
1329 url = SysAllocString(This->window->url);
1331 SafeArrayPutElement(sa, &ind, &var);
1334 V_VT(&var) = VT_UNKNOWN;
1335 V_UNKNOWN(&var) = (IUnknown*)&This->window->IHTMLWindow2_iface;
1336 SafeArrayPutElement(sa, &ind, &var);
1338 /* FIXME: what are the following fields for? */
1340 V_VT(&var) = VT_UNKNOWN;
1341 V_UNKNOWN(&var) = NULL;
1342 SafeArrayPutElement(sa, &ind, &var);
1345 V_VT(&var) = VT_BOOL;
1346 V_BOOL(&var) = FALSE;
1347 SafeArrayPutElement(sa, &ind, &var);
1350 V_VT(&var) = VT_BOOL;
1351 V_BOOL(&var) = FALSE;
1352 SafeArrayPutElement(sa, &ind, &var);
1355 V_VT(&var) = VT_BSTR;
1356 unk = SysAllocString(NULL);
1358 SafeArrayPutElement(sa, &ind, &var);
1361 V_VT(&var) = VT_UNKNOWN;
1362 V_UNKNOWN(&var) = NULL;
1363 SafeArrayPutElement(sa, &ind, &var);
1365 V_VT(&var) = VT_ARRAY;
1367 V_VT(&varOut) = VT_BOOL;
1368 V_BOOL(&varOut) = VARIANT_TRUE;
1369 IOleCommandTarget_Exec(olecmd, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1373 SafeArrayDestroy(sa);
1374 IOleCommandTarget_Release(olecmd);
1377 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1379 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1381 if(result != E_ABORT) {
1383 handle_navigation_error(This, result);
1384 else if(This->window) {
1385 result = async_stop_request(This);
1386 if(SUCCEEDED(result))
1391 on_stop_nsrequest(This, result);
1395 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1397 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1399 return read_stream_data(This, stream);
1402 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1404 nsRedirectCallback *callback;
1405 nsIChannelEventSink *sink;
1406 nsChannel *new_channel;
1410 TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1412 if(!This->nschannel || !This->nschannel->notif_callback)
1415 nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1416 if(NS_FAILED(nsres))
1419 hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1420 if(SUCCEEDED(hres)) {
1421 hres = create_redirect_callback(new_channel, This, &callback);
1422 nsIChannel_Release(&new_channel->nsIHttpChannel_iface);
1425 if(SUCCEEDED(hres)) {
1426 nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1427 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1428 &callback->nsIAsyncVerifyRedirectCallback_iface);
1430 if(NS_FAILED(nsres))
1431 FIXME("AsyncOnChannelRedirect failed: %08x\n", hres);
1432 else if(This->nschannel != callback->nschannel)
1433 FIXME("nschannel not updated\n");
1435 nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1438 nsIChannelEventSink_Release(sink);
1442 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1444 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1446 switch(status_code) {
1447 case BINDSTATUS_MIMETYPEAVAILABLE:
1448 if(!This->nschannel)
1451 heap_free(This->nschannel->content_type);
1452 This->nschannel->content_type = heap_strdupWtoA(status_text);
1454 case BINDSTATUS_REDIRECTING:
1455 return handle_redirect(This, status_text);
1456 case BINDSTATUS_BEGINDOWNLOADDATA: {
1457 IWinInetHttpInfo *http_info;
1458 DWORD status, size = sizeof(DWORD);
1461 if(!This->bsc.binding)
1464 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1468 hres = IWinInetHttpInfo_QueryInfo(http_info,
1469 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1470 IWinInetHttpInfo_Release(http_info);
1471 if(FAILED(hres) || status == HTTP_STATUS_OK)
1474 handle_navigation_error(This, status);
1481 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1482 LPCWSTR response_headers)
1484 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1487 This->nschannel->response_status = response_code;
1489 if(response_headers) {
1490 const WCHAR *headers;
1492 headers = strchrW(response_headers, '\r');
1493 if(headers && headers[1] == '\n') {
1495 hres = parse_headers(headers, &This->nschannel->response_headers);
1497 WARN("parsing headers failed: %08x\n", hres);
1506 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1508 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1509 http_header_t *iter;
1513 static const WCHAR content_lengthW[] =
1514 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1516 if(!This->nschannel)
1519 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1520 if(strcmpW(iter->header, content_lengthW))
1521 len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1527 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1529 return E_OUTOFMEMORY;
1531 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1532 if(!strcmpW(iter->header, content_lengthW))
1535 len = strlenW(iter->header);
1536 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1542 len = strlenW(iter->data);
1543 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1555 static const BSCallbackVtbl nsChannelBSCVtbl = {
1556 nsChannelBSC_destroy,
1557 nsChannelBSC_init_bindinfo,
1558 nsChannelBSC_start_binding,
1559 nsChannelBSC_stop_binding,
1560 nsChannelBSC_read_data,
1561 nsChannelBSC_on_progress,
1562 nsChannelBSC_on_response,
1563 nsChannelBSC_beginning_transaction
1566 HRESULT create_channelbsc(IMoniker *mon, WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1570 ret = heap_alloc_zero(sizeof(*ret));
1572 return E_OUTOFMEMORY;
1574 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1577 ret->bsc.headers = heap_strdupW(headers);
1578 if(!ret->bsc.headers) {
1579 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1580 return E_OUTOFMEMORY;
1585 ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1586 if(!ret->bsc.post_data) {
1587 heap_free(ret->bsc.headers);
1588 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1589 return E_OUTOFMEMORY;
1592 memcpy(ret->bsc.post_data, post_data, post_data_size);
1593 ret->bsc.post_data_len = post_data_size;
1600 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1602 if(window->bscallback) {
1603 if(window->bscallback->bsc.binding)
1604 IBinding_Abort(window->bscallback->bsc.binding);
1605 window->bscallback->bsc.doc = NULL;
1606 window->bscallback->window = NULL;
1607 IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1610 window->bscallback = callback;
1613 callback->window = window;
1614 IBindStatusCallback_AddRef(&callback->bsc.IBindStatusCallback_iface);
1615 callback->bsc.doc = window->doc;
1622 nsChannelBSC *bscallback;
1623 } start_doc_binding_task_t;
1625 static void start_doc_binding_proc(task_t *_task)
1627 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1629 start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1630 IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1633 HRESULT async_start_doc_binding(HTMLWindow *window, nsChannelBSC *bscallback)
1635 start_doc_binding_task_t *task;
1637 task = heap_alloc(sizeof(start_doc_binding_task_t));
1639 return E_OUTOFMEMORY;
1641 task->window = window;
1642 task->bscallback = bscallback;
1643 IBindStatusCallback_AddRef(&bscallback->bsc.IBindStatusCallback_iface);
1645 push_task(&task->header, start_doc_binding_proc, window->task_magic);
1649 void abort_document_bindings(HTMLDocumentNode *doc)
1651 BSCallback *iter, *next;
1653 LIST_FOR_EACH_ENTRY_SAFE(iter, next, &doc->bindings, BSCallback, entry) {
1654 TRACE("Aborting %p\n", iter);
1657 remove_target_tasks(iter->doc->basedoc.task_magic);
1660 IBinding_Abort(iter->binding);
1662 list_remove(&iter->entry);
1663 list_init(&iter->entry);
1664 iter->vtbl->stop_binding(iter, E_ABORT);
1671 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1673 HRESULT hres = S_OK;
1675 if(!bscallback->nschannel) {
1676 ERR("NULL nschannel\n");
1680 bscallback->nschannel->content_type = heap_strdupA("text/html");
1681 if(!bscallback->nschannel->content_type)
1682 return E_OUTOFMEMORY;
1684 list_add_head(&bscallback->bsc.doc->bindings, &bscallback->bsc.entry);
1686 hres = read_stream_data(bscallback, stream);
1688 hres = async_stop_request(bscallback);
1690 IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1696 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1698 nsIChannel_AddRef(&channel->nsIHttpChannel_iface);
1699 This->nschannel = channel;
1701 nsIStreamListener_AddRef(listener);
1702 This->nslistener = listener;
1705 nsISupports_AddRef(context);
1706 This->nscontext = context;
1709 if(This->bsc.headers) {
1712 hres = parse_headers(This->bsc.headers, &channel->request_headers);
1713 heap_free(This->bsc.headers);
1714 This->bsc.headers = NULL;
1716 WARN("parse_headers failed: %08x\n", hres);
1720 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
1722 IHlinkFrame *hlink_frame;
1723 nsChannelBSC *callback;
1724 IServiceProvider *sp;
1732 hres = IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IServiceProvider,
1737 hres = IServiceProvider_QueryService(sp, &IID_IHlinkFrame, &IID_IHlinkFrame,
1738 (void**)&hlink_frame);
1739 IServiceProvider_Release(sp);
1743 hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
1745 IHlinkFrame_Release(hlink_frame);
1750 read_post_data_stream(callback, nschannel);
1752 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
1754 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
1755 &IID_IHlink, (LPVOID*)&hlink);
1758 hres = CreateURLMoniker(NULL, url, &mon);
1760 if(SUCCEEDED(hres)) {
1761 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
1763 if(hlnf & HLNF_OPENINNEWWINDOW) {
1764 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1765 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1768 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
1769 &callback->bsc.IBindStatusCallback_iface, hlink);
1770 IMoniker_Release(mon);
1771 *cancel = hres == S_OK;
1775 IHlinkFrame_Release(hlink_frame);
1776 IBindCtx_Release(bindctx);
1777 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
1781 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
1783 WCHAR url[INTERNET_MAX_URL_LENGTH];
1789 }else if(base_url) {
1792 hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
1793 url, sizeof(url)/sizeof(WCHAR), &len, 0);
1797 strcpyW(url, new_url);
1800 if(window->doc_obj && window->doc_obj->hostui) {
1801 OLECHAR *translated_url = NULL;
1803 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
1806 TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
1807 strcpyW(url, translated_url);
1808 CoTaskMemFree(translated_url);
1812 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
1815 hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
1820 TRACE("Navigation handled by hlink frame\n");
1825 hres = create_doc_uri(window, url, &uri);
1829 hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
1830 nsISupports_Release((nsISupports*)uri);