mshtml: Use do_query_service in QueryService implementation.
[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
39 #include "mshtml_private.h"
40 #include "binding.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
43
44 #define CONTENT_LENGTH "Content-Length"
45 #define UTF16_STR "utf-16"
46
47 struct nsProtocolStream {
48     nsIInputStream nsIInputStream_iface;
49
50     LONG ref;
51
52     char buf[1024];
53     DWORD buf_size;
54 };
55
56 struct BSCallbackVtbl {
57     void (*destroy)(BSCallback*);
58     HRESULT (*init_bindinfo)(BSCallback*);
59     HRESULT (*start_binding)(BSCallback*);
60     HRESULT (*stop_binding)(BSCallback*,HRESULT);
61     HRESULT (*read_data)(BSCallback*,IStream*);
62     HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
63     HRESULT (*on_response)(BSCallback*,DWORD,LPCWSTR);
64     HRESULT (*beginning_transaction)(BSCallback*,WCHAR**);
65 };
66
67 static inline nsProtocolStream *impl_from_nsIInputStream(nsIInputStream *iface)
68 {
69     return CONTAINING_RECORD(iface, nsProtocolStream, nsIInputStream_iface);
70 }
71
72 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
73         void **result)
74 {
75     nsProtocolStream *This = impl_from_nsIInputStream(iface);
76
77     *result = NULL;
78
79     if(IsEqualGUID(&IID_nsISupports, riid)) {
80         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
81         *result  = &This->nsIInputStream_iface;
82     }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
83         TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
84         *result  = &This->nsIInputStream_iface;
85     }
86
87     if(*result) {
88         nsIInputStream_AddRef(&This->nsIInputStream_iface);
89         return NS_OK;
90     }
91
92     WARN("unsupported interface %s\n", debugstr_guid(riid));
93     return NS_NOINTERFACE;
94 }
95
96 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
97 {
98     nsProtocolStream *This = impl_from_nsIInputStream(iface);
99     LONG ref = InterlockedIncrement(&This->ref);
100
101     TRACE("(%p) ref=%d\n", This, ref);
102
103     return ref;
104 }
105
106
107 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
108 {
109     nsProtocolStream *This = impl_from_nsIInputStream(iface);
110     LONG ref = InterlockedDecrement(&This->ref);
111
112     TRACE("(%p) ref=%d\n", This, ref);
113
114     if(!ref)
115         heap_free(This);
116
117     return ref;
118 }
119
120 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
121 {
122     nsProtocolStream *This = impl_from_nsIInputStream(iface);
123     FIXME("(%p)\n", This);
124     return NS_ERROR_NOT_IMPLEMENTED;
125 }
126
127 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
128 {
129     nsProtocolStream *This = impl_from_nsIInputStream(iface);
130     FIXME("(%p)->(%p)\n", This, _retval);
131     return NS_ERROR_NOT_IMPLEMENTED;
132 }
133
134 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
135                                          PRUint32 *_retval)
136 {
137     nsProtocolStream *This = impl_from_nsIInputStream(iface);
138     DWORD read = aCount;
139
140     TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
141
142     if(read > This->buf_size)
143         read = This->buf_size;
144
145     if(read) {
146         memcpy(aBuf, This->buf, read);
147         if(read < This->buf_size)
148             memmove(This->buf, This->buf+read, This->buf_size-read);
149         This->buf_size -= read;
150     }
151
152     *_retval = read;
153     return NS_OK;
154 }
155
156 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
157         nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
158         void *aClousure, PRUint32 aCount, PRUint32 *_retval)
159 {
160     nsProtocolStream *This = impl_from_nsIInputStream(iface);
161     PRUint32 written = 0;
162     nsresult nsres;
163
164     TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
165
166     if(!This->buf_size)
167         return S_OK;
168
169     if(aCount > This->buf_size)
170         aCount = This->buf_size;
171
172     nsres = aWriter(&This->nsIInputStream_iface, aClousure, This->buf, 0, aCount, &written);
173     if(NS_FAILED(nsres))
174         TRACE("aWritter failed: %08x\n", nsres);
175     else if(written != This->buf_size)
176         FIXME("written %d != buf_size %d\n", written, This->buf_size);
177
178     This->buf_size -= written; 
179
180     *_retval = written;
181     return nsres;
182 }
183
184 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
185 {
186     nsProtocolStream *This = impl_from_nsIInputStream(iface);
187     FIXME("(%p)->(%p)\n", This, _retval);
188     return NS_ERROR_NOT_IMPLEMENTED;
189 }
190
191 static const nsIInputStreamVtbl nsInputStreamVtbl = {
192     nsInputStream_QueryInterface,
193     nsInputStream_AddRef,
194     nsInputStream_Release,
195     nsInputStream_Close,
196     nsInputStream_Available,
197     nsInputStream_Read,
198     nsInputStream_ReadSegments,
199     nsInputStream_IsNonBlocking
200 };
201
202 static nsProtocolStream *create_nsprotocol_stream(void)
203 {
204     nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
205
206     ret->nsIInputStream_iface.lpVtbl = &nsInputStreamVtbl;
207     ret->ref = 1;
208     ret->buf_size = 0;
209
210     return ret;
211 }
212
213 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
214 {
215     return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
216 }
217
218 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
219         REFIID riid, void **ppv)
220 {
221     BSCallback *This = impl_from_IBindStatusCallback(iface);
222
223     *ppv = NULL;
224     if(IsEqualGUID(&IID_IUnknown, riid)) {
225         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
226         *ppv = &This->IBindStatusCallback_iface;
227     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
228         TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
229         *ppv = &This->IBindStatusCallback_iface;
230     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
231         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
232         *ppv = &This->IServiceProvider_iface;
233     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
234         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
235         *ppv = &This->IHttpNegotiate2_iface;
236     }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
237         TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
238         *ppv = &This->IHttpNegotiate2_iface;
239     }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
240         TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
241         *ppv = &This->IInternetBindInfo_iface;
242     }
243
244     if(*ppv) {
245         IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
246         return S_OK;
247     }
248
249     TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
250     return E_NOINTERFACE;
251 }
252
253 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
254 {
255     BSCallback *This = impl_from_IBindStatusCallback(iface);
256     LONG ref = InterlockedIncrement(&This->ref);
257
258     TRACE("(%p) ref = %d\n", This, ref);
259
260     return ref;
261 }
262
263 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
264 {
265     BSCallback *This = impl_from_IBindStatusCallback(iface);
266     LONG ref = InterlockedDecrement(&This->ref);
267
268     TRACE("(%p) ref = %d\n", This, ref);
269
270     if(!ref) {
271         if(This->post_data)
272             GlobalFree(This->post_data);
273         if(This->mon)
274             IMoniker_Release(This->mon);
275         if(This->binding)
276             IBinding_Release(This->binding);
277         list_remove(&This->entry);
278         list_init(&This->entry);
279         heap_free(This->headers);
280
281         This->vtbl->destroy(This);
282     }
283
284     return ref;
285 }
286
287 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
288         DWORD dwReserved, IBinding *pbind)
289 {
290     BSCallback *This = impl_from_IBindStatusCallback(iface);
291
292     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
293
294     IBinding_AddRef(pbind);
295     This->binding = pbind;
296
297     if(This->doc)
298         list_add_head(&This->doc->bindings, &This->entry);
299
300     return This->vtbl->start_binding(This);
301 }
302
303 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
304 {
305     BSCallback *This = impl_from_IBindStatusCallback(iface);
306     FIXME("(%p)->(%p)\n", This, pnPriority);
307     return E_NOTIMPL;
308 }
309
310 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
311 {
312     BSCallback *This = impl_from_IBindStatusCallback(iface);
313     FIXME("(%p)->(%d)\n", This, reserved);
314     return E_NOTIMPL;
315 }
316
317 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
318         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
319 {
320     BSCallback *This = impl_from_IBindStatusCallback(iface);
321
322     TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
323             debugstr_w(szStatusText));
324
325     return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
326 }
327
328 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
329         HRESULT hresult, LPCWSTR szError)
330 {
331     BSCallback *This = impl_from_IBindStatusCallback(iface);
332     HRESULT hres;
333
334     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
335
336     /* NOTE: IE7 calls GetBindResult here */
337
338     hres = This->vtbl->stop_binding(This, hresult);
339
340     if(This->binding) {
341         IBinding_Release(This->binding);
342         This->binding = NULL;
343     }
344
345     list_remove(&This->entry);
346     list_init(&This->entry);
347     This->doc = NULL;
348
349     return hres;
350 }
351
352 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
353         DWORD *grfBINDF, BINDINFO *pbindinfo)
354 {
355     BSCallback *This = impl_from_IBindStatusCallback(iface);
356     DWORD size;
357
358     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
359
360     if(!This->bindinfo_ready) {
361         HRESULT hres;
362
363         hres = This->vtbl->init_bindinfo(This);
364         if(FAILED(hres))
365             return hres;
366
367         This->bindinfo_ready = TRUE;
368     }
369
370     *grfBINDF = This->bindf;
371
372     size = pbindinfo->cbSize;
373     memset(pbindinfo, 0, size);
374     pbindinfo->cbSize = size;
375
376     pbindinfo->cbstgmedData = This->post_data_len;
377     pbindinfo->dwCodePage = CP_UTF8;
378     pbindinfo->dwOptions = 0x80000;
379
380     if(This->post_data) {
381         pbindinfo->dwBindVerb = BINDVERB_POST;
382
383         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
384         pbindinfo->stgmedData.u.hGlobal = This->post_data;
385         pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
386         IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
387     }
388
389     return S_OK;
390 }
391
392 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
393         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
394 {
395     BSCallback *This = impl_from_IBindStatusCallback(iface);
396
397     TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
398
399     return This->vtbl->read_data(This, pstgmed->u.pstm);
400 }
401
402 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
403         REFIID riid, IUnknown *punk)
404 {
405     BSCallback *This = impl_from_IBindStatusCallback(iface);
406     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
407     return E_NOTIMPL;
408 }
409
410 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
411     BindStatusCallback_QueryInterface,
412     BindStatusCallback_AddRef,
413     BindStatusCallback_Release,
414     BindStatusCallback_OnStartBinding,
415     BindStatusCallback_GetPriority,
416     BindStatusCallback_OnLowResource,
417     BindStatusCallback_OnProgress,
418     BindStatusCallback_OnStopBinding,
419     BindStatusCallback_GetBindInfo,
420     BindStatusCallback_OnDataAvailable,
421     BindStatusCallback_OnObjectAvailable
422 };
423
424 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
425 {
426     return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
427 }
428
429 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
430                                                    REFIID riid, void **ppv)
431 {
432     BSCallback *This = impl_from_IHttpNegotiate2(iface);
433     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
434 }
435
436 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
437 {
438     BSCallback *This = impl_from_IHttpNegotiate2(iface);
439     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
440 }
441
442 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
443 {
444     BSCallback *This = impl_from_IHttpNegotiate2(iface);
445     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
446 }
447
448 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
449         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
450 {
451     BSCallback *This = impl_from_IHttpNegotiate2(iface);
452     HRESULT hres;
453
454     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
455           dwReserved, pszAdditionalHeaders);
456
457     *pszAdditionalHeaders = NULL;
458
459     hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
460     if(hres != S_FALSE)
461         return hres;
462
463     if(This->headers) {
464         DWORD size;
465
466         size = (strlenW(This->headers)+1)*sizeof(WCHAR);
467         *pszAdditionalHeaders = CoTaskMemAlloc(size);
468         if(!*pszAdditionalHeaders)
469             return E_OUTOFMEMORY;
470         memcpy(*pszAdditionalHeaders, This->headers, size);
471     }
472
473     return S_OK;
474 }
475
476 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
477         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
478 {
479     BSCallback *This = impl_from_IHttpNegotiate2(iface);
480
481     TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
482           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
483
484     return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
485 }
486
487 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
488         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
489 {
490     BSCallback *This = impl_from_IHttpNegotiate2(iface);
491     FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
492     return E_NOTIMPL;
493 }
494
495 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
496     HttpNegotiate_QueryInterface,
497     HttpNegotiate_AddRef,
498     HttpNegotiate_Release,
499     HttpNegotiate_BeginningTransaction,
500     HttpNegotiate_OnResponse,
501     HttpNegotiate_GetRootSecurityId
502 };
503
504 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
505 {
506     return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
507 }
508
509 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
510                                                       REFIID riid, void **ppv)
511 {
512     BSCallback *This = impl_from_IInternetBindInfo(iface);
513     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
514 }
515
516 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
517 {
518     BSCallback *This = impl_from_IInternetBindInfo(iface);
519     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
520 }
521
522 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
523 {
524     BSCallback *This = impl_from_IInternetBindInfo(iface);
525     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
526 }
527
528 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
529                                                    DWORD *grfBINDF, BINDINFO *pbindinfo)
530 {
531     BSCallback *This = impl_from_IInternetBindInfo(iface);
532     FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
533     return E_NOTIMPL;
534 }
535
536 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
537         ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
538 {
539     BSCallback *This = impl_from_IInternetBindInfo(iface);
540     FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
541     return E_NOTIMPL;
542 }
543
544 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
545     InternetBindInfo_QueryInterface,
546     InternetBindInfo_AddRef,
547     InternetBindInfo_Release,
548     InternetBindInfo_GetBindInfo,
549     InternetBindInfo_GetBindString
550 };
551
552 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
553 {
554     return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
555 }
556
557 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
558                                                         REFIID riid, void **ppv)
559 {
560     BSCallback *This = impl_from_IServiceProvider(iface);
561     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
562 }
563
564 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
565 {
566     BSCallback *This = impl_from_IServiceProvider(iface);
567     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
568 }
569
570 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
571 {
572     BSCallback *This = impl_from_IServiceProvider(iface);
573     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
574 }
575
576 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
577         REFGUID guidService, REFIID riid, void **ppv)
578 {
579     BSCallback *This = impl_from_IServiceProvider(iface);
580     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
581     return E_NOINTERFACE;
582 }
583
584 static const IServiceProviderVtbl ServiceProviderVtbl = {
585     BSCServiceProvider_QueryInterface,
586     BSCServiceProvider_AddRef,
587     BSCServiceProvider_Release,
588     BSCServiceProvider_QueryService
589 };
590
591 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
592 {
593     This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
594     This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
595     This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
596     This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
597     This->vtbl = vtbl;
598     This->ref = 1;
599     This->bindf = bindf;
600
601     list_init(&This->entry);
602
603     if(mon)
604         IMoniker_AddRef(mon);
605     This->mon = mon;
606 }
607
608 /* Calls undocumented 84 cmd of CGID_ShellDocView */
609 static void call_docview_84(HTMLDocumentObj *doc)
610 {
611     IOleCommandTarget *olecmd;
612     VARIANT var;
613     HRESULT hres;
614
615     if(!doc->client)
616         return;
617
618     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
619     if(FAILED(hres))
620         return;
621
622     VariantInit(&var);
623     hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
624     IOleCommandTarget_Release(olecmd);
625     if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
626         FIXME("handle result\n");
627 }
628
629 static void parse_content_type(nsChannelBSC *This, const WCHAR *value)
630 {
631     const WCHAR *ptr;
632     size_t len;
633
634     static const WCHAR charsetW[] = {'c','h','a','r','s','e','t','='};
635
636     ptr = strchrW(value, ';');
637     if(!ptr)
638         return;
639
640     ptr++;
641     while(*ptr && isspaceW(*ptr))
642         ptr++;
643
644     len = strlenW(value);
645     if(ptr + sizeof(charsetW)/sizeof(WCHAR) < value+len && !memicmpW(ptr, charsetW, sizeof(charsetW)/sizeof(WCHAR))) {
646         size_t charset_len, lena;
647         nsACString charset_str;
648         const WCHAR *charset;
649         char *charseta;
650
651         ptr += sizeof(charsetW)/sizeof(WCHAR);
652
653         if(*ptr == '\'') {
654             FIXME("Quoted value\n");
655             return;
656         }else {
657             charset = ptr;
658             while(*ptr && *ptr != ',')
659                 ptr++;
660             charset_len = ptr-charset;
661         }
662
663         lena = WideCharToMultiByte(CP_ACP, 0, charset, charset_len, NULL, 0, NULL, NULL);
664         charseta = heap_alloc(lena+1);
665         if(!charseta)
666             return;
667
668         WideCharToMultiByte(CP_ACP, 0, charset, charset_len, charseta, lena, NULL, NULL);
669         charseta[lena] = 0;
670
671         nsACString_InitDepend(&charset_str, charseta);
672         nsIHttpChannel_SetContentCharset(&This->nschannel->nsIHttpChannel_iface, &charset_str);
673         nsACString_Finish(&charset_str);
674         heap_free(charseta);
675     }else {
676         FIXME("unhandled: %s\n", debugstr_wn(ptr, len - (ptr-value)));
677     }
678 }
679
680 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
681 {
682     const WCHAR *header, *header_end, *colon, *value;
683     HRESULT hres;
684
685     header = headers;
686     while(*header) {
687         if(header[0] == '\r' && header[1] == '\n' && !header[2])
688             break;
689         for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
690         if(*colon != ':')
691             return E_FAIL;
692
693         value = colon+1;
694         while(*value == ' ')
695             value++;
696         if(!*value)
697             return E_FAIL;
698
699         for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
700
701         hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
702         if(FAILED(hres))
703             return hres;
704
705         header = header_end;
706         if(header[0] == '\r' && header[1] == '\n')
707             header += 2;
708     }
709
710     return S_OK;
711 }
712
713 static HRESULT process_response_headers(nsChannelBSC *This, const WCHAR *headers)
714 {
715     http_header_t *iter;
716     HRESULT hres;
717
718     static const WCHAR content_typeW[] = {'c','o','n','t','e','n','t','-','t','y','p','e',0};
719
720     hres = parse_headers(headers, &This->nschannel->response_headers);
721     if(FAILED(hres))
722         return hres;
723
724     LIST_FOR_EACH_ENTRY(iter, &This->nschannel->response_headers, http_header_t, entry) {
725         if(!strcmpiW(iter->header, content_typeW))
726             parse_content_type(This, iter->data);
727     }
728
729     return S_OK;
730 }
731
732 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
733 {
734     IStream *str = NULL;
735     HRESULT hres;
736
737     TRACE("(%p %p %p %p)\n", window, doc, bscallback, bctx);
738
739     bscallback->doc = doc;
740
741     /* NOTE: IE7 calls IsSystemMoniker here*/
742
743     if(window) {
744         if(bscallback->mon != window->mon)
745             set_current_mon(window, bscallback->mon);
746         call_docview_84(window->doc_obj);
747     }
748
749     if(bctx) {
750         RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
751         IBindCtx_AddRef(bctx);
752     }else {
753         hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
754         if(FAILED(hres)) {
755             WARN("CreateAsyncBindCtx failed: %08x\n", hres);
756             bscallback->vtbl->stop_binding(bscallback, hres);
757             return hres;
758         }
759     }
760
761     hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
762     IBindCtx_Release(bctx);
763     if(FAILED(hres)) {
764         WARN("BindToStorage failed: %08x\n", hres);
765         bscallback->vtbl->stop_binding(bscallback, hres);
766         return hres;
767     }
768
769     if(str)
770         IStream_Release(str);
771
772     IMoniker_Release(bscallback->mon);
773     bscallback->mon = NULL;
774
775     return S_OK;
776 }
777
778 typedef struct {
779     BSCallback bsc;
780
781     DWORD size;
782     BYTE *buf;
783     HRESULT hres;
784 } BufferBSC;
785
786 static inline BufferBSC *BufferBSC_from_BSCallback(BSCallback *iface)
787 {
788     return CONTAINING_RECORD(iface, BufferBSC, bsc);
789 }
790
791 static void BufferBSC_destroy(BSCallback *bsc)
792 {
793     BufferBSC *This = BufferBSC_from_BSCallback(bsc);
794
795     heap_free(This->buf);
796     heap_free(This);
797 }
798
799 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
800 {
801     return S_OK;
802 }
803
804 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
805 {
806     return S_OK;
807 }
808
809 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
810 {
811     BufferBSC *This = BufferBSC_from_BSCallback(bsc);
812
813     This->hres = result;
814
815     if(FAILED(result)) {
816         heap_free(This->buf);
817         This->buf = NULL;
818         This->size = 0;
819     }
820
821     return S_OK;
822 }
823
824 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
825 {
826     BufferBSC *This = BufferBSC_from_BSCallback(bsc);
827     DWORD readed;
828     HRESULT hres;
829
830     if(!This->buf) {
831         This->size = 128;
832         This->buf = heap_alloc(This->size);
833     }
834
835     do {
836         if(This->bsc.readed == This->size) {
837             This->size <<= 1;
838             This->buf = heap_realloc(This->buf, This->size);
839         }
840
841         readed = 0;
842         hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
843         This->bsc.readed += readed;
844     }while(hres == S_OK);
845
846     return S_OK;
847 }
848
849 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
850 {
851     return S_OK;
852 }
853
854 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
855         LPCWSTR response_headers)
856 {
857     return S_OK;
858 }
859
860 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
861 {
862     return S_FALSE;
863 }
864
865 static const BSCallbackVtbl BufferBSCVtbl = {
866     BufferBSC_destroy,
867     BufferBSC_init_bindinfo,
868     BufferBSC_start_binding,
869     BufferBSC_stop_binding,
870     BufferBSC_read_data,
871     BufferBSC_on_progress,
872     BufferBSC_on_response,
873     BufferBSC_beginning_transaction
874 };
875
876
877 static BufferBSC *create_bufferbsc(IMoniker *mon)
878 {
879     BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
880
881     init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
882     ret->hres = E_FAIL;
883
884     return ret;
885 }
886
887 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
888 {
889     BufferBSC *bsc = create_bufferbsc(mon);
890     HRESULT hres;
891
892     *buf = NULL;
893
894     hres = start_binding(NULL, doc, &bsc->bsc, NULL);
895     if(SUCCEEDED(hres)) {
896         hres = bsc->hres;
897         if(SUCCEEDED(hres)) {
898             *buf = bsc->buf;
899             bsc->buf = NULL;
900             *size = bsc->bsc.readed;
901             bsc->size = 0;
902         }
903     }
904
905     IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
906
907     return hres;
908 }
909
910 static HRESULT read_post_data_stream(nsChannelBSC *This, nsChannel *nschannel)
911 {
912     PRUint32 data_len = 0, available = 0;
913     char *data, *post_data;
914     nsresult nsres;
915     HRESULT hres = S_OK;
916
917     if(!nschannel->post_data_stream)
918         return S_OK;
919
920     nsres =  nsIInputStream_Available(nschannel->post_data_stream, &available);
921     if(NS_FAILED(nsres))
922         return E_FAIL;
923
924     post_data = data = GlobalAlloc(0, available);
925     if(!data)
926         return E_OUTOFMEMORY;
927
928     nsres = nsIInputStream_Read(nschannel->post_data_stream, data, available, &data_len);
929     if(NS_FAILED(nsres)) {
930         GlobalFree(data);
931         return E_FAIL;
932     }
933
934     if(nschannel->post_data_contains_headers) {
935         if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
936             post_data = data+2;
937             data_len -= 2;
938         }else {
939             WCHAR *headers;
940             DWORD size;
941             char *ptr;
942
943             post_data += data_len;
944             for(ptr = data; ptr+4 < data+data_len; ptr++) {
945                 if(!memcmp(ptr, "\r\n\r\n", 4)) {
946                     post_data = ptr+4;
947                     break;
948                 }
949             }
950
951             data_len -= post_data-data;
952
953             size = MultiByteToWideChar(CP_ACP, 0, data, post_data-data, NULL, 0);
954             headers = heap_alloc((size+1)*sizeof(WCHAR));
955             if(headers) {
956                 MultiByteToWideChar(CP_ACP, 0, data, post_data-data, headers, size);
957                 headers[size] = 0;
958                 hres = parse_headers(headers , &nschannel->request_headers);
959                 if(SUCCEEDED(hres))
960                     This->bsc.headers = headers;
961                 else
962                     heap_free(headers);
963             }else {
964                 hres = E_OUTOFMEMORY;
965             }
966         }
967     }
968
969     if(FAILED(hres)) {
970         GlobalFree(data);
971         return hres;
972     }
973
974     if(!data_len) {
975         GlobalFree(data);
976         post_data = NULL;
977     }else if(post_data != data) {
978         char *new_data;
979
980         new_data = GlobalAlloc(0, data_len);
981         if(new_data)
982             memcpy(new_data, post_data, data_len);
983         GlobalFree(data);
984         if(!new_data)
985             return E_OUTOFMEMORY;
986         post_data = new_data;
987     }
988
989     This->bsc.post_data = post_data;
990     This->bsc.post_data_len = data_len;
991     TRACE("post_data = %s\n", debugstr_a(This->bsc.post_data));
992     return S_OK;
993 }
994
995 static HRESULT on_start_nsrequest(nsChannelBSC *This)
996 {
997     nsresult nsres;
998
999     /* FIXME: it's needed for http connections from BindToObject. */
1000     if(!This->nschannel->response_status)
1001         This->nschannel->response_status = 200;
1002
1003     nsres = nsIStreamListener_OnStartRequest(This->nslistener,
1004             (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
1005     if(NS_FAILED(nsres)) {
1006         FIXME("OnStartRequest failed: %08x\n", nsres);
1007         return E_FAIL;
1008     }
1009
1010     if(This->window) {
1011         list_remove(&This->bsc.entry);
1012         list_init(&This->bsc.entry);
1013         update_window_doc(This->window);
1014         if(This->window->doc != This->bsc.doc)
1015             This->bsc.doc = This->window->doc;
1016         list_add_head(&This->bsc.doc->bindings, &This->bsc.entry);
1017         if(This->window->readystate != READYSTATE_LOADING)
1018             set_ready_state(This->window, READYSTATE_LOADING);
1019     }
1020
1021     return S_OK;
1022 }
1023
1024 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
1025 {
1026     nsresult nsres, request_result;
1027
1028     switch(result) {
1029     case S_OK:
1030         request_result = NS_OK;
1031         break;
1032     case E_ABORT:
1033         request_result = NS_BINDING_ABORTED;
1034         break;
1035     default:
1036         request_result = NS_ERROR_FAILURE;
1037     }
1038
1039     if(This->nslistener) {
1040         nsres = nsIStreamListener_OnStopRequest(This->nslistener,
1041                  (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1042                  request_result);
1043         if(NS_FAILED(nsres))
1044             WARN("OnStopRequet failed: %08x\n", nsres);
1045     }
1046
1047     if(This->nschannel->load_group) {
1048         nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1049                 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
1050         if(NS_FAILED(nsres))
1051             ERR("RemoveRequest failed: %08x\n", nsres);
1052     }
1053 }
1054
1055 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
1056 {
1057     DWORD read;
1058     nsresult nsres;
1059     HRESULT hres;
1060
1061     if(!This->nslistener) {
1062         BYTE buf[1024];
1063
1064         do {
1065             read = 0;
1066             hres = IStream_Read(stream, buf, sizeof(buf), &read);
1067         }while(hres == S_OK && read);
1068
1069         return S_OK;
1070     }
1071
1072     if(!This->nsstream)
1073         This->nsstream = create_nsprotocol_stream();
1074
1075     do {
1076         read = 0;
1077         hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
1078                 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1079         if(!read)
1080             break;
1081
1082         This->nsstream->buf_size += read;
1083
1084         if(!This->bsc.readed) {
1085             if(This->nsstream->buf_size >= 2
1086                && (BYTE)This->nsstream->buf[0] == 0xff
1087                && (BYTE)This->nsstream->buf[1] == 0xfe)
1088                 This->nschannel->charset = heap_strdupA(UTF16_STR);
1089
1090             if(!This->nschannel->content_type) {
1091                 WCHAR *mime;
1092
1093                 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
1094                 if(FAILED(hres))
1095                     return hres;
1096
1097                 TRACE("Found MIME %s\n", debugstr_w(mime));
1098
1099                 This->nschannel->content_type = heap_strdupWtoA(mime);
1100                 CoTaskMemFree(mime);
1101                 if(!This->nschannel->content_type)
1102                     return E_OUTOFMEMORY;
1103             }
1104
1105             on_start_nsrequest(This);
1106         }
1107
1108         This->bsc.readed += This->nsstream->buf_size;
1109
1110         nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1111                 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1112                 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1113                 This->nsstream->buf_size);
1114         if(NS_FAILED(nsres))
1115             ERR("OnDataAvailable failed: %08x\n", nsres);
1116
1117         if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1118             ERR("buffer is full\n");
1119             break;
1120         }
1121     }while(hres == S_OK);
1122
1123     return S_OK;
1124 }
1125
1126 typedef struct {
1127     nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1128
1129     LONG ref;
1130
1131     nsChannel *nschannel;
1132     nsChannelBSC *bsc;
1133 } nsRedirectCallback;
1134
1135 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1136 {
1137     return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1138 }
1139
1140 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1141         nsIIDRef riid, void **result)
1142 {
1143     nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1144
1145     if(IsEqualGUID(&IID_nsISupports, riid)) {
1146         TRACE("(%p)->(IID_nsISuports %p)\n", This, result);
1147         *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1148     }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1149         TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1150         *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1151     }else {
1152         *result = NULL;
1153         WARN("unimplemented iface %s\n", debugstr_guid(riid));
1154         return NS_NOINTERFACE;
1155     }
1156
1157     nsISupports_AddRef((nsISupports*)*result);
1158     return NS_OK;
1159 }
1160
1161 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1162 {
1163     nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1164     LONG ref = InterlockedIncrement(&This->ref);
1165
1166     TRACE("(%p) ref=%d\n", This, ref);
1167
1168     return ref;
1169 }
1170
1171 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1172 {
1173     nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1174     LONG ref = InterlockedDecrement(&This->ref);
1175
1176     TRACE("(%p) ref=%d\n", This, ref);
1177
1178     if(!ref) {
1179         IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1180         nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1181         heap_free(This);
1182     }
1183
1184     return ref;
1185 }
1186
1187 static nsresult NSAPI nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1188 {
1189     nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1190
1191     TRACE("(%p)->(%08x)\n", This, result);
1192
1193     if(This->bsc->nschannel)
1194         nsIHttpChannel_Release(&This->bsc->nschannel->nsIHttpChannel_iface);
1195     nsIChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1196     This->bsc->nschannel = This->nschannel;
1197
1198     if(This->nschannel->load_group) {
1199         nsresult nsres;
1200
1201         nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1202                 NULL);
1203         if(NS_FAILED(nsres))
1204             ERR("AddRequest failed: %08x\n", nsres);
1205     }
1206
1207     if(This->bsc->window) {
1208         IUri *uri = nsuri_get_uri(This->nschannel->uri);
1209
1210         if(uri) {
1211             set_current_uri(This->bsc->window, uri);
1212             IUri_Release(uri);
1213         }else {
1214             WARN("Could not get IUri from nsWineURI\n");
1215         }
1216     }
1217
1218     return NS_OK;
1219 }
1220
1221 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1222     nsAsyncVerifyRedirectCallback_QueryInterface,
1223     nsAsyncVerifyRedirectCallback_AddRef,
1224     nsAsyncVerifyRedirectCallback_Release,
1225     nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1226 };
1227
1228 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1229 {
1230     nsRedirectCallback *callback;
1231
1232     callback = heap_alloc(sizeof(*callback));
1233     if(!callback)
1234         return E_OUTOFMEMORY;
1235
1236     callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1237     callback->ref = 1;
1238
1239     nsIChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1240     callback->nschannel = nschannel;
1241
1242     IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1243     callback->bsc = bsc;
1244
1245     *ret = callback;
1246     return S_OK;
1247 }
1248
1249 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1250 {
1251     return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1252 }
1253
1254 static void nsChannelBSC_destroy(BSCallback *bsc)
1255 {
1256     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1257
1258     if(This->nschannel)
1259         nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1260     if(This->nslistener)
1261         nsIStreamListener_Release(This->nslistener);
1262     if(This->nscontext)
1263         nsISupports_Release(This->nscontext);
1264     if(This->nsstream)
1265         nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1266     heap_free(This);
1267 }
1268
1269 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1270 {
1271     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1272
1273     if(This->window)
1274         This->window->doc->skip_mutation_notif = FALSE;
1275
1276     return S_OK;
1277 }
1278
1279 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1280 {
1281     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1282     HRESULT hres;
1283
1284     if(This->nschannel && This->nschannel->post_data_stream) {
1285         hres = read_post_data_stream(This, This->nschannel);
1286         if(FAILED(hres))
1287             return hres;
1288     }
1289
1290     return S_OK;
1291 }
1292
1293 typedef struct {
1294     task_t header;
1295     nsChannelBSC *bsc;
1296 } stop_request_task_t;
1297
1298 static void stop_request_proc(task_t *_task)
1299 {
1300     stop_request_task_t *task = (stop_request_task_t*)_task;
1301
1302     TRACE("(%p)\n", task->bsc);
1303
1304     list_remove(&task->bsc->bsc.entry);
1305     list_init(&task->bsc->bsc.entry);
1306     on_stop_nsrequest(task->bsc, S_OK);
1307 }
1308
1309 static void stop_request_task_destr(task_t *_task)
1310 {
1311     stop_request_task_t *task = (stop_request_task_t*)_task;
1312
1313     IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1314     heap_free(task);
1315 }
1316
1317 static HRESULT async_stop_request(nsChannelBSC *This)
1318 {
1319     stop_request_task_t *task;
1320
1321     if(!This->bsc.readed) {
1322         TRACE("No data read, calling OnStartRequest\n");
1323         on_start_nsrequest(This);
1324     }
1325
1326     task = heap_alloc(sizeof(*task));
1327     if(!task)
1328         return E_OUTOFMEMORY;
1329
1330     IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1331     task->bsc = This;
1332     push_task(&task->header, stop_request_proc, stop_request_task_destr, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1333     return S_OK;
1334 }
1335
1336 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1337 {
1338     HTMLDocumentObj *doc;
1339     IOleCommandTarget *olecmd;
1340     BOOL is_error_url;
1341     SAFEARRAY *sa;
1342     SAFEARRAYBOUND bound;
1343     VARIANT var, varOut;
1344     LONG ind;
1345     BSTR unk;
1346     HRESULT hres;
1347
1348     if(!This->window)
1349         return;
1350
1351     doc = This->window->doc_obj;
1352     if(!doc || !doc->doc_object_service || !doc->client)
1353         return;
1354
1355     hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1356             This->window->url, &is_error_url);
1357     if(FAILED(hres) || is_error_url)
1358         return;
1359
1360     hres = IOleClientSite_QueryInterface(doc->client,
1361             &IID_IOleCommandTarget, (void**)&olecmd);
1362     if(FAILED(hres))
1363         return;
1364
1365     bound.lLbound = 0;
1366     bound.cElements = 8;
1367     sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1368     if(!sa) {
1369         IOleCommandTarget_Release(olecmd);
1370         return;
1371     }
1372
1373     ind = 0;
1374     V_VT(&var) = VT_I4;
1375     V_I4(&var) = result;
1376     SafeArrayPutElement(sa, &ind, &var);
1377
1378     ind = 1;
1379     V_VT(&var) = VT_BSTR;
1380     V_BSTR(&var) = This->window->url;
1381     SafeArrayPutElement(sa, &ind, &var);
1382
1383     ind = 3;
1384     V_VT(&var) = VT_UNKNOWN;
1385     V_UNKNOWN(&var) = (IUnknown*)&This->window->IHTMLWindow2_iface;
1386     SafeArrayPutElement(sa, &ind, &var);
1387
1388     /* FIXME: what are the following fields for? */
1389     ind = 2;
1390     V_VT(&var) = VT_UNKNOWN;
1391     V_UNKNOWN(&var) = NULL;
1392     SafeArrayPutElement(sa, &ind, &var);
1393
1394     ind = 4;
1395     V_VT(&var) = VT_BOOL;
1396     V_BOOL(&var) = FALSE;
1397     SafeArrayPutElement(sa, &ind, &var);
1398
1399     ind = 5;
1400     V_VT(&var) = VT_BOOL;
1401     V_BOOL(&var) = FALSE;
1402     SafeArrayPutElement(sa, &ind, &var);
1403
1404     ind = 6;
1405     V_VT(&var) = VT_BSTR;
1406     unk = SysAllocString(NULL);
1407     V_BSTR(&var) = unk;
1408     SafeArrayPutElement(sa, &ind, &var);
1409
1410     ind = 7;
1411     V_VT(&var) = VT_UNKNOWN;
1412     V_UNKNOWN(&var) = NULL;
1413     SafeArrayPutElement(sa, &ind, &var);
1414
1415     V_VT(&var) = VT_ARRAY;
1416     V_ARRAY(&var) = sa;
1417     V_VT(&varOut) = VT_BOOL;
1418     V_BOOL(&varOut) = VARIANT_TRUE;
1419     IOleCommandTarget_Exec(olecmd, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1420
1421     SysFreeString(unk);
1422     SafeArrayDestroy(sa);
1423     IOleCommandTarget_Release(olecmd);
1424 }
1425
1426 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1427 {
1428     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1429
1430     if(result != E_ABORT) {
1431         if(FAILED(result))
1432             handle_navigation_error(This, result);
1433         else if(This->window) {
1434             result = async_stop_request(This);
1435             if(SUCCEEDED(result))
1436                 return S_OK;
1437         }
1438     }
1439
1440     on_stop_nsrequest(This, result);
1441     return S_OK;
1442 }
1443
1444 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1445 {
1446     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1447
1448     return read_stream_data(This, stream);
1449 }
1450
1451 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1452 {
1453     nsRedirectCallback *callback;
1454     nsIChannelEventSink *sink;
1455     nsChannel *new_channel;
1456     nsresult nsres;
1457     HRESULT hres;
1458
1459     TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1460
1461     if(!This->nschannel || !This->nschannel->notif_callback)
1462         return S_OK;
1463
1464     nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1465     if(NS_FAILED(nsres))
1466         return S_OK;
1467
1468     hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1469     if(SUCCEEDED(hres)) {
1470         hres = create_redirect_callback(new_channel, This, &callback);
1471         nsIChannel_Release(&new_channel->nsIHttpChannel_iface);
1472     }
1473
1474     if(SUCCEEDED(hres)) {
1475         nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1476                 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1477                 &callback->nsIAsyncVerifyRedirectCallback_iface);
1478
1479         if(NS_FAILED(nsres))
1480             FIXME("AsyncOnChannelRedirect failed: %08x\n", hres);
1481         else if(This->nschannel != callback->nschannel)
1482             FIXME("nschannel not updated\n");
1483
1484         nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1485     }
1486
1487     nsIChannelEventSink_Release(sink);
1488     return hres;
1489 }
1490
1491 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1492 {
1493     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1494
1495     switch(status_code) {
1496     case BINDSTATUS_MIMETYPEAVAILABLE:
1497         if(!This->nschannel)
1498             return S_OK;
1499
1500         heap_free(This->nschannel->content_type);
1501         This->nschannel->content_type = heap_strdupWtoA(status_text);
1502         break;
1503     case BINDSTATUS_REDIRECTING:
1504         return handle_redirect(This, status_text);
1505     case BINDSTATUS_BEGINDOWNLOADDATA: {
1506         IWinInetHttpInfo *http_info;
1507         DWORD status, size = sizeof(DWORD);
1508         HRESULT hres;
1509
1510         if(!This->bsc.binding)
1511             break;
1512
1513         hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1514         if(FAILED(hres))
1515             break;
1516
1517         hres = IWinInetHttpInfo_QueryInfo(http_info,
1518                 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1519         IWinInetHttpInfo_Release(http_info);
1520         if(FAILED(hres) || status == HTTP_STATUS_OK)
1521             break;
1522
1523         handle_navigation_error(This, status);
1524     }
1525     }
1526
1527     return S_OK;
1528 }
1529
1530 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1531         LPCWSTR response_headers)
1532 {
1533     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1534     HRESULT hres;
1535
1536     This->nschannel->response_status = response_code;
1537
1538     if(response_headers) {
1539         const WCHAR *headers;
1540
1541         headers = strchrW(response_headers, '\r');
1542         if(headers && headers[1] == '\n') {
1543             headers += 2;
1544             hres = process_response_headers(This, headers);
1545             if(FAILED(hres)) {
1546                 WARN("parsing headers failed: %08x\n", hres);
1547                 return hres;
1548             }
1549         }
1550     }
1551
1552     return S_OK;
1553 }
1554
1555 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1556 {
1557     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1558     http_header_t *iter;
1559     DWORD len = 0;
1560     WCHAR *ptr;
1561
1562     static const WCHAR content_lengthW[] =
1563         {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1564
1565     if(!This->nschannel)
1566         return S_FALSE;
1567
1568     LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1569         if(strcmpW(iter->header, content_lengthW))
1570             len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1571     }
1572
1573     if(!len)
1574         return S_OK;
1575
1576     *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1577     if(!ptr)
1578         return E_OUTOFMEMORY;
1579
1580     LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1581         if(!strcmpW(iter->header, content_lengthW))
1582             continue;
1583
1584         len = strlenW(iter->header);
1585         memcpy(ptr, iter->header, len*sizeof(WCHAR));
1586         ptr += len;
1587
1588         *ptr++ = ':';
1589         *ptr++ = ' ';
1590
1591         len = strlenW(iter->data);
1592         memcpy(ptr, iter->data, len*sizeof(WCHAR));
1593         ptr += len;
1594
1595         *ptr++ = '\r';
1596         *ptr++ = '\n';
1597     }
1598
1599     *ptr = 0;
1600
1601     return S_OK;
1602 }
1603
1604 static const BSCallbackVtbl nsChannelBSCVtbl = {
1605     nsChannelBSC_destroy,
1606     nsChannelBSC_init_bindinfo,
1607     nsChannelBSC_start_binding,
1608     nsChannelBSC_stop_binding,
1609     nsChannelBSC_read_data,
1610     nsChannelBSC_on_progress,
1611     nsChannelBSC_on_response,
1612     nsChannelBSC_beginning_transaction
1613 };
1614
1615 HRESULT create_channelbsc(IMoniker *mon, const WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1616 {
1617     nsChannelBSC *ret;
1618
1619     ret = heap_alloc_zero(sizeof(*ret));
1620     if(!ret)
1621         return E_OUTOFMEMORY;
1622
1623     init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1624
1625     if(headers) {
1626         ret->bsc.headers = heap_strdupW(headers);
1627         if(!ret->bsc.headers) {
1628             IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1629             return E_OUTOFMEMORY;
1630         }
1631     }
1632
1633     if(post_data) {
1634         ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1635         if(!ret->bsc.post_data) {
1636             heap_free(ret->bsc.headers);
1637             IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1638             return E_OUTOFMEMORY;
1639         }
1640
1641         memcpy(ret->bsc.post_data, post_data, post_data_size);
1642         ret->bsc.post_data_len = post_data_size;
1643     }
1644
1645     *retval = ret;
1646     return S_OK;
1647 }
1648
1649 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1650 {
1651     if(window->bscallback) {
1652         if(window->bscallback->bsc.binding)
1653             IBinding_Abort(window->bscallback->bsc.binding);
1654         window->bscallback->bsc.doc = NULL;
1655         window->bscallback->window = NULL;
1656         IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1657     }
1658
1659     window->bscallback = callback;
1660
1661     if(callback) {
1662         callback->window = window;
1663         IBindStatusCallback_AddRef(&callback->bsc.IBindStatusCallback_iface);
1664         callback->bsc.doc = window->doc;
1665     }
1666 }
1667
1668 typedef struct {
1669     task_t header;
1670     HTMLWindow *window;
1671     nsChannelBSC *bscallback;
1672 } start_doc_binding_task_t;
1673
1674 static void start_doc_binding_proc(task_t *_task)
1675 {
1676     start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1677
1678     start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1679 }
1680
1681 static void start_doc_binding_task_destr(task_t *_task)
1682 {
1683     start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1684
1685     IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1686     heap_free(task);
1687 }
1688
1689 HRESULT async_start_doc_binding(HTMLWindow *window, nsChannelBSC *bscallback)
1690 {
1691     start_doc_binding_task_t *task;
1692
1693     TRACE("%p\n", bscallback);
1694
1695     task = heap_alloc(sizeof(start_doc_binding_task_t));
1696     if(!task)
1697         return E_OUTOFMEMORY;
1698
1699     task->window = window;
1700     task->bscallback = bscallback;
1701     IBindStatusCallback_AddRef(&bscallback->bsc.IBindStatusCallback_iface);
1702
1703     push_task(&task->header, start_doc_binding_proc, start_doc_binding_task_destr, window->task_magic);
1704     return S_OK;
1705 }
1706
1707 void abort_document_bindings(HTMLDocumentNode *doc)
1708 {
1709     BSCallback *iter, *next;
1710
1711     LIST_FOR_EACH_ENTRY_SAFE(iter, next, &doc->bindings, BSCallback, entry) {
1712         TRACE("Aborting %p\n", iter);
1713
1714         if(iter->doc)
1715             remove_target_tasks(iter->doc->basedoc.task_magic);
1716
1717         if(iter->binding)
1718             IBinding_Abort(iter->binding);
1719         else {
1720             list_remove(&iter->entry);
1721             list_init(&iter->entry);
1722             iter->vtbl->stop_binding(iter, E_ABORT);
1723         }
1724
1725         iter->doc = NULL;
1726     }
1727 }
1728
1729 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1730 {
1731     HRESULT hres = S_OK;
1732
1733     if(!bscallback->nschannel) {
1734         ERR("NULL nschannel\n");
1735         return E_FAIL;
1736     }
1737
1738     bscallback->nschannel->content_type = heap_strdupA("text/html");
1739     if(!bscallback->nschannel->content_type)
1740         return E_OUTOFMEMORY;
1741
1742     list_add_head(&bscallback->bsc.doc->bindings, &bscallback->bsc.entry);
1743     if(stream)
1744         hres = read_stream_data(bscallback, stream);
1745     if(SUCCEEDED(hres))
1746         hres = async_stop_request(bscallback);
1747     if(FAILED(hres))
1748         IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1749                 ERROR_SUCCESS);
1750
1751     return hres;
1752 }
1753
1754 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1755 {
1756     nsIChannel_AddRef(&channel->nsIHttpChannel_iface);
1757     This->nschannel = channel;
1758
1759     nsIStreamListener_AddRef(listener);
1760     This->nslistener = listener;
1761
1762     if(context) {
1763         nsISupports_AddRef(context);
1764         This->nscontext = context;
1765     }
1766
1767     if(This->bsc.headers) {
1768         HRESULT hres;
1769
1770         hres = parse_headers(This->bsc.headers, &channel->request_headers);
1771         heap_free(This->bsc.headers);
1772         This->bsc.headers = NULL;
1773         if(FAILED(hres))
1774             WARN("parse_headers failed: %08x\n", hres);
1775     }
1776 }
1777
1778 typedef struct {
1779     task_t header;
1780     HTMLWindow *window;
1781     IUri *uri;
1782 } navigate_javascript_task_t;
1783
1784 static void navigate_javascript_proc(task_t *_task)
1785 {
1786     navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1787     HTMLWindow *window = task->window;
1788     VARIANT v;
1789     BSTR code;
1790     HRESULT hres;
1791
1792     static const WCHAR jscriptW[] = {'j','s','c','r','i','p','t',0};
1793
1794     task->window->readystate = READYSTATE_COMPLETE;
1795
1796     hres = IUri_GetPath(task->uri, &code);
1797     if(FAILED(hres))
1798         return;
1799
1800     set_download_state(window->doc_obj, 1);
1801
1802     V_VT(&v) = VT_EMPTY;
1803     hres = exec_script(window, code, jscriptW, &v);
1804     SysFreeString(code);
1805     if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
1806         FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
1807         VariantClear(&v);
1808     }
1809
1810     if(window->doc_obj->view_sink)
1811         IAdviseSink_OnViewChange(window->doc_obj->view_sink, DVASPECT_CONTENT, -1);
1812
1813     set_download_state(window->doc_obj, 0);
1814 }
1815
1816 static void navigate_javascript_task_destr(task_t *_task)
1817 {
1818     navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1819
1820     IUri_Release(task->uri);
1821     heap_free(task);
1822 }
1823
1824 typedef struct {
1825     task_t header;
1826     HTMLWindow *window;
1827     nsChannelBSC *bscallback;
1828     IMoniker *mon;
1829 } navigate_task_t;
1830
1831 static void navigate_proc(task_t *_task)
1832 {
1833     navigate_task_t *task = (navigate_task_t*)_task;
1834     HRESULT hres;
1835
1836     hres = set_moniker(&task->window->doc_obj->basedoc, task->mon, NULL, task->bscallback, TRUE);
1837     if(SUCCEEDED(hres))
1838         start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1839 }
1840
1841 static void navigate_task_destr(task_t *_task)
1842 {
1843     navigate_task_t *task = (navigate_task_t*)_task;
1844
1845     IUnknown_Release((IUnknown*)task->bscallback);
1846     IMoniker_Release(task->mon);
1847     heap_free(task);
1848 }
1849
1850 static HRESULT navigate_fragment(HTMLWindow *window, IUri *uri)
1851 {
1852     nsIDOMLocation *nslocation;
1853     nsAString nsfrag_str;
1854     BSTR frag;
1855     nsresult nsres;
1856     HRESULT hres;
1857
1858     set_current_uri(window, uri);
1859
1860     nsres = nsIDOMWindow_GetLocation(window->nswindow, &nslocation);
1861     if(FAILED(nsres) || !nslocation)
1862         return E_FAIL;
1863
1864     hres = IUri_GetFragment(uri, &frag);
1865     if(FAILED(hres)) {
1866         nsIDOMLocation_Release(nslocation);
1867         return hres;
1868     }
1869
1870     nsAString_InitDepend(&nsfrag_str, frag);
1871     nsres = nsIDOMLocation_SetHash(nslocation, &nsfrag_str);
1872     nsAString_Finish(&nsfrag_str);
1873     nsIDOMLocation_Release(nslocation);
1874     SysFreeString(frag);
1875     if(NS_FAILED(nsres)) {
1876         ERR("SetHash failed: %08x\n", nsres);
1877         return E_FAIL;
1878     }
1879
1880     if(window->doc_obj->doc_object_service) {
1881         IDocObjectService_FireNavigateComplete2(window->doc_obj->doc_object_service, &window->IHTMLWindow2_iface, 0x10);
1882         IDocObjectService_FireDocumentComplete(window->doc_obj->doc_object_service, &window->IHTMLWindow2_iface, 0);
1883
1884     }
1885
1886     return S_OK;
1887 }
1888
1889 HRESULT super_navigate(HTMLWindow *window, IUri *uri, const WCHAR *headers, BYTE *post_data, DWORD post_data_size)
1890 {
1891     nsChannelBSC *bsc;
1892     IMoniker *mon;
1893     DWORD scheme;
1894     HRESULT hres;
1895
1896     if(window->doc_obj->client) {
1897         IOleCommandTarget *cmdtrg;
1898
1899         hres = IOleClientSite_QueryInterface(window->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1900         if(SUCCEEDED(hres)) {
1901             VARIANT in, out;
1902             BSTR url_str;
1903
1904             hres = IUri_GetDisplayUri(uri, &url_str);
1905             if(SUCCEEDED(hres)) {
1906                 V_VT(&in) = VT_BSTR;
1907                 V_BSTR(&in) = url_str;
1908                 V_VT(&out) = VT_BOOL;
1909                 V_BOOL(&out) = VARIANT_TRUE;
1910                 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
1911                 IOleCommandTarget_Release(cmdtrg);
1912                 if(SUCCEEDED(hres))
1913                     VariantClear(&out);
1914                 SysFreeString(url_str);
1915             }
1916         }
1917     }
1918
1919     if(window->uri && compare_ignoring_frag(window->uri, uri)) {
1920         TRACE("fragment navigate\n");
1921         return navigate_fragment(window, uri);
1922     }
1923
1924     hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1925     if(FAILED(hres))
1926         return hres;
1927
1928     /* FIXME: Why not set_ready_state? */
1929     window->readystate = READYSTATE_UNINITIALIZED;
1930
1931     hres = create_channelbsc(mon, headers, post_data, post_data_size, &bsc);
1932     if(FAILED(hres)) {
1933         IMoniker_Release(mon);
1934         return hres;
1935     }
1936
1937     prepare_for_binding(&window->doc_obj->basedoc, mon, TRUE);
1938
1939     hres = IUri_GetScheme(uri, &scheme);
1940
1941     if(scheme != URL_SCHEME_JAVASCRIPT) {
1942         navigate_task_t *task;
1943
1944         task = heap_alloc(sizeof(*task));
1945         if(!task) {
1946             IUnknown_Release((IUnknown*)bsc);
1947             IMoniker_Release(mon);
1948             return E_OUTOFMEMORY;
1949         }
1950
1951         task->window = window;
1952         task->bscallback = bsc;
1953         task->mon = mon;
1954         push_task(&task->header, navigate_proc, navigate_task_destr, window->task_magic);
1955
1956         /* Silently and repeated when real loading starts? */
1957         window->readystate = READYSTATE_LOADING;
1958     }else {
1959         navigate_javascript_task_t *task;
1960
1961         IUnknown_Release((IUnknown*)bsc);
1962         IMoniker_Release(mon);
1963
1964         task = heap_alloc(sizeof(*task));
1965         if(!task)
1966             return E_OUTOFMEMORY;
1967
1968         IUri_AddRef(uri);
1969         task->window = window;
1970         task->uri = uri;
1971         push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, window->task_magic);
1972
1973         /* Why silently? */
1974         window->readystate = READYSTATE_COMPLETE;
1975     }
1976
1977     return S_OK;
1978 }
1979
1980 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
1981 {
1982     IHlinkFrame *hlink_frame;
1983     nsChannelBSC *callback;
1984     IBindCtx *bindctx;
1985     IMoniker *mon;
1986     IHlink *hlink;
1987     HRESULT hres;
1988
1989     *cancel = FALSE;
1990
1991     hres = do_query_service((IUnknown*)doc->doc_obj->client, &IID_IHlinkFrame, &IID_IHlinkFrame,
1992             (void**)&hlink_frame);
1993     if(FAILED(hres))
1994         return S_OK;
1995
1996     hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
1997     if(FAILED(hres)) {
1998         IHlinkFrame_Release(hlink_frame);
1999         return hres;
2000     }
2001
2002     if(nschannel)
2003         read_post_data_stream(callback, nschannel);
2004
2005     hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
2006     if(SUCCEEDED(hres))
2007         hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
2008                 &IID_IHlink, (LPVOID*)&hlink);
2009
2010     if(SUCCEEDED(hres))
2011         hres = CreateURLMoniker(NULL, url, &mon);
2012
2013     if(SUCCEEDED(hres)) {
2014         IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
2015
2016         if(hlnf & HLNF_OPENINNEWWINDOW) {
2017             static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
2018             IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
2019         }
2020
2021         hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
2022                 &callback->bsc.IBindStatusCallback_iface, hlink);
2023         IMoniker_Release(mon);
2024         *cancel = hres == S_OK;
2025         hres = S_OK;
2026     }
2027
2028     IHlinkFrame_Release(hlink_frame);
2029     IBindCtx_Release(bindctx);
2030     IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
2031     return hres;
2032 }
2033
2034 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
2035 {
2036     WCHAR url[INTERNET_MAX_URL_LENGTH];
2037     nsWineURI *uri;
2038     HRESULT hres;
2039
2040     if(!new_url) {
2041         *url = 0;
2042     }else if(base_url) {
2043         DWORD len = 0;
2044
2045         hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2046                 url, sizeof(url)/sizeof(WCHAR), &len, 0);
2047         if(FAILED(hres))
2048             return hres;
2049     }else {
2050         strcpyW(url, new_url);
2051     }
2052
2053     if(window->doc_obj && window->doc_obj->hostui) {
2054         OLECHAR *translated_url = NULL;
2055
2056         hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
2057                 &translated_url);
2058         if(hres == S_OK) {
2059             TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
2060             strcpyW(url, translated_url);
2061             CoTaskMemFree(translated_url);
2062         }
2063     }
2064
2065     if(window->doc_obj && window->doc_obj->is_webbrowser && window == window->doc_obj->basedoc.window) {
2066         BOOL cancel = FALSE;
2067         IUri *uri;
2068
2069         hres = IDocObjectService_FireBeforeNavigate2(window->doc_obj->doc_object_service, NULL, url, 0x40,
2070                 NULL, NULL, 0, NULL, TRUE, &cancel);
2071         if(SUCCEEDED(hres) && cancel) {
2072             TRACE("Navigation canceled\n");
2073             return S_OK;
2074         }
2075
2076         hres = CreateUri(url, 0, 0, &uri);
2077         if(FAILED(hres))
2078             return hres;
2079
2080         hres = super_navigate(window, uri, NULL, NULL, 0);
2081         IUri_Release(uri);
2082         return hres;
2083     }
2084
2085     if(window->doc_obj && window == window->doc_obj->basedoc.window) {
2086         BOOL cancel;
2087
2088         hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
2089         if(FAILED(hres))
2090             return hres;
2091
2092         if(cancel) {
2093             TRACE("Navigation handled by hlink frame\n");
2094             return S_OK;
2095         }
2096     }
2097
2098     hres = create_doc_uri(window, url, &uri);
2099     if(FAILED(hres))
2100         return hres;
2101
2102     hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
2103     nsISupports_Release((nsISupports*)uri);
2104     return hres;
2105 }