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 UTF16_STR "utf-16"
49 static const WCHAR emptyW[] = {0};
51 struct nsProtocolStream {
52 nsIInputStream nsIInputStream_iface;
60 struct BSCallbackVtbl {
61 void (*destroy)(BSCallback*);
62 HRESULT (*init_bindinfo)(BSCallback*);
63 HRESULT (*start_binding)(BSCallback*);
64 HRESULT (*stop_binding)(BSCallback*,HRESULT);
65 HRESULT (*read_data)(BSCallback*,IStream*);
66 HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
67 HRESULT (*on_response)(BSCallback*,DWORD,LPCWSTR);
68 HRESULT (*beginning_transaction)(BSCallback*,WCHAR**);
71 static inline nsProtocolStream *impl_from_nsIInputStream(nsIInputStream *iface)
73 return CONTAINING_RECORD(iface, nsProtocolStream, nsIInputStream_iface);
76 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
79 nsProtocolStream *This = impl_from_nsIInputStream(iface);
83 if(IsEqualGUID(&IID_nsISupports, riid)) {
84 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
85 *result = &This->nsIInputStream_iface;
86 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
87 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
88 *result = &This->nsIInputStream_iface;
92 nsIInputStream_AddRef(&This->nsIInputStream_iface);
96 WARN("unsupported interface %s\n", debugstr_guid(riid));
97 return NS_NOINTERFACE;
100 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
102 nsProtocolStream *This = impl_from_nsIInputStream(iface);
103 LONG ref = InterlockedIncrement(&This->ref);
105 TRACE("(%p) ref=%d\n", This, ref);
111 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
113 nsProtocolStream *This = impl_from_nsIInputStream(iface);
114 LONG ref = InterlockedDecrement(&This->ref);
116 TRACE("(%p) ref=%d\n", This, ref);
124 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
126 nsProtocolStream *This = impl_from_nsIInputStream(iface);
127 FIXME("(%p)\n", This);
128 return NS_ERROR_NOT_IMPLEMENTED;
131 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
133 nsProtocolStream *This = impl_from_nsIInputStream(iface);
134 FIXME("(%p)->(%p)\n", This, _retval);
135 return NS_ERROR_NOT_IMPLEMENTED;
138 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
141 nsProtocolStream *This = impl_from_nsIInputStream(iface);
144 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
146 if(read > This->buf_size)
147 read = This->buf_size;
150 memcpy(aBuf, This->buf, read);
151 if(read < This->buf_size)
152 memmove(This->buf, This->buf+read, This->buf_size-read);
153 This->buf_size -= read;
160 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
161 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
162 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
164 nsProtocolStream *This = impl_from_nsIInputStream(iface);
165 PRUint32 written = 0;
168 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
173 if(aCount > This->buf_size)
174 aCount = This->buf_size;
176 nsres = aWriter(&This->nsIInputStream_iface, aClousure, This->buf, 0, aCount, &written);
178 TRACE("aWritter failed: %08x\n", nsres);
179 else if(written != This->buf_size)
180 FIXME("written %d != buf_size %d\n", written, This->buf_size);
182 This->buf_size -= written;
188 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
190 nsProtocolStream *This = impl_from_nsIInputStream(iface);
191 FIXME("(%p)->(%p)\n", This, _retval);
192 return NS_ERROR_NOT_IMPLEMENTED;
195 static const nsIInputStreamVtbl nsInputStreamVtbl = {
196 nsInputStream_QueryInterface,
197 nsInputStream_AddRef,
198 nsInputStream_Release,
200 nsInputStream_Available,
202 nsInputStream_ReadSegments,
203 nsInputStream_IsNonBlocking
206 static nsProtocolStream *create_nsprotocol_stream(void)
208 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
210 ret->nsIInputStream_iface.lpVtbl = &nsInputStreamVtbl;
217 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
219 return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
222 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
223 REFIID riid, void **ppv)
225 BSCallback *This = impl_from_IBindStatusCallback(iface);
228 if(IsEqualGUID(&IID_IUnknown, riid)) {
229 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
230 *ppv = &This->IBindStatusCallback_iface;
231 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
232 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
233 *ppv = &This->IBindStatusCallback_iface;
234 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
235 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
236 *ppv = &This->IServiceProvider_iface;
237 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
238 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
239 *ppv = &This->IHttpNegotiate2_iface;
240 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
241 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
242 *ppv = &This->IHttpNegotiate2_iface;
243 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
244 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
245 *ppv = &This->IInternetBindInfo_iface;
249 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
253 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
254 return E_NOINTERFACE;
257 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
259 BSCallback *This = impl_from_IBindStatusCallback(iface);
260 LONG ref = InterlockedIncrement(&This->ref);
262 TRACE("(%p) ref = %d\n", This, ref);
267 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
269 BSCallback *This = impl_from_IBindStatusCallback(iface);
270 LONG ref = InterlockedDecrement(&This->ref);
272 TRACE("(%p) ref = %d\n", This, ref);
276 GlobalFree(This->post_data);
278 IMoniker_Release(This->mon);
280 IBinding_Release(This->binding);
281 list_remove(&This->entry);
282 list_init(&This->entry);
283 heap_free(This->headers);
285 This->vtbl->destroy(This);
291 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
292 DWORD dwReserved, IBinding *pbind)
294 BSCallback *This = impl_from_IBindStatusCallback(iface);
296 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
298 IBinding_AddRef(pbind);
299 This->binding = pbind;
302 list_add_head(&This->doc->bindings, &This->entry);
304 return This->vtbl->start_binding(This);
307 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
309 BSCallback *This = impl_from_IBindStatusCallback(iface);
310 FIXME("(%p)->(%p)\n", This, pnPriority);
314 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
316 BSCallback *This = impl_from_IBindStatusCallback(iface);
317 FIXME("(%p)->(%d)\n", This, reserved);
321 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
322 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
324 BSCallback *This = impl_from_IBindStatusCallback(iface);
326 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
327 debugstr_w(szStatusText));
329 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
332 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
333 HRESULT hresult, LPCWSTR szError)
335 BSCallback *This = impl_from_IBindStatusCallback(iface);
338 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
340 /* NOTE: IE7 calls GetBindResult here */
342 hres = This->vtbl->stop_binding(This, hresult);
345 IBinding_Release(This->binding);
346 This->binding = NULL;
349 list_remove(&This->entry);
350 list_init(&This->entry);
356 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
357 DWORD *grfBINDF, BINDINFO *pbindinfo)
359 BSCallback *This = impl_from_IBindStatusCallback(iface);
362 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
364 if(!This->bindinfo_ready) {
367 hres = This->vtbl->init_bindinfo(This);
371 This->bindinfo_ready = TRUE;
374 *grfBINDF = This->bindf;
376 size = pbindinfo->cbSize;
377 memset(pbindinfo, 0, size);
378 pbindinfo->cbSize = size;
380 pbindinfo->cbstgmedData = This->post_data_len;
381 pbindinfo->dwCodePage = CP_UTF8;
382 pbindinfo->dwOptions = 0x80000;
384 if(This->post_data) {
385 pbindinfo->dwBindVerb = BINDVERB_POST;
387 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
388 pbindinfo->stgmedData.u.hGlobal = This->post_data;
389 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
390 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
396 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
397 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
399 BSCallback *This = impl_from_IBindStatusCallback(iface);
401 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
403 return This->vtbl->read_data(This, pstgmed->u.pstm);
406 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
407 REFIID riid, IUnknown *punk)
409 BSCallback *This = impl_from_IBindStatusCallback(iface);
410 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
414 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
415 BindStatusCallback_QueryInterface,
416 BindStatusCallback_AddRef,
417 BindStatusCallback_Release,
418 BindStatusCallback_OnStartBinding,
419 BindStatusCallback_GetPriority,
420 BindStatusCallback_OnLowResource,
421 BindStatusCallback_OnProgress,
422 BindStatusCallback_OnStopBinding,
423 BindStatusCallback_GetBindInfo,
424 BindStatusCallback_OnDataAvailable,
425 BindStatusCallback_OnObjectAvailable
428 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
430 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
433 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
434 REFIID riid, void **ppv)
436 BSCallback *This = impl_from_IHttpNegotiate2(iface);
437 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
440 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
442 BSCallback *This = impl_from_IHttpNegotiate2(iface);
443 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
446 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
448 BSCallback *This = impl_from_IHttpNegotiate2(iface);
449 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
452 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
453 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
455 BSCallback *This = impl_from_IHttpNegotiate2(iface);
458 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
459 dwReserved, pszAdditionalHeaders);
461 *pszAdditionalHeaders = NULL;
463 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
470 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
471 *pszAdditionalHeaders = CoTaskMemAlloc(size);
472 if(!*pszAdditionalHeaders)
473 return E_OUTOFMEMORY;
474 memcpy(*pszAdditionalHeaders, This->headers, size);
480 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
481 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
483 BSCallback *This = impl_from_IHttpNegotiate2(iface);
485 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
486 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
488 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
491 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
492 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
494 BSCallback *This = impl_from_IHttpNegotiate2(iface);
495 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
499 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
500 HttpNegotiate_QueryInterface,
501 HttpNegotiate_AddRef,
502 HttpNegotiate_Release,
503 HttpNegotiate_BeginningTransaction,
504 HttpNegotiate_OnResponse,
505 HttpNegotiate_GetRootSecurityId
508 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
510 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
513 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
514 REFIID riid, void **ppv)
516 BSCallback *This = impl_from_IInternetBindInfo(iface);
517 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
520 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
522 BSCallback *This = impl_from_IInternetBindInfo(iface);
523 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
526 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
528 BSCallback *This = impl_from_IInternetBindInfo(iface);
529 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
532 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
533 DWORD *grfBINDF, BINDINFO *pbindinfo)
535 BSCallback *This = impl_from_IInternetBindInfo(iface);
536 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
540 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
541 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
543 BSCallback *This = impl_from_IInternetBindInfo(iface);
544 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
548 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
549 InternetBindInfo_QueryInterface,
550 InternetBindInfo_AddRef,
551 InternetBindInfo_Release,
552 InternetBindInfo_GetBindInfo,
553 InternetBindInfo_GetBindString
556 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
558 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
561 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
562 REFIID riid, void **ppv)
564 BSCallback *This = impl_from_IServiceProvider(iface);
565 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
568 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
570 BSCallback *This = impl_from_IServiceProvider(iface);
571 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
574 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
576 BSCallback *This = impl_from_IServiceProvider(iface);
577 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
580 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
581 REFGUID guidService, REFIID riid, void **ppv)
583 BSCallback *This = impl_from_IServiceProvider(iface);
584 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
585 return E_NOINTERFACE;
588 static const IServiceProviderVtbl ServiceProviderVtbl = {
589 BSCServiceProvider_QueryInterface,
590 BSCServiceProvider_AddRef,
591 BSCServiceProvider_Release,
592 BSCServiceProvider_QueryService
595 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
597 This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
598 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
599 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
600 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
605 list_init(&This->entry);
608 IMoniker_AddRef(mon);
612 /* Calls undocumented 84 cmd of CGID_ShellDocView */
613 static void call_docview_84(HTMLDocumentObj *doc)
615 IOleCommandTarget *olecmd;
622 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
627 hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
628 IOleCommandTarget_Release(olecmd);
629 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
630 FIXME("handle result\n");
633 static void parse_content_type(nsChannelBSC *This, const WCHAR *value)
638 static const WCHAR charsetW[] = {'c','h','a','r','s','e','t','='};
640 ptr = strchrW(value, ';');
645 while(*ptr && isspaceW(*ptr))
648 len = strlenW(value);
649 if(ptr + sizeof(charsetW)/sizeof(WCHAR) < value+len && !memicmpW(ptr, charsetW, sizeof(charsetW)/sizeof(WCHAR))) {
650 size_t charset_len, lena;
651 nsACString charset_str;
652 const WCHAR *charset;
655 ptr += sizeof(charsetW)/sizeof(WCHAR);
658 FIXME("Quoted value\n");
662 while(*ptr && *ptr != ',')
664 charset_len = ptr-charset;
667 lena = WideCharToMultiByte(CP_ACP, 0, charset, charset_len, NULL, 0, NULL, NULL);
668 charseta = heap_alloc(lena+1);
672 WideCharToMultiByte(CP_ACP, 0, charset, charset_len, charseta, lena, NULL, NULL);
675 nsACString_InitDepend(&charset_str, charseta);
676 nsIHttpChannel_SetContentCharset(&This->nschannel->nsIHttpChannel_iface, &charset_str);
677 nsACString_Finish(&charset_str);
680 FIXME("unhandled: %s\n", debugstr_wn(ptr, len - (ptr-value)));
684 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
686 const WCHAR *header, *header_end, *colon, *value;
691 if(header[0] == '\r' && header[1] == '\n' && !header[2])
693 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
703 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
705 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
710 if(header[0] == '\r' && header[1] == '\n')
717 static HRESULT process_response_headers(nsChannelBSC *This, const WCHAR *headers)
722 static const WCHAR content_typeW[] = {'c','o','n','t','e','n','t','-','t','y','p','e',0};
724 hres = parse_headers(headers, &This->nschannel->response_headers);
728 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->response_headers, http_header_t, entry) {
729 if(!strcmpiW(iter->header, content_typeW))
730 parse_content_type(This, iter->data);
736 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
741 TRACE("(%p %p %p %p)\n", window, doc, bscallback, bctx);
743 bscallback->doc = doc;
745 /* NOTE: IE7 calls IsSystemMoniker here*/
748 if(bscallback->mon != window->mon)
749 set_current_mon(window, bscallback->mon);
750 call_docview_84(window->doc_obj);
754 RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
755 IBindCtx_AddRef(bctx);
757 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
759 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
760 bscallback->vtbl->stop_binding(bscallback, hres);
765 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
766 IBindCtx_Release(bctx);
768 WARN("BindToStorage failed: %08x\n", hres);
769 bscallback->vtbl->stop_binding(bscallback, hres);
774 IStream_Release(str);
776 IMoniker_Release(bscallback->mon);
777 bscallback->mon = NULL;
790 static inline BufferBSC *BufferBSC_from_BSCallback(BSCallback *iface)
792 return CONTAINING_RECORD(iface, BufferBSC, bsc);
795 static void BufferBSC_destroy(BSCallback *bsc)
797 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
799 heap_free(This->buf);
803 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
808 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
813 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
815 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
820 heap_free(This->buf);
828 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
830 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
836 This->buf = heap_alloc(This->size);
840 if(This->bsc.readed == This->size) {
842 This->buf = heap_realloc(This->buf, This->size);
846 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
847 This->bsc.readed += readed;
848 }while(hres == S_OK);
853 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
858 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
859 LPCWSTR response_headers)
864 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
869 static const BSCallbackVtbl BufferBSCVtbl = {
871 BufferBSC_init_bindinfo,
872 BufferBSC_start_binding,
873 BufferBSC_stop_binding,
875 BufferBSC_on_progress,
876 BufferBSC_on_response,
877 BufferBSC_beginning_transaction
881 static BufferBSC *create_bufferbsc(IMoniker *mon)
883 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
885 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
891 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
893 BufferBSC *bsc = create_bufferbsc(mon);
898 hres = start_binding(NULL, doc, &bsc->bsc, NULL);
899 if(SUCCEEDED(hres)) {
901 if(SUCCEEDED(hres)) {
904 *size = bsc->bsc.readed;
909 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
914 static HRESULT read_post_data_stream(nsChannelBSC *This, nsChannel *nschannel)
916 PRUint32 data_len = 0, available = 0;
917 char *data, *post_data;
921 if(!nschannel->post_data_stream)
924 nsres = nsIInputStream_Available(nschannel->post_data_stream, &available);
928 post_data = data = GlobalAlloc(0, available);
930 return E_OUTOFMEMORY;
932 nsres = nsIInputStream_Read(nschannel->post_data_stream, data, available, &data_len);
933 if(NS_FAILED(nsres)) {
938 if(nschannel->post_data_contains_headers) {
939 if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
947 post_data += data_len;
948 for(ptr = data; ptr+4 < data+data_len; ptr++) {
949 if(!memcmp(ptr, "\r\n\r\n", 4)) {
955 data_len -= post_data-data;
957 size = MultiByteToWideChar(CP_ACP, 0, data, post_data-data, NULL, 0);
958 headers = heap_alloc((size+1)*sizeof(WCHAR));
960 MultiByteToWideChar(CP_ACP, 0, data, post_data-data, headers, size);
962 hres = parse_headers(headers , &nschannel->request_headers);
964 This->bsc.headers = headers;
968 hres = E_OUTOFMEMORY;
981 }else if(post_data != data) {
984 new_data = GlobalAlloc(0, data_len);
986 memcpy(new_data, post_data, data_len);
989 return E_OUTOFMEMORY;
990 post_data = new_data;
993 This->bsc.post_data = post_data;
994 This->bsc.post_data_len = data_len;
995 TRACE("post_data = %s\n", debugstr_a(This->bsc.post_data));
999 static HRESULT on_start_nsrequest(nsChannelBSC *This)
1003 /* FIXME: it's needed for http connections from BindToObject. */
1004 if(!This->nschannel->response_status)
1005 This->nschannel->response_status = 200;
1007 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
1008 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
1009 if(NS_FAILED(nsres)) {
1010 FIXME("OnStartRequest failed: %08x\n", nsres);
1015 list_remove(&This->bsc.entry);
1016 list_init(&This->bsc.entry);
1017 update_window_doc(This->window);
1018 if(This->window->doc != This->bsc.doc)
1019 This->bsc.doc = This->window->doc;
1020 list_add_head(&This->bsc.doc->bindings, &This->bsc.entry);
1021 if(This->window->readystate != READYSTATE_LOADING)
1022 set_ready_state(This->window, READYSTATE_LOADING);
1028 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
1030 nsresult nsres, request_result;
1034 request_result = NS_OK;
1037 request_result = NS_BINDING_ABORTED;
1040 request_result = NS_ERROR_FAILURE;
1043 if(This->nslistener) {
1044 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
1045 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1047 if(NS_FAILED(nsres))
1048 WARN("OnStopRequest failed: %08x\n", nsres);
1051 if(This->nschannel->load_group) {
1052 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1053 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
1054 if(NS_FAILED(nsres))
1055 ERR("RemoveRequest failed: %08x\n", nsres);
1059 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
1061 static const WCHAR mimeTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
1067 if(!This->nslistener) {
1072 hres = IStream_Read(stream, buf, sizeof(buf), &read);
1073 }while(hres == S_OK && read);
1079 This->nsstream = create_nsprotocol_stream();
1083 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
1084 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1088 This->nsstream->buf_size += read;
1090 if(!This->bsc.readed) {
1091 if(This->nsstream->buf_size >= 2
1092 && (BYTE)This->nsstream->buf[0] == 0xff
1093 && (BYTE)This->nsstream->buf[1] == 0xfe)
1094 This->nschannel->charset = heap_strdupA(UTF16_STR);
1096 if(!This->nschannel->content_type) {
1099 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size,
1100 This->window ? mimeTextHtml : NULL, 0, &mime, 0);
1104 TRACE("Found MIME %s\n", debugstr_w(mime));
1106 This->nschannel->content_type = heap_strdupWtoA(mime);
1107 CoTaskMemFree(mime);
1108 if(!This->nschannel->content_type)
1109 return E_OUTOFMEMORY;
1112 on_start_nsrequest(This);
1115 This->bsc.readed += This->nsstream->buf_size;
1117 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1118 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1119 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1120 This->nsstream->buf_size);
1121 if(NS_FAILED(nsres))
1122 ERR("OnDataAvailable failed: %08x\n", nsres);
1124 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1125 ERR("buffer is full\n");
1128 }while(hres == S_OK);
1134 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1138 nsChannel *nschannel;
1140 } nsRedirectCallback;
1142 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1144 return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1147 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1148 nsIIDRef riid, void **result)
1150 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1152 if(IsEqualGUID(&IID_nsISupports, riid)) {
1153 TRACE("(%p)->(IID_nsISuports %p)\n", This, result);
1154 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1155 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1156 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1157 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1160 WARN("unimplemented iface %s\n", debugstr_guid(riid));
1161 return NS_NOINTERFACE;
1164 nsISupports_AddRef((nsISupports*)*result);
1168 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1170 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1171 LONG ref = InterlockedIncrement(&This->ref);
1173 TRACE("(%p) ref=%d\n", This, ref);
1178 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1180 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1181 LONG ref = InterlockedDecrement(&This->ref);
1183 TRACE("(%p) ref=%d\n", This, ref);
1186 IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1187 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1194 static nsresult NSAPI nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1196 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1198 TRACE("(%p)->(%08x)\n", This, result);
1200 if(This->bsc->nschannel)
1201 nsIHttpChannel_Release(&This->bsc->nschannel->nsIHttpChannel_iface);
1202 nsIChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1203 This->bsc->nschannel = This->nschannel;
1205 if(This->nschannel->load_group) {
1208 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1210 if(NS_FAILED(nsres))
1211 ERR("AddRequest failed: %08x\n", nsres);
1214 if(This->bsc->window) {
1215 IUri *uri = nsuri_get_uri(This->nschannel->uri);
1218 set_current_uri(This->bsc->window, uri);
1221 WARN("Could not get IUri from nsWineURI\n");
1228 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1229 nsAsyncVerifyRedirectCallback_QueryInterface,
1230 nsAsyncVerifyRedirectCallback_AddRef,
1231 nsAsyncVerifyRedirectCallback_Release,
1232 nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1235 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1237 nsRedirectCallback *callback;
1239 callback = heap_alloc(sizeof(*callback));
1241 return E_OUTOFMEMORY;
1243 callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1246 nsIChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1247 callback->nschannel = nschannel;
1249 IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1250 callback->bsc = bsc;
1256 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1258 return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1261 static void nsChannelBSC_destroy(BSCallback *bsc)
1263 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1266 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1267 if(This->nslistener)
1268 nsIStreamListener_Release(This->nslistener);
1270 nsISupports_Release(This->nscontext);
1272 nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1276 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1278 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1281 This->window->doc->skip_mutation_notif = FALSE;
1286 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1288 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1291 if(This->nschannel && This->nschannel->post_data_stream) {
1292 hres = read_post_data_stream(This, This->nschannel);
1303 } stop_request_task_t;
1305 static void stop_request_proc(task_t *_task)
1307 stop_request_task_t *task = (stop_request_task_t*)_task;
1309 TRACE("(%p)\n", task->bsc);
1311 list_remove(&task->bsc->bsc.entry);
1312 list_init(&task->bsc->bsc.entry);
1313 on_stop_nsrequest(task->bsc, S_OK);
1316 static void stop_request_task_destr(task_t *_task)
1318 stop_request_task_t *task = (stop_request_task_t*)_task;
1320 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1324 static HRESULT async_stop_request(nsChannelBSC *This)
1326 stop_request_task_t *task;
1328 if(!This->bsc.readed) {
1329 TRACE("No data read, calling OnStartRequest\n");
1330 on_start_nsrequest(This);
1333 task = heap_alloc(sizeof(*task));
1335 return E_OUTOFMEMORY;
1337 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1339 push_task(&task->header, stop_request_proc, stop_request_task_destr, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1343 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1345 HTMLDocumentObj *doc;
1346 IOleCommandTarget *olecmd;
1349 SAFEARRAYBOUND bound;
1350 VARIANT var, varOut;
1358 doc = This->window->doc_obj;
1359 if(!doc || !doc->doc_object_service || !doc->client)
1362 hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1363 This->window->url, &is_error_url);
1364 if(FAILED(hres) || is_error_url)
1367 hres = IOleClientSite_QueryInterface(doc->client,
1368 &IID_IOleCommandTarget, (void**)&olecmd);
1373 bound.cElements = 8;
1374 sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1376 IOleCommandTarget_Release(olecmd);
1382 V_I4(&var) = result;
1383 SafeArrayPutElement(sa, &ind, &var);
1386 V_VT(&var) = VT_BSTR;
1387 V_BSTR(&var) = This->window->url;
1388 SafeArrayPutElement(sa, &ind, &var);
1391 V_VT(&var) = VT_UNKNOWN;
1392 V_UNKNOWN(&var) = (IUnknown*)&This->window->IHTMLWindow2_iface;
1393 SafeArrayPutElement(sa, &ind, &var);
1395 /* FIXME: what are the following fields for? */
1397 V_VT(&var) = VT_UNKNOWN;
1398 V_UNKNOWN(&var) = NULL;
1399 SafeArrayPutElement(sa, &ind, &var);
1402 V_VT(&var) = VT_BOOL;
1403 V_BOOL(&var) = FALSE;
1404 SafeArrayPutElement(sa, &ind, &var);
1407 V_VT(&var) = VT_BOOL;
1408 V_BOOL(&var) = FALSE;
1409 SafeArrayPutElement(sa, &ind, &var);
1412 V_VT(&var) = VT_BSTR;
1413 unk = SysAllocString(NULL);
1415 SafeArrayPutElement(sa, &ind, &var);
1418 V_VT(&var) = VT_UNKNOWN;
1419 V_UNKNOWN(&var) = NULL;
1420 SafeArrayPutElement(sa, &ind, &var);
1422 V_VT(&var) = VT_ARRAY;
1424 V_VT(&varOut) = VT_BOOL;
1425 V_BOOL(&varOut) = VARIANT_TRUE;
1426 IOleCommandTarget_Exec(olecmd, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1429 SafeArrayDestroy(sa);
1430 IOleCommandTarget_Release(olecmd);
1433 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1435 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1437 if(result != E_ABORT) {
1439 handle_navigation_error(This, result);
1440 else if(This->window) {
1441 result = async_stop_request(This);
1442 if(SUCCEEDED(result))
1447 on_stop_nsrequest(This, result);
1451 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1453 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1455 return read_stream_data(This, stream);
1458 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1460 nsRedirectCallback *callback;
1461 nsIChannelEventSink *sink;
1462 nsChannel *new_channel;
1466 TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1468 if(!This->nschannel || !This->nschannel->notif_callback)
1471 nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1472 if(NS_FAILED(nsres))
1475 hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1476 if(SUCCEEDED(hres)) {
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(HTMLWindow *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;
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(HTMLWindow *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);
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 HTMLWindow *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);
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(HTMLWindow *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->IHTMLWindow2_iface, 0x10);
1889 IDocObjectService_FireDocumentComplete(window->doc_obj->doc_object_service, &window->IHTMLWindow2_iface, 0);
1896 HRESULT super_navigate(HTMLWindow *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 task->window = window;
1958 task->bscallback = bsc;
1960 push_task(&task->header, navigate_proc, navigate_task_destr, window->task_magic);
1962 /* Silently and repeated when real loading starts? */
1963 window->readystate = READYSTATE_LOADING;
1965 navigate_javascript_task_t *task;
1967 IUnknown_Release((IUnknown*)bsc);
1968 IMoniker_Release(mon);
1970 task = heap_alloc(sizeof(*task));
1972 return E_OUTOFMEMORY;
1975 task->window = window;
1977 push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, window->task_magic);
1980 window->readystate = READYSTATE_COMPLETE;
1986 HRESULT navigate_new_window(HTMLWindow *window, IUri *uri, const WCHAR *name, IHTMLWindow2 **ret)
1988 IWebBrowser2 *web_browser;
1989 IHTMLWindow2 *new_window;
1994 hres = create_channelbsc(NULL, NULL, NULL, 0, &bsc);
1998 hres = CreateAsyncBindCtx(0, &bsc->bsc.IBindStatusCallback_iface, NULL, &bind_ctx);
2000 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2004 hres = CoCreateInstance(&CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
2005 &IID_IWebBrowser2, (void**)&web_browser);
2006 if(SUCCEEDED(hres)) {
2007 ITargetFramePriv2 *target_frame_priv;
2009 hres = IWebBrowser_QueryInterface(web_browser, &IID_ITargetFramePriv2, (void**)&target_frame_priv);
2010 if(SUCCEEDED(hres)) {
2011 hres = ITargetFramePriv2_AggregatedNavigation2(target_frame_priv,
2012 HLNF_DISABLEWINDOWRESTRICTIONS|HLNF_OPENINNEWWINDOW, bind_ctx, &bsc->bsc.IBindStatusCallback_iface,
2014 ITargetFramePriv2_Release(target_frame_priv);
2017 hres = do_query_service((IUnknown*)web_browser, &SID_SHTMLWindow, &IID_IHTMLWindow2, (void**)&new_window);
2020 IWebBrowser2_Quit(web_browser);
2021 IWebBrowser2_Release(web_browser);
2024 WARN("Could not create InternetExplorer instance: %08x\n", hres);
2027 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2028 IBindCtx_Release(bind_ctx);
2032 IWebBrowser2_put_Visible(web_browser, VARIANT_TRUE);
2033 IWebBrowser2_Release(web_browser);
2038 IHTMLWindow2_Release(new_window);
2042 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
2044 IHlinkFrame *hlink_frame;
2045 nsChannelBSC *callback;
2053 hres = do_query_service((IUnknown*)doc->doc_obj->client, &IID_IHlinkFrame, &IID_IHlinkFrame,
2054 (void**)&hlink_frame);
2058 hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
2060 IHlinkFrame_Release(hlink_frame);
2065 read_post_data_stream(callback, nschannel);
2067 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
2069 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
2070 &IID_IHlink, (LPVOID*)&hlink);
2073 hres = CreateURLMoniker(NULL, url, &mon);
2075 if(SUCCEEDED(hres)) {
2076 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
2078 if(hlnf & HLNF_OPENINNEWWINDOW) {
2079 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
2080 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
2083 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
2084 &callback->bsc.IBindStatusCallback_iface, hlink);
2085 IMoniker_Release(mon);
2086 *cancel = hres == S_OK;
2090 IHlinkFrame_Release(hlink_frame);
2091 IBindCtx_Release(bindctx);
2092 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
2096 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
2098 WCHAR url[INTERNET_MAX_URL_LENGTH];
2104 }else if(base_url) {
2107 hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2108 url, sizeof(url)/sizeof(WCHAR), &len, 0);
2112 strcpyW(url, new_url);
2115 if(window->doc_obj && window->doc_obj->hostui) {
2116 OLECHAR *translated_url = NULL;
2118 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
2121 TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
2122 strcpyW(url, translated_url);
2123 CoTaskMemFree(translated_url);
2127 if(window->doc_obj && window->doc_obj->is_webbrowser && window == window->doc_obj->basedoc.window) {
2128 BOOL cancel = FALSE;
2131 hres = IDocObjectService_FireBeforeNavigate2(window->doc_obj->doc_object_service, NULL, url, 0x40,
2132 NULL, NULL, 0, NULL, TRUE, &cancel);
2133 if(SUCCEEDED(hres) && cancel) {
2134 TRACE("Navigation canceled\n");
2138 hres = CreateUri(url, 0, 0, &uri);
2142 hres = super_navigate(window, uri, NULL, NULL, 0);
2147 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
2150 hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
2155 TRACE("Navigation handled by hlink frame\n");
2160 hres = create_doc_uri(window, url, &uri);
2164 hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
2165 nsISupports_Release((nsISupports*)uri);