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 "shdeprecated.h"
39 #include "wine/debug.h"
41 #include "mshtml_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
46 #define CONTENT_LENGTH "Content-Length"
47 #define UTF8_STR "utf-8"
48 #define UTF16_STR "utf-16"
50 static const WCHAR emptyW[] = {0};
52 struct nsProtocolStream {
53 nsIInputStream nsIInputStream_iface;
61 struct BSCallbackVtbl {
62 void (*destroy)(BSCallback*);
63 HRESULT (*init_bindinfo)(BSCallback*);
64 HRESULT (*start_binding)(BSCallback*);
65 HRESULT (*stop_binding)(BSCallback*,HRESULT);
66 HRESULT (*read_data)(BSCallback*,IStream*);
67 HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
68 HRESULT (*on_response)(BSCallback*,DWORD,LPCWSTR);
69 HRESULT (*beginning_transaction)(BSCallback*,WCHAR**);
72 static inline nsProtocolStream *impl_from_nsIInputStream(nsIInputStream *iface)
74 return CONTAINING_RECORD(iface, nsProtocolStream, nsIInputStream_iface);
77 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
80 nsProtocolStream *This = impl_from_nsIInputStream(iface);
84 if(IsEqualGUID(&IID_nsISupports, riid)) {
85 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
86 *result = &This->nsIInputStream_iface;
87 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
88 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
89 *result = &This->nsIInputStream_iface;
93 nsIInputStream_AddRef(&This->nsIInputStream_iface);
97 WARN("unsupported interface %s\n", debugstr_guid(riid));
98 return NS_NOINTERFACE;
101 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
103 nsProtocolStream *This = impl_from_nsIInputStream(iface);
104 LONG ref = InterlockedIncrement(&This->ref);
106 TRACE("(%p) ref=%d\n", This, ref);
112 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
114 nsProtocolStream *This = impl_from_nsIInputStream(iface);
115 LONG ref = InterlockedDecrement(&This->ref);
117 TRACE("(%p) ref=%d\n", This, ref);
125 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
127 nsProtocolStream *This = impl_from_nsIInputStream(iface);
128 FIXME("(%p)\n", This);
129 return NS_ERROR_NOT_IMPLEMENTED;
132 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
134 nsProtocolStream *This = impl_from_nsIInputStream(iface);
135 FIXME("(%p)->(%p)\n", This, _retval);
136 return NS_ERROR_NOT_IMPLEMENTED;
139 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
142 nsProtocolStream *This = impl_from_nsIInputStream(iface);
145 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
147 if(read > This->buf_size)
148 read = This->buf_size;
151 memcpy(aBuf, This->buf, read);
152 if(read < This->buf_size)
153 memmove(This->buf, This->buf+read, This->buf_size-read);
154 This->buf_size -= read;
161 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
162 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
163 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
165 nsProtocolStream *This = impl_from_nsIInputStream(iface);
166 PRUint32 written = 0;
169 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
174 if(aCount > This->buf_size)
175 aCount = This->buf_size;
177 nsres = aWriter(&This->nsIInputStream_iface, aClousure, This->buf, 0, aCount, &written);
179 TRACE("aWritter failed: %08x\n", nsres);
180 else if(written != This->buf_size)
181 FIXME("written %d != buf_size %d\n", written, This->buf_size);
183 This->buf_size -= written;
189 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, cpp_bool *_retval)
191 nsProtocolStream *This = impl_from_nsIInputStream(iface);
192 FIXME("(%p)->(%p)\n", This, _retval);
193 return NS_ERROR_NOT_IMPLEMENTED;
196 static const nsIInputStreamVtbl nsInputStreamVtbl = {
197 nsInputStream_QueryInterface,
198 nsInputStream_AddRef,
199 nsInputStream_Release,
201 nsInputStream_Available,
203 nsInputStream_ReadSegments,
204 nsInputStream_IsNonBlocking
207 static nsProtocolStream *create_nsprotocol_stream(void)
209 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
211 ret->nsIInputStream_iface.lpVtbl = &nsInputStreamVtbl;
218 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
220 return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
223 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
224 REFIID riid, void **ppv)
226 BSCallback *This = impl_from_IBindStatusCallback(iface);
229 if(IsEqualGUID(&IID_IUnknown, riid)) {
230 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
231 *ppv = &This->IBindStatusCallback_iface;
232 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
233 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
234 *ppv = &This->IBindStatusCallback_iface;
235 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
236 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
237 *ppv = &This->IServiceProvider_iface;
238 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
239 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
240 *ppv = &This->IHttpNegotiate2_iface;
241 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
242 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
243 *ppv = &This->IHttpNegotiate2_iface;
244 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
245 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
246 *ppv = &This->IInternetBindInfo_iface;
250 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
254 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
255 return E_NOINTERFACE;
258 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
260 BSCallback *This = impl_from_IBindStatusCallback(iface);
261 LONG ref = InterlockedIncrement(&This->ref);
263 TRACE("(%p) ref = %d\n", This, ref);
268 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
270 BSCallback *This = impl_from_IBindStatusCallback(iface);
271 LONG ref = InterlockedDecrement(&This->ref);
273 TRACE("(%p) ref = %d\n", This, ref);
277 GlobalFree(This->post_data);
279 IMoniker_Release(This->mon);
281 IBinding_Release(This->binding);
282 list_remove(&This->entry);
283 list_init(&This->entry);
284 heap_free(This->headers);
286 This->vtbl->destroy(This);
292 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
293 DWORD dwReserved, IBinding *pbind)
295 BSCallback *This = impl_from_IBindStatusCallback(iface);
297 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
299 IBinding_AddRef(pbind);
300 This->binding = pbind;
303 list_add_head(&This->doc->bindings, &This->entry);
305 return This->vtbl->start_binding(This);
308 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
310 BSCallback *This = impl_from_IBindStatusCallback(iface);
311 FIXME("(%p)->(%p)\n", This, pnPriority);
315 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
317 BSCallback *This = impl_from_IBindStatusCallback(iface);
318 FIXME("(%p)->(%d)\n", This, reserved);
322 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
323 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
325 BSCallback *This = impl_from_IBindStatusCallback(iface);
327 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
328 debugstr_w(szStatusText));
330 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
333 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
334 HRESULT hresult, LPCWSTR szError)
336 BSCallback *This = impl_from_IBindStatusCallback(iface);
339 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
341 /* NOTE: IE7 calls GetBindResult here */
343 hres = This->vtbl->stop_binding(This, hresult);
346 IBinding_Release(This->binding);
347 This->binding = NULL;
350 list_remove(&This->entry);
351 list_init(&This->entry);
357 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
358 DWORD *grfBINDF, BINDINFO *pbindinfo)
360 BSCallback *This = impl_from_IBindStatusCallback(iface);
363 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
365 if(!This->bindinfo_ready) {
368 hres = This->vtbl->init_bindinfo(This);
372 This->bindinfo_ready = TRUE;
375 *grfBINDF = This->bindf;
377 size = pbindinfo->cbSize;
378 memset(pbindinfo, 0, size);
379 pbindinfo->cbSize = size;
381 pbindinfo->cbstgmedData = This->post_data_len;
382 pbindinfo->dwCodePage = CP_UTF8;
383 pbindinfo->dwOptions = 0x80000;
385 if(This->post_data) {
386 pbindinfo->dwBindVerb = BINDVERB_POST;
388 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
389 pbindinfo->stgmedData.u.hGlobal = This->post_data;
390 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
391 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
397 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
398 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
400 BSCallback *This = impl_from_IBindStatusCallback(iface);
402 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
404 return This->vtbl->read_data(This, pstgmed->u.pstm);
407 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
408 REFIID riid, IUnknown *punk)
410 BSCallback *This = impl_from_IBindStatusCallback(iface);
411 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
415 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
416 BindStatusCallback_QueryInterface,
417 BindStatusCallback_AddRef,
418 BindStatusCallback_Release,
419 BindStatusCallback_OnStartBinding,
420 BindStatusCallback_GetPriority,
421 BindStatusCallback_OnLowResource,
422 BindStatusCallback_OnProgress,
423 BindStatusCallback_OnStopBinding,
424 BindStatusCallback_GetBindInfo,
425 BindStatusCallback_OnDataAvailable,
426 BindStatusCallback_OnObjectAvailable
429 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
431 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
434 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
435 REFIID riid, void **ppv)
437 BSCallback *This = impl_from_IHttpNegotiate2(iface);
438 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
441 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
443 BSCallback *This = impl_from_IHttpNegotiate2(iface);
444 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
447 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
449 BSCallback *This = impl_from_IHttpNegotiate2(iface);
450 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
453 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
454 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
456 BSCallback *This = impl_from_IHttpNegotiate2(iface);
459 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
460 dwReserved, pszAdditionalHeaders);
462 *pszAdditionalHeaders = NULL;
464 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
471 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
472 *pszAdditionalHeaders = CoTaskMemAlloc(size);
473 if(!*pszAdditionalHeaders)
474 return E_OUTOFMEMORY;
475 memcpy(*pszAdditionalHeaders, This->headers, size);
481 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
482 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
484 BSCallback *This = impl_from_IHttpNegotiate2(iface);
486 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
487 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
489 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
492 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
493 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
495 BSCallback *This = impl_from_IHttpNegotiate2(iface);
496 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
500 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
501 HttpNegotiate_QueryInterface,
502 HttpNegotiate_AddRef,
503 HttpNegotiate_Release,
504 HttpNegotiate_BeginningTransaction,
505 HttpNegotiate_OnResponse,
506 HttpNegotiate_GetRootSecurityId
509 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
511 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
514 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
515 REFIID riid, void **ppv)
517 BSCallback *This = impl_from_IInternetBindInfo(iface);
518 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
521 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
523 BSCallback *This = impl_from_IInternetBindInfo(iface);
524 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
527 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
529 BSCallback *This = impl_from_IInternetBindInfo(iface);
530 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
533 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
534 DWORD *grfBINDF, BINDINFO *pbindinfo)
536 BSCallback *This = impl_from_IInternetBindInfo(iface);
537 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
541 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
542 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
544 BSCallback *This = impl_from_IInternetBindInfo(iface);
545 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
549 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
550 InternetBindInfo_QueryInterface,
551 InternetBindInfo_AddRef,
552 InternetBindInfo_Release,
553 InternetBindInfo_GetBindInfo,
554 InternetBindInfo_GetBindString
557 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
559 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
562 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
563 REFIID riid, void **ppv)
565 BSCallback *This = impl_from_IServiceProvider(iface);
566 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
569 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
571 BSCallback *This = impl_from_IServiceProvider(iface);
572 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
575 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
577 BSCallback *This = impl_from_IServiceProvider(iface);
578 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
581 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
582 REFGUID guidService, REFIID riid, void **ppv)
584 BSCallback *This = impl_from_IServiceProvider(iface);
586 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
588 if(This->doc && IsEqualGUID(guidService, &IID_IWindowForBindingUI))
589 return IServiceProvider_QueryService(&This->doc->basedoc.IServiceProvider_iface, guidService, riid, ppv);
590 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->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
603 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
604 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
605 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
610 list_init(&This->entry);
613 IMoniker_AddRef(mon);
617 static void parse_content_type(nsChannelBSC *This, const WCHAR *value)
622 static const WCHAR charsetW[] = {'c','h','a','r','s','e','t','='};
624 ptr = strchrW(value, ';');
629 while(*ptr && isspaceW(*ptr))
632 len = strlenW(value);
633 if(ptr + sizeof(charsetW)/sizeof(WCHAR) < value+len && !memicmpW(ptr, charsetW, sizeof(charsetW)/sizeof(WCHAR))) {
634 size_t charset_len, lena;
635 nsACString charset_str;
636 const WCHAR *charset;
639 ptr += sizeof(charsetW)/sizeof(WCHAR);
642 FIXME("Quoted value\n");
646 while(*ptr && *ptr != ',')
648 charset_len = ptr-charset;
651 lena = WideCharToMultiByte(CP_ACP, 0, charset, charset_len, NULL, 0, NULL, NULL);
652 charseta = heap_alloc(lena+1);
656 WideCharToMultiByte(CP_ACP, 0, charset, charset_len, charseta, lena, NULL, NULL);
659 nsACString_InitDepend(&charset_str, charseta);
660 nsIHttpChannel_SetContentCharset(&This->nschannel->nsIHttpChannel_iface, &charset_str);
661 nsACString_Finish(&charset_str);
664 FIXME("unhandled: %s\n", debugstr_wn(ptr, len - (ptr-value)));
668 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
670 const WCHAR *header, *header_end, *colon, *value;
675 if(header[0] == '\r' && header[1] == '\n' && !header[2])
677 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
687 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
689 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
694 if(header[0] == '\r' && header[1] == '\n')
701 static HRESULT process_response_headers(nsChannelBSC *This, const WCHAR *headers)
706 static const WCHAR content_typeW[] = {'c','o','n','t','e','n','t','-','t','y','p','e',0};
708 hres = parse_headers(headers, &This->nschannel->response_headers);
712 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->response_headers, http_header_t, entry) {
713 if(!strcmpiW(iter->header, content_typeW))
714 parse_content_type(This, iter->data);
720 HRESULT start_binding(HTMLOuterWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
725 TRACE("(%p %p %p %p)\n", window, doc, bscallback, bctx);
727 bscallback->doc = doc;
729 bscallback->doc = window->doc;
731 /* NOTE: IE7 calls IsSystemMoniker here*/
733 if(window && bscallback->mon != window->mon)
734 set_current_mon(window, bscallback->mon);
737 RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
738 IBindCtx_AddRef(bctx);
740 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
742 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
743 bscallback->vtbl->stop_binding(bscallback, hres);
748 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
749 IBindCtx_Release(bctx);
751 WARN("BindToStorage failed: %08x\n", hres);
752 bscallback->vtbl->stop_binding(bscallback, hres);
757 IStream_Release(str);
759 IMoniker_Release(bscallback->mon);
760 bscallback->mon = NULL;
773 static inline BufferBSC *BufferBSC_from_BSCallback(BSCallback *iface)
775 return CONTAINING_RECORD(iface, BufferBSC, bsc);
778 static void BufferBSC_destroy(BSCallback *bsc)
780 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
782 heap_free(This->buf);
786 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
791 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
796 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
798 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
803 heap_free(This->buf);
811 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
813 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
819 This->buf = heap_alloc(This->size);
823 if(This->bsc.readed == This->size) {
825 This->buf = heap_realloc(This->buf, This->size);
829 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
830 This->bsc.readed += readed;
831 }while(hres == S_OK);
836 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
841 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
842 LPCWSTR response_headers)
847 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
852 static const BSCallbackVtbl BufferBSCVtbl = {
854 BufferBSC_init_bindinfo,
855 BufferBSC_start_binding,
856 BufferBSC_stop_binding,
858 BufferBSC_on_progress,
859 BufferBSC_on_response,
860 BufferBSC_beginning_transaction
864 static BufferBSC *create_bufferbsc(IMoniker *mon)
866 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
868 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
874 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
876 BufferBSC *bsc = create_bufferbsc(mon);
881 hres = start_binding(NULL, doc, &bsc->bsc, NULL);
882 if(SUCCEEDED(hres)) {
884 if(SUCCEEDED(hres)) {
887 *size = bsc->bsc.readed;
892 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
897 static HRESULT read_post_data_stream(nsChannelBSC *This, nsChannel *nschannel)
899 PRUint32 data_len = 0, available = 0;
900 char *data, *post_data;
904 if(!nschannel->post_data_stream)
907 nsres = nsIInputStream_Available(nschannel->post_data_stream, &available);
911 post_data = data = GlobalAlloc(0, available);
913 return E_OUTOFMEMORY;
915 nsres = nsIInputStream_Read(nschannel->post_data_stream, data, available, &data_len);
916 if(NS_FAILED(nsres)) {
921 if(nschannel->post_data_contains_headers) {
922 if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
930 post_data += data_len;
931 for(ptr = data; ptr+4 < data+data_len; ptr++) {
932 if(!memcmp(ptr, "\r\n\r\n", 4)) {
938 data_len -= post_data-data;
940 size = MultiByteToWideChar(CP_ACP, 0, data, post_data-data, NULL, 0);
941 headers = heap_alloc((size+1)*sizeof(WCHAR));
943 MultiByteToWideChar(CP_ACP, 0, data, post_data-data, headers, size);
945 hres = parse_headers(headers , &nschannel->request_headers);
947 This->bsc.headers = headers;
951 hres = E_OUTOFMEMORY;
964 }else if(post_data != data) {
967 new_data = GlobalAlloc(0, data_len);
969 memcpy(new_data, post_data, data_len);
972 return E_OUTOFMEMORY;
973 post_data = new_data;
976 This->bsc.post_data = post_data;
977 This->bsc.post_data_len = data_len;
978 TRACE("post_data = %s\n", debugstr_a(This->bsc.post_data));
982 static HRESULT on_start_nsrequest(nsChannelBSC *This)
986 /* FIXME: it's needed for http connections from BindToObject. */
987 if(!This->nschannel->response_status)
988 This->nschannel->response_status = 200;
990 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
991 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
992 if(NS_FAILED(nsres)) {
993 FIXME("OnStartRequest failed: %08x\n", nsres);
998 list_remove(&This->bsc.entry);
999 list_init(&This->bsc.entry);
1000 update_window_doc(This->window);
1001 if(This->window->doc != This->bsc.doc) {
1003 list_remove(&This->bsc.entry);
1004 This->bsc.doc = This->window->doc;
1006 list_add_head(&This->bsc.doc->bindings, &This->bsc.entry);
1007 if(This->window->readystate != READYSTATE_LOADING)
1008 set_ready_state(This->window, READYSTATE_LOADING);
1014 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
1016 nsresult nsres, request_result;
1020 request_result = NS_OK;
1023 request_result = NS_BINDING_ABORTED;
1026 request_result = NS_ERROR_FAILURE;
1029 if(This->nslistener) {
1030 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
1031 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1033 if(NS_FAILED(nsres))
1034 WARN("OnStopRequest failed: %08x\n", nsres);
1037 if(This->nschannel->load_group) {
1038 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1039 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
1040 if(NS_FAILED(nsres))
1041 ERR("RemoveRequest failed: %08x\n", nsres);
1045 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
1047 static const WCHAR mimeTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
1053 if(!This->nslistener) {
1058 hres = IStream_Read(stream, buf, sizeof(buf), &read);
1059 }while(hres == S_OK && read);
1065 This->nsstream = create_nsprotocol_stream();
1069 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
1070 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1074 This->nsstream->buf_size += read;
1076 if(!This->bsc.readed) {
1077 if(This->nsstream->buf_size >= 2
1078 && (BYTE)This->nsstream->buf[0] == 0xff
1079 && (BYTE)This->nsstream->buf[1] == 0xfe)
1080 This->nschannel->charset = heap_strdupA(UTF16_STR);
1081 if(This->nsstream->buf_size >= 3
1082 && (BYTE)This->nsstream->buf[0] == 0xef
1083 && (BYTE)This->nsstream->buf[1] == 0xbb
1084 && (BYTE)This->nsstream->buf[2] == 0xbf)
1085 This->nschannel->charset = heap_strdupA(UTF8_STR);
1087 if(!This->nschannel->content_type) {
1090 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size,
1091 This->window ? mimeTextHtml : NULL, 0, &mime, 0);
1095 TRACE("Found MIME %s\n", debugstr_w(mime));
1097 This->nschannel->content_type = heap_strdupWtoA(mime);
1098 CoTaskMemFree(mime);
1099 if(!This->nschannel->content_type)
1100 return E_OUTOFMEMORY;
1103 on_start_nsrequest(This);
1106 This->bsc.readed += This->nsstream->buf_size;
1108 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1109 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1110 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1111 This->nsstream->buf_size);
1112 if(NS_FAILED(nsres))
1113 ERR("OnDataAvailable failed: %08x\n", nsres);
1115 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1116 ERR("buffer is full\n");
1119 }while(hres == S_OK);
1125 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1129 nsChannel *nschannel;
1131 } nsRedirectCallback;
1133 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1135 return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1138 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1139 nsIIDRef riid, void **result)
1141 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1143 if(IsEqualGUID(&IID_nsISupports, riid)) {
1144 TRACE("(%p)->(IID_nsISuports %p)\n", This, result);
1145 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1146 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1147 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1148 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1151 WARN("unimplemented iface %s\n", debugstr_guid(riid));
1152 return NS_NOINTERFACE;
1155 nsISupports_AddRef((nsISupports*)*result);
1159 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1161 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1162 LONG ref = InterlockedIncrement(&This->ref);
1164 TRACE("(%p) ref=%d\n", This, ref);
1169 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1171 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1172 LONG ref = InterlockedDecrement(&This->ref);
1174 TRACE("(%p) ref=%d\n", This, ref);
1177 IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1178 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1185 static nsresult NSAPI nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1187 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1188 nsChannel *old_nschannel;
1191 TRACE("(%p)->(%08x)\n", This, result);
1193 old_nschannel = This->bsc->nschannel;
1194 nsIChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1195 This->bsc->nschannel = This->nschannel;
1197 if(This->nschannel->load_group) {
1198 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1200 if(NS_FAILED(nsres))
1201 ERR("AddRequest failed: %08x\n", nsres);
1204 if(This->bsc->window) {
1205 IUri *uri = nsuri_get_uri(This->nschannel->uri);
1208 set_current_uri(This->bsc->window, uri);
1211 WARN("Could not get IUri from nsWineURI\n");
1216 nsres = nsILoadGroup_RemoveRequest(old_nschannel->load_group,
1217 (nsIRequest*)&old_nschannel->nsIHttpChannel_iface, NULL, NS_OK);
1218 if(NS_FAILED(nsres))
1219 ERR("RemoveRequest failed: %08x\n", nsres);
1220 nsIHttpChannel_Release(&old_nschannel->nsIHttpChannel_iface);
1226 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1227 nsAsyncVerifyRedirectCallback_QueryInterface,
1228 nsAsyncVerifyRedirectCallback_AddRef,
1229 nsAsyncVerifyRedirectCallback_Release,
1230 nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1233 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1235 nsRedirectCallback *callback;
1237 callback = heap_alloc(sizeof(*callback));
1239 return E_OUTOFMEMORY;
1241 callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1244 nsIChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1245 callback->nschannel = nschannel;
1247 IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1248 callback->bsc = bsc;
1254 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1256 return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1259 static void nsChannelBSC_destroy(BSCallback *bsc)
1261 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1264 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1265 if(This->nslistener)
1266 nsIStreamListener_Release(This->nslistener);
1268 nsISupports_Release(This->nscontext);
1270 nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1274 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1276 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1279 This->window->doc->skip_mutation_notif = FALSE;
1284 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1286 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1289 if(This->nschannel && This->nschannel->post_data_stream) {
1290 hres = read_post_data_stream(This, This->nschannel);
1301 } stop_request_task_t;
1303 static void stop_request_proc(task_t *_task)
1305 stop_request_task_t *task = (stop_request_task_t*)_task;
1307 TRACE("(%p)\n", task->bsc);
1309 list_remove(&task->bsc->bsc.entry);
1310 list_init(&task->bsc->bsc.entry);
1311 on_stop_nsrequest(task->bsc, S_OK);
1314 static void stop_request_task_destr(task_t *_task)
1316 stop_request_task_t *task = (stop_request_task_t*)_task;
1318 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1322 static HRESULT async_stop_request(nsChannelBSC *This)
1324 stop_request_task_t *task;
1326 if(!This->bsc.readed) {
1327 TRACE("No data read, calling OnStartRequest\n");
1328 on_start_nsrequest(This);
1331 task = heap_alloc(sizeof(*task));
1333 return E_OUTOFMEMORY;
1335 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1337 push_task(&task->header, stop_request_proc, stop_request_task_destr, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1341 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1343 HTMLDocumentObj *doc;
1344 IOleCommandTarget *olecmd;
1347 SAFEARRAYBOUND bound;
1348 VARIANT var, varOut;
1356 doc = This->window->doc_obj;
1357 if(!doc || !doc->doc_object_service || !doc->client)
1360 hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1361 This->window->url, &is_error_url);
1362 if(FAILED(hres) || is_error_url)
1365 hres = IOleClientSite_QueryInterface(doc->client,
1366 &IID_IOleCommandTarget, (void**)&olecmd);
1371 bound.cElements = 8;
1372 sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1374 IOleCommandTarget_Release(olecmd);
1380 V_I4(&var) = result;
1381 SafeArrayPutElement(sa, &ind, &var);
1384 V_VT(&var) = VT_BSTR;
1385 V_BSTR(&var) = This->window->url;
1386 SafeArrayPutElement(sa, &ind, &var);
1389 V_VT(&var) = VT_UNKNOWN;
1390 V_UNKNOWN(&var) = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
1391 SafeArrayPutElement(sa, &ind, &var);
1393 /* FIXME: what are the following fields for? */
1395 V_VT(&var) = VT_UNKNOWN;
1396 V_UNKNOWN(&var) = NULL;
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_BOOL;
1406 V_BOOL(&var) = FALSE;
1407 SafeArrayPutElement(sa, &ind, &var);
1410 V_VT(&var) = VT_BSTR;
1411 unk = SysAllocString(NULL);
1413 SafeArrayPutElement(sa, &ind, &var);
1416 V_VT(&var) = VT_UNKNOWN;
1417 V_UNKNOWN(&var) = NULL;
1418 SafeArrayPutElement(sa, &ind, &var);
1420 V_VT(&var) = VT_ARRAY;
1422 V_VT(&varOut) = VT_BOOL;
1423 V_BOOL(&varOut) = VARIANT_TRUE;
1424 IOleCommandTarget_Exec(olecmd, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1427 SafeArrayDestroy(sa);
1428 IOleCommandTarget_Release(olecmd);
1431 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1433 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1435 if(result != E_ABORT) {
1437 handle_navigation_error(This, result);
1438 else if(This->window) {
1439 result = async_stop_request(This);
1440 if(SUCCEEDED(result))
1445 on_stop_nsrequest(This, result);
1449 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1451 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1453 return read_stream_data(This, stream);
1456 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1458 nsRedirectCallback *callback;
1459 nsIChannelEventSink *sink;
1460 nsChannel *new_channel;
1464 TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1466 if(!This->nschannel || !This->nschannel->notif_callback)
1469 nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1470 if(NS_FAILED(nsres))
1473 hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1474 if(SUCCEEDED(hres)) {
1475 TRACE("%p %p->%p\n", This, This->nschannel, new_channel);
1477 hres = create_redirect_callback(new_channel, This, &callback);
1478 nsIChannel_Release(&new_channel->nsIHttpChannel_iface);
1481 if(SUCCEEDED(hres)) {
1482 nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1483 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1484 &callback->nsIAsyncVerifyRedirectCallback_iface);
1486 if(NS_FAILED(nsres))
1487 FIXME("AsyncOnChannelRedirect failed: %08x\n", hres);
1488 else if(This->nschannel != callback->nschannel)
1489 FIXME("nschannel not updated\n");
1491 nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1494 nsIChannelEventSink_Release(sink);
1498 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1500 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1502 switch(status_code) {
1503 case BINDSTATUS_MIMETYPEAVAILABLE:
1504 if(!This->nschannel)
1507 heap_free(This->nschannel->content_type);
1508 This->nschannel->content_type = heap_strdupWtoA(status_text);
1510 case BINDSTATUS_REDIRECTING:
1511 return handle_redirect(This, status_text);
1512 case BINDSTATUS_BEGINDOWNLOADDATA: {
1513 IWinInetHttpInfo *http_info;
1514 DWORD status, size = sizeof(DWORD);
1517 if(!This->bsc.binding)
1520 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1524 hres = IWinInetHttpInfo_QueryInfo(http_info,
1525 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1526 IWinInetHttpInfo_Release(http_info);
1527 if(FAILED(hres) || status == HTTP_STATUS_OK)
1530 handle_navigation_error(This, status);
1537 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1538 LPCWSTR response_headers)
1540 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1543 This->nschannel->response_status = response_code;
1545 if(response_headers) {
1546 const WCHAR *headers;
1548 headers = strchrW(response_headers, '\r');
1549 if(headers && headers[1] == '\n') {
1551 hres = process_response_headers(This, headers);
1553 WARN("parsing headers failed: %08x\n", hres);
1562 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1564 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1565 http_header_t *iter;
1569 static const WCHAR content_lengthW[] =
1570 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1572 if(!This->nschannel)
1575 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1576 if(strcmpW(iter->header, content_lengthW))
1577 len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1583 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1585 return E_OUTOFMEMORY;
1587 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1588 if(!strcmpW(iter->header, content_lengthW))
1591 len = strlenW(iter->header);
1592 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1598 len = strlenW(iter->data);
1599 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1611 static const BSCallbackVtbl nsChannelBSCVtbl = {
1612 nsChannelBSC_destroy,
1613 nsChannelBSC_init_bindinfo,
1614 nsChannelBSC_start_binding,
1615 nsChannelBSC_stop_binding,
1616 nsChannelBSC_read_data,
1617 nsChannelBSC_on_progress,
1618 nsChannelBSC_on_response,
1619 nsChannelBSC_beginning_transaction
1622 HRESULT create_channelbsc(IMoniker *mon, const WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1626 ret = heap_alloc_zero(sizeof(*ret));
1628 return E_OUTOFMEMORY;
1630 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1633 ret->bsc.headers = heap_strdupW(headers);
1634 if(!ret->bsc.headers) {
1635 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1636 return E_OUTOFMEMORY;
1641 ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1642 if(!ret->bsc.post_data) {
1643 heap_free(ret->bsc.headers);
1644 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1645 return E_OUTOFMEMORY;
1648 memcpy(ret->bsc.post_data, post_data, post_data_size);
1649 ret->bsc.post_data_len = post_data_size;
1656 void set_window_bscallback(HTMLOuterWindow *window, nsChannelBSC *callback)
1658 if(window->bscallback) {
1659 if(window->bscallback->bsc.binding)
1660 IBinding_Abort(window->bscallback->bsc.binding);
1661 window->bscallback->bsc.doc = NULL;
1662 window->bscallback->window = NULL;
1663 IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1666 window->bscallback = callback;
1669 callback->window = window;
1670 IBindStatusCallback_AddRef(&callback->bsc.IBindStatusCallback_iface);
1671 callback->bsc.doc = window->doc;
1677 HTMLOuterWindow *window;
1678 nsChannelBSC *bscallback;
1679 } start_doc_binding_task_t;
1681 static void start_doc_binding_proc(task_t *_task)
1683 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1685 start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1688 static void start_doc_binding_task_destr(task_t *_task)
1690 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1692 IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1696 HRESULT async_start_doc_binding(HTMLOuterWindow *window, nsChannelBSC *bscallback)
1698 start_doc_binding_task_t *task;
1700 TRACE("%p\n", bscallback);
1702 task = heap_alloc(sizeof(start_doc_binding_task_t));
1704 return E_OUTOFMEMORY;
1706 task->window = window;
1707 task->bscallback = bscallback;
1708 IBindStatusCallback_AddRef(&bscallback->bsc.IBindStatusCallback_iface);
1710 push_task(&task->header, start_doc_binding_proc, start_doc_binding_task_destr, window->task_magic);
1714 void abort_document_bindings(HTMLDocumentNode *doc)
1716 BSCallback *iter, *next;
1718 LIST_FOR_EACH_ENTRY_SAFE(iter, next, &doc->bindings, BSCallback, entry) {
1719 TRACE("Aborting %p\n", iter);
1722 remove_target_tasks(iter->doc->basedoc.task_magic);
1725 IBinding_Abort(iter->binding);
1727 list_remove(&iter->entry);
1728 list_init(&iter->entry);
1729 iter->vtbl->stop_binding(iter, E_ABORT);
1736 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1738 HRESULT hres = S_OK;
1740 if(!bscallback->nschannel) {
1741 ERR("NULL nschannel\n");
1745 bscallback->nschannel->content_type = heap_strdupA("text/html");
1746 if(!bscallback->nschannel->content_type)
1747 return E_OUTOFMEMORY;
1749 list_add_head(&bscallback->bsc.doc->bindings, &bscallback->bsc.entry);
1751 hres = read_stream_data(bscallback, stream);
1753 hres = async_stop_request(bscallback);
1755 IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1761 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1763 nsIChannel_AddRef(&channel->nsIHttpChannel_iface);
1764 This->nschannel = channel;
1766 nsIStreamListener_AddRef(listener);
1767 This->nslistener = listener;
1770 nsISupports_AddRef(context);
1771 This->nscontext = context;
1774 if(This->bsc.headers) {
1777 hres = parse_headers(This->bsc.headers, &channel->request_headers);
1778 heap_free(This->bsc.headers);
1779 This->bsc.headers = NULL;
1781 WARN("parse_headers failed: %08x\n", hres);
1787 HTMLOuterWindow *window;
1789 } navigate_javascript_task_t;
1791 static void navigate_javascript_proc(task_t *_task)
1793 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1794 HTMLOuterWindow *window = task->window;
1799 static const WCHAR jscriptW[] = {'j','s','c','r','i','p','t',0};
1801 task->window->readystate = READYSTATE_COMPLETE;
1803 hres = IUri_GetPath(task->uri, &code);
1807 set_download_state(window->doc_obj, 1);
1809 V_VT(&v) = VT_EMPTY;
1810 hres = exec_script(window, code, jscriptW, &v);
1811 SysFreeString(code);
1812 if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
1813 FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
1817 if(window->doc_obj->view_sink)
1818 IAdviseSink_OnViewChange(window->doc_obj->view_sink, DVASPECT_CONTENT, -1);
1820 set_download_state(window->doc_obj, 0);
1823 static void navigate_javascript_task_destr(task_t *_task)
1825 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1827 IUri_Release(task->uri);
1833 HTMLOuterWindow *window;
1834 nsChannelBSC *bscallback;
1838 static void navigate_proc(task_t *_task)
1840 navigate_task_t *task = (navigate_task_t*)_task;
1843 hres = set_moniker(&task->window->doc_obj->basedoc, task->mon, NULL, task->bscallback, TRUE);
1845 start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1848 static void navigate_task_destr(task_t *_task)
1850 navigate_task_t *task = (navigate_task_t*)_task;
1852 IUnknown_Release((IUnknown*)task->bscallback);
1853 IMoniker_Release(task->mon);
1857 static HRESULT navigate_fragment(HTMLOuterWindow *window, IUri *uri)
1859 nsIDOMLocation *nslocation;
1860 nsAString nsfrag_str;
1865 set_current_uri(window, uri);
1867 nsres = nsIDOMWindow_GetLocation(window->nswindow, &nslocation);
1868 if(FAILED(nsres) || !nslocation)
1871 hres = IUri_GetFragment(uri, &frag);
1873 nsIDOMLocation_Release(nslocation);
1877 nsAString_InitDepend(&nsfrag_str, frag);
1878 nsres = nsIDOMLocation_SetHash(nslocation, &nsfrag_str);
1879 nsAString_Finish(&nsfrag_str);
1880 nsIDOMLocation_Release(nslocation);
1881 SysFreeString(frag);
1882 if(NS_FAILED(nsres)) {
1883 ERR("SetHash failed: %08x\n", nsres);
1887 if(window->doc_obj->doc_object_service) {
1888 IDocObjectService_FireNavigateComplete2(window->doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0x10);
1889 IDocObjectService_FireDocumentComplete(window->doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0);
1896 HRESULT super_navigate(HTMLOuterWindow *window, IUri *uri, const WCHAR *headers, BYTE *post_data, DWORD post_data_size)
1903 if(window->doc_obj->client) {
1904 IOleCommandTarget *cmdtrg;
1906 hres = IOleClientSite_QueryInterface(window->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1907 if(SUCCEEDED(hres)) {
1911 hres = IUri_GetDisplayUri(uri, &url_str);
1912 if(SUCCEEDED(hres)) {
1913 V_VT(&in) = VT_BSTR;
1914 V_BSTR(&in) = url_str;
1915 V_VT(&out) = VT_BOOL;
1916 V_BOOL(&out) = VARIANT_TRUE;
1917 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
1918 IOleCommandTarget_Release(cmdtrg);
1921 SysFreeString(url_str);
1926 if(window->uri && !post_data_size && compare_ignoring_frag(window->uri, uri)) {
1927 TRACE("fragment navigate\n");
1928 return navigate_fragment(window, uri);
1931 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1935 /* FIXME: Why not set_ready_state? */
1936 window->readystate = READYSTATE_UNINITIALIZED;
1938 hres = create_channelbsc(mon, headers, post_data, post_data_size, &bsc);
1940 IMoniker_Release(mon);
1944 prepare_for_binding(&window->doc_obj->basedoc, mon, TRUE);
1946 hres = IUri_GetScheme(uri, &scheme);
1947 if(SUCCEEDED(hres) && scheme != URL_SCHEME_JAVASCRIPT) {
1948 navigate_task_t *task;
1950 task = heap_alloc(sizeof(*task));
1952 IUnknown_Release((IUnknown*)bsc);
1953 IMoniker_Release(mon);
1954 return E_OUTOFMEMORY;
1957 /* Silently and repeated when real loading starts? */
1958 window->readystate = READYSTATE_LOADING;
1959 call_docview_84(window->doc_obj);
1961 task->window = window;
1962 task->bscallback = bsc;
1964 push_task(&task->header, navigate_proc, navigate_task_destr, window->task_magic);
1967 navigate_javascript_task_t *task;
1969 IUnknown_Release((IUnknown*)bsc);
1970 IMoniker_Release(mon);
1972 task = heap_alloc(sizeof(*task));
1974 return E_OUTOFMEMORY;
1977 window->readystate = READYSTATE_COMPLETE;
1978 call_docview_84(window->doc_obj);
1981 task->window = window;
1983 push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, window->task_magic);
1989 HRESULT navigate_new_window(HTMLOuterWindow *window, IUri *uri, const WCHAR *name, IHTMLWindow2 **ret)
1991 IWebBrowser2 *web_browser;
1992 IHTMLWindow2 *new_window;
1997 hres = create_channelbsc(NULL, NULL, NULL, 0, &bsc);
2001 hres = CreateAsyncBindCtx(0, &bsc->bsc.IBindStatusCallback_iface, NULL, &bind_ctx);
2003 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2007 hres = CoCreateInstance(&CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
2008 &IID_IWebBrowser2, (void**)&web_browser);
2009 if(SUCCEEDED(hres)) {
2010 ITargetFramePriv2 *target_frame_priv;
2012 hres = IWebBrowser_QueryInterface(web_browser, &IID_ITargetFramePriv2, (void**)&target_frame_priv);
2013 if(SUCCEEDED(hres)) {
2014 hres = ITargetFramePriv2_AggregatedNavigation2(target_frame_priv,
2015 HLNF_DISABLEWINDOWRESTRICTIONS|HLNF_OPENINNEWWINDOW, bind_ctx, &bsc->bsc.IBindStatusCallback_iface,
2017 ITargetFramePriv2_Release(target_frame_priv);
2020 hres = do_query_service((IUnknown*)web_browser, &SID_SHTMLWindow, &IID_IHTMLWindow2, (void**)&new_window);
2023 IWebBrowser2_Quit(web_browser);
2024 IWebBrowser2_Release(web_browser);
2027 WARN("Could not create InternetExplorer instance: %08x\n", hres);
2030 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2031 IBindCtx_Release(bind_ctx);
2035 IWebBrowser2_put_Visible(web_browser, VARIANT_TRUE);
2036 IWebBrowser2_Release(web_browser);
2041 IHTMLWindow2_Release(new_window);
2045 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
2047 IHlinkFrame *hlink_frame;
2048 nsChannelBSC *callback;
2056 hres = do_query_service((IUnknown*)doc->doc_obj->client, &IID_IHlinkFrame, &IID_IHlinkFrame,
2057 (void**)&hlink_frame);
2061 hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
2063 IHlinkFrame_Release(hlink_frame);
2068 read_post_data_stream(callback, nschannel);
2070 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
2072 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
2073 &IID_IHlink, (LPVOID*)&hlink);
2076 hres = CreateURLMoniker(NULL, url, &mon);
2078 if(SUCCEEDED(hres)) {
2079 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
2081 if(hlnf & HLNF_OPENINNEWWINDOW) {
2082 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
2083 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
2086 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
2087 &callback->bsc.IBindStatusCallback_iface, hlink);
2088 IMoniker_Release(mon);
2089 *cancel = hres == S_OK;
2093 IHlinkFrame_Release(hlink_frame);
2094 IBindCtx_Release(bindctx);
2095 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
2099 HRESULT navigate_url(HTMLOuterWindow *window, const WCHAR *new_url, const WCHAR *base_url)
2101 WCHAR url[INTERNET_MAX_URL_LENGTH];
2107 }else if(base_url) {
2110 hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2111 url, sizeof(url)/sizeof(WCHAR), &len, 0);
2115 strcpyW(url, new_url);
2118 if(window->doc_obj && window->doc_obj->hostui) {
2119 OLECHAR *translated_url = NULL;
2121 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
2124 TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
2125 strcpyW(url, translated_url);
2126 CoTaskMemFree(translated_url);
2130 if(window->doc_obj && window->doc_obj->is_webbrowser && window == window->doc_obj->basedoc.window) {
2131 BOOL cancel = FALSE;
2134 hres = IDocObjectService_FireBeforeNavigate2(window->doc_obj->doc_object_service, NULL, url, 0x40,
2135 NULL, NULL, 0, NULL, TRUE, &cancel);
2136 if(SUCCEEDED(hres) && cancel) {
2137 TRACE("Navigation canceled\n");
2141 hres = CreateUri(url, 0, 0, &uri);
2145 hres = super_navigate(window, uri, NULL, NULL, 0);
2150 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
2153 hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
2158 TRACE("Navigation handled by hlink frame\n");
2163 hres = create_doc_uri(window, url, &uri);
2167 hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
2168 nsISupports_Release((nsISupports*)uri);