msacm32: acmFormatEnum: Implement ACM_FORMATENUMF_SUGGEST.
[wine] / dlls / mshtml / navigate.c
1 /*
2  * Copyright 2006-2007 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 "ole2.h"
31 #include "hlguids.h"
32 #include "shlguid.h"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 #include "mshtml_private.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40
41 #define CONTENT_LENGTH "Content-Length"
42 #define UTF16_STR "utf-16"
43
44 typedef struct {
45     const nsIInputStreamVtbl *lpInputStreamVtbl;
46
47     LONG ref;
48
49     char buf[1024];
50     DWORD buf_size;
51 } nsProtocolStream;
52
53 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
54
55 typedef struct {
56     void (*destroy)(BSCallback*);
57     HRESULT (*start_binding)(BSCallback*);
58     HRESULT (*stop_binding)(BSCallback*,HRESULT);
59     HRESULT (*read_data)(BSCallback*,IStream*);
60     HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
61 } BSCallbackVtbl;
62
63 struct BSCallback {
64     const IBindStatusCallbackVtbl *lpBindStatusCallbackVtbl;
65     const IServiceProviderVtbl    *lpServiceProviderVtbl;
66     const IHttpNegotiate2Vtbl     *lpHttpNegotiate2Vtbl;
67     const IInternetBindInfoVtbl   *lpInternetBindInfoVtbl;
68
69     const BSCallbackVtbl          *vtbl;
70
71     LONG ref;
72
73     LPWSTR headers;
74     HGLOBAL post_data;
75     ULONG post_data_len;
76     ULONG readed;
77     DWORD bindf;
78
79     IMoniker *mon;
80     IBinding *binding;
81
82     HTMLDocument *doc;
83
84     struct list entry;
85 };
86
87 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
88
89 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
90                                                    nsQIResult result)
91 {
92     nsProtocolStream *This = NSINSTREAM_THIS(iface);
93
94     *result = NULL;
95
96     if(IsEqualGUID(&IID_nsISupports, riid)) {
97         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
98         *result  = NSINSTREAM(This);
99     }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
100         TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
101         *result  = NSINSTREAM(This);
102     }
103
104     if(*result) {
105         nsIInputStream_AddRef(NSINSTREAM(This));
106         return NS_OK;
107     }
108
109     WARN("unsupported interface %s\n", debugstr_guid(riid));
110     return NS_NOINTERFACE;
111 }
112
113 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
114 {
115     nsProtocolStream *This = NSINSTREAM_THIS(iface);
116     LONG ref = InterlockedIncrement(&This->ref);
117
118     TRACE("(%p) ref=%d\n", This, ref);
119
120     return ref;
121 }
122
123
124 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
125 {
126     nsProtocolStream *This = NSINSTREAM_THIS(iface);
127     LONG ref = InterlockedDecrement(&This->ref);
128
129     TRACE("(%p) ref=%d\n", This, ref);
130
131     if(!ref)
132         heap_free(This);
133
134     return ref;
135 }
136
137 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
138 {
139     nsProtocolStream *This = NSINSTREAM_THIS(iface);
140     FIXME("(%p)\n", This);
141     return NS_ERROR_NOT_IMPLEMENTED;
142 }
143
144 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
145 {
146     nsProtocolStream *This = NSINSTREAM_THIS(iface);
147     FIXME("(%p)->(%p)\n", This, _retval);
148     return NS_ERROR_NOT_IMPLEMENTED;
149 }
150
151 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
152                                          PRUint32 *_retval)
153 {
154     nsProtocolStream *This = NSINSTREAM_THIS(iface);
155
156     TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
157
158     /* Gecko always calls Read with big enough buffer */
159     if(aCount < This->buf_size)
160         FIXME("aCount < This->buf_size\n");
161
162     *_retval = This->buf_size;
163     if(This->buf_size)
164         memcpy(aBuf, This->buf, This->buf_size);
165     This->buf_size = 0;
166
167     return NS_OK;
168 }
169
170 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
171         nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
172         void *aClousure, PRUint32 aCount, PRUint32 *_retval)
173 {
174     nsProtocolStream *This = NSINSTREAM_THIS(iface);
175     PRUint32 written = 0;
176     nsresult nsres;
177
178     TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
179
180     if(!This->buf_size)
181         return S_OK;
182
183     if(aCount > This->buf_size)
184         aCount = This->buf_size;
185
186     nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, aCount, &written);
187     if(NS_FAILED(nsres))
188         TRACE("aWritter failed: %08x\n", nsres);
189     else if(written != This->buf_size)
190         FIXME("written %d != buf_size %d\n", written, This->buf_size);
191
192     This->buf_size -= written; 
193
194     *_retval = written;
195     return nsres;
196 }
197
198 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
199 {
200     nsProtocolStream *This = NSINSTREAM_THIS(iface);
201     FIXME("(%p)->(%p)\n", This, _retval);
202     return NS_ERROR_NOT_IMPLEMENTED;
203 }
204
205 #undef NSINSTREAM_THIS
206
207 static const nsIInputStreamVtbl nsInputStreamVtbl = {
208     nsInputStream_QueryInterface,
209     nsInputStream_AddRef,
210     nsInputStream_Release,
211     nsInputStream_Close,
212     nsInputStream_Available,
213     nsInputStream_Read,
214     nsInputStream_ReadSegments,
215     nsInputStream_IsNonBlocking
216 };
217
218 static nsProtocolStream *create_nsprotocol_stream(void)
219 {
220     nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
221
222     ret->lpInputStreamVtbl = &nsInputStreamVtbl;
223     ret->ref = 1;
224     ret->buf_size = 0;
225
226     return ret;
227 }
228
229 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
230
231 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
232         REFIID riid, void **ppv)
233 {
234     BSCallback *This = STATUSCLB_THIS(iface);
235
236     *ppv = NULL;
237     if(IsEqualGUID(&IID_IUnknown, riid)) {
238         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
239         *ppv = STATUSCLB(This);
240     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
241         TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
242         *ppv = STATUSCLB(This);
243     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
244         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
245         *ppv = SERVPROV(This);
246     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
247         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
248         *ppv = HTTPNEG(This);
249     }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
250         TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
251         *ppv = HTTPNEG(This);
252     }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
253         TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
254         *ppv = BINDINFO(This);
255     }
256
257     if(*ppv) {
258         IBindStatusCallback_AddRef(STATUSCLB(This));
259         return S_OK;
260     }
261
262     TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
263     return E_NOINTERFACE;
264 }
265
266 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
267 {
268     BSCallback *This = STATUSCLB_THIS(iface);
269     LONG ref = InterlockedIncrement(&This->ref);
270
271     TRACE("(%p) ref = %d\n", This, ref);
272
273     return ref;
274 }
275
276 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
277 {
278     BSCallback *This = STATUSCLB_THIS(iface);
279     LONG ref = InterlockedDecrement(&This->ref);
280
281     TRACE("(%p) ref = %d\n", This, ref);
282
283     if(!ref) {
284         if(This->post_data)
285             GlobalFree(This->post_data);
286         if(This->mon)
287             IMoniker_Release(This->mon);
288         if(This->binding)
289             IBinding_Release(This->binding);
290         list_remove(&This->entry);
291         heap_free(This->headers);
292
293         This->vtbl->destroy(This);
294     }
295
296     return ref;
297 }
298
299 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
300         DWORD dwReserved, IBinding *pbind)
301 {
302     BSCallback *This = STATUSCLB_THIS(iface);
303
304     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
305
306     IBinding_AddRef(pbind);
307     This->binding = pbind;
308
309     if(This->doc)
310         list_add_head(&This->doc->bindings, &This->entry);
311
312     return This->vtbl->start_binding(This);
313 }
314
315 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
316 {
317     BSCallback *This = STATUSCLB_THIS(iface);
318     FIXME("(%p)->(%p)\n", This, pnPriority);
319     return E_NOTIMPL;
320 }
321
322 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
323 {
324     BSCallback *This = STATUSCLB_THIS(iface);
325     FIXME("(%p)->(%d)\n", This, reserved);
326     return E_NOTIMPL;
327 }
328
329 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
330         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
331 {
332     BSCallback *This = STATUSCLB_THIS(iface);
333
334     TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
335             debugstr_w(szStatusText));
336
337     return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
338 }
339
340 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
341         HRESULT hresult, LPCWSTR szError)
342 {
343     BSCallback *This = STATUSCLB_THIS(iface);
344
345     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
346
347     /* NOTE: IE7 calls GetBindResult here */
348
349     if(This->binding) {
350         IBinding_Release(This->binding);
351         This->binding = NULL;
352     }
353
354     list_remove(&This->entry);
355
356     return This->vtbl->stop_binding(This, hresult);
357 }
358
359 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
360         DWORD *grfBINDF, BINDINFO *pbindinfo)
361 {
362     BSCallback *This = STATUSCLB_THIS(iface);
363     DWORD size;
364
365     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
366
367     *grfBINDF = This->bindf;
368
369     size = pbindinfo->cbSize;
370     memset(pbindinfo, 0, size);
371     pbindinfo->cbSize = size;
372
373     pbindinfo->cbstgmedData = This->post_data_len;
374     pbindinfo->dwCodePage = CP_UTF8;
375     pbindinfo->dwOptions = 0x80000;
376
377     if(This->post_data) {
378         pbindinfo->dwBindVerb = BINDVERB_POST;
379
380         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
381         pbindinfo->stgmedData.u.hGlobal = This->post_data;
382         pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
383         IBindStatusCallback_AddRef(STATUSCLB(This));
384     }
385
386     return S_OK;
387 }
388
389 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
390         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
391 {
392     BSCallback *This = STATUSCLB_THIS(iface);
393
394     TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
395
396     return This->vtbl->read_data(This, pstgmed->u.pstm);
397 }
398
399 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
400         REFIID riid, IUnknown *punk)
401 {
402     BSCallback *This = STATUSCLB_THIS(iface);
403     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
404     return E_NOTIMPL;
405 }
406
407 #undef STATUSCLB_THIS
408
409 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
410     BindStatusCallback_QueryInterface,
411     BindStatusCallback_AddRef,
412     BindStatusCallback_Release,
413     BindStatusCallback_OnStartBinding,
414     BindStatusCallback_GetPriority,
415     BindStatusCallback_OnLowResource,
416     BindStatusCallback_OnProgress,
417     BindStatusCallback_OnStopBinding,
418     BindStatusCallback_GetBindInfo,
419     BindStatusCallback_OnDataAvailable,
420     BindStatusCallback_OnObjectAvailable
421 };
422
423 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
424
425 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
426                                                    REFIID riid, void **ppv)
427 {
428     BSCallback *This = HTTPNEG_THIS(iface);
429     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
430 }
431
432 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
433 {
434     BSCallback *This = HTTPNEG_THIS(iface);
435     return IBindStatusCallback_AddRef(STATUSCLB(This));
436 }
437
438 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
439 {
440     BSCallback *This = HTTPNEG_THIS(iface);
441     return IBindStatusCallback_Release(STATUSCLB(This));
442 }
443
444 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
445         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
446 {
447     BSCallback *This = HTTPNEG_THIS(iface);
448     DWORD size;
449
450     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
451           dwReserved, pszAdditionalHeaders);
452
453     if(!This->headers) {
454         *pszAdditionalHeaders = NULL;
455         return S_OK;
456     }
457
458     size = (strlenW(This->headers)+1)*sizeof(WCHAR);
459     *pszAdditionalHeaders = CoTaskMemAlloc(size);
460     memcpy(*pszAdditionalHeaders, This->headers, size);
461
462     return S_OK;
463 }
464
465 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
466         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
467 {
468     BSCallback *This = HTTPNEG_THIS(iface);
469     FIXME("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
470           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
471     return E_NOTIMPL;
472 }
473
474 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
475         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
476 {
477     BSCallback *This = HTTPNEG_THIS(iface);
478     FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
479     return E_NOTIMPL;
480 }
481
482 #undef HTTPNEG
483
484 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
485     HttpNegotiate_QueryInterface,
486     HttpNegotiate_AddRef,
487     HttpNegotiate_Release,
488     HttpNegotiate_BeginningTransaction,
489     HttpNegotiate_OnResponse,
490     HttpNegotiate_GetRootSecurityId
491 };
492
493 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
494
495 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
496                                                       REFIID riid, void **ppv)
497 {
498     BSCallback *This = BINDINFO_THIS(iface);
499     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
500 }
501
502 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
503 {
504     BSCallback *This = BINDINFO_THIS(iface);
505     return IBindStatusCallback_AddRef(STATUSCLB(This));
506 }
507
508 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
509 {
510     BSCallback *This = BINDINFO_THIS(iface);
511     return IBindStatusCallback_Release(STATUSCLB(This));
512 }
513
514 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
515                                                    DWORD *grfBINDF, BINDINFO *pbindinfo)
516 {
517     BSCallback *This = BINDINFO_THIS(iface);
518     FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
519     return E_NOTIMPL;
520 }
521
522 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
523         ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
524 {
525     BSCallback *This = BINDINFO_THIS(iface);
526     FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
527     return E_NOTIMPL;
528 }
529
530 #undef BINDINFO_THIS
531
532 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
533     InternetBindInfo_QueryInterface,
534     InternetBindInfo_AddRef,
535     InternetBindInfo_Release,
536     InternetBindInfo_GetBindInfo,
537     InternetBindInfo_GetBindString
538 };
539
540 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
541
542 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
543                                                         REFIID riid, void **ppv)
544 {
545     BSCallback *This = SERVPROV_THIS(iface);
546     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
547 }
548
549 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
550 {
551     BSCallback *This = SERVPROV_THIS(iface);
552     return IBindStatusCallback_AddRef(STATUSCLB(This));
553 }
554
555 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
556 {
557     BSCallback *This = SERVPROV_THIS(iface);
558     return IBindStatusCallback_Release(STATUSCLB(This));
559 }
560
561 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
562         REFGUID guidService, REFIID riid, void **ppv)
563 {
564     BSCallback *This = SERVPROV_THIS(iface);
565     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
566     return E_NOINTERFACE;
567 }
568
569 #undef SERVPROV_THIS
570
571 static const IServiceProviderVtbl ServiceProviderVtbl = {
572     BSCServiceProvider_QueryInterface,
573     BSCServiceProvider_AddRef,
574     BSCServiceProvider_Release,
575     BSCServiceProvider_QueryService
576 };
577
578 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
579 {
580     This->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
581     This->lpServiceProviderVtbl    = &ServiceProviderVtbl;
582     This->lpHttpNegotiate2Vtbl     = &HttpNegotiate2Vtbl;
583     This->lpInternetBindInfoVtbl   = &InternetBindInfoVtbl;
584     This->vtbl = vtbl;
585     This->ref = 1;
586     This->bindf = bindf;
587
588     list_init(&This->entry);
589
590     if(mon)
591         IMoniker_AddRef(mon);
592     This->mon = mon;
593 }
594
595 /* Calls undocumented 84 cmd of CGID_ShellDocView */
596 static void call_docview_84(HTMLDocument *doc)
597 {
598     IOleCommandTarget *olecmd;
599     VARIANT var;
600     HRESULT hres;
601
602     if(!doc->client)
603         return;
604
605     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
606     if(FAILED(hres))
607         return;
608
609     VariantInit(&var);
610     hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
611     IOleCommandTarget_Release(olecmd);
612     if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
613         FIXME("handle result\n");
614 }
615
616 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
617                             HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
618 {
619     PRUint32 post_data_len = 0, available = 0;
620     HGLOBAL post_data = NULL;
621     LPWSTR headers = NULL;
622     DWORD headers_len = 0, len;
623     const char *ptr, *ptr2, *post_data_end;
624
625     nsIInputStream_Available(post_data_stream, &available);
626     post_data = GlobalAlloc(0, available+1);
627     nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
628     
629     TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
630
631     ptr = ptr2 = post_data;
632     post_data_end = (const char*)post_data+post_data_len;
633
634     while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
635         while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
636             ptr++;
637
638         if(!*ptr) {
639             FIXME("*ptr = 0\n");
640             return;
641         }
642
643         ptr += 2;
644
645         if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
646            && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
647                              CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
648                              ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
649             ptr2 = ptr;
650             continue;
651         }
652
653         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
654
655         if(headers)
656             headers = heap_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
657         else
658             headers = heap_alloc((len+1)*sizeof(WCHAR));
659
660         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, len);
661         headers_len += len;
662
663         ptr2 = ptr;
664     }
665
666     headers[headers_len] = 0;
667     *headers_ret = headers;
668
669     if(ptr >= post_data_end-2) {
670         GlobalFree(post_data);
671         return;
672     }
673
674     ptr += 2;
675
676     if(headers_len) {
677         post_data_len -= ptr-(const char*)post_data;
678         memmove(post_data, ptr, post_data_len);
679         post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
680     }
681
682     *post_data_ret = post_data;
683     *post_data_len_ret = post_data_len;
684 }
685
686 HRESULT start_binding(HTMLDocument *doc, BSCallback *bscallback, IBindCtx *bctx)
687 {
688     IStream *str = NULL;
689     HRESULT hres;
690
691     bscallback->doc = doc;
692
693     /* NOTE: IE7 calls IsSystemMoniker here*/
694
695     call_docview_84(doc);
696
697     if(bctx) {
698         RegisterBindStatusCallback(bctx, STATUSCLB(bscallback), NULL, 0);
699         IBindCtx_AddRef(bctx);
700     }else {
701         hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
702         if(FAILED(hres)) {
703             WARN("CreateAsyncBindCtx failed: %08x\n", hres);
704             bscallback->vtbl->stop_binding(bscallback, hres);
705             return hres;
706         }
707     }
708
709     hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
710     IBindCtx_Release(bctx);
711     if(FAILED(hres)) {
712         WARN("BindToStorage failed: %08x\n", hres);
713         bscallback->vtbl->stop_binding(bscallback, hres);
714         return hres;
715     }
716
717     if(str)
718         IStream_Release(str);
719
720     IMoniker_Release(bscallback->mon);
721     bscallback->mon = NULL;
722
723     return S_OK;
724 }
725
726 typedef struct {
727     BSCallback bsc;
728
729     DWORD size;
730     BYTE *buf;
731     HRESULT hres;
732 } BufferBSC;
733
734 #define BUFFERBSC_THIS(bsc) ((BufferBSC*) bsc)
735
736 static void BufferBSC_destroy(BSCallback *bsc)
737 {
738     BufferBSC *This = BUFFERBSC_THIS(bsc);
739
740     heap_free(This->buf);
741     heap_free(This);
742 }
743
744 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
745 {
746     return S_OK;
747 }
748
749 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
750 {
751     BufferBSC *This = BUFFERBSC_THIS(bsc);
752
753     This->hres = result;
754
755     if(FAILED(result)) {
756         heap_free(This->buf);
757         This->buf = NULL;
758         This->size = 0;
759     }
760
761     return S_OK;
762 }
763
764 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
765 {
766     BufferBSC *This = BUFFERBSC_THIS(bsc);
767     DWORD readed;
768     HRESULT hres;
769
770     if(!This->buf) {
771         This->size = 128;
772         This->buf = heap_alloc(This->size);
773     }
774
775     do {
776         if(This->bsc.readed == This->size) {
777             This->size <<= 1;
778             This->buf = heap_realloc(This->buf, This->size);
779         }
780
781         readed = 0;
782         hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
783         This->bsc.readed += readed;
784     }while(hres == S_OK);
785
786     return S_OK;
787 }
788
789 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
790 {
791     return S_OK;
792 }
793
794 #undef BUFFERBSC_THIS
795
796 static const BSCallbackVtbl BufferBSCVtbl = {
797     BufferBSC_destroy,
798     BufferBSC_start_binding,
799     BufferBSC_stop_binding,
800     BufferBSC_read_data,
801     BufferBSC_on_progress,
802 };
803
804
805 static BufferBSC *create_bufferbsc(IMoniker *mon)
806 {
807     BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
808
809     init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
810     ret->hres = E_FAIL;
811
812     return ret;
813 }
814
815 HRESULT bind_mon_to_buffer(HTMLDocument *doc, IMoniker *mon, void **buf, DWORD *size)
816 {
817     BufferBSC *bsc = create_bufferbsc(mon);
818     HRESULT hres;
819
820     *buf = NULL;
821
822     hres = start_binding(doc, &bsc->bsc, NULL);
823     if(SUCCEEDED(hres)) {
824         hres = bsc->hres;
825         if(SUCCEEDED(hres)) {
826             *buf = bsc->buf;
827             bsc->buf = NULL;
828             *size = bsc->bsc.readed;
829             bsc->size = 0;
830         }
831     }
832
833     IBindStatusCallback_Release(STATUSCLB(&bsc->bsc));
834
835     return hres;
836 }
837
838 struct nsChannelBSC {
839     BSCallback bsc;
840
841     nsChannel *nschannel;
842     nsIStreamListener *nslistener;
843     nsISupports *nscontext;
844
845     nsProtocolStream *nsstream;
846 };
847
848 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
849 {
850     nsresult nsres;
851     HRESULT hres;
852
853     if(!This->nslistener) {
854         BYTE buf[1024];
855         DWORD read;
856
857         do {
858             read = 0;
859             hres = IStream_Read(stream, buf, sizeof(buf), &read);
860         }while(hres == S_OK && read);
861
862         return S_OK;
863     }
864
865     if(!This->nsstream)
866         This->nsstream = create_nsprotocol_stream();
867
868     do {
869         hres = IStream_Read(stream, This->nsstream->buf, sizeof(This->nsstream->buf),
870                 &This->nsstream->buf_size);
871         if(!This->nsstream->buf_size)
872             break;
873
874         if(!This->bsc.readed && This->nsstream->buf_size >= 2 && *(WORD*)This->nsstream->buf == 0xfeff) {
875             This->nschannel->charset = heap_alloc(sizeof(UTF16_STR));
876             memcpy(This->nschannel->charset, UTF16_STR, sizeof(UTF16_STR));
877         }
878
879         if(!This->bsc.readed) {
880             nsres = nsIStreamListener_OnStartRequest(This->nslistener,
881                     (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
882             if(NS_FAILED(nsres))
883                 FIXME("OnStartRequest failed: %08x\n", nsres);
884
885             /* events are reset when a new document URI is loaded, so re-initialise them here */
886             if(This->bsc.doc && This->bsc.doc->bscallback == This && This->bsc.doc->nscontainer) {
887                 update_nsdocument(This->bsc.doc);
888                 init_nsevents(This->bsc.doc->nscontainer);
889             }
890         }
891
892         This->bsc.readed += This->nsstream->buf_size;
893
894         nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
895                 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
896                 NSINSTREAM(This->nsstream), This->bsc.readed-This->nsstream->buf_size,
897                 This->nsstream->buf_size);
898         if(NS_FAILED(nsres))
899             ERR("OnDataAvailable failed: %08x\n", nsres);
900
901         if(This->nsstream->buf_size)
902             FIXME("buffer is not empty!\n");
903     }while(hres == S_OK);
904
905     return S_OK;
906 }
907
908 static void on_stop_nsrequest(nsChannelBSC *This)
909 {
910     if(!This->nslistener)
911         return;
912
913     nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
914             This->nscontext, NS_OK);
915 }
916
917 static void add_nsrequest(nsChannelBSC *This)
918 {
919     nsresult nsres;
920
921     if(!This->nschannel || !This->nschannel->load_group)
922         return;
923
924     nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
925             (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
926
927     if(NS_FAILED(nsres))
928         ERR("AddRequest failed:%08x\n", nsres);
929 }
930
931 #define NSCHANNELBSC_THIS(bsc) ((nsChannelBSC*) bsc)
932
933 static void nsChannelBSC_destroy(BSCallback *bsc)
934 {
935     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
936
937     if(This->nschannel)
938         nsIChannel_Release(NSCHANNEL(This->nschannel));
939     if(This->nslistener)
940         nsIStreamListener_Release(This->nslistener);
941     if(This->nscontext)
942         nsISupports_Release(This->nscontext);
943     if(This->nsstream)
944         nsIInputStream_Release(NSINSTREAM(This->nsstream));
945     heap_free(This);
946 }
947
948 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
949 {
950     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
951
952     add_nsrequest(This);
953
954     return S_OK;
955 }
956
957 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
958 {
959     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
960
961     on_stop_nsrequest(This);
962
963     if(This->nslistener) {
964         if(This->nschannel->load_group) {
965             nsresult nsres;
966
967             nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
968                     (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
969             if(NS_FAILED(nsres))
970                 ERR("RemoveRequest failed: %08x\n", nsres);
971         }
972     }
973
974     if(FAILED(result))
975         return S_OK;
976
977     if(This->bsc.doc && This->bsc.doc->bscallback == This && !This->bsc.doc->nscontainer) {
978         task_t *task = heap_alloc(sizeof(task_t));
979
980         task->doc = This->bsc.doc;
981         task->task_id = TASK_PARSECOMPLETE;
982         task->next = NULL;
983
984         /*
985          * This should be done in the worker thread that parses HTML,
986          * but we don't have such thread.
987          */
988         push_task(task);
989     }
990
991     return S_OK;
992 }
993
994 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
995 {
996     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
997
998     return read_stream_data(This, stream);
999 }
1000
1001 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1002 {
1003     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1004
1005     switch(status_code) {
1006     case BINDSTATUS_MIMETYPEAVAILABLE:
1007         if(!This->nschannel)
1008             return S_OK;
1009
1010         heap_free(This->nschannel->content_type);
1011         This->nschannel->content_type = heap_strdupWtoA(status_text);
1012     }
1013
1014     return S_OK;
1015 }
1016
1017 #undef NSCHANNELBSC_THIS
1018
1019 static const BSCallbackVtbl nsChannelBSCVtbl = {
1020     nsChannelBSC_destroy,
1021     nsChannelBSC_start_binding,
1022     nsChannelBSC_stop_binding,
1023     nsChannelBSC_read_data,
1024     nsChannelBSC_on_progress,
1025 };
1026
1027 nsChannelBSC *create_channelbsc(IMoniker *mon)
1028 {
1029     nsChannelBSC *ret = heap_alloc_zero(sizeof(*ret));
1030
1031     init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1032
1033     return ret;
1034 }
1035
1036 IMoniker *get_channelbsc_mon(nsChannelBSC *This)
1037 {
1038     if(This->bsc.mon)
1039         IMoniker_AddRef(This->bsc.mon);
1040     return This->bsc.mon;
1041 }
1042
1043 void set_document_bscallback(HTMLDocument *doc, nsChannelBSC *callback)
1044 {
1045     BSCallback *iter;
1046
1047     if(doc->bscallback) {
1048         if(doc->bscallback->bsc.binding)
1049             IBinding_Abort(doc->bscallback->bsc.binding);
1050         doc->bscallback->bsc.doc = NULL;
1051         IBindStatusCallback_Release(STATUSCLB(&doc->bscallback->bsc));
1052     }
1053
1054     LIST_FOR_EACH_ENTRY(iter, &doc->bindings, BSCallback, entry) {
1055         iter->doc = NULL;
1056         list_remove(&iter->entry);
1057     }
1058
1059     doc->bscallback = callback;
1060
1061     if(callback) {
1062         IBindStatusCallback_AddRef(STATUSCLB(&callback->bsc));
1063         callback->bsc.doc = doc;
1064     }
1065 }
1066
1067 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1068 {
1069     HRESULT hres;
1070
1071     const char text_html[] = "text/html";
1072
1073     add_nsrequest(bscallback);
1074
1075     if(bscallback->nschannel)
1076         bscallback->nschannel->content_type = heap_strdupA(text_html);
1077
1078     hres = read_stream_data(bscallback, stream);
1079     IBindStatusCallback_OnStopBinding(STATUSCLB(&bscallback->bsc), hres, ERROR_SUCCESS);
1080
1081     return hres;
1082 }
1083
1084 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1085 {
1086     nsIChannel_AddRef(NSCHANNEL(channel));
1087     This->nschannel = channel;
1088
1089     nsIStreamListener_AddRef(listener);
1090     This->nslistener = listener;
1091
1092     if(context) {
1093         nsISupports_AddRef(context);
1094         This->nscontext = context;
1095     }
1096 }
1097
1098 void hlink_frame_navigate(HTMLDocument *doc, IHlinkFrame *hlink_frame,
1099                           LPCWSTR uri, nsIInputStream *post_data_stream, DWORD hlnf)
1100 {
1101     BSCallback *callback;
1102     IBindCtx *bindctx;
1103     IMoniker *mon;
1104     IHlink *hlink;
1105     HRESULT hr;
1106
1107     callback = &create_channelbsc(NULL)->bsc;
1108
1109     if(post_data_stream) {
1110         parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
1111                         &callback->post_data_len);
1112         TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
1113               debugstr_an(callback->post_data, callback->post_data_len));
1114     }
1115
1116     hr = CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
1117     if (FAILED(hr)) {
1118         IBindStatusCallback_Release(STATUSCLB(callback));
1119         return;
1120     }
1121
1122     hr = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER, &IID_IHlink, (LPVOID*)&hlink);
1123     if (FAILED(hr)) {
1124         IBindCtx_Release(bindctx);
1125         IBindStatusCallback_Release(STATUSCLB(callback));
1126         return;
1127     }
1128
1129     hr = CreateURLMoniker(NULL, uri, &mon);
1130     if (SUCCEEDED(hr)) {
1131         IHlink_SetMonikerReference(hlink, 0, mon, NULL);
1132
1133         if(hlnf & HLNF_OPENINNEWWINDOW) {
1134             static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1135             IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1136         }
1137
1138         IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
1139
1140         IMoniker_Release(mon);
1141     }
1142
1143     IBindCtx_Release(bindctx);
1144     IBindStatusCallback_Release(STATUSCLB(callback));
1145 }