hlink: Use ifaces instead of vtbl pointers in ExtensionService.
[wine] / dlls / mshtml / navigate.c
1 /*
2  * Copyright 2006-2010 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "ole2.h"
32 #include "hlguids.h"
33 #include "shlguid.h"
34 #include "wininet.h"
35 #include "shlwapi.h"
36
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39
40 #include "mshtml_private.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
43
44 #define CONTENT_LENGTH "Content-Length"
45 #define UTF16_STR "utf-16"
46
47 typedef struct {
48     const nsIInputStreamVtbl *lpInputStreamVtbl;
49
50     LONG ref;
51
52     char buf[1024];
53     DWORD buf_size;
54 } nsProtocolStream;
55
56 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
57
58 typedef struct {
59     void (*destroy)(BSCallback*);
60     HRESULT (*init_bindinfo)(BSCallback*);
61     HRESULT (*start_binding)(BSCallback*);
62     HRESULT (*stop_binding)(BSCallback*,HRESULT);
63     HRESULT (*read_data)(BSCallback*,IStream*);
64     HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
65     HRESULT (*on_response)(BSCallback*,DWORD,LPCWSTR);
66     HRESULT (*beginning_transaction)(BSCallback*,WCHAR**);
67 } BSCallbackVtbl;
68
69 struct BSCallback {
70     IBindStatusCallback IBindStatusCallback_iface;
71     IServiceProvider    IServiceProvider_iface;
72     IHttpNegotiate2     IHttpNegotiate2_iface;
73     IInternetBindInfo   IInternetBindInfo_iface;
74
75     const BSCallbackVtbl          *vtbl;
76
77     LONG ref;
78
79     LPWSTR headers;
80     HGLOBAL post_data;
81     ULONG post_data_len;
82     ULONG readed;
83     DWORD bindf;
84     BOOL bindinfo_ready;
85
86     IMoniker *mon;
87     IBinding *binding;
88
89     HTMLDocumentNode *doc;
90
91     struct list entry;
92 };
93
94 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
95
96 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
97         void **result)
98 {
99     nsProtocolStream *This = NSINSTREAM_THIS(iface);
100
101     *result = NULL;
102
103     if(IsEqualGUID(&IID_nsISupports, riid)) {
104         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
105         *result  = NSINSTREAM(This);
106     }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
107         TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
108         *result  = NSINSTREAM(This);
109     }
110
111     if(*result) {
112         nsIInputStream_AddRef(NSINSTREAM(This));
113         return NS_OK;
114     }
115
116     WARN("unsupported interface %s\n", debugstr_guid(riid));
117     return NS_NOINTERFACE;
118 }
119
120 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
121 {
122     nsProtocolStream *This = NSINSTREAM_THIS(iface);
123     LONG ref = InterlockedIncrement(&This->ref);
124
125     TRACE("(%p) ref=%d\n", This, ref);
126
127     return ref;
128 }
129
130
131 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
132 {
133     nsProtocolStream *This = NSINSTREAM_THIS(iface);
134     LONG ref = InterlockedDecrement(&This->ref);
135
136     TRACE("(%p) ref=%d\n", This, ref);
137
138     if(!ref)
139         heap_free(This);
140
141     return ref;
142 }
143
144 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
145 {
146     nsProtocolStream *This = NSINSTREAM_THIS(iface);
147     FIXME("(%p)\n", This);
148     return NS_ERROR_NOT_IMPLEMENTED;
149 }
150
151 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
152 {
153     nsProtocolStream *This = NSINSTREAM_THIS(iface);
154     FIXME("(%p)->(%p)\n", This, _retval);
155     return NS_ERROR_NOT_IMPLEMENTED;
156 }
157
158 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
159                                          PRUint32 *_retval)
160 {
161     nsProtocolStream *This = NSINSTREAM_THIS(iface);
162     DWORD read = aCount;
163
164     TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
165
166     if(read > This->buf_size)
167         read = This->buf_size;
168
169     if(read) {
170         memcpy(aBuf, This->buf, read);
171         if(read < This->buf_size)
172             memmove(This->buf, This->buf+read, This->buf_size-read);
173         This->buf_size -= read;
174     }
175
176     *_retval = read;
177     return NS_OK;
178 }
179
180 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
181         nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
182         void *aClousure, PRUint32 aCount, PRUint32 *_retval)
183 {
184     nsProtocolStream *This = NSINSTREAM_THIS(iface);
185     PRUint32 written = 0;
186     nsresult nsres;
187
188     TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
189
190     if(!This->buf_size)
191         return S_OK;
192
193     if(aCount > This->buf_size)
194         aCount = This->buf_size;
195
196     nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, aCount, &written);
197     if(NS_FAILED(nsres))
198         TRACE("aWritter failed: %08x\n", nsres);
199     else if(written != This->buf_size)
200         FIXME("written %d != buf_size %d\n", written, This->buf_size);
201
202     This->buf_size -= written; 
203
204     *_retval = written;
205     return nsres;
206 }
207
208 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
209 {
210     nsProtocolStream *This = NSINSTREAM_THIS(iface);
211     FIXME("(%p)->(%p)\n", This, _retval);
212     return NS_ERROR_NOT_IMPLEMENTED;
213 }
214
215 #undef NSINSTREAM_THIS
216
217 static const nsIInputStreamVtbl nsInputStreamVtbl = {
218     nsInputStream_QueryInterface,
219     nsInputStream_AddRef,
220     nsInputStream_Release,
221     nsInputStream_Close,
222     nsInputStream_Available,
223     nsInputStream_Read,
224     nsInputStream_ReadSegments,
225     nsInputStream_IsNonBlocking
226 };
227
228 static nsProtocolStream *create_nsprotocol_stream(void)
229 {
230     nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
231
232     ret->lpInputStreamVtbl = &nsInputStreamVtbl;
233     ret->ref = 1;
234     ret->buf_size = 0;
235
236     return ret;
237 }
238
239 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
240 {
241     return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
242 }
243
244 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
245         REFIID riid, void **ppv)
246 {
247     BSCallback *This = impl_from_IBindStatusCallback(iface);
248
249     *ppv = NULL;
250     if(IsEqualGUID(&IID_IUnknown, riid)) {
251         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
252         *ppv = &This->IBindStatusCallback_iface;
253     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
254         TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
255         *ppv = &This->IBindStatusCallback_iface;
256     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
257         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
258         *ppv = &This->IServiceProvider_iface;
259     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
260         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
261         *ppv = &This->IHttpNegotiate2_iface;
262     }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
263         TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
264         *ppv = &This->IHttpNegotiate2_iface;
265     }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
266         TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
267         *ppv = &This->IInternetBindInfo_iface;
268     }
269
270     if(*ppv) {
271         IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
272         return S_OK;
273     }
274
275     TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
276     return E_NOINTERFACE;
277 }
278
279 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
280 {
281     BSCallback *This = impl_from_IBindStatusCallback(iface);
282     LONG ref = InterlockedIncrement(&This->ref);
283
284     TRACE("(%p) ref = %d\n", This, ref);
285
286     return ref;
287 }
288
289 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
290 {
291     BSCallback *This = impl_from_IBindStatusCallback(iface);
292     LONG ref = InterlockedDecrement(&This->ref);
293
294     TRACE("(%p) ref = %d\n", This, ref);
295
296     if(!ref) {
297         if(This->post_data)
298             GlobalFree(This->post_data);
299         if(This->mon)
300             IMoniker_Release(This->mon);
301         if(This->binding)
302             IBinding_Release(This->binding);
303         list_remove(&This->entry);
304         heap_free(This->headers);
305
306         This->vtbl->destroy(This);
307     }
308
309     return ref;
310 }
311
312 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
313         DWORD dwReserved, IBinding *pbind)
314 {
315     BSCallback *This = impl_from_IBindStatusCallback(iface);
316
317     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
318
319     IBinding_AddRef(pbind);
320     This->binding = pbind;
321
322     if(This->doc)
323         list_add_head(&This->doc->bindings, &This->entry);
324
325     return This->vtbl->start_binding(This);
326 }
327
328 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
329 {
330     BSCallback *This = impl_from_IBindStatusCallback(iface);
331     FIXME("(%p)->(%p)\n", This, pnPriority);
332     return E_NOTIMPL;
333 }
334
335 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
336 {
337     BSCallback *This = impl_from_IBindStatusCallback(iface);
338     FIXME("(%p)->(%d)\n", This, reserved);
339     return E_NOTIMPL;
340 }
341
342 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
343         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
344 {
345     BSCallback *This = impl_from_IBindStatusCallback(iface);
346
347     TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
348             debugstr_w(szStatusText));
349
350     return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
351 }
352
353 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
354         HRESULT hresult, LPCWSTR szError)
355 {
356     BSCallback *This = impl_from_IBindStatusCallback(iface);
357     HRESULT hres;
358
359     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
360
361     /* NOTE: IE7 calls GetBindResult here */
362
363     hres = This->vtbl->stop_binding(This, hresult);
364
365     if(This->binding) {
366         IBinding_Release(This->binding);
367         This->binding = NULL;
368     }
369
370     list_remove(&This->entry);
371     This->doc = NULL;
372
373     return hres;
374 }
375
376 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
377         DWORD *grfBINDF, BINDINFO *pbindinfo)
378 {
379     BSCallback *This = impl_from_IBindStatusCallback(iface);
380     DWORD size;
381
382     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
383
384     if(!This->bindinfo_ready) {
385         HRESULT hres;
386
387         hres = This->vtbl->init_bindinfo(This);
388         if(FAILED(hres))
389             return hres;
390
391         This->bindinfo_ready = TRUE;
392     }
393
394     *grfBINDF = This->bindf;
395
396     size = pbindinfo->cbSize;
397     memset(pbindinfo, 0, size);
398     pbindinfo->cbSize = size;
399
400     pbindinfo->cbstgmedData = This->post_data_len;
401     pbindinfo->dwCodePage = CP_UTF8;
402     pbindinfo->dwOptions = 0x80000;
403
404     if(This->post_data) {
405         pbindinfo->dwBindVerb = BINDVERB_POST;
406
407         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
408         pbindinfo->stgmedData.u.hGlobal = This->post_data;
409         pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
410         IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
411     }
412
413     return S_OK;
414 }
415
416 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
417         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
418 {
419     BSCallback *This = impl_from_IBindStatusCallback(iface);
420
421     TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
422
423     return This->vtbl->read_data(This, pstgmed->u.pstm);
424 }
425
426 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
427         REFIID riid, IUnknown *punk)
428 {
429     BSCallback *This = impl_from_IBindStatusCallback(iface);
430     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
431     return E_NOTIMPL;
432 }
433
434 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
435     BindStatusCallback_QueryInterface,
436     BindStatusCallback_AddRef,
437     BindStatusCallback_Release,
438     BindStatusCallback_OnStartBinding,
439     BindStatusCallback_GetPriority,
440     BindStatusCallback_OnLowResource,
441     BindStatusCallback_OnProgress,
442     BindStatusCallback_OnStopBinding,
443     BindStatusCallback_GetBindInfo,
444     BindStatusCallback_OnDataAvailable,
445     BindStatusCallback_OnObjectAvailable
446 };
447
448 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
449 {
450     return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
451 }
452
453 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
454                                                    REFIID riid, void **ppv)
455 {
456     BSCallback *This = impl_from_IHttpNegotiate2(iface);
457     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
458 }
459
460 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
461 {
462     BSCallback *This = impl_from_IHttpNegotiate2(iface);
463     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
464 }
465
466 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
467 {
468     BSCallback *This = impl_from_IHttpNegotiate2(iface);
469     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
470 }
471
472 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
473         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
474 {
475     BSCallback *This = impl_from_IHttpNegotiate2(iface);
476     HRESULT hres;
477
478     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
479           dwReserved, pszAdditionalHeaders);
480
481     *pszAdditionalHeaders = NULL;
482
483     hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
484     if(hres != S_FALSE)
485         return hres;
486
487     if(This->headers) {
488         DWORD size;
489
490         size = (strlenW(This->headers)+1)*sizeof(WCHAR);
491         *pszAdditionalHeaders = CoTaskMemAlloc(size);
492         if(!*pszAdditionalHeaders)
493             return E_OUTOFMEMORY;
494         memcpy(*pszAdditionalHeaders, This->headers, size);
495     }
496
497     return S_OK;
498 }
499
500 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
501         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
502 {
503     BSCallback *This = impl_from_IHttpNegotiate2(iface);
504
505     TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
506           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
507
508     return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
509 }
510
511 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
512         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
513 {
514     BSCallback *This = impl_from_IHttpNegotiate2(iface);
515     FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
516     return E_NOTIMPL;
517 }
518
519 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
520     HttpNegotiate_QueryInterface,
521     HttpNegotiate_AddRef,
522     HttpNegotiate_Release,
523     HttpNegotiate_BeginningTransaction,
524     HttpNegotiate_OnResponse,
525     HttpNegotiate_GetRootSecurityId
526 };
527
528 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
529 {
530     return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
531 }
532
533 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
534                                                       REFIID riid, void **ppv)
535 {
536     BSCallback *This = impl_from_IInternetBindInfo(iface);
537     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
538 }
539
540 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
541 {
542     BSCallback *This = impl_from_IInternetBindInfo(iface);
543     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
544 }
545
546 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
547 {
548     BSCallback *This = impl_from_IInternetBindInfo(iface);
549     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
550 }
551
552 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
553                                                    DWORD *grfBINDF, BINDINFO *pbindinfo)
554 {
555     BSCallback *This = impl_from_IInternetBindInfo(iface);
556     FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
557     return E_NOTIMPL;
558 }
559
560 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
561         ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
562 {
563     BSCallback *This = impl_from_IInternetBindInfo(iface);
564     FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
565     return E_NOTIMPL;
566 }
567
568 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
569     InternetBindInfo_QueryInterface,
570     InternetBindInfo_AddRef,
571     InternetBindInfo_Release,
572     InternetBindInfo_GetBindInfo,
573     InternetBindInfo_GetBindString
574 };
575
576 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
577 {
578     return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
579 }
580
581 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
582                                                         REFIID riid, void **ppv)
583 {
584     BSCallback *This = impl_from_IServiceProvider(iface);
585     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
586 }
587
588 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
589 {
590     BSCallback *This = impl_from_IServiceProvider(iface);
591     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
592 }
593
594 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
595 {
596     BSCallback *This = impl_from_IServiceProvider(iface);
597     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
598 }
599
600 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
601         REFGUID guidService, REFIID riid, void **ppv)
602 {
603     BSCallback *This = impl_from_IServiceProvider(iface);
604     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
605     return E_NOINTERFACE;
606 }
607
608 static const IServiceProviderVtbl ServiceProviderVtbl = {
609     BSCServiceProvider_QueryInterface,
610     BSCServiceProvider_AddRef,
611     BSCServiceProvider_Release,
612     BSCServiceProvider_QueryService
613 };
614
615 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
616 {
617     This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
618     This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
619     This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
620     This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
621     This->vtbl = vtbl;
622     This->ref = 1;
623     This->bindf = bindf;
624
625     list_init(&This->entry);
626
627     if(mon)
628         IMoniker_AddRef(mon);
629     This->mon = mon;
630 }
631
632 /* Calls undocumented 84 cmd of CGID_ShellDocView */
633 static void call_docview_84(HTMLDocumentObj *doc)
634 {
635     IOleCommandTarget *olecmd;
636     VARIANT var;
637     HRESULT hres;
638
639     if(!doc->client)
640         return;
641
642     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
643     if(FAILED(hres))
644         return;
645
646     VariantInit(&var);
647     hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
648     IOleCommandTarget_Release(olecmd);
649     if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
650         FIXME("handle result\n");
651 }
652
653 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
654 {
655     const WCHAR *header, *header_end, *colon, *value;
656     HRESULT hres;
657
658     header = headers;
659     while(*header) {
660         if(header[0] == '\r' && header[1] == '\n' && !header[2])
661             break;
662         for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
663         if(*colon != ':')
664             return E_FAIL;
665
666         value = colon+1;
667         while(*value == ' ')
668             value++;
669         if(!*value)
670             return E_FAIL;
671
672         for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
673
674         hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
675         if(FAILED(hres))
676             return hres;
677
678         header = header_end;
679         if(header[0] == '\r' && header[1] == '\n')
680             header += 2;
681     }
682
683     return S_OK;
684 }
685
686 static HRESULT read_post_data_stream(nsIInputStream *stream, HGLOBAL *post_data,
687         ULONG *post_data_len)
688 {
689     PRUint32 data_len = 0, available = 0;
690     char *data;
691     nsresult nsres;
692
693     nsres =  nsIInputStream_Available(stream, &available);
694     if(NS_FAILED(nsres))
695         return E_FAIL;
696
697     data = GlobalAlloc(0, available+1);
698     if(!data)
699         return E_OUTOFMEMORY;
700
701     nsres = nsIInputStream_Read(stream, data, available, &data_len);
702     if(NS_FAILED(nsres)) {
703         GlobalFree(data);
704         return E_FAIL;
705     }
706
707     data[data_len] = 0;
708     *post_data = data;
709     *post_data_len = data_len;
710     return S_OK;
711 }
712
713 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
714 {
715     IStream *str = NULL;
716     HRESULT hres;
717
718     bscallback->doc = doc;
719
720     /* NOTE: IE7 calls IsSystemMoniker here*/
721
722     if(window) {
723         if(bscallback->mon != window->mon)
724             set_current_mon(window, bscallback->mon);
725         call_docview_84(window->doc_obj);
726     }
727
728     if(bctx) {
729         RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
730         IBindCtx_AddRef(bctx);
731     }else {
732         hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
733         if(FAILED(hres)) {
734             WARN("CreateAsyncBindCtx failed: %08x\n", hres);
735             bscallback->vtbl->stop_binding(bscallback, hres);
736             return hres;
737         }
738     }
739
740     hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
741     IBindCtx_Release(bctx);
742     if(FAILED(hres)) {
743         WARN("BindToStorage failed: %08x\n", hres);
744         bscallback->vtbl->stop_binding(bscallback, hres);
745         return hres;
746     }
747
748     if(str)
749         IStream_Release(str);
750
751     IMoniker_Release(bscallback->mon);
752     bscallback->mon = NULL;
753
754     return S_OK;
755 }
756
757 typedef struct {
758     BSCallback bsc;
759
760     DWORD size;
761     BYTE *buf;
762     HRESULT hres;
763 } BufferBSC;
764
765 #define BUFFERBSC_THIS(bsc) ((BufferBSC*) bsc)
766
767 static void BufferBSC_destroy(BSCallback *bsc)
768 {
769     BufferBSC *This = BUFFERBSC_THIS(bsc);
770
771     heap_free(This->buf);
772     heap_free(This);
773 }
774
775 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
776 {
777     return S_OK;
778 }
779
780 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
781 {
782     return S_OK;
783 }
784
785 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
786 {
787     BufferBSC *This = BUFFERBSC_THIS(bsc);
788
789     This->hres = result;
790
791     if(FAILED(result)) {
792         heap_free(This->buf);
793         This->buf = NULL;
794         This->size = 0;
795     }
796
797     return S_OK;
798 }
799
800 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
801 {
802     BufferBSC *This = BUFFERBSC_THIS(bsc);
803     DWORD readed;
804     HRESULT hres;
805
806     if(!This->buf) {
807         This->size = 128;
808         This->buf = heap_alloc(This->size);
809     }
810
811     do {
812         if(This->bsc.readed == This->size) {
813             This->size <<= 1;
814             This->buf = heap_realloc(This->buf, This->size);
815         }
816
817         readed = 0;
818         hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
819         This->bsc.readed += readed;
820     }while(hres == S_OK);
821
822     return S_OK;
823 }
824
825 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
826 {
827     return S_OK;
828 }
829
830 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
831         LPCWSTR response_headers)
832 {
833     return S_OK;
834 }
835
836 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
837 {
838     return S_FALSE;
839 }
840
841 #undef BUFFERBSC_THIS
842
843 static const BSCallbackVtbl BufferBSCVtbl = {
844     BufferBSC_destroy,
845     BufferBSC_init_bindinfo,
846     BufferBSC_start_binding,
847     BufferBSC_stop_binding,
848     BufferBSC_read_data,
849     BufferBSC_on_progress,
850     BufferBSC_on_response,
851     BufferBSC_beginning_transaction
852 };
853
854
855 static BufferBSC *create_bufferbsc(IMoniker *mon)
856 {
857     BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
858
859     init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
860     ret->hres = E_FAIL;
861
862     return ret;
863 }
864
865 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
866 {
867     BufferBSC *bsc = create_bufferbsc(mon);
868     HRESULT hres;
869
870     *buf = NULL;
871
872     hres = start_binding(NULL, doc, &bsc->bsc, NULL);
873     if(SUCCEEDED(hres)) {
874         hres = bsc->hres;
875         if(SUCCEEDED(hres)) {
876             *buf = bsc->buf;
877             bsc->buf = NULL;
878             *size = bsc->bsc.readed;
879             bsc->size = 0;
880         }
881     }
882
883     IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
884
885     return hres;
886 }
887
888 struct nsChannelBSC {
889     BSCallback bsc;
890
891     HTMLWindow *window;
892
893     nsChannel *nschannel;
894     nsIStreamListener *nslistener;
895     nsISupports *nscontext;
896
897     nsProtocolStream *nsstream;
898 };
899
900 static HRESULT on_start_nsrequest(nsChannelBSC *This)
901 {
902     nsresult nsres;
903
904     /* FIXME: it's needed for http connections from BindToObject. */
905     if(!This->nschannel->response_status)
906         This->nschannel->response_status = 200;
907
908     nsres = nsIStreamListener_OnStartRequest(This->nslistener,
909             (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
910     if(NS_FAILED(nsres)) {
911         FIXME("OnStartRequest failed: %08x\n", nsres);
912         return E_FAIL;
913     }
914
915     if(This->window) {
916         update_window_doc(This->window);
917         if(This->window->doc != This->bsc.doc)
918             This->bsc.doc = This->window->doc;
919         if(This->window->readystate != READYSTATE_LOADING)
920             set_ready_state(This->window, READYSTATE_LOADING);
921     }
922
923     return S_OK;
924 }
925
926 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
927 {
928     nsresult nsres, request_result;
929
930     switch(result) {
931     case S_OK:
932         request_result = NS_OK;
933         break;
934     case E_ABORT:
935         request_result = NS_BINDING_ABORTED;
936         break;
937     default:
938         request_result = NS_ERROR_FAILURE;
939     }
940
941     if(!This->bsc.readed && SUCCEEDED(result)) {
942         TRACE("No data read! Calling OnStartRequest\n");
943         on_start_nsrequest(This);
944     }
945
946     if(This->nslistener) {
947         nsres = nsIStreamListener_OnStopRequest(This->nslistener,
948                  (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
949                  request_result);
950         if(NS_FAILED(nsres))
951             WARN("OnStopRequet failed: %08x\n", nsres);
952     }
953
954     if(This->nschannel->load_group) {
955         nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
956                 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
957         if(NS_FAILED(nsres))
958             ERR("RemoveRequest failed: %08x\n", nsres);
959     }
960 }
961
962 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
963 {
964     DWORD read;
965     nsresult nsres;
966     HRESULT hres;
967
968     if(!This->nslistener) {
969         BYTE buf[1024];
970
971         do {
972             read = 0;
973             hres = IStream_Read(stream, buf, sizeof(buf), &read);
974         }while(hres == S_OK && read);
975
976         return S_OK;
977     }
978
979     if(!This->nsstream)
980         This->nsstream = create_nsprotocol_stream();
981
982     do {
983         read = 0;
984         hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
985                 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
986         if(!read)
987             break;
988
989         This->nsstream->buf_size += read;
990
991         if(!This->bsc.readed) {
992             if(This->nsstream->buf_size >= 2
993                && (BYTE)This->nsstream->buf[0] == 0xff
994                && (BYTE)This->nsstream->buf[1] == 0xfe)
995                 This->nschannel->charset = heap_strdupA(UTF16_STR);
996
997             if(!This->nschannel->content_type) {
998                 WCHAR *mime;
999
1000                 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
1001                 if(FAILED(hres))
1002                     return hres;
1003
1004                 TRACE("Found MIME %s\n", debugstr_w(mime));
1005
1006                 This->nschannel->content_type = heap_strdupWtoA(mime);
1007                 CoTaskMemFree(mime);
1008                 if(!This->nschannel->content_type)
1009                     return E_OUTOFMEMORY;
1010             }
1011
1012             on_start_nsrequest(This);
1013         }
1014
1015         This->bsc.readed += This->nsstream->buf_size;
1016
1017         nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1018                 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1019                 NSINSTREAM(This->nsstream), This->bsc.readed-This->nsstream->buf_size,
1020                 This->nsstream->buf_size);
1021         if(NS_FAILED(nsres))
1022             ERR("OnDataAvailable failed: %08x\n", nsres);
1023
1024         if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1025             ERR("buffer is full\n");
1026             break;
1027         }
1028     }while(hres == S_OK);
1029
1030     return S_OK;
1031 }
1032
1033 #define NSCHANNELBSC_THIS(bsc) ((nsChannelBSC*) bsc)
1034
1035 static void nsChannelBSC_destroy(BSCallback *bsc)
1036 {
1037     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1038
1039     if(This->nschannel)
1040         nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1041     if(This->nslistener)
1042         nsIStreamListener_Release(This->nslistener);
1043     if(This->nscontext)
1044         nsISupports_Release(This->nscontext);
1045     if(This->nsstream)
1046         nsIInputStream_Release(NSINSTREAM(This->nsstream));
1047     heap_free(This);
1048 }
1049
1050 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1051 {
1052     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1053
1054     if(This->window)
1055         This->window->doc->skip_mutation_notif = FALSE;
1056
1057     return S_OK;
1058 }
1059
1060 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1061 {
1062     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1063     HRESULT hres;
1064
1065     if(This->nschannel && This->nschannel->post_data_stream) {
1066         hres = read_post_data_stream(This->nschannel->post_data_stream,
1067                 &This->bsc.post_data, &This->bsc.post_data_len);
1068         if(FAILED(hres))
1069             return hres;
1070
1071         TRACE("post_data = %s\n", debugstr_an(This->bsc.post_data, This->bsc.post_data_len));
1072     }
1073
1074     return S_OK;
1075 }
1076
1077 typedef struct {
1078     task_t header;
1079     nsChannelBSC *bsc;
1080 } stop_request_task_t;
1081
1082 static void stop_request_proc(task_t *_task)
1083 {
1084     stop_request_task_t *task = (stop_request_task_t*)_task;
1085
1086     TRACE("(%p)\n", task->bsc);
1087
1088     on_stop_nsrequest(task->bsc, S_OK);
1089     IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1090 }
1091
1092 static HRESULT async_stop_request(nsChannelBSC *This)
1093 {
1094     stop_request_task_t *task;
1095
1096     task = heap_alloc(sizeof(*task));
1097     if(!task)
1098         return E_OUTOFMEMORY;
1099
1100     IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1101     task->bsc = This;
1102     push_task(&task->header, stop_request_proc, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1103     return S_OK;
1104 }
1105
1106 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1107 {
1108     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1109
1110     if(This->window && SUCCEEDED(result)) {
1111         result = async_stop_request(This);
1112         if(SUCCEEDED(result))
1113            return S_OK;
1114     }
1115
1116     on_stop_nsrequest(This, result);
1117     return S_OK;
1118 }
1119
1120 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1121 {
1122     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1123
1124     return read_stream_data(This, stream);
1125 }
1126
1127 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1128 {
1129     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1130
1131     switch(status_code) {
1132     case BINDSTATUS_MIMETYPEAVAILABLE:
1133         if(!This->nschannel)
1134             return S_OK;
1135
1136         heap_free(This->nschannel->content_type);
1137         This->nschannel->content_type = heap_strdupWtoA(status_text);
1138         break;
1139     case BINDSTATUS_REDIRECTING:
1140         TRACE("redirect to %s\n", debugstr_w(status_text));
1141
1142         /* FIXME: We should find a better way to handle this */
1143         set_wine_url(This->nschannel->uri, status_text);
1144     }
1145
1146     return S_OK;
1147 }
1148
1149 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1150         LPCWSTR response_headers)
1151 {
1152     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1153     HRESULT hres;
1154
1155     This->nschannel->response_status = response_code;
1156
1157     if(response_headers) {
1158         const WCHAR *headers;
1159
1160         headers = strchrW(response_headers, '\r');
1161         if(headers && headers[1] == '\n') {
1162             headers += 2;
1163             hres = parse_headers(headers, &This->nschannel->response_headers);
1164             if(FAILED(hres)) {
1165                 WARN("parsing headers failed: %08x\n", hres);
1166                 return hres;
1167             }
1168         }
1169     }
1170
1171     return S_OK;
1172 }
1173
1174 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1175 {
1176     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1177     http_header_t *iter;
1178     DWORD len = 0;
1179     WCHAR *ptr;
1180
1181     static const WCHAR content_lengthW[] =
1182         {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1183
1184     if(!This->nschannel)
1185         return S_FALSE;
1186
1187     LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1188         if(strcmpW(iter->header, content_lengthW))
1189             len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1190     }
1191
1192     if(!len)
1193         return S_OK;
1194
1195     *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1196     if(!ptr)
1197         return E_OUTOFMEMORY;
1198
1199     LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1200         if(!strcmpW(iter->header, content_lengthW))
1201             continue;
1202
1203         len = strlenW(iter->header);
1204         memcpy(ptr, iter->header, len*sizeof(WCHAR));
1205         ptr += len;
1206
1207         *ptr++ = ':';
1208         *ptr++ = ' ';
1209
1210         len = strlenW(iter->data);
1211         memcpy(ptr, iter->data, len*sizeof(WCHAR));
1212         ptr += len;
1213
1214         *ptr++ = '\r';
1215         *ptr++ = '\n';
1216     }
1217
1218     *ptr = 0;
1219
1220     return S_OK;
1221 }
1222
1223 #undef NSCHANNELBSC_THIS
1224
1225 static const BSCallbackVtbl nsChannelBSCVtbl = {
1226     nsChannelBSC_destroy,
1227     nsChannelBSC_init_bindinfo,
1228     nsChannelBSC_start_binding,
1229     nsChannelBSC_stop_binding,
1230     nsChannelBSC_read_data,
1231     nsChannelBSC_on_progress,
1232     nsChannelBSC_on_response,
1233     nsChannelBSC_beginning_transaction
1234 };
1235
1236 HRESULT create_channelbsc(IMoniker *mon, WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1237 {
1238     nsChannelBSC *ret;
1239
1240     ret = heap_alloc_zero(sizeof(*ret));
1241     if(!ret)
1242         return E_OUTOFMEMORY;
1243
1244     init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1245
1246     if(headers) {
1247         ret->bsc.headers = heap_strdupW(headers);
1248         if(!ret->bsc.headers) {
1249             IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1250             return E_OUTOFMEMORY;
1251         }
1252     }
1253
1254     if(post_data) {
1255         ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1256         if(!ret->bsc.post_data) {
1257             heap_free(ret->bsc.headers);
1258             IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1259             return E_OUTOFMEMORY;
1260         }
1261
1262         memcpy(ret->bsc.post_data, post_data, post_data_size);
1263         ret->bsc.post_data_len = post_data_size;
1264     }
1265
1266     *retval = ret;
1267     return S_OK;
1268 }
1269
1270 IMoniker *get_channelbsc_mon(nsChannelBSC *This)
1271 {
1272     if(This->bsc.mon)
1273         IMoniker_AddRef(This->bsc.mon);
1274     return This->bsc.mon;
1275 }
1276
1277 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1278 {
1279     if(window->bscallback) {
1280         if(window->bscallback->bsc.binding)
1281             IBinding_Abort(window->bscallback->bsc.binding);
1282         window->bscallback->bsc.doc = NULL;
1283         window->bscallback->window = NULL;
1284         IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1285     }
1286
1287     window->bscallback = callback;
1288
1289     if(callback) {
1290         callback->window = window;
1291         IBindStatusCallback_AddRef(&callback->bsc.IBindStatusCallback_iface);
1292         callback->bsc.doc = window->doc;
1293     }
1294 }
1295
1296 typedef struct {
1297     task_t header;
1298     HTMLWindow *window;
1299     nsChannelBSC *bscallback;
1300 } start_doc_binding_task_t;
1301
1302 static void start_doc_binding_proc(task_t *_task)
1303 {
1304     start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1305
1306     start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1307     IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1308 }
1309
1310 HRESULT async_start_doc_binding(HTMLWindow *window, nsChannelBSC *bscallback)
1311 {
1312     start_doc_binding_task_t *task;
1313
1314     task = heap_alloc(sizeof(start_doc_binding_task_t));
1315     if(!task)
1316         return E_OUTOFMEMORY;
1317
1318     task->window = window;
1319     task->bscallback = bscallback;
1320     IBindStatusCallback_AddRef(&bscallback->bsc.IBindStatusCallback_iface);
1321
1322     push_task(&task->header, start_doc_binding_proc, window->task_magic);
1323     return S_OK;
1324 }
1325
1326 void abort_document_bindings(HTMLDocumentNode *doc)
1327 {
1328     BSCallback *iter;
1329
1330     LIST_FOR_EACH_ENTRY(iter, &doc->bindings, BSCallback, entry) {
1331         if(iter->binding)
1332             IBinding_Abort(iter->binding);
1333         iter->doc = NULL;
1334         list_remove(&iter->entry);
1335     }
1336 }
1337
1338 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1339 {
1340     HRESULT hres = S_OK;
1341
1342     if(!bscallback->nschannel) {
1343         ERR("NULL nschannel\n");
1344         return E_FAIL;
1345     }
1346
1347     bscallback->nschannel->content_type = heap_strdupA("text/html");
1348     if(!bscallback->nschannel->content_type)
1349         return E_OUTOFMEMORY;
1350
1351     if(stream)
1352         hres = read_stream_data(bscallback, stream);
1353     if(SUCCEEDED(hres))
1354         hres = async_stop_request(bscallback);
1355     if(FAILED(hres))
1356         IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1357                 ERROR_SUCCESS);
1358
1359     return hres;
1360 }
1361
1362 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1363 {
1364     nsIChannel_AddRef(&channel->nsIHttpChannel_iface);
1365     This->nschannel = channel;
1366
1367     nsIStreamListener_AddRef(listener);
1368     This->nslistener = listener;
1369
1370     if(context) {
1371         nsISupports_AddRef(context);
1372         This->nscontext = context;
1373     }
1374
1375     if(This->bsc.headers) {
1376         HRESULT hres;
1377
1378         hres = parse_headers(This->bsc.headers, &channel->request_headers);
1379         heap_free(This->bsc.headers);
1380         This->bsc.headers = NULL;
1381         if(FAILED(hres))
1382             WARN("parse_headers failed: %08x\n", hres);
1383     }
1384 }
1385
1386 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url,
1387         nsIInputStream *post_data_stream, DWORD hlnf, BOOL *cancel)
1388 {
1389     IHlinkFrame *hlink_frame;
1390     nsChannelBSC *callback;
1391     IServiceProvider *sp;
1392     IBindCtx *bindctx;
1393     IMoniker *mon;
1394     IHlink *hlink;
1395     HRESULT hres;
1396
1397     *cancel = FALSE;
1398
1399     hres = IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IServiceProvider,
1400             (void**)&sp);
1401     if(FAILED(hres))
1402         return S_OK;
1403
1404     hres = IServiceProvider_QueryService(sp, &IID_IHlinkFrame, &IID_IHlinkFrame,
1405             (void**)&hlink_frame);
1406     IServiceProvider_Release(sp);
1407     if(FAILED(hres))
1408         return S_OK;
1409
1410     hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
1411     if(FAILED(hres)) {
1412         IHlinkFrame_Release(hlink_frame);
1413         return hres;
1414     }
1415
1416     if(post_data_stream) {
1417         read_post_data_stream(post_data_stream, &callback->bsc.post_data, &callback->bsc.post_data_len);
1418         TRACE("post_data = %s\n", debugstr_an(callback->bsc.post_data, callback->bsc.post_data_len));
1419     }
1420
1421     hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
1422     if(SUCCEEDED(hres))
1423         hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
1424                 &IID_IHlink, (LPVOID*)&hlink);
1425
1426     if(SUCCEEDED(hres))
1427         hres = CreateURLMoniker(NULL, url, &mon);
1428
1429     if(SUCCEEDED(hres)) {
1430         IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
1431
1432         if(hlnf & HLNF_OPENINNEWWINDOW) {
1433             static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1434             IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1435         }
1436
1437         hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
1438                 &callback->bsc.IBindStatusCallback_iface, hlink);
1439         IMoniker_Release(mon);
1440         *cancel = hres == S_OK;
1441         hres = S_OK;
1442     }
1443
1444     IHlinkFrame_Release(hlink_frame);
1445     IBindCtx_Release(bindctx);
1446     IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
1447     return hres;
1448 }
1449
1450 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
1451 {
1452     WCHAR url[INTERNET_MAX_URL_LENGTH];
1453     nsWineURI *uri;
1454     HRESULT hres;
1455
1456     if(!new_url) {
1457         *url = 0;
1458     }else if(base_url) {
1459         DWORD len = 0;
1460
1461         hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
1462                 url, sizeof(url)/sizeof(WCHAR), &len, 0);
1463         if(FAILED(hres))
1464             return hres;
1465     }else {
1466         strcpyW(url, new_url);
1467     }
1468
1469     if(window->doc_obj && window->doc_obj->hostui) {
1470         OLECHAR *translated_url = NULL;
1471
1472         hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
1473                 &translated_url);
1474         if(hres == S_OK) {
1475             TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
1476             strcpyW(url, translated_url);
1477             CoTaskMemFree(translated_url);
1478         }
1479     }
1480
1481     if(window->doc_obj && window == window->doc_obj->basedoc.window) {
1482         BOOL cancel;
1483
1484         hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
1485         if(FAILED(hres))
1486             return hres;
1487
1488         if(cancel) {
1489             TRACE("Navigation handled by hlink frame\n");
1490             return S_OK;
1491         }
1492     }
1493
1494     hres = create_doc_uri(window, url, &uri);
1495     if(FAILED(hres))
1496         return hres;
1497
1498     hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
1499     nsISupports_Release((nsISupports*)uri);
1500     return hres;
1501 }