mshtml: Split IHTMLPrivateWindow::SuperNavigate 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 HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
630 {
631     const WCHAR *header, *header_end, *colon, *value;
632     HRESULT hres;
633
634     header = headers;
635     while(*header) {
636         if(header[0] == '\r' && header[1] == '\n' && !header[2])
637             break;
638         for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
639         if(*colon != ':')
640             return E_FAIL;
641
642         value = colon+1;
643         while(*value == ' ')
644             value++;
645         if(!*value)
646             return E_FAIL;
647
648         for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
649
650         hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
651         if(FAILED(hres))
652             return hres;
653
654         header = header_end;
655         if(header[0] == '\r' && header[1] == '\n')
656             header += 2;
657     }
658
659     return S_OK;
660 }
661
662 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
663 {
664     IStream *str = NULL;
665     HRESULT hres;
666
667     TRACE("(%p %p %p %p)\n", window, doc, bscallback, bctx);
668
669     bscallback->doc = doc;
670
671     /* NOTE: IE7 calls IsSystemMoniker here*/
672
673     if(window) {
674         if(bscallback->mon != window->mon)
675             set_current_mon(window, bscallback->mon);
676         call_docview_84(window->doc_obj);
677     }
678
679     if(bctx) {
680         RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
681         IBindCtx_AddRef(bctx);
682     }else {
683         hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
684         if(FAILED(hres)) {
685             WARN("CreateAsyncBindCtx failed: %08x\n", hres);
686             bscallback->vtbl->stop_binding(bscallback, hres);
687             return hres;
688         }
689     }
690
691     hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
692     IBindCtx_Release(bctx);
693     if(FAILED(hres)) {
694         WARN("BindToStorage failed: %08x\n", hres);
695         bscallback->vtbl->stop_binding(bscallback, hres);
696         return hres;
697     }
698
699     if(str)
700         IStream_Release(str);
701
702     IMoniker_Release(bscallback->mon);
703     bscallback->mon = NULL;
704
705     return S_OK;
706 }
707
708 typedef struct {
709     BSCallback bsc;
710
711     DWORD size;
712     BYTE *buf;
713     HRESULT hres;
714 } BufferBSC;
715
716 static inline BufferBSC *BufferBSC_from_BSCallback(BSCallback *iface)
717 {
718     return CONTAINING_RECORD(iface, BufferBSC, bsc);
719 }
720
721 static void BufferBSC_destroy(BSCallback *bsc)
722 {
723     BufferBSC *This = BufferBSC_from_BSCallback(bsc);
724
725     heap_free(This->buf);
726     heap_free(This);
727 }
728
729 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
730 {
731     return S_OK;
732 }
733
734 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
735 {
736     return S_OK;
737 }
738
739 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
740 {
741     BufferBSC *This = BufferBSC_from_BSCallback(bsc);
742
743     This->hres = result;
744
745     if(FAILED(result)) {
746         heap_free(This->buf);
747         This->buf = NULL;
748         This->size = 0;
749     }
750
751     return S_OK;
752 }
753
754 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
755 {
756     BufferBSC *This = BufferBSC_from_BSCallback(bsc);
757     DWORD readed;
758     HRESULT hres;
759
760     if(!This->buf) {
761         This->size = 128;
762         This->buf = heap_alloc(This->size);
763     }
764
765     do {
766         if(This->bsc.readed == This->size) {
767             This->size <<= 1;
768             This->buf = heap_realloc(This->buf, This->size);
769         }
770
771         readed = 0;
772         hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
773         This->bsc.readed += readed;
774     }while(hres == S_OK);
775
776     return S_OK;
777 }
778
779 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
780 {
781     return S_OK;
782 }
783
784 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
785         LPCWSTR response_headers)
786 {
787     return S_OK;
788 }
789
790 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
791 {
792     return S_FALSE;
793 }
794
795 static const BSCallbackVtbl BufferBSCVtbl = {
796     BufferBSC_destroy,
797     BufferBSC_init_bindinfo,
798     BufferBSC_start_binding,
799     BufferBSC_stop_binding,
800     BufferBSC_read_data,
801     BufferBSC_on_progress,
802     BufferBSC_on_response,
803     BufferBSC_beginning_transaction
804 };
805
806
807 static BufferBSC *create_bufferbsc(IMoniker *mon)
808 {
809     BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
810
811     init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
812     ret->hres = E_FAIL;
813
814     return ret;
815 }
816
817 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
818 {
819     BufferBSC *bsc = create_bufferbsc(mon);
820     HRESULT hres;
821
822     *buf = NULL;
823
824     hres = start_binding(NULL, doc, &bsc->bsc, NULL);
825     if(SUCCEEDED(hres)) {
826         hres = bsc->hres;
827         if(SUCCEEDED(hres)) {
828             *buf = bsc->buf;
829             bsc->buf = NULL;
830             *size = bsc->bsc.readed;
831             bsc->size = 0;
832         }
833     }
834
835     IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
836
837     return hres;
838 }
839
840 static HRESULT read_post_data_stream(nsChannelBSC *This, nsChannel *nschannel)
841 {
842     PRUint32 data_len = 0, available = 0;
843     char *data, *post_data;
844     nsresult nsres;
845     HRESULT hres = S_OK;
846
847     if(!nschannel->post_data_stream)
848         return S_OK;
849
850     nsres =  nsIInputStream_Available(nschannel->post_data_stream, &available);
851     if(NS_FAILED(nsres))
852         return E_FAIL;
853
854     post_data = data = GlobalAlloc(0, available);
855     if(!data)
856         return E_OUTOFMEMORY;
857
858     nsres = nsIInputStream_Read(nschannel->post_data_stream, data, available, &data_len);
859     if(NS_FAILED(nsres)) {
860         GlobalFree(data);
861         return E_FAIL;
862     }
863
864     if(nschannel->post_data_contains_headers) {
865         if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
866             post_data = data+2;
867             data_len -= 2;
868         }else {
869             WCHAR *headers;
870             DWORD size;
871             char *ptr;
872
873             post_data += data_len;
874             for(ptr = data; ptr+4 < data+data_len; ptr++) {
875                 if(!memcmp(ptr, "\r\n\r\n", 4)) {
876                     post_data = ptr+4;
877                     break;
878                 }
879             }
880
881             data_len -= post_data-data;
882
883             size = MultiByteToWideChar(CP_ACP, 0, data, post_data-data, NULL, 0);
884             headers = heap_alloc((size+1)*sizeof(WCHAR));
885             if(headers) {
886                 MultiByteToWideChar(CP_ACP, 0, data, post_data-data, headers, size);
887                 headers[size] = 0;
888                 hres = parse_headers(headers , &nschannel->request_headers);
889                 if(SUCCEEDED(hres))
890                     This->bsc.headers = headers;
891                 else
892                     heap_free(headers);
893             }else {
894                 hres = E_OUTOFMEMORY;
895             }
896         }
897     }
898
899     if(FAILED(hres)) {
900         GlobalFree(data);
901         return hres;
902     }
903
904     if(!data_len) {
905         GlobalFree(data);
906         post_data = NULL;
907     }else if(post_data != data) {
908         char *new_data;
909
910         new_data = GlobalAlloc(0, data_len);
911         if(new_data)
912             memcpy(new_data, post_data, data_len);
913         GlobalFree(data);
914         if(!new_data)
915             return E_OUTOFMEMORY;
916         post_data = new_data;
917     }
918
919     This->bsc.post_data = post_data;
920     This->bsc.post_data_len = data_len;
921     TRACE("post_data = %s\n", debugstr_a(This->bsc.post_data));
922     return S_OK;
923 }
924
925 static HRESULT on_start_nsrequest(nsChannelBSC *This)
926 {
927     nsresult nsres;
928
929     /* FIXME: it's needed for http connections from BindToObject. */
930     if(!This->nschannel->response_status)
931         This->nschannel->response_status = 200;
932
933     nsres = nsIStreamListener_OnStartRequest(This->nslistener,
934             (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
935     if(NS_FAILED(nsres)) {
936         FIXME("OnStartRequest failed: %08x\n", nsres);
937         return E_FAIL;
938     }
939
940     if(This->window) {
941         list_remove(&This->bsc.entry);
942         list_init(&This->bsc.entry);
943         update_window_doc(This->window);
944         if(This->window->doc != This->bsc.doc)
945             This->bsc.doc = This->window->doc;
946         list_add_head(&This->bsc.doc->bindings, &This->bsc.entry);
947         if(This->window->readystate != READYSTATE_LOADING)
948             set_ready_state(This->window, READYSTATE_LOADING);
949     }
950
951     return S_OK;
952 }
953
954 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
955 {
956     nsresult nsres, request_result;
957
958     switch(result) {
959     case S_OK:
960         request_result = NS_OK;
961         break;
962     case E_ABORT:
963         request_result = NS_BINDING_ABORTED;
964         break;
965     default:
966         request_result = NS_ERROR_FAILURE;
967     }
968
969     if(This->nslistener) {
970         nsres = nsIStreamListener_OnStopRequest(This->nslistener,
971                  (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
972                  request_result);
973         if(NS_FAILED(nsres))
974             WARN("OnStopRequet failed: %08x\n", nsres);
975     }
976
977     if(This->nschannel->load_group) {
978         nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
979                 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
980         if(NS_FAILED(nsres))
981             ERR("RemoveRequest failed: %08x\n", nsres);
982     }
983 }
984
985 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
986 {
987     DWORD read;
988     nsresult nsres;
989     HRESULT hres;
990
991     if(!This->nslistener) {
992         BYTE buf[1024];
993
994         do {
995             read = 0;
996             hres = IStream_Read(stream, buf, sizeof(buf), &read);
997         }while(hres == S_OK && read);
998
999         return S_OK;
1000     }
1001
1002     if(!This->nsstream)
1003         This->nsstream = create_nsprotocol_stream();
1004
1005     do {
1006         read = 0;
1007         hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
1008                 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1009         if(!read)
1010             break;
1011
1012         This->nsstream->buf_size += read;
1013
1014         if(!This->bsc.readed) {
1015             if(This->nsstream->buf_size >= 2
1016                && (BYTE)This->nsstream->buf[0] == 0xff
1017                && (BYTE)This->nsstream->buf[1] == 0xfe)
1018                 This->nschannel->charset = heap_strdupA(UTF16_STR);
1019
1020             if(!This->nschannel->content_type) {
1021                 WCHAR *mime;
1022
1023                 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
1024                 if(FAILED(hres))
1025                     return hres;
1026
1027                 TRACE("Found MIME %s\n", debugstr_w(mime));
1028
1029                 This->nschannel->content_type = heap_strdupWtoA(mime);
1030                 CoTaskMemFree(mime);
1031                 if(!This->nschannel->content_type)
1032                     return E_OUTOFMEMORY;
1033             }
1034
1035             on_start_nsrequest(This);
1036         }
1037
1038         This->bsc.readed += This->nsstream->buf_size;
1039
1040         nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1041                 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1042                 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1043                 This->nsstream->buf_size);
1044         if(NS_FAILED(nsres))
1045             ERR("OnDataAvailable failed: %08x\n", nsres);
1046
1047         if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1048             ERR("buffer is full\n");
1049             break;
1050         }
1051     }while(hres == S_OK);
1052
1053     return S_OK;
1054 }
1055
1056 typedef struct {
1057     nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1058
1059     LONG ref;
1060
1061     nsChannel *nschannel;
1062     nsChannelBSC *bsc;
1063 } nsRedirectCallback;
1064
1065 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1066 {
1067     return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1068 }
1069
1070 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1071         nsIIDRef riid, void **result)
1072 {
1073     nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1074
1075     if(IsEqualGUID(&IID_nsISupports, riid)) {
1076         TRACE("(%p)->(IID_nsISuports %p)\n", This, result);
1077         *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1078     }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1079         TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1080         *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1081     }else {
1082         *result = NULL;
1083         WARN("unimplemented iface %s\n", debugstr_guid(riid));
1084         return NS_NOINTERFACE;
1085     }
1086
1087     nsISupports_AddRef((nsISupports*)*result);
1088     return NS_OK;
1089 }
1090
1091 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1092 {
1093     nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1094     LONG ref = InterlockedIncrement(&This->ref);
1095
1096     TRACE("(%p) ref=%d\n", This, ref);
1097
1098     return ref;
1099 }
1100
1101 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1102 {
1103     nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1104     LONG ref = InterlockedDecrement(&This->ref);
1105
1106     TRACE("(%p) ref=%d\n", This, ref);
1107
1108     if(!ref) {
1109         IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1110         nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1111         heap_free(This);
1112     }
1113
1114     return ref;
1115 }
1116
1117 static nsresult NSAPI nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1118 {
1119     nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1120
1121     TRACE("(%p)->(%08x)\n", This, result);
1122
1123     if(This->bsc->nschannel)
1124         nsIHttpChannel_Release(&This->bsc->nschannel->nsIHttpChannel_iface);
1125     nsIChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1126     This->bsc->nschannel = This->nschannel;
1127
1128     if(This->nschannel->load_group) {
1129         nsresult nsres;
1130
1131         nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1132                 NULL);
1133         if(NS_FAILED(nsres))
1134             ERR("AddRequest failed: %08x\n", nsres);
1135     }
1136
1137     return NS_OK;
1138 }
1139
1140 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1141     nsAsyncVerifyRedirectCallback_QueryInterface,
1142     nsAsyncVerifyRedirectCallback_AddRef,
1143     nsAsyncVerifyRedirectCallback_Release,
1144     nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1145 };
1146
1147 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1148 {
1149     nsRedirectCallback *callback;
1150
1151     callback = heap_alloc(sizeof(*callback));
1152     if(!callback)
1153         return E_OUTOFMEMORY;
1154
1155     callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1156     callback->ref = 1;
1157
1158     nsIChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1159     callback->nschannel = nschannel;
1160
1161     IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1162     callback->bsc = bsc;
1163
1164     *ret = callback;
1165     return S_OK;
1166 }
1167
1168 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1169 {
1170     return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1171 }
1172
1173 static void nsChannelBSC_destroy(BSCallback *bsc)
1174 {
1175     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1176
1177     if(This->nschannel)
1178         nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1179     if(This->nslistener)
1180         nsIStreamListener_Release(This->nslistener);
1181     if(This->nscontext)
1182         nsISupports_Release(This->nscontext);
1183     if(This->nsstream)
1184         nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1185     heap_free(This);
1186 }
1187
1188 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1189 {
1190     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1191
1192     if(This->window)
1193         This->window->doc->skip_mutation_notif = FALSE;
1194
1195     return S_OK;
1196 }
1197
1198 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1199 {
1200     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1201     HRESULT hres;
1202
1203     if(This->nschannel && This->nschannel->post_data_stream) {
1204         hres = read_post_data_stream(This, This->nschannel);
1205         if(FAILED(hres))
1206             return hres;
1207     }
1208
1209     return S_OK;
1210 }
1211
1212 typedef struct {
1213     task_t header;
1214     nsChannelBSC *bsc;
1215 } stop_request_task_t;
1216
1217 static void stop_request_proc(task_t *_task)
1218 {
1219     stop_request_task_t *task = (stop_request_task_t*)_task;
1220
1221     TRACE("(%p)\n", task->bsc);
1222
1223     list_remove(&task->bsc->bsc.entry);
1224     list_init(&task->bsc->bsc.entry);
1225     on_stop_nsrequest(task->bsc, S_OK);
1226 }
1227
1228 static void stop_request_task_destr(task_t *_task)
1229 {
1230     stop_request_task_t *task = (stop_request_task_t*)_task;
1231
1232     IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1233     heap_free(task);
1234 }
1235
1236 static HRESULT async_stop_request(nsChannelBSC *This)
1237 {
1238     stop_request_task_t *task;
1239
1240     if(!This->bsc.readed) {
1241         TRACE("No data read, calling OnStartRequest\n");
1242         on_start_nsrequest(This);
1243     }
1244
1245     task = heap_alloc(sizeof(*task));
1246     if(!task)
1247         return E_OUTOFMEMORY;
1248
1249     IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1250     task->bsc = This;
1251     push_task(&task->header, stop_request_proc, stop_request_task_destr, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1252     return S_OK;
1253 }
1254
1255 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1256 {
1257     HTMLDocumentObj *doc;
1258     IOleCommandTarget *olecmd;
1259     BOOL is_error_url;
1260     SAFEARRAY *sa;
1261     SAFEARRAYBOUND bound;
1262     VARIANT var, varOut;
1263     LONG ind;
1264     BSTR unk;
1265     HRESULT hres;
1266
1267     if(!This->window)
1268         return;
1269
1270     doc = This->window->doc_obj;
1271     if(!doc || !doc->doc_object_service || !doc->client)
1272         return;
1273
1274     hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1275             This->window->url, &is_error_url);
1276     if(FAILED(hres) || is_error_url)
1277         return;
1278
1279     hres = IOleClientSite_QueryInterface(doc->client,
1280             &IID_IOleCommandTarget, (void**)&olecmd);
1281     if(FAILED(hres))
1282         return;
1283
1284     bound.lLbound = 0;
1285     bound.cElements = 8;
1286     sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1287     if(!sa) {
1288         IOleCommandTarget_Release(olecmd);
1289         return;
1290     }
1291
1292     ind = 0;
1293     V_VT(&var) = VT_I4;
1294     V_I4(&var) = result;
1295     SafeArrayPutElement(sa, &ind, &var);
1296
1297     ind = 1;
1298     V_VT(&var) = VT_BSTR;
1299     V_BSTR(&var) = This->window->url;
1300     SafeArrayPutElement(sa, &ind, &var);
1301
1302     ind = 3;
1303     V_VT(&var) = VT_UNKNOWN;
1304     V_UNKNOWN(&var) = (IUnknown*)&This->window->IHTMLWindow2_iface;
1305     SafeArrayPutElement(sa, &ind, &var);
1306
1307     /* FIXME: what are the following fields for? */
1308     ind = 2;
1309     V_VT(&var) = VT_UNKNOWN;
1310     V_UNKNOWN(&var) = NULL;
1311     SafeArrayPutElement(sa, &ind, &var);
1312
1313     ind = 4;
1314     V_VT(&var) = VT_BOOL;
1315     V_BOOL(&var) = FALSE;
1316     SafeArrayPutElement(sa, &ind, &var);
1317
1318     ind = 5;
1319     V_VT(&var) = VT_BOOL;
1320     V_BOOL(&var) = FALSE;
1321     SafeArrayPutElement(sa, &ind, &var);
1322
1323     ind = 6;
1324     V_VT(&var) = VT_BSTR;
1325     unk = SysAllocString(NULL);
1326     V_BSTR(&var) = unk;
1327     SafeArrayPutElement(sa, &ind, &var);
1328
1329     ind = 7;
1330     V_VT(&var) = VT_UNKNOWN;
1331     V_UNKNOWN(&var) = NULL;
1332     SafeArrayPutElement(sa, &ind, &var);
1333
1334     V_VT(&var) = VT_ARRAY;
1335     V_ARRAY(&var) = sa;
1336     V_VT(&varOut) = VT_BOOL;
1337     V_BOOL(&varOut) = VARIANT_TRUE;
1338     IOleCommandTarget_Exec(olecmd, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1339
1340     SysFreeString(unk);
1341     SafeArrayDestroy(sa);
1342     IOleCommandTarget_Release(olecmd);
1343 }
1344
1345 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1346 {
1347     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1348
1349     if(result != E_ABORT) {
1350         if(FAILED(result))
1351             handle_navigation_error(This, result);
1352         else if(This->window) {
1353             result = async_stop_request(This);
1354             if(SUCCEEDED(result))
1355                 return S_OK;
1356         }
1357     }
1358
1359     on_stop_nsrequest(This, result);
1360     return S_OK;
1361 }
1362
1363 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1364 {
1365     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1366
1367     return read_stream_data(This, stream);
1368 }
1369
1370 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1371 {
1372     nsRedirectCallback *callback;
1373     nsIChannelEventSink *sink;
1374     nsChannel *new_channel;
1375     nsresult nsres;
1376     HRESULT hres;
1377
1378     TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1379
1380     if(!This->nschannel || !This->nschannel->notif_callback)
1381         return S_OK;
1382
1383     nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1384     if(NS_FAILED(nsres))
1385         return S_OK;
1386
1387     hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1388     if(SUCCEEDED(hres)) {
1389         hres = create_redirect_callback(new_channel, This, &callback);
1390         nsIChannel_Release(&new_channel->nsIHttpChannel_iface);
1391     }
1392
1393     if(SUCCEEDED(hres)) {
1394         nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1395                 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1396                 &callback->nsIAsyncVerifyRedirectCallback_iface);
1397
1398         if(NS_FAILED(nsres))
1399             FIXME("AsyncOnChannelRedirect failed: %08x\n", hres);
1400         else if(This->nschannel != callback->nschannel)
1401             FIXME("nschannel not updated\n");
1402
1403         nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1404     }
1405
1406     nsIChannelEventSink_Release(sink);
1407     return hres;
1408 }
1409
1410 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1411 {
1412     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1413
1414     switch(status_code) {
1415     case BINDSTATUS_MIMETYPEAVAILABLE:
1416         if(!This->nschannel)
1417             return S_OK;
1418
1419         heap_free(This->nschannel->content_type);
1420         This->nschannel->content_type = heap_strdupWtoA(status_text);
1421         break;
1422     case BINDSTATUS_REDIRECTING:
1423         return handle_redirect(This, status_text);
1424     case BINDSTATUS_BEGINDOWNLOADDATA: {
1425         IWinInetHttpInfo *http_info;
1426         DWORD status, size = sizeof(DWORD);
1427         HRESULT hres;
1428
1429         if(!This->bsc.binding)
1430             break;
1431
1432         hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1433         if(FAILED(hres))
1434             break;
1435
1436         hres = IWinInetHttpInfo_QueryInfo(http_info,
1437                 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1438         IWinInetHttpInfo_Release(http_info);
1439         if(FAILED(hres) || status == HTTP_STATUS_OK)
1440             break;
1441
1442         handle_navigation_error(This, status);
1443     }
1444     }
1445
1446     return S_OK;
1447 }
1448
1449 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1450         LPCWSTR response_headers)
1451 {
1452     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1453     HRESULT hres;
1454
1455     This->nschannel->response_status = response_code;
1456
1457     if(response_headers) {
1458         const WCHAR *headers;
1459
1460         headers = strchrW(response_headers, '\r');
1461         if(headers && headers[1] == '\n') {
1462             headers += 2;
1463             hres = parse_headers(headers, &This->nschannel->response_headers);
1464             if(FAILED(hres)) {
1465                 WARN("parsing headers failed: %08x\n", hres);
1466                 return hres;
1467             }
1468         }
1469     }
1470
1471     return S_OK;
1472 }
1473
1474 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1475 {
1476     nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1477     http_header_t *iter;
1478     DWORD len = 0;
1479     WCHAR *ptr;
1480
1481     static const WCHAR content_lengthW[] =
1482         {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1483
1484     if(!This->nschannel)
1485         return S_FALSE;
1486
1487     LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1488         if(strcmpW(iter->header, content_lengthW))
1489             len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1490     }
1491
1492     if(!len)
1493         return S_OK;
1494
1495     *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1496     if(!ptr)
1497         return E_OUTOFMEMORY;
1498
1499     LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1500         if(!strcmpW(iter->header, content_lengthW))
1501             continue;
1502
1503         len = strlenW(iter->header);
1504         memcpy(ptr, iter->header, len*sizeof(WCHAR));
1505         ptr += len;
1506
1507         *ptr++ = ':';
1508         *ptr++ = ' ';
1509
1510         len = strlenW(iter->data);
1511         memcpy(ptr, iter->data, len*sizeof(WCHAR));
1512         ptr += len;
1513
1514         *ptr++ = '\r';
1515         *ptr++ = '\n';
1516     }
1517
1518     *ptr = 0;
1519
1520     return S_OK;
1521 }
1522
1523 static const BSCallbackVtbl nsChannelBSCVtbl = {
1524     nsChannelBSC_destroy,
1525     nsChannelBSC_init_bindinfo,
1526     nsChannelBSC_start_binding,
1527     nsChannelBSC_stop_binding,
1528     nsChannelBSC_read_data,
1529     nsChannelBSC_on_progress,
1530     nsChannelBSC_on_response,
1531     nsChannelBSC_beginning_transaction
1532 };
1533
1534 HRESULT create_channelbsc(IMoniker *mon, const WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1535 {
1536     nsChannelBSC *ret;
1537
1538     ret = heap_alloc_zero(sizeof(*ret));
1539     if(!ret)
1540         return E_OUTOFMEMORY;
1541
1542     init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1543
1544     if(headers) {
1545         ret->bsc.headers = heap_strdupW(headers);
1546         if(!ret->bsc.headers) {
1547             IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1548             return E_OUTOFMEMORY;
1549         }
1550     }
1551
1552     if(post_data) {
1553         ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1554         if(!ret->bsc.post_data) {
1555             heap_free(ret->bsc.headers);
1556             IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1557             return E_OUTOFMEMORY;
1558         }
1559
1560         memcpy(ret->bsc.post_data, post_data, post_data_size);
1561         ret->bsc.post_data_len = post_data_size;
1562     }
1563
1564     *retval = ret;
1565     return S_OK;
1566 }
1567
1568 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1569 {
1570     if(window->bscallback) {
1571         if(window->bscallback->bsc.binding)
1572             IBinding_Abort(window->bscallback->bsc.binding);
1573         window->bscallback->bsc.doc = NULL;
1574         window->bscallback->window = NULL;
1575         IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1576     }
1577
1578     window->bscallback = callback;
1579
1580     if(callback) {
1581         callback->window = window;
1582         IBindStatusCallback_AddRef(&callback->bsc.IBindStatusCallback_iface);
1583         callback->bsc.doc = window->doc;
1584     }
1585 }
1586
1587 typedef struct {
1588     task_t header;
1589     HTMLWindow *window;
1590     nsChannelBSC *bscallback;
1591 } start_doc_binding_task_t;
1592
1593 static void start_doc_binding_proc(task_t *_task)
1594 {
1595     start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1596
1597     start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1598 }
1599
1600 static void start_doc_binding_task_destr(task_t *_task)
1601 {
1602     start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1603
1604     IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1605     heap_free(task);
1606 }
1607
1608 HRESULT async_start_doc_binding(HTMLWindow *window, nsChannelBSC *bscallback)
1609 {
1610     start_doc_binding_task_t *task;
1611
1612     TRACE("%p\n", bscallback);
1613
1614     task = heap_alloc(sizeof(start_doc_binding_task_t));
1615     if(!task)
1616         return E_OUTOFMEMORY;
1617
1618     task->window = window;
1619     task->bscallback = bscallback;
1620     IBindStatusCallback_AddRef(&bscallback->bsc.IBindStatusCallback_iface);
1621
1622     push_task(&task->header, start_doc_binding_proc, start_doc_binding_task_destr, window->task_magic);
1623     return S_OK;
1624 }
1625
1626 void abort_document_bindings(HTMLDocumentNode *doc)
1627 {
1628     BSCallback *iter, *next;
1629
1630     LIST_FOR_EACH_ENTRY_SAFE(iter, next, &doc->bindings, BSCallback, entry) {
1631         TRACE("Aborting %p\n", iter);
1632
1633         if(iter->doc)
1634             remove_target_tasks(iter->doc->basedoc.task_magic);
1635
1636         if(iter->binding)
1637             IBinding_Abort(iter->binding);
1638         else {
1639             list_remove(&iter->entry);
1640             list_init(&iter->entry);
1641             iter->vtbl->stop_binding(iter, E_ABORT);
1642         }
1643
1644         iter->doc = NULL;
1645     }
1646 }
1647
1648 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1649 {
1650     HRESULT hres = S_OK;
1651
1652     if(!bscallback->nschannel) {
1653         ERR("NULL nschannel\n");
1654         return E_FAIL;
1655     }
1656
1657     bscallback->nschannel->content_type = heap_strdupA("text/html");
1658     if(!bscallback->nschannel->content_type)
1659         return E_OUTOFMEMORY;
1660
1661     list_add_head(&bscallback->bsc.doc->bindings, &bscallback->bsc.entry);
1662     if(stream)
1663         hres = read_stream_data(bscallback, stream);
1664     if(SUCCEEDED(hres))
1665         hres = async_stop_request(bscallback);
1666     if(FAILED(hres))
1667         IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1668                 ERROR_SUCCESS);
1669
1670     return hres;
1671 }
1672
1673 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1674 {
1675     nsIChannel_AddRef(&channel->nsIHttpChannel_iface);
1676     This->nschannel = channel;
1677
1678     nsIStreamListener_AddRef(listener);
1679     This->nslistener = listener;
1680
1681     if(context) {
1682         nsISupports_AddRef(context);
1683         This->nscontext = context;
1684     }
1685
1686     if(This->bsc.headers) {
1687         HRESULT hres;
1688
1689         hres = parse_headers(This->bsc.headers, &channel->request_headers);
1690         heap_free(This->bsc.headers);
1691         This->bsc.headers = NULL;
1692         if(FAILED(hres))
1693             WARN("parse_headers failed: %08x\n", hres);
1694     }
1695 }
1696
1697 typedef struct {
1698     task_t header;
1699     HTMLWindow *window;
1700     IUri *uri;
1701 } navigate_javascript_task_t;
1702
1703 static void navigate_javascript_proc(task_t *_task)
1704 {
1705     navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1706     HTMLWindow *window = task->window;
1707     VARIANT v;
1708     BSTR code;
1709     HRESULT hres;
1710
1711     static const WCHAR jscriptW[] = {'j','s','c','r','i','p','t',0};
1712
1713     task->window->readystate = READYSTATE_COMPLETE;
1714
1715     hres = IUri_GetPath(task->uri, &code);
1716     if(FAILED(hres))
1717         return;
1718
1719     set_download_state(window->doc_obj, 1);
1720
1721     V_VT(&v) = VT_EMPTY;
1722     hres = exec_script(window, code, jscriptW, &v);
1723     SysFreeString(code);
1724     if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
1725         FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
1726         VariantClear(&v);
1727     }
1728
1729     if(window->doc_obj->view_sink)
1730         IAdviseSink_OnViewChange(window->doc_obj->view_sink, DVASPECT_CONTENT, -1);
1731
1732     set_download_state(window->doc_obj, 0);
1733 }
1734
1735 static void navigate_javascript_task_destr(task_t *_task)
1736 {
1737     navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1738
1739     IUri_Release(task->uri);
1740     heap_free(task);
1741 }
1742
1743 typedef struct {
1744     task_t header;
1745     HTMLWindow *window;
1746     nsChannelBSC *bscallback;
1747     IMoniker *mon;
1748 } navigate_task_t;
1749
1750 static void navigate_proc(task_t *_task)
1751 {
1752     navigate_task_t *task = (navigate_task_t*)_task;
1753     HRESULT hres;
1754
1755     hres = set_moniker(&task->window->doc_obj->basedoc, task->mon, NULL, task->bscallback, TRUE);
1756     if(SUCCEEDED(hres))
1757         start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1758 }
1759
1760 static void navigate_task_destr(task_t *_task)
1761 {
1762     navigate_task_t *task = (navigate_task_t*)_task;
1763
1764     IUnknown_Release((IUnknown*)task->bscallback);
1765     IMoniker_Release(task->mon);
1766     heap_free(task);
1767 }
1768
1769 HRESULT super_navigate(HTMLWindow *window, IUri *uri, const WCHAR *headers, BYTE *post_data, DWORD post_data_size)
1770 {
1771     nsChannelBSC *bsc;
1772     IMoniker *mon;
1773     DWORD scheme;
1774     HRESULT hres;
1775
1776     if(window->doc_obj->client) {
1777         IOleCommandTarget *cmdtrg;
1778
1779         hres = IOleClientSite_QueryInterface(window->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1780         if(SUCCEEDED(hres)) {
1781             VARIANT in, out;
1782             BSTR url_str;
1783
1784             hres = IUri_GetDisplayUri(uri, &url_str);
1785             if(SUCCEEDED(hres)) {
1786                 V_VT(&in) = VT_BSTR;
1787                 V_BSTR(&in) = url_str;
1788                 V_VT(&out) = VT_BOOL;
1789                 V_BOOL(&out) = VARIANT_TRUE;
1790                 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
1791                 IOleCommandTarget_Release(cmdtrg);
1792                 if(SUCCEEDED(hres))
1793                     VariantClear(&out);
1794                 SysFreeString(url_str);
1795             }
1796         }
1797     }
1798
1799     hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1800     if(FAILED(hres))
1801         return hres;
1802
1803     /* FIXME: Why not set_ready_state? */
1804     window->readystate = READYSTATE_UNINITIALIZED;
1805
1806     hres = create_channelbsc(mon, headers, post_data, post_data_size, &bsc);
1807     if(FAILED(hres)) {
1808         IMoniker_Release(mon);
1809         return hres;
1810     }
1811
1812     prepare_for_binding(&window->doc_obj->basedoc, mon, TRUE);
1813
1814     hres = IUri_GetScheme(uri, &scheme);
1815
1816     if(scheme != URL_SCHEME_JAVASCRIPT) {
1817         navigate_task_t *task;
1818
1819         task = heap_alloc(sizeof(*task));
1820         if(!task) {
1821             IUnknown_Release((IUnknown*)bsc);
1822             IMoniker_Release(mon);
1823             return E_OUTOFMEMORY;
1824         }
1825
1826         task->window = window;
1827         task->bscallback = bsc;
1828         task->mon = mon;
1829         push_task(&task->header, navigate_proc, navigate_task_destr, window->task_magic);
1830
1831         /* Silently and repeated when real loading starts? */
1832         window->readystate = READYSTATE_LOADING;
1833     }else {
1834         navigate_javascript_task_t *task;
1835
1836         IUnknown_Release((IUnknown*)bsc);
1837         IMoniker_Release(mon);
1838
1839         task = heap_alloc(sizeof(*task));
1840         if(!task)
1841             return E_OUTOFMEMORY;
1842
1843         IUri_AddRef(uri);
1844         task->window = window;
1845         task->uri = uri;
1846         push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, window->task_magic);
1847
1848         /* Why silently? */
1849         window->readystate = READYSTATE_COMPLETE;
1850     }
1851
1852     return S_OK;
1853 }
1854
1855 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
1856 {
1857     IHlinkFrame *hlink_frame;
1858     nsChannelBSC *callback;
1859     IServiceProvider *sp;
1860     IBindCtx *bindctx;
1861     IMoniker *mon;
1862     IHlink *hlink;
1863     HRESULT hres;
1864
1865     *cancel = FALSE;
1866
1867     hres = IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IServiceProvider,
1868             (void**)&sp);
1869     if(FAILED(hres))
1870         return S_OK;
1871
1872     hres = IServiceProvider_QueryService(sp, &IID_IHlinkFrame, &IID_IHlinkFrame,
1873             (void**)&hlink_frame);
1874     IServiceProvider_Release(sp);
1875     if(FAILED(hres))
1876         return S_OK;
1877
1878     hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
1879     if(FAILED(hres)) {
1880         IHlinkFrame_Release(hlink_frame);
1881         return hres;
1882     }
1883
1884     if(nschannel)
1885         read_post_data_stream(callback, nschannel);
1886
1887     hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
1888     if(SUCCEEDED(hres))
1889         hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
1890                 &IID_IHlink, (LPVOID*)&hlink);
1891
1892     if(SUCCEEDED(hres))
1893         hres = CreateURLMoniker(NULL, url, &mon);
1894
1895     if(SUCCEEDED(hres)) {
1896         IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
1897
1898         if(hlnf & HLNF_OPENINNEWWINDOW) {
1899             static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1900             IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1901         }
1902
1903         hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
1904                 &callback->bsc.IBindStatusCallback_iface, hlink);
1905         IMoniker_Release(mon);
1906         *cancel = hres == S_OK;
1907         hres = S_OK;
1908     }
1909
1910     IHlinkFrame_Release(hlink_frame);
1911     IBindCtx_Release(bindctx);
1912     IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
1913     return hres;
1914 }
1915
1916 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
1917 {
1918     WCHAR url[INTERNET_MAX_URL_LENGTH];
1919     nsWineURI *uri;
1920     HRESULT hres;
1921
1922     if(!new_url) {
1923         *url = 0;
1924     }else if(base_url) {
1925         DWORD len = 0;
1926
1927         hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
1928                 url, sizeof(url)/sizeof(WCHAR), &len, 0);
1929         if(FAILED(hres))
1930             return hres;
1931     }else {
1932         strcpyW(url, new_url);
1933     }
1934
1935     if(window->doc_obj && window->doc_obj->hostui) {
1936         OLECHAR *translated_url = NULL;
1937
1938         hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
1939                 &translated_url);
1940         if(hres == S_OK) {
1941             TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
1942             strcpyW(url, translated_url);
1943             CoTaskMemFree(translated_url);
1944         }
1945     }
1946
1947     if(window->doc_obj && window == window->doc_obj->basedoc.window) {
1948         BOOL cancel;
1949
1950         hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
1951         if(FAILED(hres))
1952             return hres;
1953
1954         if(cancel) {
1955             TRACE("Navigation handled by hlink frame\n");
1956             return S_OK;
1957         }
1958     }
1959
1960     hres = create_doc_uri(window, url, &uri);
1961     if(FAILED(hres))
1962         return hres;
1963
1964     hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
1965     nsISupports_Release((nsISupports*)uri);
1966     return hres;
1967 }