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