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"
39 #include "mshtml_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44 #define CONTENT_LENGTH "Content-Length"
45 #define UTF16_STR "utf-16"
47 struct nsProtocolStream {
48 nsIInputStream nsIInputStream_iface;
56 struct BSCallbackVtbl {
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**);
67 static inline nsProtocolStream *impl_from_nsIInputStream(nsIInputStream *iface)
69 return CONTAINING_RECORD(iface, nsProtocolStream, nsIInputStream_iface);
72 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
75 nsProtocolStream *This = impl_from_nsIInputStream(iface);
79 if(IsEqualGUID(&IID_nsISupports, riid)) {
80 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
81 *result = &This->nsIInputStream_iface;
82 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
83 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
84 *result = &This->nsIInputStream_iface;
88 nsIInputStream_AddRef(&This->nsIInputStream_iface);
92 WARN("unsupported interface %s\n", debugstr_guid(riid));
93 return NS_NOINTERFACE;
96 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
98 nsProtocolStream *This = impl_from_nsIInputStream(iface);
99 LONG ref = InterlockedIncrement(&This->ref);
101 TRACE("(%p) ref=%d\n", This, ref);
107 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
109 nsProtocolStream *This = impl_from_nsIInputStream(iface);
110 LONG ref = InterlockedDecrement(&This->ref);
112 TRACE("(%p) ref=%d\n", This, ref);
120 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
122 nsProtocolStream *This = impl_from_nsIInputStream(iface);
123 FIXME("(%p)\n", This);
124 return NS_ERROR_NOT_IMPLEMENTED;
127 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
129 nsProtocolStream *This = impl_from_nsIInputStream(iface);
130 FIXME("(%p)->(%p)\n", This, _retval);
131 return NS_ERROR_NOT_IMPLEMENTED;
134 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
137 nsProtocolStream *This = impl_from_nsIInputStream(iface);
140 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
142 if(read > This->buf_size)
143 read = This->buf_size;
146 memcpy(aBuf, This->buf, read);
147 if(read < This->buf_size)
148 memmove(This->buf, This->buf+read, This->buf_size-read);
149 This->buf_size -= read;
156 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
157 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
158 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
160 nsProtocolStream *This = impl_from_nsIInputStream(iface);
161 PRUint32 written = 0;
164 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
169 if(aCount > This->buf_size)
170 aCount = This->buf_size;
172 nsres = aWriter(&This->nsIInputStream_iface, aClousure, This->buf, 0, aCount, &written);
174 TRACE("aWritter failed: %08x\n", nsres);
175 else if(written != This->buf_size)
176 FIXME("written %d != buf_size %d\n", written, This->buf_size);
178 This->buf_size -= written;
184 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
186 nsProtocolStream *This = impl_from_nsIInputStream(iface);
187 FIXME("(%p)->(%p)\n", This, _retval);
188 return NS_ERROR_NOT_IMPLEMENTED;
191 static const nsIInputStreamVtbl nsInputStreamVtbl = {
192 nsInputStream_QueryInterface,
193 nsInputStream_AddRef,
194 nsInputStream_Release,
196 nsInputStream_Available,
198 nsInputStream_ReadSegments,
199 nsInputStream_IsNonBlocking
202 static nsProtocolStream *create_nsprotocol_stream(void)
204 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
206 ret->nsIInputStream_iface.lpVtbl = &nsInputStreamVtbl;
213 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
215 return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
218 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
219 REFIID riid, void **ppv)
221 BSCallback *This = impl_from_IBindStatusCallback(iface);
224 if(IsEqualGUID(&IID_IUnknown, riid)) {
225 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
226 *ppv = &This->IBindStatusCallback_iface;
227 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
228 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
229 *ppv = &This->IBindStatusCallback_iface;
230 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
231 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
232 *ppv = &This->IServiceProvider_iface;
233 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
234 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
235 *ppv = &This->IHttpNegotiate2_iface;
236 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
237 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
238 *ppv = &This->IHttpNegotiate2_iface;
239 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
240 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
241 *ppv = &This->IInternetBindInfo_iface;
245 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
249 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
250 return E_NOINTERFACE;
253 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
255 BSCallback *This = impl_from_IBindStatusCallback(iface);
256 LONG ref = InterlockedIncrement(&This->ref);
258 TRACE("(%p) ref = %d\n", This, ref);
263 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
265 BSCallback *This = impl_from_IBindStatusCallback(iface);
266 LONG ref = InterlockedDecrement(&This->ref);
268 TRACE("(%p) ref = %d\n", This, ref);
272 GlobalFree(This->post_data);
274 IMoniker_Release(This->mon);
276 IBinding_Release(This->binding);
277 list_remove(&This->entry);
278 list_init(&This->entry);
279 heap_free(This->headers);
281 This->vtbl->destroy(This);
287 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
288 DWORD dwReserved, IBinding *pbind)
290 BSCallback *This = impl_from_IBindStatusCallback(iface);
292 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
294 IBinding_AddRef(pbind);
295 This->binding = pbind;
298 list_add_head(&This->doc->bindings, &This->entry);
300 return This->vtbl->start_binding(This);
303 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
305 BSCallback *This = impl_from_IBindStatusCallback(iface);
306 FIXME("(%p)->(%p)\n", This, pnPriority);
310 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
312 BSCallback *This = impl_from_IBindStatusCallback(iface);
313 FIXME("(%p)->(%d)\n", This, reserved);
317 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
318 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
320 BSCallback *This = impl_from_IBindStatusCallback(iface);
322 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
323 debugstr_w(szStatusText));
325 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
328 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
329 HRESULT hresult, LPCWSTR szError)
331 BSCallback *This = impl_from_IBindStatusCallback(iface);
334 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
336 /* NOTE: IE7 calls GetBindResult here */
338 hres = This->vtbl->stop_binding(This, hresult);
341 IBinding_Release(This->binding);
342 This->binding = NULL;
345 list_remove(&This->entry);
346 list_init(&This->entry);
352 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
353 DWORD *grfBINDF, BINDINFO *pbindinfo)
355 BSCallback *This = impl_from_IBindStatusCallback(iface);
358 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
360 if(!This->bindinfo_ready) {
363 hres = This->vtbl->init_bindinfo(This);
367 This->bindinfo_ready = TRUE;
370 *grfBINDF = This->bindf;
372 size = pbindinfo->cbSize;
373 memset(pbindinfo, 0, size);
374 pbindinfo->cbSize = size;
376 pbindinfo->cbstgmedData = This->post_data_len;
377 pbindinfo->dwCodePage = CP_UTF8;
378 pbindinfo->dwOptions = 0x80000;
380 if(This->post_data) {
381 pbindinfo->dwBindVerb = BINDVERB_POST;
383 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
384 pbindinfo->stgmedData.u.hGlobal = This->post_data;
385 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
386 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
392 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
393 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
395 BSCallback *This = impl_from_IBindStatusCallback(iface);
397 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
399 return This->vtbl->read_data(This, pstgmed->u.pstm);
402 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
403 REFIID riid, IUnknown *punk)
405 BSCallback *This = impl_from_IBindStatusCallback(iface);
406 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
410 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
411 BindStatusCallback_QueryInterface,
412 BindStatusCallback_AddRef,
413 BindStatusCallback_Release,
414 BindStatusCallback_OnStartBinding,
415 BindStatusCallback_GetPriority,
416 BindStatusCallback_OnLowResource,
417 BindStatusCallback_OnProgress,
418 BindStatusCallback_OnStopBinding,
419 BindStatusCallback_GetBindInfo,
420 BindStatusCallback_OnDataAvailable,
421 BindStatusCallback_OnObjectAvailable
424 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
426 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
429 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
430 REFIID riid, void **ppv)
432 BSCallback *This = impl_from_IHttpNegotiate2(iface);
433 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
436 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
438 BSCallback *This = impl_from_IHttpNegotiate2(iface);
439 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
442 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
444 BSCallback *This = impl_from_IHttpNegotiate2(iface);
445 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
448 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
449 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
451 BSCallback *This = impl_from_IHttpNegotiate2(iface);
454 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
455 dwReserved, pszAdditionalHeaders);
457 *pszAdditionalHeaders = NULL;
459 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
466 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
467 *pszAdditionalHeaders = CoTaskMemAlloc(size);
468 if(!*pszAdditionalHeaders)
469 return E_OUTOFMEMORY;
470 memcpy(*pszAdditionalHeaders, This->headers, size);
476 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
477 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
479 BSCallback *This = impl_from_IHttpNegotiate2(iface);
481 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
482 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
484 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
487 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
488 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
490 BSCallback *This = impl_from_IHttpNegotiate2(iface);
491 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
495 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
496 HttpNegotiate_QueryInterface,
497 HttpNegotiate_AddRef,
498 HttpNegotiate_Release,
499 HttpNegotiate_BeginningTransaction,
500 HttpNegotiate_OnResponse,
501 HttpNegotiate_GetRootSecurityId
504 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
506 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
509 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
510 REFIID riid, void **ppv)
512 BSCallback *This = impl_from_IInternetBindInfo(iface);
513 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
516 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
518 BSCallback *This = impl_from_IInternetBindInfo(iface);
519 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
522 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
524 BSCallback *This = impl_from_IInternetBindInfo(iface);
525 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
528 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
529 DWORD *grfBINDF, BINDINFO *pbindinfo)
531 BSCallback *This = impl_from_IInternetBindInfo(iface);
532 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
536 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
537 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
539 BSCallback *This = impl_from_IInternetBindInfo(iface);
540 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
544 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
545 InternetBindInfo_QueryInterface,
546 InternetBindInfo_AddRef,
547 InternetBindInfo_Release,
548 InternetBindInfo_GetBindInfo,
549 InternetBindInfo_GetBindString
552 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
554 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
557 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
558 REFIID riid, void **ppv)
560 BSCallback *This = impl_from_IServiceProvider(iface);
561 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
564 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
566 BSCallback *This = impl_from_IServiceProvider(iface);
567 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
570 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
572 BSCallback *This = impl_from_IServiceProvider(iface);
573 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
576 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
577 REFGUID guidService, REFIID riid, void **ppv)
579 BSCallback *This = impl_from_IServiceProvider(iface);
580 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
581 return E_NOINTERFACE;
584 static const IServiceProviderVtbl ServiceProviderVtbl = {
585 BSCServiceProvider_QueryInterface,
586 BSCServiceProvider_AddRef,
587 BSCServiceProvider_Release,
588 BSCServiceProvider_QueryService
591 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
593 This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
594 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
595 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
596 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
601 list_init(&This->entry);
604 IMoniker_AddRef(mon);
608 /* Calls undocumented 84 cmd of CGID_ShellDocView */
609 static void call_docview_84(HTMLDocumentObj *doc)
611 IOleCommandTarget *olecmd;
618 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
623 hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
624 IOleCommandTarget_Release(olecmd);
625 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
626 FIXME("handle result\n");
629 static void parse_content_type(nsChannelBSC *This, const WCHAR *value)
634 static const WCHAR charsetW[] = {'c','h','a','r','s','e','t','='};
636 ptr = strchrW(value, ';');
641 while(*ptr && isspaceW(*ptr))
644 len = strlenW(value);
645 if(ptr + sizeof(charsetW)/sizeof(WCHAR) < value+len && !memicmpW(ptr, charsetW, sizeof(charsetW)/sizeof(WCHAR))) {
646 size_t charset_len, lena;
647 nsACString charset_str;
648 const WCHAR *charset;
651 ptr += sizeof(charsetW)/sizeof(WCHAR);
654 FIXME("Quoted value\n");
658 while(*ptr && *ptr != ',')
660 charset_len = ptr-charset;
663 lena = WideCharToMultiByte(CP_ACP, 0, charset, charset_len, NULL, 0, NULL, NULL);
664 charseta = heap_alloc(lena+1);
668 WideCharToMultiByte(CP_ACP, 0, charset, charset_len, charseta, lena, NULL, NULL);
671 nsACString_InitDepend(&charset_str, charseta);
672 nsIHttpChannel_SetContentCharset(&This->nschannel->nsIHttpChannel_iface, &charset_str);
673 nsACString_Finish(&charset_str);
676 FIXME("unhandled: %s\n", debugstr_wn(ptr, len - (ptr-value)));
680 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
682 const WCHAR *header, *header_end, *colon, *value;
687 if(header[0] == '\r' && header[1] == '\n' && !header[2])
689 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
699 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
701 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
706 if(header[0] == '\r' && header[1] == '\n')
713 static HRESULT process_response_headers(nsChannelBSC *This, const WCHAR *headers)
718 static const WCHAR content_typeW[] = {'c','o','n','t','e','n','t','-','t','y','p','e',0};
720 hres = parse_headers(headers, &This->nschannel->response_headers);
724 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->response_headers, http_header_t, entry) {
725 if(!strcmpiW(iter->header, content_typeW))
726 parse_content_type(This, iter->data);
732 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
737 TRACE("(%p %p %p %p)\n", window, doc, bscallback, bctx);
739 bscallback->doc = doc;
741 /* NOTE: IE7 calls IsSystemMoniker here*/
744 if(bscallback->mon != window->mon)
745 set_current_mon(window, bscallback->mon);
746 call_docview_84(window->doc_obj);
750 RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
751 IBindCtx_AddRef(bctx);
753 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
755 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
756 bscallback->vtbl->stop_binding(bscallback, hres);
761 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
762 IBindCtx_Release(bctx);
764 WARN("BindToStorage failed: %08x\n", hres);
765 bscallback->vtbl->stop_binding(bscallback, hres);
770 IStream_Release(str);
772 IMoniker_Release(bscallback->mon);
773 bscallback->mon = NULL;
786 static inline BufferBSC *BufferBSC_from_BSCallback(BSCallback *iface)
788 return CONTAINING_RECORD(iface, BufferBSC, bsc);
791 static void BufferBSC_destroy(BSCallback *bsc)
793 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
795 heap_free(This->buf);
799 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
804 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
809 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
811 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
816 heap_free(This->buf);
824 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
826 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
832 This->buf = heap_alloc(This->size);
836 if(This->bsc.readed == This->size) {
838 This->buf = heap_realloc(This->buf, This->size);
842 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
843 This->bsc.readed += readed;
844 }while(hres == S_OK);
849 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
854 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
855 LPCWSTR response_headers)
860 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
865 static const BSCallbackVtbl BufferBSCVtbl = {
867 BufferBSC_init_bindinfo,
868 BufferBSC_start_binding,
869 BufferBSC_stop_binding,
871 BufferBSC_on_progress,
872 BufferBSC_on_response,
873 BufferBSC_beginning_transaction
877 static BufferBSC *create_bufferbsc(IMoniker *mon)
879 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
881 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
887 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
889 BufferBSC *bsc = create_bufferbsc(mon);
894 hres = start_binding(NULL, doc, &bsc->bsc, NULL);
895 if(SUCCEEDED(hres)) {
897 if(SUCCEEDED(hres)) {
900 *size = bsc->bsc.readed;
905 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
910 static HRESULT read_post_data_stream(nsChannelBSC *This, nsChannel *nschannel)
912 PRUint32 data_len = 0, available = 0;
913 char *data, *post_data;
917 if(!nschannel->post_data_stream)
920 nsres = nsIInputStream_Available(nschannel->post_data_stream, &available);
924 post_data = data = GlobalAlloc(0, available);
926 return E_OUTOFMEMORY;
928 nsres = nsIInputStream_Read(nschannel->post_data_stream, data, available, &data_len);
929 if(NS_FAILED(nsres)) {
934 if(nschannel->post_data_contains_headers) {
935 if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
943 post_data += data_len;
944 for(ptr = data; ptr+4 < data+data_len; ptr++) {
945 if(!memcmp(ptr, "\r\n\r\n", 4)) {
951 data_len -= post_data-data;
953 size = MultiByteToWideChar(CP_ACP, 0, data, post_data-data, NULL, 0);
954 headers = heap_alloc((size+1)*sizeof(WCHAR));
956 MultiByteToWideChar(CP_ACP, 0, data, post_data-data, headers, size);
958 hres = parse_headers(headers , &nschannel->request_headers);
960 This->bsc.headers = headers;
964 hres = E_OUTOFMEMORY;
977 }else if(post_data != data) {
980 new_data = GlobalAlloc(0, data_len);
982 memcpy(new_data, post_data, data_len);
985 return E_OUTOFMEMORY;
986 post_data = new_data;
989 This->bsc.post_data = post_data;
990 This->bsc.post_data_len = data_len;
991 TRACE("post_data = %s\n", debugstr_a(This->bsc.post_data));
995 static HRESULT on_start_nsrequest(nsChannelBSC *This)
999 /* FIXME: it's needed for http connections from BindToObject. */
1000 if(!This->nschannel->response_status)
1001 This->nschannel->response_status = 200;
1003 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
1004 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
1005 if(NS_FAILED(nsres)) {
1006 FIXME("OnStartRequest failed: %08x\n", nsres);
1011 list_remove(&This->bsc.entry);
1012 list_init(&This->bsc.entry);
1013 update_window_doc(This->window);
1014 if(This->window->doc != This->bsc.doc)
1015 This->bsc.doc = This->window->doc;
1016 list_add_head(&This->bsc.doc->bindings, &This->bsc.entry);
1017 if(This->window->readystate != READYSTATE_LOADING)
1018 set_ready_state(This->window, READYSTATE_LOADING);
1024 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
1026 nsresult nsres, request_result;
1030 request_result = NS_OK;
1033 request_result = NS_BINDING_ABORTED;
1036 request_result = NS_ERROR_FAILURE;
1039 if(This->nslistener) {
1040 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
1041 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1043 if(NS_FAILED(nsres))
1044 WARN("OnStopRequet failed: %08x\n", nsres);
1047 if(This->nschannel->load_group) {
1048 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1049 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
1050 if(NS_FAILED(nsres))
1051 ERR("RemoveRequest failed: %08x\n", nsres);
1055 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
1061 if(!This->nslistener) {
1066 hres = IStream_Read(stream, buf, sizeof(buf), &read);
1067 }while(hres == S_OK && read);
1073 This->nsstream = create_nsprotocol_stream();
1077 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
1078 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1082 This->nsstream->buf_size += read;
1084 if(!This->bsc.readed) {
1085 if(This->nsstream->buf_size >= 2
1086 && (BYTE)This->nsstream->buf[0] == 0xff
1087 && (BYTE)This->nsstream->buf[1] == 0xfe)
1088 This->nschannel->charset = heap_strdupA(UTF16_STR);
1090 if(!This->nschannel->content_type) {
1093 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
1097 TRACE("Found MIME %s\n", debugstr_w(mime));
1099 This->nschannel->content_type = heap_strdupWtoA(mime);
1100 CoTaskMemFree(mime);
1101 if(!This->nschannel->content_type)
1102 return E_OUTOFMEMORY;
1105 on_start_nsrequest(This);
1108 This->bsc.readed += This->nsstream->buf_size;
1110 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1111 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1112 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1113 This->nsstream->buf_size);
1114 if(NS_FAILED(nsres))
1115 ERR("OnDataAvailable failed: %08x\n", nsres);
1117 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1118 ERR("buffer is full\n");
1121 }while(hres == S_OK);
1127 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1131 nsChannel *nschannel;
1133 } nsRedirectCallback;
1135 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1137 return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1140 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1141 nsIIDRef riid, void **result)
1143 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1145 if(IsEqualGUID(&IID_nsISupports, riid)) {
1146 TRACE("(%p)->(IID_nsISuports %p)\n", This, result);
1147 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1148 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1149 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1150 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1153 WARN("unimplemented iface %s\n", debugstr_guid(riid));
1154 return NS_NOINTERFACE;
1157 nsISupports_AddRef((nsISupports*)*result);
1161 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1163 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1164 LONG ref = InterlockedIncrement(&This->ref);
1166 TRACE("(%p) ref=%d\n", This, ref);
1171 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1173 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1174 LONG ref = InterlockedDecrement(&This->ref);
1176 TRACE("(%p) ref=%d\n", This, ref);
1179 IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1180 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1187 static nsresult NSAPI nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1189 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1191 TRACE("(%p)->(%08x)\n", This, result);
1193 if(This->bsc->nschannel)
1194 nsIHttpChannel_Release(&This->bsc->nschannel->nsIHttpChannel_iface);
1195 nsIChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1196 This->bsc->nschannel = This->nschannel;
1198 if(This->nschannel->load_group) {
1201 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1203 if(NS_FAILED(nsres))
1204 ERR("AddRequest failed: %08x\n", nsres);
1207 if(This->bsc->window) {
1208 IUri *uri = nsuri_get_uri(This->nschannel->uri);
1211 set_current_uri(This->bsc->window, uri);
1214 WARN("Could not get IUri from nsWineURI\n");
1221 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1222 nsAsyncVerifyRedirectCallback_QueryInterface,
1223 nsAsyncVerifyRedirectCallback_AddRef,
1224 nsAsyncVerifyRedirectCallback_Release,
1225 nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1228 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1230 nsRedirectCallback *callback;
1232 callback = heap_alloc(sizeof(*callback));
1234 return E_OUTOFMEMORY;
1236 callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1239 nsIChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1240 callback->nschannel = nschannel;
1242 IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1243 callback->bsc = bsc;
1249 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1251 return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1254 static void nsChannelBSC_destroy(BSCallback *bsc)
1256 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1259 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1260 if(This->nslistener)
1261 nsIStreamListener_Release(This->nslistener);
1263 nsISupports_Release(This->nscontext);
1265 nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1269 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1271 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1274 This->window->doc->skip_mutation_notif = FALSE;
1279 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1281 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1284 if(This->nschannel && This->nschannel->post_data_stream) {
1285 hres = read_post_data_stream(This, This->nschannel);
1296 } stop_request_task_t;
1298 static void stop_request_proc(task_t *_task)
1300 stop_request_task_t *task = (stop_request_task_t*)_task;
1302 TRACE("(%p)\n", task->bsc);
1304 list_remove(&task->bsc->bsc.entry);
1305 list_init(&task->bsc->bsc.entry);
1306 on_stop_nsrequest(task->bsc, S_OK);
1309 static void stop_request_task_destr(task_t *_task)
1311 stop_request_task_t *task = (stop_request_task_t*)_task;
1313 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1317 static HRESULT async_stop_request(nsChannelBSC *This)
1319 stop_request_task_t *task;
1321 if(!This->bsc.readed) {
1322 TRACE("No data read, calling OnStartRequest\n");
1323 on_start_nsrequest(This);
1326 task = heap_alloc(sizeof(*task));
1328 return E_OUTOFMEMORY;
1330 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1332 push_task(&task->header, stop_request_proc, stop_request_task_destr, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1336 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1338 HTMLDocumentObj *doc;
1339 IOleCommandTarget *olecmd;
1342 SAFEARRAYBOUND bound;
1343 VARIANT var, varOut;
1351 doc = This->window->doc_obj;
1352 if(!doc || !doc->doc_object_service || !doc->client)
1355 hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1356 This->window->url, &is_error_url);
1357 if(FAILED(hres) || is_error_url)
1360 hres = IOleClientSite_QueryInterface(doc->client,
1361 &IID_IOleCommandTarget, (void**)&olecmd);
1366 bound.cElements = 8;
1367 sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1369 IOleCommandTarget_Release(olecmd);
1375 V_I4(&var) = result;
1376 SafeArrayPutElement(sa, &ind, &var);
1379 V_VT(&var) = VT_BSTR;
1380 V_BSTR(&var) = This->window->url;
1381 SafeArrayPutElement(sa, &ind, &var);
1384 V_VT(&var) = VT_UNKNOWN;
1385 V_UNKNOWN(&var) = (IUnknown*)&This->window->IHTMLWindow2_iface;
1386 SafeArrayPutElement(sa, &ind, &var);
1388 /* FIXME: what are the following fields for? */
1390 V_VT(&var) = VT_UNKNOWN;
1391 V_UNKNOWN(&var) = NULL;
1392 SafeArrayPutElement(sa, &ind, &var);
1395 V_VT(&var) = VT_BOOL;
1396 V_BOOL(&var) = FALSE;
1397 SafeArrayPutElement(sa, &ind, &var);
1400 V_VT(&var) = VT_BOOL;
1401 V_BOOL(&var) = FALSE;
1402 SafeArrayPutElement(sa, &ind, &var);
1405 V_VT(&var) = VT_BSTR;
1406 unk = SysAllocString(NULL);
1408 SafeArrayPutElement(sa, &ind, &var);
1411 V_VT(&var) = VT_UNKNOWN;
1412 V_UNKNOWN(&var) = NULL;
1413 SafeArrayPutElement(sa, &ind, &var);
1415 V_VT(&var) = VT_ARRAY;
1417 V_VT(&varOut) = VT_BOOL;
1418 V_BOOL(&varOut) = VARIANT_TRUE;
1419 IOleCommandTarget_Exec(olecmd, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1422 SafeArrayDestroy(sa);
1423 IOleCommandTarget_Release(olecmd);
1426 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1428 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1430 if(result != E_ABORT) {
1432 handle_navigation_error(This, result);
1433 else if(This->window) {
1434 result = async_stop_request(This);
1435 if(SUCCEEDED(result))
1440 on_stop_nsrequest(This, result);
1444 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1446 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1448 return read_stream_data(This, stream);
1451 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1453 nsRedirectCallback *callback;
1454 nsIChannelEventSink *sink;
1455 nsChannel *new_channel;
1459 TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1461 if(!This->nschannel || !This->nschannel->notif_callback)
1464 nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1465 if(NS_FAILED(nsres))
1468 hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1469 if(SUCCEEDED(hres)) {
1470 hres = create_redirect_callback(new_channel, This, &callback);
1471 nsIChannel_Release(&new_channel->nsIHttpChannel_iface);
1474 if(SUCCEEDED(hres)) {
1475 nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1476 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1477 &callback->nsIAsyncVerifyRedirectCallback_iface);
1479 if(NS_FAILED(nsres))
1480 FIXME("AsyncOnChannelRedirect failed: %08x\n", hres);
1481 else if(This->nschannel != callback->nschannel)
1482 FIXME("nschannel not updated\n");
1484 nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1487 nsIChannelEventSink_Release(sink);
1491 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1493 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1495 switch(status_code) {
1496 case BINDSTATUS_MIMETYPEAVAILABLE:
1497 if(!This->nschannel)
1500 heap_free(This->nschannel->content_type);
1501 This->nschannel->content_type = heap_strdupWtoA(status_text);
1503 case BINDSTATUS_REDIRECTING:
1504 return handle_redirect(This, status_text);
1505 case BINDSTATUS_BEGINDOWNLOADDATA: {
1506 IWinInetHttpInfo *http_info;
1507 DWORD status, size = sizeof(DWORD);
1510 if(!This->bsc.binding)
1513 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1517 hres = IWinInetHttpInfo_QueryInfo(http_info,
1518 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1519 IWinInetHttpInfo_Release(http_info);
1520 if(FAILED(hres) || status == HTTP_STATUS_OK)
1523 handle_navigation_error(This, status);
1530 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1531 LPCWSTR response_headers)
1533 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1536 This->nschannel->response_status = response_code;
1538 if(response_headers) {
1539 const WCHAR *headers;
1541 headers = strchrW(response_headers, '\r');
1542 if(headers && headers[1] == '\n') {
1544 hres = process_response_headers(This, headers);
1546 WARN("parsing headers failed: %08x\n", hres);
1555 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1557 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1558 http_header_t *iter;
1562 static const WCHAR content_lengthW[] =
1563 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1565 if(!This->nschannel)
1568 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1569 if(strcmpW(iter->header, content_lengthW))
1570 len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1576 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1578 return E_OUTOFMEMORY;
1580 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1581 if(!strcmpW(iter->header, content_lengthW))
1584 len = strlenW(iter->header);
1585 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1591 len = strlenW(iter->data);
1592 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1604 static const BSCallbackVtbl nsChannelBSCVtbl = {
1605 nsChannelBSC_destroy,
1606 nsChannelBSC_init_bindinfo,
1607 nsChannelBSC_start_binding,
1608 nsChannelBSC_stop_binding,
1609 nsChannelBSC_read_data,
1610 nsChannelBSC_on_progress,
1611 nsChannelBSC_on_response,
1612 nsChannelBSC_beginning_transaction
1615 HRESULT create_channelbsc(IMoniker *mon, const WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1619 ret = heap_alloc_zero(sizeof(*ret));
1621 return E_OUTOFMEMORY;
1623 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1626 ret->bsc.headers = heap_strdupW(headers);
1627 if(!ret->bsc.headers) {
1628 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1629 return E_OUTOFMEMORY;
1634 ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1635 if(!ret->bsc.post_data) {
1636 heap_free(ret->bsc.headers);
1637 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1638 return E_OUTOFMEMORY;
1641 memcpy(ret->bsc.post_data, post_data, post_data_size);
1642 ret->bsc.post_data_len = post_data_size;
1649 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1651 if(window->bscallback) {
1652 if(window->bscallback->bsc.binding)
1653 IBinding_Abort(window->bscallback->bsc.binding);
1654 window->bscallback->bsc.doc = NULL;
1655 window->bscallback->window = NULL;
1656 IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1659 window->bscallback = callback;
1662 callback->window = window;
1663 IBindStatusCallback_AddRef(&callback->bsc.IBindStatusCallback_iface);
1664 callback->bsc.doc = window->doc;
1671 nsChannelBSC *bscallback;
1672 } start_doc_binding_task_t;
1674 static void start_doc_binding_proc(task_t *_task)
1676 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1678 start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1681 static void start_doc_binding_task_destr(task_t *_task)
1683 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1685 IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1689 HRESULT async_start_doc_binding(HTMLWindow *window, nsChannelBSC *bscallback)
1691 start_doc_binding_task_t *task;
1693 TRACE("%p\n", bscallback);
1695 task = heap_alloc(sizeof(start_doc_binding_task_t));
1697 return E_OUTOFMEMORY;
1699 task->window = window;
1700 task->bscallback = bscallback;
1701 IBindStatusCallback_AddRef(&bscallback->bsc.IBindStatusCallback_iface);
1703 push_task(&task->header, start_doc_binding_proc, start_doc_binding_task_destr, window->task_magic);
1707 void abort_document_bindings(HTMLDocumentNode *doc)
1709 BSCallback *iter, *next;
1711 LIST_FOR_EACH_ENTRY_SAFE(iter, next, &doc->bindings, BSCallback, entry) {
1712 TRACE("Aborting %p\n", iter);
1715 remove_target_tasks(iter->doc->basedoc.task_magic);
1718 IBinding_Abort(iter->binding);
1720 list_remove(&iter->entry);
1721 list_init(&iter->entry);
1722 iter->vtbl->stop_binding(iter, E_ABORT);
1729 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1731 HRESULT hres = S_OK;
1733 if(!bscallback->nschannel) {
1734 ERR("NULL nschannel\n");
1738 bscallback->nschannel->content_type = heap_strdupA("text/html");
1739 if(!bscallback->nschannel->content_type)
1740 return E_OUTOFMEMORY;
1742 list_add_head(&bscallback->bsc.doc->bindings, &bscallback->bsc.entry);
1744 hres = read_stream_data(bscallback, stream);
1746 hres = async_stop_request(bscallback);
1748 IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1754 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1756 nsIChannel_AddRef(&channel->nsIHttpChannel_iface);
1757 This->nschannel = channel;
1759 nsIStreamListener_AddRef(listener);
1760 This->nslistener = listener;
1763 nsISupports_AddRef(context);
1764 This->nscontext = context;
1767 if(This->bsc.headers) {
1770 hres = parse_headers(This->bsc.headers, &channel->request_headers);
1771 heap_free(This->bsc.headers);
1772 This->bsc.headers = NULL;
1774 WARN("parse_headers failed: %08x\n", hres);
1782 } navigate_javascript_task_t;
1784 static void navigate_javascript_proc(task_t *_task)
1786 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1787 HTMLWindow *window = task->window;
1792 static const WCHAR jscriptW[] = {'j','s','c','r','i','p','t',0};
1794 task->window->readystate = READYSTATE_COMPLETE;
1796 hres = IUri_GetPath(task->uri, &code);
1800 set_download_state(window->doc_obj, 1);
1802 V_VT(&v) = VT_EMPTY;
1803 hres = exec_script(window, code, jscriptW, &v);
1804 SysFreeString(code);
1805 if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
1806 FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
1810 if(window->doc_obj->view_sink)
1811 IAdviseSink_OnViewChange(window->doc_obj->view_sink, DVASPECT_CONTENT, -1);
1813 set_download_state(window->doc_obj, 0);
1816 static void navigate_javascript_task_destr(task_t *_task)
1818 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1820 IUri_Release(task->uri);
1827 nsChannelBSC *bscallback;
1831 static void navigate_proc(task_t *_task)
1833 navigate_task_t *task = (navigate_task_t*)_task;
1836 hres = set_moniker(&task->window->doc_obj->basedoc, task->mon, NULL, task->bscallback, TRUE);
1838 start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1841 static void navigate_task_destr(task_t *_task)
1843 navigate_task_t *task = (navigate_task_t*)_task;
1845 IUnknown_Release((IUnknown*)task->bscallback);
1846 IMoniker_Release(task->mon);
1850 static HRESULT navigate_fragment(HTMLWindow *window, IUri *uri)
1852 nsIDOMLocation *nslocation;
1853 nsAString nsfrag_str;
1858 set_current_uri(window, uri);
1860 nsres = nsIDOMWindow_GetLocation(window->nswindow, &nslocation);
1861 if(FAILED(nsres) || !nslocation)
1864 hres = IUri_GetFragment(uri, &frag);
1866 nsIDOMLocation_Release(nslocation);
1870 nsAString_InitDepend(&nsfrag_str, frag);
1871 nsres = nsIDOMLocation_SetHash(nslocation, &nsfrag_str);
1872 nsAString_Finish(&nsfrag_str);
1873 nsIDOMLocation_Release(nslocation);
1874 SysFreeString(frag);
1875 if(NS_FAILED(nsres)) {
1876 ERR("SetHash failed: %08x\n", nsres);
1880 if(window->doc_obj->doc_object_service) {
1881 IDocObjectService_FireNavigateComplete2(window->doc_obj->doc_object_service, &window->IHTMLWindow2_iface, 0x10);
1882 IDocObjectService_FireDocumentComplete(window->doc_obj->doc_object_service, &window->IHTMLWindow2_iface, 0);
1889 HRESULT super_navigate(HTMLWindow *window, IUri *uri, const WCHAR *headers, BYTE *post_data, DWORD post_data_size)
1896 if(window->doc_obj->client) {
1897 IOleCommandTarget *cmdtrg;
1899 hres = IOleClientSite_QueryInterface(window->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1900 if(SUCCEEDED(hres)) {
1904 hres = IUri_GetDisplayUri(uri, &url_str);
1905 if(SUCCEEDED(hres)) {
1906 V_VT(&in) = VT_BSTR;
1907 V_BSTR(&in) = url_str;
1908 V_VT(&out) = VT_BOOL;
1909 V_BOOL(&out) = VARIANT_TRUE;
1910 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
1911 IOleCommandTarget_Release(cmdtrg);
1914 SysFreeString(url_str);
1919 if(window->uri && compare_ignoring_frag(window->uri, uri)) {
1920 TRACE("fragment navigate\n");
1921 return navigate_fragment(window, uri);
1924 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1928 /* FIXME: Why not set_ready_state? */
1929 window->readystate = READYSTATE_UNINITIALIZED;
1931 hres = create_channelbsc(mon, headers, post_data, post_data_size, &bsc);
1933 IMoniker_Release(mon);
1937 prepare_for_binding(&window->doc_obj->basedoc, mon, TRUE);
1939 hres = IUri_GetScheme(uri, &scheme);
1941 if(scheme != URL_SCHEME_JAVASCRIPT) {
1942 navigate_task_t *task;
1944 task = heap_alloc(sizeof(*task));
1946 IUnknown_Release((IUnknown*)bsc);
1947 IMoniker_Release(mon);
1948 return E_OUTOFMEMORY;
1951 task->window = window;
1952 task->bscallback = bsc;
1954 push_task(&task->header, navigate_proc, navigate_task_destr, window->task_magic);
1956 /* Silently and repeated when real loading starts? */
1957 window->readystate = READYSTATE_LOADING;
1959 navigate_javascript_task_t *task;
1961 IUnknown_Release((IUnknown*)bsc);
1962 IMoniker_Release(mon);
1964 task = heap_alloc(sizeof(*task));
1966 return E_OUTOFMEMORY;
1969 task->window = window;
1971 push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, window->task_magic);
1974 window->readystate = READYSTATE_COMPLETE;
1980 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
1982 IHlinkFrame *hlink_frame;
1983 nsChannelBSC *callback;
1991 hres = do_query_service((IUnknown*)doc->doc_obj->client, &IID_IHlinkFrame, &IID_IHlinkFrame,
1992 (void**)&hlink_frame);
1996 hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
1998 IHlinkFrame_Release(hlink_frame);
2003 read_post_data_stream(callback, nschannel);
2005 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
2007 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
2008 &IID_IHlink, (LPVOID*)&hlink);
2011 hres = CreateURLMoniker(NULL, url, &mon);
2013 if(SUCCEEDED(hres)) {
2014 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
2016 if(hlnf & HLNF_OPENINNEWWINDOW) {
2017 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
2018 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
2021 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
2022 &callback->bsc.IBindStatusCallback_iface, hlink);
2023 IMoniker_Release(mon);
2024 *cancel = hres == S_OK;
2028 IHlinkFrame_Release(hlink_frame);
2029 IBindCtx_Release(bindctx);
2030 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
2034 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
2036 WCHAR url[INTERNET_MAX_URL_LENGTH];
2042 }else if(base_url) {
2045 hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2046 url, sizeof(url)/sizeof(WCHAR), &len, 0);
2050 strcpyW(url, new_url);
2053 if(window->doc_obj && window->doc_obj->hostui) {
2054 OLECHAR *translated_url = NULL;
2056 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
2059 TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
2060 strcpyW(url, translated_url);
2061 CoTaskMemFree(translated_url);
2065 if(window->doc_obj && window->doc_obj->is_webbrowser && window == window->doc_obj->basedoc.window) {
2066 BOOL cancel = FALSE;
2069 hres = IDocObjectService_FireBeforeNavigate2(window->doc_obj->doc_object_service, NULL, url, 0x40,
2070 NULL, NULL, 0, NULL, TRUE, &cancel);
2071 if(SUCCEEDED(hres) && cancel) {
2072 TRACE("Navigation canceled\n");
2076 hres = CreateUri(url, 0, 0, &uri);
2080 hres = super_navigate(window, uri, NULL, NULL, 0);
2085 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
2088 hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
2093 TRACE("Navigation handled by hlink frame\n");
2098 hres = create_doc_uri(window, url, &uri);
2102 hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
2103 nsISupports_Release((nsISupports*)uri);