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